shape.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from qtpy import QtGui
  2. from .lib import distance
  3. # TODO(unknown):
  4. # - [opt] Store paths instead of creating new ones at each paint.
  5. DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128)
  6. DEFAULT_FILL_COLOR = QtGui.QColor(255, 0, 0, 128)
  7. DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255)
  8. DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 128, 255, 155)
  9. DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255)
  10. DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 0, 0)
  11. class Shape(object):
  12. P_SQUARE, P_ROUND = 0, 1
  13. MOVE_VERTEX, NEAR_VERTEX = 0, 1
  14. # The following class variables influence the drawing of all shape objects.
  15. line_color = DEFAULT_LINE_COLOR
  16. fill_color = DEFAULT_FILL_COLOR
  17. select_line_color = DEFAULT_SELECT_LINE_COLOR
  18. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  19. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  20. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  21. point_type = P_ROUND
  22. point_size = 8
  23. scale = 1.0
  24. def __init__(self, label=None, line_color=None):
  25. self.label = label
  26. self.points = []
  27. self.fill = False
  28. self.selected = False
  29. self._highlightIndex = None
  30. self._highlightMode = self.NEAR_VERTEX
  31. self._highlightSettings = {
  32. self.NEAR_VERTEX: (4, self.P_ROUND),
  33. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  34. }
  35. self._closed = False
  36. if line_color is not None:
  37. # Override the class line_color attribute
  38. # with an object attribute. Currently this
  39. # is used for drawing the pending line a different color.
  40. self.line_color = line_color
  41. def close(self):
  42. assert len(self.points) > 2, 'Polygon should be created with points >2'
  43. self._closed = True
  44. def addPoint(self, point):
  45. if self.points and point == self.points[0]:
  46. self.close()
  47. else:
  48. self.points.append(point)
  49. def popPoint(self):
  50. if self.points:
  51. return self.points.pop()
  52. return None
  53. def isClosed(self):
  54. return self._closed
  55. def setOpen(self):
  56. self._closed = False
  57. def paint(self, painter):
  58. if self.points:
  59. color = self.select_line_color \
  60. if self.selected else self.line_color
  61. pen = QtGui.QPen(color)
  62. # Try using integer sizes for smoother drawing(?)
  63. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  64. painter.setPen(pen)
  65. line_path = QtGui.QPainterPath()
  66. vrtx_path = QtGui.QPainterPath()
  67. line_path.moveTo(self.points[0])
  68. # Uncommenting the following line will draw 2 paths
  69. # for the 1st vertex, and make it non-filled, which
  70. # may be desirable.
  71. # self.drawVertex(vrtx_path, 0)
  72. for i, p in enumerate(self.points):
  73. line_path.lineTo(p)
  74. self.drawVertex(vrtx_path, i)
  75. if self.isClosed():
  76. line_path.lineTo(self.points[0])
  77. painter.drawPath(line_path)
  78. painter.drawPath(vrtx_path)
  79. painter.fillPath(vrtx_path, self.vertex_fill_color)
  80. if self.fill:
  81. color = self.select_fill_color \
  82. if self.selected else self.fill_color
  83. painter.fillPath(line_path, color)
  84. def drawVertex(self, path, i):
  85. d = self.point_size / self.scale
  86. shape = self.point_type
  87. point = self.points[i]
  88. if i == self._highlightIndex:
  89. size, shape = self._highlightSettings[self._highlightMode]
  90. d *= size
  91. if self._highlightIndex is not None:
  92. self.vertex_fill_color = self.hvertex_fill_color
  93. else:
  94. self.vertex_fill_color = Shape.vertex_fill_color
  95. if shape == self.P_SQUARE:
  96. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  97. elif shape == self.P_ROUND:
  98. path.addEllipse(point, d / 2.0, d / 2.0)
  99. else:
  100. assert False, "unsupported vertex shape"
  101. def nearestVertex(self, point, epsilon):
  102. min_distance = float('inf')
  103. min_i = None
  104. for i, p in enumerate(self.points):
  105. dist = distance(p - point)
  106. if dist <= epsilon and dist < min_distance:
  107. min_distance = dist
  108. min_i = i
  109. return min_i
  110. def containsPoint(self, point):
  111. return self.makePath().contains(point)
  112. def makePath(self):
  113. path = QtGui.QPainterPath(self.points[0])
  114. for p in self.points[1:]:
  115. path.lineTo(p)
  116. return path
  117. def boundingRect(self):
  118. return self.makePath().boundingRect()
  119. def moveBy(self, offset):
  120. self.points = [p + offset for p in self.points]
  121. def moveVertexBy(self, i, offset):
  122. self.points[i] = self.points[i] + offset
  123. def highlightVertex(self, i, action):
  124. self._highlightIndex = i
  125. self._highlightMode = action
  126. def highlightClear(self):
  127. self._highlightIndex = None
  128. def copy(self):
  129. shape = Shape(self.label)
  130. shape.points = [p for p in self.points]
  131. shape.fill = self.fill
  132. shape.selected = self.selected
  133. shape._closed = self._closed
  134. if self.line_color != Shape.line_color:
  135. shape.line_color = self.line_color
  136. if self.fill_color != Shape.fill_color:
  137. shape.fill_color = self.fill_color
  138. return shape
  139. def __len__(self):
  140. return len(self.points)
  141. def __getitem__(self, key):
  142. return self.points[key]
  143. def __setitem__(self, key, value):
  144. self.points[key] = value