shape.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from PyQt4.QtGui import *
  4. from PyQt4.QtCore import *
  5. from lib import distance
  6. # TODO:
  7. # - [opt] Store paths instead of creating new ones at each paint.
  8. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  9. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  10. DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
  11. DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
  12. DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
  13. DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
  14. class Shape(object):
  15. P_SQUARE, P_ROUND = range(2)
  16. MOVE_VERTEX, NEAR_VERTEX = range(2)
  17. ## The following class variables influence the drawing
  18. ## of _all_ shape objects.
  19. line_color = DEFAULT_LINE_COLOR
  20. fill_color = DEFAULT_FILL_COLOR
  21. select_line_color = DEFAULT_SELECT_LINE_COLOR
  22. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  23. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  24. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  25. point_type = P_ROUND
  26. point_size = 8
  27. scale = 1.0
  28. def __init__(self, label=None, line_color=None):
  29. self.label = label
  30. self.points = []
  31. self.fill = False
  32. self.selected = False
  33. self._highlightIndex = None
  34. self._highlightMode = self.NEAR_VERTEX
  35. self._highlightSettings = {
  36. self.NEAR_VERTEX: (4, self.P_ROUND),
  37. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  38. }
  39. self._closed = False
  40. if line_color is not None:
  41. # Override the class line_color attribute
  42. # with an object attribute. Currently this
  43. # is used for drawing the pending line a different color.
  44. self.line_color = line_color
  45. def close(self):
  46. assert len(self.points) > 2
  47. self._closed = True
  48. def addPoint(self, point):
  49. if self.points and point == self.points[0]:
  50. self.close()
  51. else:
  52. self.points.append(point)
  53. def popPoint(self):
  54. if self.points:
  55. return self.points.pop()
  56. return None
  57. def isClosed(self):
  58. return self._closed
  59. def setOpen(self):
  60. self._closed = False
  61. def paint(self, painter):
  62. if self.points:
  63. color = self.select_line_color if self.selected else self.line_color
  64. pen = 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 = QPainterPath()
  69. vrtx_path = 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 if self.selected else self.fill_color
  85. painter.fillPath(line_path, color)
  86. def drawVertex(self, path, i):
  87. d = self.point_size / self.scale
  88. shape = self.point_type
  89. point = self.points[i]
  90. if i == self._highlightIndex:
  91. size, shape = self._highlightSettings[self._highlightMode]
  92. d *= size
  93. if self._highlightIndex is not None:
  94. self.vertex_fill_color = self.hvertex_fill_color
  95. else:
  96. self.vertex_fill_color = Shape.vertex_fill_color
  97. if shape == self.P_SQUARE:
  98. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  99. elif shape == self.P_ROUND:
  100. path.addEllipse(point, d/2.0, d/2.0)
  101. else:
  102. assert False, "unsupported vertex shape"
  103. def nearestVertex(self, point, epsilon):
  104. for i, p in enumerate(self.points):
  105. if distance(p - point) <= epsilon:
  106. return i
  107. return None
  108. def containsPoint(self, point):
  109. return self.makePath().contains(point)
  110. def makePath(self):
  111. path = QPainterPath(self.points[0])
  112. for p in self.points[1:]:
  113. path.lineTo(p)
  114. return path
  115. def boundingRect(self):
  116. return self.makePath().boundingRect()
  117. def moveBy(self, offset):
  118. self.points = [p + offset for p in self.points]
  119. def moveVertexBy(self, i, offset):
  120. self.points[i] = self.points[i] + offset
  121. def highlightVertex(self, i, action):
  122. self._highlightIndex = i
  123. self._highlightMode = action
  124. def highlightClear(self):
  125. self._highlightIndex = None
  126. def copy(self):
  127. shape = Shape("Copy of %s" % self.label )
  128. shape.points= [p for p in self.points]
  129. shape.fill = self.fill
  130. shape.selected = self.selected
  131. shape._closed = self._closed
  132. if self.line_color != Shape.line_color:
  133. shape.line_color = self.line_color
  134. if self.fill_color != Shape.fill_color:
  135. shape.fill_color = self.fill_color
  136. return shape
  137. def __len__(self):
  138. return len(self.points)
  139. def __getitem__(self, key):
  140. return self.points[key]
  141. def __setitem__(self, key, value):
  142. self.points[key] = value