فهرست منبع

Add Return as a shortcut for closing a shape

Fix off by one error in double click event handler and refactor closing
and finalising code.
Michael Pitidis 13 سال پیش
والد
کامیت
1867977598
3فایلهای تغییر یافته به همراه28 افزوده شده و 19 حذف شده
  1. 19 14
      canvas.py
  2. 2 2
      labelme.py
  3. 7 3
      shape.py

+ 19 - 14
canvas.py

@@ -176,7 +176,7 @@ class Canvas(QWidget):
                     self.current.addPoint(self.line[1])
                     self.line[0] = self.current[-1]
                     if self.current.isClosed():
-                        self.finalise(ev)
+                        self.finalise()
                 elif not self.outOfPixmap(pos):
                     self.current = Shape()
                     self.current.addPoint(pos)
@@ -232,17 +232,15 @@ class Canvas(QWidget):
     def setHiding(self, enable=True):
         self._hideBackround = self.hideBackround if enable else False
 
+    def canCloseShape(self):
+        return self.drawing() and self.current and len(self.current) > 2
+
     def mouseDoubleClickEvent(self, ev):
-        if self.current and self.drawing():
-            # Shapes need to have at least 3 vertices.
-            if len(self.current) < 4:
-                return
-            # Replace the last point with the starting point.
-            # We have to do this because the mousePressEvent handler
-            # adds that point before this handler is called!
+        # We need at least 4 points here, since the mousePress handler
+        # adds an extra one before this handler is called.
+        if self.canCloseShape() and len(self.current) > 3:
             self.current.popPoint()
-            self.current.addPoint(self.current[0])
-            self.finalise(ev)
+            self.finalise()
 
     def selectShape(self, shape):
         self.deSelectShape()
@@ -385,13 +383,14 @@ class Canvas(QWidget):
         w, h = self.pixmap.width(), self.pixmap.height()
         return not (0 <= p.x() <= w and 0 <= p.y() <= h)
 
-    def finalise(self, ev):
+    def finalise(self):
         assert self.current
+        self.current.close()
         self.shapes.append(self.current)
         self.current = None
         self.setHiding(False)
-        self.repaint()
-        self.newShape.emit(self.mapToGlobal(ev.pos()))
+        self.newShape.emit(self.mapToGlobal(toPoint(self.line[1])))
+        self.update()
 
     def closeEnough(self, p1, p2):
         #d = distance(p1 - p2)
@@ -469,10 +468,13 @@ class Canvas(QWidget):
         ev.accept()
 
     def keyPressEvent(self, ev):
-        if ev.key() == Qt.Key_Escape and self.current:
+        key = ev.key()
+        if key == Qt.Key_Escape and self.current:
             self.current = None
             self.drawingPolygon.emit(False)
             self.update()
+        elif key == Qt.Key_Return and self.canCloseShape():
+            self.finalise()
 
     def setLastLabel(self, text):
         assert text
@@ -519,6 +521,9 @@ class Canvas(QWidget):
         self.update()
 
 
+def toPoint(pointf):
+    return QPoint(int(pointf.x()), int(pointf.y()))
+
 def pp(p):
     return '%.2f, %.2f' % (p.x(), p.y())
 

+ 2 - 2
labelme.py

@@ -168,7 +168,7 @@ class MainWindow(QMainWindow, WindowMixin):
         hideAll = action('&Hide\nPolygons', partial(self.togglePolygons, False),
                 'Ctrl+H', 'hide', u'Hide all polygons',
                 enabled=False)
-        showAll = action('&Show\nPolygons', partial(self.togglePolygons, True),
+        showAll = action('&Show\nnPolygons', partial(self.togglePolygons, True),
                 'Ctrl+A', 'hide', u'Show all polygons',
                 enabled=False)
 
@@ -503,7 +503,7 @@ class MainWindow(QMainWindow, WindowMixin):
             shape = Shape(label=label)
             for x, y in points:
                 shape.addPoint(QPointF(x, y))
-            shape.addPoint(shape[0]) # Close shape
+            shape.close()
             s.append(shape)
             self.addLabel(shape)
             if line_color:

+ 7 - 3
shape.py

@@ -54,11 +54,15 @@ class Shape(object):
             # is used for drawing the pending line a different color.
             self.line_color = line_color
 
+    def close(self):
+        assert len(self.points) > 2
+        self._closed = True
+
     def addPoint(self, point):
         if self.points and point == self.points[0]:
-            self._closed = True
-            return
-        self.points.append(point)
+            self.close()
+        else:
+            self.points.append(point)
 
     def popPoint(self):
         if self.points: