Эх сурвалжийг харах

Experiment with persistent draw mode

The user has to explicitly opt out of draw mode now, by toggling the flag.

Fix a few cursor related bugs as well.
Michael Pitidis 13 жил өмнө
parent
commit
34eea4385c
2 өөрчлөгдсөн 40 нэмэгдсэн , 27 устгасан
  1. 29 21
      canvas.py
  2. 11 6
      labelme.py

+ 29 - 21
canvas.py

@@ -25,6 +25,7 @@ class Canvas(QWidget):
     newShape = pyqtSignal(QPoint)
     selectionChanged = pyqtSignal(bool)
     shapeMoved = pyqtSignal()
+    drawMode = pyqtSignal(bool)
 
     SELECT, EDIT = range(2)
 
@@ -50,12 +51,19 @@ class Canvas(QWidget):
         self.hideBackround = False
         self.highlightedShape = None
         self._painter = QPainter()
+        self._cursor = CURSOR_DEFAULT
         # Menus:
         self.menus = (QMenu(), QMenu())
         # Set widget options.
         self.setMouseTracking(True)
         self.setFocusPolicy(Qt.WheelFocus)
 
+    def enterEvent(self, ev):
+        self.overrideCursor(self._cursor)
+
+    def leaveEvent(self, ev):
+        self.restoreCursor()
+
     def focusOutEvent(self, ev):
         self.restoreCursor()
 
@@ -74,6 +82,24 @@ class Canvas(QWidget):
 
         self.restoreCursor()
 
+        # Polygon drawing.
+        if self.editing():
+            self.overrideCursor(CURSOR_DRAW)
+            if self.current:
+                color = self.lineColor
+                if self.outOfPixmap(pos):
+                    # Don't allow the user to draw outside the pixmap.
+                    # Project the point to the pixmap's edges.
+                    pos = self.intersectionPoint(self.current[-1], pos)
+                elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
+                    # Attract line to starting point and colorise to alert the user:
+                    pos = self.current[0]
+                    color = self.current.line_color
+                self.line[1] = pos
+                self.line.line_color = color
+                self.repaint()
+            return
+
         # Polygon copy moving.
         if Qt.RightButton & ev.buttons():
             if self.selectedShapeCopy and self.prevPoint:
@@ -85,25 +111,6 @@ class Canvas(QWidget):
                 self.repaint()
             return
 
-        # Polygon drawing.
-        if self.editing():
-            self.overrideCursor(CURSOR_DRAW)
-
-        if self.current and self.editing():
-            color = self.lineColor
-            if self.outOfPixmap(pos):
-                # Don't allow the user to draw outside the pixmap.
-                # Project the point to the pixmap's edges.
-                pos = self.intersectionPoint(self.current[-1], pos)
-            elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]):
-                # Attract line to starting point and colorise to alert the user:
-                pos = self.current[0]
-                color = self.current.line_color
-            self.line[1] = pos
-            self.line.line_color = color
-            self.repaint()
-            return
-
         # Polygon moving.
         if Qt.LeftButton & ev.buttons() and self.selectedShape and self.prevPoint:
             self.overrideCursor(CURSOR_MOVE)
@@ -144,6 +151,7 @@ class Canvas(QWidget):
                     self.line.points = [pos, pos]
                     self.setHiding()
                     self.repaint()
+                    self.drawMode.emit(True)
             else:
                 self.selectShapePoint(pos)
                 self.prevPoint = pos
@@ -322,7 +330,6 @@ class Canvas(QWidget):
         assert self.current
         self.shapes.append(self.current)
         self.current = None
-        self.setEditing(False)
         self.setHiding(False)
         self.repaint()
         self.newShape.emit(self.mapToGlobal(ev.pos()))
@@ -406,6 +413,7 @@ class Canvas(QWidget):
         if ev.key() == Qt.Key_Escape and self.current:
             self.current = None
             self.repaint()
+            self.drawMode.emit(False)
 
     def setLastLabel(self, text):
         assert text
@@ -449,12 +457,12 @@ class Canvas(QWidget):
         self.repaint()
 
     def overrideCursor(self, cursor):
+        self._cursor = cursor
         QApplication.setOverrideCursor(cursor)
 
     def restoreCursor(self):
         QApplication.restoreOverrideCursor()
 
-
     def resetState(self):
         self.restoreCursor()
         self.pixmap = None

+ 11 - 6
labelme.py

@@ -112,6 +112,7 @@ class MainWindow(QMainWindow, WindowMixin):
         self.canvas.newShape.connect(self.newShape)
         self.canvas.shapeMoved.connect(self.setDirty)
         self.canvas.selectionChanged.connect(self.shapeSelectionChanged)
+        self.canvas.drawMode.connect(self.drawMode)
 
         self.setCentralWidget(scroll)
         self.addDockWidget(Qt.RightDockWidgetArea, self.dock)
@@ -130,8 +131,9 @@ class MainWindow(QMainWindow, WindowMixin):
                 'Ctrl+C', 'color', u'Choose polygon line color')
         color2 = action('Polygon &Fill Color', self.chooseColor2,
                 'Ctrl+Shift+C', 'color', u'Choose polygon fill color')
-        label = action('&New Polygon', self.newLabel,
-                'Ctrl+N', 'new', u'Start a new polygon', enabled=False)
+        label = action('&Draw Polygon', self.newLabel,
+                'Ctrl+N', 'new', u'Start a new polygon',
+                checkable=True, enabled=False)
         copy = action('&Copy Polygon', self.copySelectedShape,
                 'Ctrl+C', 'copy', u'Copy selected polygon', enabled=False)
         delete = action('&Delete Polygon', self.deleteSelectedShape,
@@ -612,10 +614,13 @@ class MainWindow(QMainWindow, WindowMixin):
             self.canvas.update()
             self.setDirty()
 
-    def newLabel(self):
-        self.canvas.deSelectShape()
-        self.canvas.setEditing()
-        self.actions.label.setEnabled(False)
+    def newLabel(self, value=True):
+        if value:
+            self.canvas.deSelectShape()
+        self.canvas.setEditing(value)
+
+    def drawMode(self, started=False):
+        self.actions.label.setEnabled(not started)
 
     def deleteSelectedShape(self):
         yes, no = QMessageBox.Yes, QMessageBox.No