canvas.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. from qtpy import QtCore
  2. from qtpy import QtGui
  3. from qtpy import QtWidgets
  4. from labelme.lib import distance
  5. from labelme import QT5
  6. from labelme.shape import Shape
  7. # TODO(unknown):
  8. # - [maybe] Find optimal epsilon value.
  9. CURSOR_DEFAULT = QtCore.Qt.ArrowCursor
  10. CURSOR_POINT = QtCore.Qt.PointingHandCursor
  11. CURSOR_DRAW = QtCore.Qt.CrossCursor
  12. CURSOR_MOVE = QtCore.Qt.ClosedHandCursor
  13. CURSOR_GRAB = QtCore.Qt.OpenHandCursor
  14. class Canvas(QtWidgets.QWidget):
  15. zoomRequest = QtCore.Signal(int, QtCore.QPoint)
  16. scrollRequest = QtCore.Signal(int, int)
  17. newShape = QtCore.Signal()
  18. selectionChanged = QtCore.Signal(bool)
  19. shapeMoved = QtCore.Signal()
  20. drawingPolygon = QtCore.Signal(bool)
  21. edgeSelected = QtCore.Signal(bool)
  22. CREATE, EDIT = 0, 1
  23. # polygon, rectangle
  24. createMode = 'polygon'
  25. epsilon = 11.0
  26. def __init__(self, *args, **kwargs):
  27. super(Canvas, self).__init__(*args, **kwargs)
  28. # Initialise local state.
  29. self.mode = self.EDIT
  30. self.shapes = []
  31. self.shapesBackups = []
  32. self.current = None
  33. self.selectedShape = None # save the selected shape here
  34. self.selectedShapeCopy = None
  35. self.lineColor = QtGui.QColor(0, 0, 255)
  36. # self.line represents
  37. # if createMode == 'polygon': edge from last point to current
  38. # if createMode == 'rectangle': the rectangle
  39. self.line = Shape(line_color=self.lineColor)
  40. self.prevPoint = QtCore.QPoint()
  41. self.prevMovePoint = QtCore.QPoint()
  42. self.offsets = QtCore.QPoint(), QtCore.QPoint()
  43. self.scale = 1.0
  44. self.pixmap = QtGui.QPixmap()
  45. self.visible = {}
  46. self._hideBackround = False
  47. self.hideBackround = False
  48. self.hShape = None
  49. self.hVertex = None
  50. self.hEdge = None
  51. self.movingShape = False
  52. self._painter = QtGui.QPainter()
  53. self._cursor = CURSOR_DEFAULT
  54. # Menus:
  55. self.menus = (QtWidgets.QMenu(), QtWidgets.QMenu())
  56. # Set widget options.
  57. self.setMouseTracking(True)
  58. self.setFocusPolicy(QtCore.Qt.WheelFocus)
  59. def storeShapes(self):
  60. shapesBackup = []
  61. for shape in self.shapes:
  62. shapesBackup.append(shape.copy())
  63. if len(self.shapesBackups) >= 10:
  64. self.shapesBackups = self.shapesBackups[-9:]
  65. self.shapesBackups.append(shapesBackup)
  66. @property
  67. def isShapeRestorable(self):
  68. if len(self.shapesBackups) < 2:
  69. return False
  70. return True
  71. def restoreShape(self):
  72. if not self.isShapeRestorable:
  73. return
  74. self.shapesBackups.pop() # latest
  75. shapesBackup = self.shapesBackups.pop()
  76. self.shapes = shapesBackup
  77. self.storeShapes()
  78. self.repaint()
  79. def enterEvent(self, ev):
  80. self.overrideCursor(self._cursor)
  81. def leaveEvent(self, ev):
  82. self.restoreCursor()
  83. def focusOutEvent(self, ev):
  84. self.restoreCursor()
  85. def isVisible(self, shape):
  86. return self.visible.get(shape, True)
  87. def drawing(self):
  88. return self.mode == self.CREATE
  89. def editing(self):
  90. return self.mode == self.EDIT
  91. def setEditing(self, value=True):
  92. self.mode = self.EDIT if value else self.CREATE
  93. if not value: # Create
  94. self.unHighlight()
  95. self.deSelectShape()
  96. def unHighlight(self):
  97. if self.hShape:
  98. self.hShape.highlightClear()
  99. self.hVertex = self.hShape = None
  100. def selectedVertex(self):
  101. return self.hVertex is not None
  102. def mouseMoveEvent(self, ev):
  103. """Update line with last point and current coordinates."""
  104. if QT5:
  105. pos = self.transformPos(ev.pos())
  106. else:
  107. pos = self.transformPos(ev.posF())
  108. self.prevMovePoint = pos
  109. self.restoreCursor()
  110. # Polygon drawing.
  111. if self.drawing():
  112. self.overrideCursor(CURSOR_DRAW)
  113. if not self.current:
  114. return
  115. color = self.lineColor
  116. if self.outOfPixmap(pos):
  117. # Don't allow the user to draw outside the pixmap.
  118. # Project the point to the pixmap's edges.
  119. pos = self.intersectionPoint(self.current[-1], pos)
  120. elif len(self.current) > 1 and \
  121. self.closeEnough(pos, self.current[0]):
  122. # Attract line to starting point and
  123. # colorise to alert the user.
  124. pos = self.current[0]
  125. color = self.current.line_color
  126. self.overrideCursor(CURSOR_POINT)
  127. self.current.highlightVertex(0, Shape.NEAR_VERTEX)
  128. if self.createMode == 'polygon':
  129. self.line[0] = self.current[-1]
  130. self.line[1] = pos
  131. elif self.createMode == 'rectangle':
  132. self.line.points = list(self.getRectangleFromLine(
  133. (self.current[0], pos)
  134. ))
  135. self.line.close()
  136. else:
  137. raise ValueError
  138. self.line.line_color = color
  139. self.repaint()
  140. self.current.highlightClear()
  141. # Polygon copy moving.
  142. if QtCore.Qt.RightButton & ev.buttons():
  143. if self.selectedShapeCopy and self.prevPoint:
  144. self.overrideCursor(CURSOR_MOVE)
  145. self.boundedMoveShape(self.selectedShapeCopy, pos)
  146. self.repaint()
  147. elif self.selectedShape:
  148. self.selectedShapeCopy = self.selectedShape.copy()
  149. self.repaint()
  150. return
  151. # Polygon/Vertex moving.
  152. self.movingShape = False
  153. if QtCore.Qt.LeftButton & ev.buttons():
  154. if self.selectedVertex():
  155. self.boundedMoveVertex(pos)
  156. self.repaint()
  157. self.movingShape = True
  158. elif self.selectedShape and self.prevPoint:
  159. self.overrideCursor(CURSOR_MOVE)
  160. self.boundedMoveShape(self.selectedShape, pos)
  161. self.repaint()
  162. self.movingShape = True
  163. return
  164. # Just hovering over the canvas, 2 posibilities:
  165. # - Highlight shapes
  166. # - Highlight vertex
  167. # Update shape/vertex fill and tooltip value accordingly.
  168. self.setToolTip("Image")
  169. for shape in reversed([s for s in self.shapes if self.isVisible(s)]):
  170. # Look for a nearby vertex to highlight. If that fails,
  171. # check if we happen to be inside a shape.
  172. index = shape.nearestVertex(pos, self.epsilon)
  173. index_edge = shape.nearestEdge(pos, self.epsilon)
  174. if index is not None:
  175. if self.selectedVertex():
  176. self.hShape.highlightClear()
  177. self.hVertex = index
  178. self.hShape = shape
  179. self.hEdge = index_edge
  180. shape.highlightVertex(index, shape.MOVE_VERTEX)
  181. self.overrideCursor(CURSOR_POINT)
  182. self.setToolTip("Click & drag to move point")
  183. self.setStatusTip(self.toolTip())
  184. self.update()
  185. break
  186. elif shape.containsPoint(pos):
  187. if self.selectedVertex():
  188. self.hShape.highlightClear()
  189. self.hVertex = None
  190. self.hShape = shape
  191. self.hEdge = index_edge
  192. self.setToolTip(
  193. "Click & drag to move shape '%s'" % shape.label)
  194. self.setStatusTip(self.toolTip())
  195. self.overrideCursor(CURSOR_GRAB)
  196. self.update()
  197. break
  198. else: # Nothing found, clear highlights, reset state.
  199. if self.hShape:
  200. self.hShape.highlightClear()
  201. self.update()
  202. self.hVertex, self.hShape, self.hEdge = None, None, None
  203. self.edgeSelected.emit(self.hEdge is not None)
  204. def addPointToEdge(self):
  205. if (self.hShape is None and
  206. self.hEdge is None and
  207. self.prevMovePoint is None):
  208. return
  209. shape = self.hShape
  210. index = self.hEdge
  211. point = self.prevMovePoint
  212. shape.insertPoint(index, point)
  213. shape.highlightVertex(index, shape.MOVE_VERTEX)
  214. self.hShape = shape
  215. self.hVertex = index
  216. self.hEdge = None
  217. def getRectangleFromLine(self, line):
  218. pt1 = line[0]
  219. pt3 = line[1]
  220. pt2 = QtCore.QPoint(pt3.x(), pt1.y())
  221. pt4 = QtCore.QPoint(pt1.x(), pt3.y())
  222. return pt1, pt2, pt3, pt4
  223. def mousePressEvent(self, ev):
  224. if QT5:
  225. pos = self.transformPos(ev.pos())
  226. else:
  227. pos = self.transformPos(ev.posF())
  228. if ev.button() == QtCore.Qt.LeftButton:
  229. if self.drawing():
  230. if self.current:
  231. # Add point to existing shape.
  232. if self.createMode == 'polygon':
  233. self.current.addPoint(self.line[1])
  234. self.line[0] = self.current[-1]
  235. if self.current.isClosed():
  236. self.finalise()
  237. elif self.createMode == 'rectangle':
  238. assert len(self.current.points) == 1
  239. self.current.points = self.line.points
  240. self.finalise()
  241. else:
  242. raise ValueError
  243. elif not self.outOfPixmap(pos):
  244. # Create new shape.
  245. self.current = Shape()
  246. self.current.addPoint(pos)
  247. self.line.points = [pos, pos]
  248. self.setHiding()
  249. self.drawingPolygon.emit(True)
  250. self.update()
  251. else:
  252. self.selectShapePoint(pos)
  253. self.prevPoint = pos
  254. self.repaint()
  255. elif ev.button() == QtCore.Qt.RightButton and self.editing():
  256. self.selectShapePoint(pos)
  257. self.prevPoint = pos
  258. self.repaint()
  259. def mouseReleaseEvent(self, ev):
  260. if ev.button() == QtCore.Qt.RightButton:
  261. menu = self.menus[bool(self.selectedShapeCopy)]
  262. self.restoreCursor()
  263. if not menu.exec_(self.mapToGlobal(ev.pos()))\
  264. and self.selectedShapeCopy:
  265. # Cancel the move by deleting the shadow copy.
  266. self.selectedShapeCopy = None
  267. self.repaint()
  268. elif ev.button() == QtCore.Qt.LeftButton and self.selectedShape:
  269. self.overrideCursor(CURSOR_GRAB)
  270. if self.movingShape:
  271. self.storeShapes()
  272. self.shapeMoved.emit()
  273. def endMove(self, copy=False):
  274. assert self.selectedShape and self.selectedShapeCopy
  275. shape = self.selectedShapeCopy
  276. # del shape.fill_color
  277. # del shape.line_color
  278. if copy:
  279. self.shapes.append(shape)
  280. self.selectedShape.selected = False
  281. self.selectedShape = shape
  282. self.repaint()
  283. else:
  284. shape.label = self.selectedShape.label
  285. self.deleteSelected()
  286. self.shapes.append(shape)
  287. self.storeShapes()
  288. self.selectedShapeCopy = None
  289. def hideBackroundShapes(self, value):
  290. self.hideBackround = value
  291. if self.selectedShape:
  292. # Only hide other shapes if there is a current selection.
  293. # Otherwise the user will not be able to select a shape.
  294. self.setHiding(True)
  295. self.repaint()
  296. def setHiding(self, enable=True):
  297. self._hideBackround = self.hideBackround if enable else False
  298. def canCloseShape(self):
  299. return self.drawing() and self.current and len(self.current) > 2
  300. def mouseDoubleClickEvent(self, ev):
  301. # We need at least 4 points here, since the mousePress handler
  302. # adds an extra one before this handler is called.
  303. if self.canCloseShape() and len(self.current) > 3:
  304. self.current.popPoint()
  305. self.finalise()
  306. def selectShape(self, shape):
  307. self.deSelectShape()
  308. shape.selected = True
  309. self.selectedShape = shape
  310. self.setHiding()
  311. self.selectionChanged.emit(True)
  312. self.update()
  313. def selectShapePoint(self, point):
  314. """Select the first shape created which contains this point."""
  315. self.deSelectShape()
  316. if self.selectedVertex(): # A vertex is marked for selection.
  317. index, shape = self.hVertex, self.hShape
  318. shape.highlightVertex(index, shape.MOVE_VERTEX)
  319. return
  320. for shape in reversed(self.shapes):
  321. if self.isVisible(shape) and shape.containsPoint(point):
  322. shape.selected = True
  323. self.selectedShape = shape
  324. self.calculateOffsets(shape, point)
  325. self.setHiding()
  326. self.selectionChanged.emit(True)
  327. return
  328. def calculateOffsets(self, shape, point):
  329. rect = shape.boundingRect()
  330. x1 = rect.x() - point.x()
  331. y1 = rect.y() - point.y()
  332. x2 = (rect.x() + rect.width()) - point.x()
  333. y2 = (rect.y() + rect.height()) - point.y()
  334. self.offsets = QtCore.QPoint(x1, y1), QtCore.QPoint(x2, y2)
  335. def boundedMoveVertex(self, pos):
  336. index, shape = self.hVertex, self.hShape
  337. point = shape[index]
  338. if self.outOfPixmap(pos):
  339. pos = self.intersectionPoint(point, pos)
  340. shape.moveVertexBy(index, pos - point)
  341. def boundedMoveShape(self, shape, pos):
  342. if self.outOfPixmap(pos):
  343. return False # No need to move
  344. o1 = pos + self.offsets[0]
  345. if self.outOfPixmap(o1):
  346. pos -= QtCore.QPoint(min(0, o1.x()), min(0, o1.y()))
  347. o2 = pos + self.offsets[1]
  348. if self.outOfPixmap(o2):
  349. pos += QtCore.QPoint(min(0, self.pixmap.width() - o2.x()),
  350. min(0, self.pixmap.height() - o2.y()))
  351. # XXX: The next line tracks the new position of the cursor
  352. # relative to the shape, but also results in making it
  353. # a bit "shaky" when nearing the border and allows it to
  354. # go outside of the shape's area for some reason.
  355. # self.calculateOffsets(self.selectedShape, pos)
  356. dp = pos - self.prevPoint
  357. if dp:
  358. shape.moveBy(dp)
  359. self.prevPoint = pos
  360. return True
  361. return False
  362. def deSelectShape(self):
  363. if self.selectedShape:
  364. self.selectedShape.selected = False
  365. self.selectedShape = None
  366. self.setHiding(False)
  367. self.selectionChanged.emit(False)
  368. self.update()
  369. def deleteSelected(self):
  370. if self.selectedShape:
  371. shape = self.selectedShape
  372. self.shapes.remove(self.selectedShape)
  373. self.storeShapes()
  374. self.selectedShape = None
  375. self.update()
  376. return shape
  377. def copySelectedShape(self):
  378. if self.selectedShape:
  379. shape = self.selectedShape.copy()
  380. self.deSelectShape()
  381. self.shapes.append(shape)
  382. self.storeShapes()
  383. shape.selected = True
  384. self.selectedShape = shape
  385. self.boundedShiftShape(shape)
  386. return shape
  387. def boundedShiftShape(self, shape):
  388. # Try to move in one direction, and if it fails in another.
  389. # Give up if both fail.
  390. point = shape[0]
  391. offset = QtCore.QPoint(2.0, 2.0)
  392. self.calculateOffsets(shape, point)
  393. self.prevPoint = point
  394. if not self.boundedMoveShape(shape, point - offset):
  395. self.boundedMoveShape(shape, point + offset)
  396. def paintEvent(self, event):
  397. if not self.pixmap:
  398. return super(Canvas, self).paintEvent(event)
  399. p = self._painter
  400. p.begin(self)
  401. p.setRenderHint(QtGui.QPainter.Antialiasing)
  402. p.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
  403. p.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
  404. p.scale(self.scale, self.scale)
  405. p.translate(self.offsetToCenter())
  406. p.drawPixmap(0, 0, self.pixmap)
  407. Shape.scale = self.scale
  408. for shape in self.shapes:
  409. if (shape.selected or not self._hideBackround) and \
  410. self.isVisible(shape):
  411. shape.fill = shape.selected or shape == self.hShape
  412. shape.paint(p)
  413. if self.current:
  414. self.current.paint(p)
  415. self.line.paint(p)
  416. if self.selectedShapeCopy:
  417. self.selectedShapeCopy.paint(p)
  418. p.end()
  419. def transformPos(self, point):
  420. """Convert from widget-logical coordinates to painter-logical ones."""
  421. return point / self.scale - self.offsetToCenter()
  422. def offsetToCenter(self):
  423. s = self.scale
  424. area = super(Canvas, self).size()
  425. w, h = self.pixmap.width() * s, self.pixmap.height() * s
  426. aw, ah = area.width(), area.height()
  427. x = (aw - w) / (2 * s) if aw > w else 0
  428. y = (ah - h) / (2 * s) if ah > h else 0
  429. return QtCore.QPoint(x, y)
  430. def outOfPixmap(self, p):
  431. w, h = self.pixmap.width(), self.pixmap.height()
  432. return not (0 <= p.x() <= w and 0 <= p.y() <= h)
  433. def finalise(self):
  434. assert self.current
  435. self.current.close()
  436. self.shapes.append(self.current)
  437. self.storeShapes()
  438. self.current = None
  439. self.setHiding(False)
  440. self.newShape.emit()
  441. self.update()
  442. def closeEnough(self, p1, p2):
  443. # d = distance(p1 - p2)
  444. # m = (p1-p2).manhattanLength()
  445. # print "d %.2f, m %d, %.2f" % (d, m, d - m)
  446. return distance(p1 - p2) < self.epsilon
  447. def intersectionPoint(self, p1, p2):
  448. # Cycle through each image edge in clockwise fashion,
  449. # and find the one intersecting the current line segment.
  450. # http://paulbourke.net/geometry/lineline2d/
  451. size = self.pixmap.size()
  452. points = [(0, 0),
  453. (size.width(), 0),
  454. (size.width(), size.height()),
  455. (0, size.height())]
  456. x1, y1 = p1.x(), p1.y()
  457. x2, y2 = p2.x(), p2.y()
  458. d, i, (x, y) = min(self.intersectingEdges((x1, y1), (x2, y2), points))
  459. x3, y3 = points[i]
  460. x4, y4 = points[(i + 1) % 4]
  461. if (x, y) == (x1, y1):
  462. # Handle cases where previous point is on one of the edges.
  463. if x3 == x4:
  464. return QtCore.QPoint(x3, min(max(0, y2), max(y3, y4)))
  465. else: # y3 == y4
  466. return QtCore.QPoint(min(max(0, x2), max(x3, x4)), y3)
  467. return QtCore.QPoint(x, y)
  468. def intersectingEdges(self, point1, point2, points):
  469. """Find intersecting edges.
  470. For each edge formed by `points', yield the intersection
  471. with the line segment `(x1,y1) - (x2,y2)`, if it exists.
  472. Also return the distance of `(x2,y2)' to the middle of the
  473. edge along with its index, so that the one closest can be chosen.
  474. """
  475. (x1, y1) = point1
  476. (x2, y2) = point2
  477. for i in range(4):
  478. x3, y3 = points[i]
  479. x4, y4 = points[(i + 1) % 4]
  480. denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
  481. nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)
  482. nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)
  483. if denom == 0:
  484. # This covers two cases:
  485. # nua == nub == 0: Coincident
  486. # otherwise: Parallel
  487. continue
  488. ua, ub = nua / denom, nub / denom
  489. if 0 <= ua <= 1 and 0 <= ub <= 1:
  490. x = x1 + ua * (x2 - x1)
  491. y = y1 + ua * (y2 - y1)
  492. m = QtCore.QPoint((x3 + x4) / 2, (y3 + y4) / 2)
  493. d = distance(m - QtCore.QPoint(x2, y2))
  494. yield d, i, (x, y)
  495. # These two, along with a call to adjustSize are required for the
  496. # scroll area.
  497. def sizeHint(self):
  498. return self.minimumSizeHint()
  499. def minimumSizeHint(self):
  500. if self.pixmap:
  501. return self.scale * self.pixmap.size()
  502. return super(Canvas, self).minimumSizeHint()
  503. def wheelEvent(self, ev):
  504. if QT5:
  505. mods = ev.modifiers()
  506. delta = ev.angleDelta()
  507. if QtCore.Qt.ControlModifier == int(mods):
  508. # with Ctrl/Command key
  509. # zoom
  510. self.zoomRequest.emit(delta.y(), ev.pos())
  511. else:
  512. # scroll
  513. self.scrollRequest.emit(delta.x(), QtCore.Qt.Horizontal)
  514. self.scrollRequest.emit(delta.y(), QtCore.Qt.Vertical)
  515. else:
  516. if ev.orientation() == QtCore.Qt.Vertical:
  517. mods = ev.modifiers()
  518. if QtCore.Qt.ControlModifier == int(mods):
  519. # with Ctrl/Command key
  520. self.zoomRequest.emit(ev.delta(), ev.pos())
  521. else:
  522. self.scrollRequest.emit(
  523. ev.delta(),
  524. QtCore.Qt.Horizontal
  525. if (QtCore.Qt.ShiftModifier == int(mods))
  526. else QtCore.Qt.Vertical)
  527. else:
  528. self.scrollRequest.emit(ev.delta(), QtCore.Qt.Horizontal)
  529. ev.accept()
  530. def keyPressEvent(self, ev):
  531. key = ev.key()
  532. if key == QtCore.Qt.Key_Escape and self.current:
  533. self.current = None
  534. self.drawingPolygon.emit(False)
  535. self.update()
  536. elif key == QtCore.Qt.Key_Return and self.canCloseShape():
  537. self.finalise()
  538. def setLastLabel(self, text):
  539. assert text
  540. self.shapes[-1].label = text
  541. self.shapesBackups.pop()
  542. self.storeShapes()
  543. return self.shapes[-1]
  544. def undoLastLine(self):
  545. assert self.shapes
  546. self.current = self.shapes.pop()
  547. self.current.setOpen()
  548. if self.createMode == 'polygon':
  549. self.line.points = [self.current[-1], self.current[0]]
  550. elif self.createMode == 'rectangle':
  551. self.current.points = self.current.points[0:1]
  552. else:
  553. raise ValueError
  554. self.drawingPolygon.emit(True)
  555. def undoLastPoint(self):
  556. if not self.current or self.current.isClosed():
  557. return
  558. self.current.popPoint()
  559. if len(self.current) > 0:
  560. self.line[0] = self.current[-1]
  561. else:
  562. self.current = None
  563. self.drawingPolygon.emit(False)
  564. self.repaint()
  565. def loadPixmap(self, pixmap):
  566. self.pixmap = pixmap
  567. self.shapes = []
  568. self.repaint()
  569. def loadShapes(self, shapes):
  570. self.shapes = list(shapes)
  571. self.storeShapes()
  572. self.current = None
  573. self.repaint()
  574. def setShapeVisible(self, shape, value):
  575. self.visible[shape] = value
  576. self.repaint()
  577. def overrideCursor(self, cursor):
  578. self.restoreCursor()
  579. self._cursor = cursor
  580. QtWidgets.QApplication.setOverrideCursor(cursor)
  581. def restoreCursor(self):
  582. QtWidgets.QApplication.restoreOverrideCursor()
  583. def resetState(self):
  584. self.restoreCursor()
  585. self.pixmap = None
  586. self.shapesBackups = []
  587. self.update()