shape.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(255, 0, 0)
  13. class Shape(object):
  14. P_SQUARE, P_ROUND = range(2)
  15. MOVE_VERTEX, NEAR_VERTEX = range(2)
  16. ## The following class variables influence the drawing
  17. ## 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. point_type = P_ROUND
  24. point_size = 8
  25. scale = 1.0
  26. def __init__(self, label=None, line_color=None):
  27. self.label = label
  28. self.points = []
  29. self.fill = False
  30. self.selected = False
  31. self._highlightIndex = None
  32. self._highlightMode = self.NEAR_VERTEX
  33. self._highlightSettings = {
  34. self.NEAR_VERTEX: (4, self.P_ROUND),
  35. self.MOVE_VERTEX: (2, self.P_SQUARE),
  36. }
  37. self._closed = False
  38. if line_color is not None:
  39. # Override the class line_color attribute
  40. # with an object attribute. Currently this
  41. # is used for drawing the pending line a different color.
  42. self.line_color = line_color
  43. def addPoint(self, point):
  44. if self.points and point == self.points[0]:
  45. self._closed = True
  46. return
  47. self.points.append(point)
  48. def popPoint(self):
  49. if self.points:
  50. return self.points.pop()
  51. return None
  52. def isClosed(self):
  53. return self._closed
  54. def setOpen(self):
  55. self._closed = False
  56. def paint(self, painter):
  57. if self.points:
  58. color = self.select_line_color if self.selected else self.line_color
  59. pen = QPen(color)
  60. # Try using integer sizes for smoother drawing(?)
  61. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  62. painter.setPen(pen)
  63. line_path = QPainterPath()
  64. vrtx_path = QPainterPath()
  65. line_path.moveTo(self.points[0])
  66. # Uncommenting the following line will draw 2 paths
  67. # for the 1st vertex, and make it non-filled, which
  68. # may be desirable.
  69. #self.drawVertex(vrtx_path, 0)
  70. for i, p in enumerate(self.points):
  71. line_path.lineTo(p)
  72. self.drawVertex(vrtx_path, i)
  73. if self.isClosed():
  74. line_path.lineTo(self.points[0])
  75. painter.drawPath(line_path)
  76. painter.drawPath(vrtx_path)
  77. painter.fillPath(vrtx_path, self.vertex_fill_color)
  78. if self.fill:
  79. color = self.select_fill_color if self.selected else self.fill_color
  80. painter.fillPath(line_path, color)
  81. def drawVertex(self, path, i):
  82. d = self.point_size / self.scale
  83. shape = self.point_type
  84. point = self.points[i]
  85. if i == self._highlightIndex:
  86. size, shape = self._highlightSettings[self._highlightMode]
  87. d *= size
  88. if shape == self.P_SQUARE:
  89. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  90. elif shape == self.P_ROUND:
  91. path.addEllipse(point, d/2.0, d/2.0)
  92. else:
  93. assert False, "unsupported vertex shape"
  94. def nearestVertex(self, point, epsilon):
  95. for i, p in enumerate(self.points):
  96. if distance(p - point) <= epsilon:
  97. return i
  98. return None
  99. def containsPoint(self, point):
  100. return self.makePath().contains(point)
  101. def makePath(self):
  102. path = QPainterPath(self.points[0])
  103. for p in self.points[1:]:
  104. path.lineTo(p)
  105. return path
  106. def boundingRect(self):
  107. return self.makePath().boundingRect()
  108. def moveBy(self, offset):
  109. self.points = [p + offset for p in self.points]
  110. def moveVertexBy(self, i, offset):
  111. self.points[i] = self.points[i] + offset
  112. def highlightVertex(self, i, action):
  113. self._highlightIndex = i
  114. self._highlightMode = action
  115. def highlightClear(self):
  116. self._highlightIndex = None
  117. def copy(self):
  118. shape = Shape("Copy of %s" % self.label )
  119. shape.points= [p for p in self.points]
  120. shape.fill = self.fill
  121. shape.selected = self.selected
  122. shape._closed = self._closed
  123. shape.moveBy(QPointF(2.0, 2.0))
  124. return shape
  125. def __len__(self):
  126. return len(self.points)
  127. def __getitem__(self, key):
  128. return self.points[key]
  129. def __setitem__(self, key, value):
  130. self.points[key] = value