shape.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5. # FIXME:
  6. # - Add support for highlighting vertices.
  7. # TODO:
  8. # - [opt] Store paths instead of creating new ones at each paint.
  9. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  10. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  11. DEFAULT_SELECT_COLOR = QColor(255, 255, 255)
  12. class Shape(object):
  13. P_SQUARE, P_ROUND = range(2)
  14. ## The following class variables influence the drawing
  15. ## of _all_ shape objects.
  16. line_color = DEFAULT_LINE_COLOR
  17. fill_color = DEFAULT_FILL_COLOR
  18. select_color = DEFAULT_SELECT_COLOR
  19. point_type = P_SQUARE
  20. point_size = 8
  21. scale = 1.0
  22. def __init__(self, label=None, line_color=None):
  23. self.label = label
  24. self.points = []
  25. self.fill = False
  26. self.selected = False
  27. if line_color is not None:
  28. # Override the class line_color attribute
  29. # with an object attribute. Currently this
  30. # is used for drawing the pending line a different color.
  31. self.line_color = line_color
  32. def addPoint(self, point):
  33. self.points.append(point)
  34. def popPoint(self):
  35. if self.points:
  36. return self.points.pop()
  37. return None
  38. def isClosed(self):
  39. return len(self.points) > 1 and self[0] == self[-1]
  40. def paint(self, painter):
  41. if self.points:
  42. pen = QPen(self.select_color if self.selected else self.line_color)
  43. # Try using integer sizes for smoother drawing(?)
  44. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  45. painter.setPen(pen)
  46. line_path = QPainterPath()
  47. vrtx_path = QPainterPath()
  48. line_path.moveTo(QPointF(self.points[0]))
  49. self.drawVertex(vrtx_path, self.points[0])
  50. for p in self.points[1:]:
  51. line_path.lineTo(QPointF(p))
  52. # Skip last element, otherwise its vertex is not filled.
  53. if p != self.points[0]:
  54. self.drawVertex(vrtx_path, p)
  55. painter.drawPath(line_path)
  56. painter.fillPath(vrtx_path, self.line_color)
  57. if self.fill:
  58. painter.fillPath(line_path, self.fill_color)
  59. def drawVertex(self, path, point):
  60. d = self.point_size / self.scale
  61. if self.point_type == self.P_SQUARE:
  62. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  63. else:
  64. path.addEllipse(point, d/2.0, d/2.0)
  65. def containsPoint(self, point):
  66. return self.makePath().contains(point)
  67. def makePath(self):
  68. path = QPainterPath(self.points[0])
  69. for p in self.points[1:]:
  70. path.lineTo(p)
  71. return path
  72. def boundingRect(self):
  73. return self.makePath().boundingRect()
  74. def moveBy(self, offset):
  75. self.points = [p + offset for p in self.points]
  76. def copy(self):
  77. shape = Shape("Copy of %s" % self.label )
  78. shape.points= [p for p in self.points]
  79. shape.fill = self.fill
  80. shape.selected = self.selected
  81. return shape
  82. def __len__(self):
  83. return len(self.points)
  84. def __getitem__(self, key):
  85. return self.points[key]
  86. def __setitem__(self, key, value):
  87. self.points[key] = value