shape.py 5.6 KB

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