shape.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. try:
  22. from PyQt5.QtGui import *
  23. from PyQt5.QtCore import *
  24. except ImportError:
  25. from PyQt4.QtGui import *
  26. from PyQt4.QtCore import *
  27. from .lib import distance
  28. # TODO:
  29. # - [opt] Store paths instead of creating new ones at each paint.
  30. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  31. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  32. DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
  33. DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
  34. DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
  35. DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
  36. class Shape(object):
  37. P_SQUARE, P_ROUND = 0, 1
  38. MOVE_VERTEX, NEAR_VERTEX = 0, 1
  39. ## The following class variables influence the drawing
  40. ## of _all_ shape objects.
  41. line_color = DEFAULT_LINE_COLOR
  42. fill_color = DEFAULT_FILL_COLOR
  43. select_line_color = DEFAULT_SELECT_LINE_COLOR
  44. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  45. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  46. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  47. point_type = P_ROUND
  48. point_size = 8
  49. scale = 1.0
  50. def __init__(self, label=None, line_color=None):
  51. self.label = label
  52. self.points = []
  53. self.fill = False
  54. self.selected = False
  55. self._highlightIndex = None
  56. self._highlightMode = self.NEAR_VERTEX
  57. self._highlightSettings = {
  58. self.NEAR_VERTEX: (4, self.P_ROUND),
  59. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  60. }
  61. self._closed = False
  62. if line_color is not None:
  63. # Override the class line_color attribute
  64. # with an object attribute. Currently this
  65. # is used for drawing the pending line a different color.
  66. self.line_color = line_color
  67. def close(self):
  68. assert len(self.points) > 2
  69. self._closed = True
  70. def addPoint(self, point):
  71. if self.points and point == self.points[0]:
  72. self.close()
  73. else:
  74. self.points.append(point)
  75. def popPoint(self):
  76. if self.points:
  77. return self.points.pop()
  78. return None
  79. def isClosed(self):
  80. return self._closed
  81. def setOpen(self):
  82. self._closed = False
  83. def paint(self, painter):
  84. if self.points:
  85. color = self.select_line_color if self.selected else self.line_color
  86. pen = QPen(color)
  87. # Try using integer sizes for smoother drawing(?)
  88. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  89. painter.setPen(pen)
  90. line_path = QPainterPath()
  91. vrtx_path = QPainterPath()
  92. line_path.moveTo(self.points[0])
  93. # Uncommenting the following line will draw 2 paths
  94. # for the 1st vertex, and make it non-filled, which
  95. # may be desirable.
  96. #self.drawVertex(vrtx_path, 0)
  97. for i, p in enumerate(self.points):
  98. line_path.lineTo(p)
  99. self.drawVertex(vrtx_path, i)
  100. if self.isClosed():
  101. line_path.lineTo(self.points[0])
  102. painter.drawPath(line_path)
  103. painter.drawPath(vrtx_path)
  104. painter.fillPath(vrtx_path, self.vertex_fill_color)
  105. if self.fill:
  106. color = self.select_fill_color if self.selected else self.fill_color
  107. painter.fillPath(line_path, color)
  108. def drawVertex(self, path, i):
  109. d = self.point_size / self.scale
  110. shape = self.point_type
  111. point = self.points[i]
  112. if i == self._highlightIndex:
  113. size, shape = self._highlightSettings[self._highlightMode]
  114. d *= size
  115. if self._highlightIndex is not None:
  116. self.vertex_fill_color = self.hvertex_fill_color
  117. else:
  118. self.vertex_fill_color = Shape.vertex_fill_color
  119. if shape == self.P_SQUARE:
  120. path.addRect(point.x() - d/2, point.y() - d/2, d, d)
  121. elif shape == self.P_ROUND:
  122. path.addEllipse(point, d/2.0, d/2.0)
  123. else:
  124. assert False, "unsupported vertex shape"
  125. def nearestVertex(self, point, epsilon):
  126. for i, p in enumerate(self.points):
  127. if distance(p - point) <= epsilon:
  128. return i
  129. return None
  130. def containsPoint(self, point):
  131. return self.makePath().contains(point)
  132. def makePath(self):
  133. path = QPainterPath(self.points[0])
  134. for p in self.points[1:]:
  135. path.lineTo(p)
  136. return path
  137. def boundingRect(self):
  138. return self.makePath().boundingRect()
  139. def moveBy(self, offset):
  140. self.points = [p + offset for p in self.points]
  141. def moveVertexBy(self, i, offset):
  142. self.points[i] = self.points[i] + offset
  143. def highlightVertex(self, i, action):
  144. self._highlightIndex = i
  145. self._highlightMode = action
  146. def highlightClear(self):
  147. self._highlightIndex = None
  148. def copy(self):
  149. shape = Shape("Copy of %s" % self.label )
  150. shape.points= [p for p in self.points]
  151. shape.fill = self.fill
  152. shape.selected = self.selected
  153. shape._closed = self._closed
  154. if self.line_color != Shape.line_color:
  155. shape.line_color = self.line_color
  156. if self.fill_color != Shape.fill_color:
  157. shape.fill_color = self.fill_color
  158. return shape
  159. def __len__(self):
  160. return len(self.points)
  161. def __getitem__(self, key):
  162. return self.points[key]
  163. def __setitem__(self, key, value):
  164. self.points[key] = value