shape.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. #!/usr/bin/python
  14. # -*- coding: utf-8 -*-
  15. try:
  16. from PyQt5.QtGui import *
  17. from PyQt5.QtCore import *
  18. except ImportError:
  19. from PyQt4.QtGui import *
  20. from PyQt4.QtCore import *
  21. from libs.utils import distance
  22. import sys
  23. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  24. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  25. DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
  26. DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
  27. DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
  28. DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
  29. MIN_Y_LABEL = 10
  30. class Shape(object):
  31. P_SQUARE, P_ROUND = range(2)
  32. MOVE_VERTEX, NEAR_VERTEX = range(2)
  33. # The following class variables influence the drawing
  34. # of _all_ shape objects.
  35. line_color = DEFAULT_LINE_COLOR
  36. fill_color = DEFAULT_FILL_COLOR
  37. select_line_color = DEFAULT_SELECT_LINE_COLOR
  38. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  39. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  40. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  41. point_type = P_ROUND
  42. point_size = 8
  43. scale = 1.0
  44. def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False):
  45. self.label = label
  46. self.points = []
  47. self.fill = False
  48. self.selected = False
  49. self.difficult = difficult
  50. self.paintLabel = paintLabel
  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. self._closed = True
  65. def reachMaxPoints(self):
  66. if len(self.points) >= 4:
  67. return True
  68. return False
  69. def addPoint(self, point):
  70. if not self.reachMaxPoints():
  71. self.points.append(point)
  72. def popPoint(self):
  73. if self.points:
  74. return self.points.pop()
  75. return None
  76. def isClosed(self):
  77. return self._closed
  78. def setOpen(self):
  79. self._closed = False
  80. def paint(self, painter):
  81. if self.points:
  82. color = self.select_line_color if self.selected else self.line_color
  83. pen = QPen(color)
  84. # Try using integer sizes for smoother drawing(?)
  85. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  86. painter.setPen(pen)
  87. line_path = QPainterPath()
  88. vrtx_path = QPainterPath()
  89. line_path.moveTo(self.points[0])
  90. # Uncommenting the following line will draw 2 paths
  91. # for the 1st vertex, and make it non-filled, which
  92. # may be desirable.
  93. #self.drawVertex(vrtx_path, 0)
  94. for i, p in enumerate(self.points):
  95. line_path.lineTo(p)
  96. self.drawVertex(vrtx_path, i)
  97. if self.isClosed():
  98. line_path.lineTo(self.points[0])
  99. painter.drawPath(line_path)
  100. painter.drawPath(vrtx_path)
  101. painter.fillPath(vrtx_path, self.vertex_fill_color)
  102. # Draw text at the top-left
  103. if self.paintLabel:
  104. min_x = sys.maxsize
  105. min_y = sys.maxsize
  106. for point in self.points:
  107. min_x = min(min_x, point.x())
  108. min_y = min(min_y, point.y())
  109. if min_x != sys.maxsize and min_y != sys.maxsize:
  110. font = QFont()
  111. font.setPointSize(8)
  112. font.setBold(True)
  113. painter.setFont(font)
  114. if(self.label == None):
  115. self.label = ""
  116. if(min_y < MIN_Y_LABEL):
  117. min_y += MIN_Y_LABEL
  118. painter.drawText(min_x, min_y, self.label)
  119. if self.fill:
  120. color = self.select_fill_color if self.selected else self.fill_color
  121. painter.fillPath(line_path, color)
  122. def drawVertex(self, path, i):
  123. d = self.point_size / self.scale
  124. shape = self.point_type
  125. point = self.points[i]
  126. if i == self._highlightIndex:
  127. size, shape = self._highlightSettings[self._highlightMode]
  128. d *= size
  129. if self._highlightIndex is not None:
  130. self.vertex_fill_color = self.hvertex_fill_color
  131. else:
  132. self.vertex_fill_color = Shape.vertex_fill_color
  133. if shape == self.P_SQUARE:
  134. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  135. elif shape == self.P_ROUND:
  136. path.addEllipse(point, d / 2.0, d / 2.0)
  137. else:
  138. assert False, "unsupported vertex shape"
  139. def nearestVertex(self, point, epsilon):
  140. for i, p in enumerate(self.points):
  141. if distance(p - point) <= epsilon:
  142. return i
  143. return None
  144. def containsPoint(self, point):
  145. return self.makePath().contains(point)
  146. def makePath(self):
  147. path = QPainterPath(self.points[0])
  148. for p in self.points[1:]:
  149. path.lineTo(p)
  150. return path
  151. def boundingRect(self):
  152. return self.makePath().boundingRect()
  153. def moveBy(self, offset):
  154. self.points = [p + offset for p in self.points]
  155. def moveVertexBy(self, i, offset):
  156. self.points[i] = self.points[i] + offset
  157. def highlightVertex(self, i, action):
  158. self._highlightIndex = i
  159. self._highlightMode = action
  160. def highlightClear(self):
  161. self._highlightIndex = None
  162. def copy(self):
  163. shape = Shape("%s" % self.label)
  164. shape.points = [p for p in self.points]
  165. shape.fill = self.fill
  166. shape.selected = self.selected
  167. shape._closed = self._closed
  168. if self.line_color != Shape.line_color:
  169. shape.line_color = self.line_color
  170. if self.fill_color != Shape.fill_color:
  171. shape.fill_color = self.fill_color
  172. shape.difficult = self.difficult
  173. return shape
  174. def __len__(self):
  175. return len(self.points)
  176. def __getitem__(self, key):
  177. return self.points[key]
  178. def __setitem__(self, key, value):
  179. self.points[key] = value