shape.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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)
  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. pen = QPen(self.select_color if self.selected else self.line_color)
  37. painter.setPen(pen)
  38. line_path = QPainterPath()
  39. vrtx_path = QPainterPath()
  40. line_path.moveTo(QPointF(self.points[0]))
  41. self.drawVertex(vrtx_path, self.points[0])
  42. for p in self.points[1:]:
  43. line_path.lineTo(QPointF(p))
  44. # Skip last element, otherwise its vertex is not filled.
  45. if p != self.points[0]:
  46. self.drawVertex(vrtx_path, p)
  47. painter.drawPath(line_path)
  48. painter.fillPath(vrtx_path, self.line_color)
  49. if self.fill:
  50. painter.fillPath(line_path, self.fill_color)
  51. def drawVertex(self, path, point):
  52. d = self.point_size
  53. if self.point_type == self.P_SQUARE:
  54. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  55. else:
  56. path.addEllipse(QPointF(point), d/2.0, d/2.0)
  57. def containsPoint(self, point):
  58. path = QPainterPath(QPointF(self.points[0]))
  59. for p in self.points[1:]:
  60. path.lineTo(QPointF(p))
  61. return path.contains(QPointF(point))
  62. def __len__(self):
  63. return len(self.points)
  64. def __getitem__(self, key):
  65. return self.points[key]
  66. def __setitem__(self, key, value):
  67. self.points[key] = value