shape.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5. class Shape(object):
  6. P_SQUARE, P_ROUND = range(2)
  7. ## The following class variables influence the drawing
  8. ## of _all_ shape objects.
  9. line_color = QColor(0, 255, 0, 128)
  10. fill_color = QColor(255, 0, 0, 128)
  11. select_color=QColor(255,255,255,255)
  12. point_type = P_SQUARE
  13. point_size = 8
  14. def __init__(self, label=None, line_color=None):
  15. self.label = label
  16. self.points = []
  17. self.fill = False
  18. self.selected=False
  19. if line_color is not None:
  20. # Override the class line_color attribute
  21. # with an object attribute. Currently this
  22. # is used for drawing the pending line a different color.
  23. self.line_color = line_color
  24. def addPoint(self, point):
  25. self.points.append(point)
  26. def popPoint(self):
  27. if self.points:
  28. return self.points.pop()
  29. return None
  30. def isClosed(self):
  31. return len(self.points) > 1 and self[0] == self[-1]
  32. # TODO:
  33. # The paths could be stored and elements added directly to them.
  34. def paint(self, painter):
  35. if self.points:
  36. if self.selected:
  37. pen = QPen(self.select_color)
  38. else:
  39. pen = QPen(self.line_color)
  40. painter.setPen(pen)
  41. line_path = QPainterPath()
  42. vrtx_path = QPainterPath()
  43. line_path.moveTo(QPointF(self.points[0]))
  44. self.drawVertex(vrtx_path, self.points[0])
  45. for p in self.points[1:]:
  46. line_path.lineTo(QPointF(p))
  47. # Skip last element, otherwise its vertex is not filled.
  48. if p != self.points[0]:
  49. self.drawVertex(vrtx_path, p)
  50. painter.drawPath(line_path)
  51. painter.fillPath(vrtx_path, self.line_color)
  52. if self.fill:
  53. painter.fillPath(line_path, self.fill_color)
  54. def drawVertex(self, path, point):
  55. d = self.point_size
  56. if self.point_type == self.P_SQUARE:
  57. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  58. else:
  59. path.addEllipse(QPointF(point), d/2.0, d/2.0)
  60. def containPoint(self,point):
  61. path=QPainterPath(QPointF(self.points[0]))
  62. for p in self.points[1:]:
  63. path.lineTo(QPointF(p))
  64. return path.contains(QPointF(point))
  65. def __len__(self):
  66. return len(self.points)
  67. def __getitem__(self, key):
  68. return self.points[key]
  69. def __setitem__(self, key, value):
  70. self.points[key] = value