shape.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 paint(self, painter):
  55. if self.points:
  56. color = self.select_line_color if self.selected else self.line_color
  57. pen = QPen(color)
  58. # Try using integer sizes for smoother drawing(?)
  59. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  60. painter.setPen(pen)
  61. line_path = QPainterPath()
  62. vrtx_path = QPainterPath()
  63. line_path.moveTo(self.points[0])
  64. # Uncommenting the following line will draw 2 paths
  65. # for the 1st vertex, and make it non-filled, which
  66. # may be desirable.
  67. #self.drawVertex(vrtx_path, 0)
  68. for i, p in enumerate(self.points):
  69. line_path.lineTo(p)
  70. self.drawVertex(vrtx_path, i)
  71. if self.isClosed():
  72. line_path.lineTo(self.points[0])
  73. painter.drawPath(line_path)
  74. painter.drawPath(vrtx_path)
  75. painter.fillPath(vrtx_path, self.vertex_fill_color)
  76. if self.fill:
  77. color = self.select_fill_color if self.selected else self.fill_color
  78. painter.fillPath(line_path, color)
  79. def drawVertex(self, path, i):
  80. d = self.point_size / self.scale
  81. shape = self.point_type
  82. point = self.points[i]
  83. if i == self._highlightIndex:
  84. size, shape = self._highlightSettings[self._highlightMode]
  85. d *= size
  86. if shape == self.P_SQUARE:
  87. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  88. elif shape == self.P_ROUND:
  89. path.addEllipse(point, d/2.0, d/2.0)
  90. else:
  91. assert False, "unsupported vertex shape"
  92. def nearestVertex(self, point, epsilon):
  93. for i, p in enumerate(self.points):
  94. if distance(p - point) <= epsilon:
  95. return i
  96. return None
  97. def containsPoint(self, point):
  98. return self.makePath().contains(point)
  99. def makePath(self):
  100. path = QPainterPath(self.points[0])
  101. for p in self.points[1:]:
  102. path.lineTo(p)
  103. return path
  104. def boundingRect(self):
  105. return self.makePath().boundingRect()
  106. def moveBy(self, offset):
  107. self.points = [p + offset for p in self.points]
  108. def moveVertexBy(self, i, offset):
  109. self.points[i] = self.points[i] + offset
  110. def highlightVertex(self, i, action):
  111. self._highlightIndex = i
  112. self._highlightMode = action
  113. def highlightClear(self):
  114. self._highlightIndex = None
  115. def copy(self):
  116. shape = Shape("Copy of %s" % self.label )
  117. shape.points= [p for p in self.points]
  118. shape.fill = self.fill
  119. shape.selected = self.selected
  120. return shape
  121. def __len__(self):
  122. return len(self.points)
  123. def __getitem__(self, key):
  124. return self.points[key]
  125. def __setitem__(self, key, value):
  126. self.points[key] = value