shape.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 addPoint(self, point):
  46. if self.points and point == self.points[0]:
  47. self._closed = True
  48. return
  49. self.points.append(point)
  50. def popPoint(self):
  51. if self.points:
  52. return self.points.pop()
  53. return None
  54. def isClosed(self):
  55. return self._closed
  56. def setOpen(self):
  57. self._closed = False
  58. def paint(self, painter):
  59. if self.points:
  60. color = self.select_line_color if self.selected else self.line_color
  61. pen = 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 = QPainterPath()
  66. vrtx_path = 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 if self.selected else self.fill_color
  82. painter.fillPath(line_path, color)
  83. def drawVertex(self, path, i):
  84. d = self.point_size / self.scale
  85. shape = self.point_type
  86. point = self.points[i]
  87. if i == self._highlightIndex:
  88. size, shape = self._highlightSettings[self._highlightMode]
  89. d *= size
  90. if self._highlightIndex is not None:
  91. self.vertex_fill_color = self.hvertex_fill_color
  92. else:
  93. self.vertex_fill_color = Shape.vertex_fill_color
  94. if shape == self.P_SQUARE:
  95. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  96. elif shape == self.P_ROUND:
  97. path.addEllipse(point, d/2.0, d/2.0)
  98. else:
  99. assert False, "unsupported vertex shape"
  100. def nearestVertex(self, point, epsilon):
  101. for i, p in enumerate(self.points):
  102. if distance(p - point) <= epsilon:
  103. return i
  104. return None
  105. def containsPoint(self, point):
  106. return self.makePath().contains(point)
  107. def makePath(self):
  108. path = QPainterPath(self.points[0])
  109. for p in self.points[1:]:
  110. path.lineTo(p)
  111. return path
  112. def boundingRect(self):
  113. return self.makePath().boundingRect()
  114. def moveBy(self, offset):
  115. self.points = [p + offset for p in self.points]
  116. def moveVertexBy(self, i, offset):
  117. self.points[i] = self.points[i] + offset
  118. def highlightVertex(self, i, action):
  119. self._highlightIndex = i
  120. self._highlightMode = action
  121. def highlightClear(self):
  122. self._highlightIndex = None
  123. def copy(self):
  124. shape = Shape("Copy of %s" % self.label )
  125. shape.points= [p for p in self.points]
  126. shape.fill = self.fill
  127. shape.selected = self.selected
  128. shape._closed = self._closed
  129. return shape
  130. def __len__(self):
  131. return len(self.points)
  132. def __getitem__(self, key):
  133. return self.points[key]
  134. def __setitem__(self, key, value):
  135. self.points[key] = value