shape.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  5. #
  6. # This file is part of Labelme.
  7. #
  8. # Labelme is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # Labelme is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. from PyQt4.QtGui import *
  22. from PyQt4.QtCore import *
  23. from lib import distance
  24. # TODO:
  25. # - [opt] Store paths instead of creating new ones at each paint.
  26. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  27. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  28. DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
  29. DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
  30. DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
  31. DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
  32. class Shape(object):
  33. P_SQUARE, P_ROUND = range(2)
  34. MOVE_VERTEX, NEAR_VERTEX = range(2)
  35. ## The following class variables influence the drawing
  36. ## of _all_ shape objects.
  37. line_color = DEFAULT_LINE_COLOR
  38. fill_color = DEFAULT_FILL_COLOR
  39. select_line_color = DEFAULT_SELECT_LINE_COLOR
  40. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  41. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  42. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  43. point_type = P_ROUND
  44. point_size = 8
  45. scale = 1.0
  46. def __init__(self, label=None, line_color=None):
  47. self.label = label
  48. self.points = []
  49. self.fill = False
  50. self.selected = False
  51. self._highlightIndex = None
  52. self._highlightMode = self.NEAR_VERTEX
  53. self._highlightSettings = {
  54. self.NEAR_VERTEX: (4, self.P_ROUND),
  55. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  56. }
  57. self._closed = False
  58. if line_color is not None:
  59. # Override the class line_color attribute
  60. # with an object attribute. Currently this
  61. # is used for drawing the pending line a different color.
  62. self.line_color = line_color
  63. def close(self):
  64. assert len(self.points) > 2
  65. self._closed = True
  66. def addPoint(self, point):
  67. if self.points and point == self.points[0]:
  68. self.close()
  69. else:
  70. self.points.append(point)
  71. def popPoint(self):
  72. if self.points:
  73. return self.points.pop()
  74. return None
  75. def isClosed(self):
  76. return self._closed
  77. def setOpen(self):
  78. self._closed = False
  79. def paint(self, painter):
  80. if self.points:
  81. color = self.select_line_color if self.selected else self.line_color
  82. pen = QPen(color)
  83. # Try using integer sizes for smoother drawing(?)
  84. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  85. painter.setPen(pen)
  86. line_path = QPainterPath()
  87. vrtx_path = QPainterPath()
  88. line_path.moveTo(self.points[0])
  89. # Uncommenting the following line will draw 2 paths
  90. # for the 1st vertex, and make it non-filled, which
  91. # may be desirable.
  92. #self.drawVertex(vrtx_path, 0)
  93. for i, p in enumerate(self.points):
  94. line_path.lineTo(p)
  95. self.drawVertex(vrtx_path, i)
  96. if self.isClosed():
  97. line_path.lineTo(self.points[0])
  98. painter.drawPath(line_path)
  99. painter.drawPath(vrtx_path)
  100. painter.fillPath(vrtx_path, self.vertex_fill_color)
  101. if self.fill:
  102. color = self.select_fill_color if self.selected else self.fill_color
  103. painter.fillPath(line_path, color)
  104. def drawVertex(self, path, i):
  105. d = self.point_size / self.scale
  106. shape = self.point_type
  107. point = self.points[i]
  108. if i == self._highlightIndex:
  109. size, shape = self._highlightSettings[self._highlightMode]
  110. d *= size
  111. if self._highlightIndex is not None:
  112. self.vertex_fill_color = self.hvertex_fill_color
  113. else:
  114. self.vertex_fill_color = Shape.vertex_fill_color
  115. if shape == self.P_SQUARE:
  116. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  117. elif shape == self.P_ROUND:
  118. path.addEllipse(point, d/2.0, d/2.0)
  119. else:
  120. assert False, "unsupported vertex shape"
  121. def nearestVertex(self, point, epsilon):
  122. for i, p in enumerate(self.points):
  123. if distance(p - point) <= epsilon:
  124. return i
  125. return None
  126. def containsPoint(self, point):
  127. return self.makePath().contains(point)
  128. def makePath(self):
  129. path = QPainterPath(self.points[0])
  130. for p in self.points[1:]:
  131. path.lineTo(p)
  132. return path
  133. def boundingRect(self):
  134. return self.makePath().boundingRect()
  135. def moveBy(self, offset):
  136. self.points = [p + offset for p in self.points]
  137. def moveVertexBy(self, i, offset):
  138. self.points[i] = self.points[i] + offset
  139. def highlightVertex(self, i, action):
  140. self._highlightIndex = i
  141. self._highlightMode = action
  142. def highlightClear(self):
  143. self._highlightIndex = None
  144. def copy(self):
  145. shape = Shape("Copy of %s" % self.label )
  146. shape.points= [p for p in self.points]
  147. shape.fill = self.fill
  148. shape.selected = self.selected
  149. shape._closed = self._closed
  150. if self.line_color != Shape.line_color:
  151. shape.line_color = self.line_color
  152. if self.fill_color != Shape.fill_color:
  153. shape.fill_color = self.fill_color
  154. return shape
  155. def __len__(self):
  156. return len(self.points)
  157. def __getitem__(self, key):
  158. return self.points[key]
  159. def __setitem__(self, key, value):
  160. self.points[key] = value