Prechádzať zdrojové kódy

Movement with arrow keys (#942)

* Movement with arrow keys

* +black

* fixed calculateOffsets

* update canvas.py

* Refactor self.calculateOffsets

Co-authored-by: Kentaro Wada <www.kentaro.wada@gmail.com>
Bc. Martin Kubovčík 3 rokov pred
rodič
commit
f696ea7a26
2 zmenil súbory, kde vykonal 63 pridanie a 18 odobranie
  1. 0 1
      labelme/app.py
  2. 63 17
      labelme/widgets/canvas.py

+ 0 - 1
labelme/app.py

@@ -37,7 +37,6 @@ from labelme.widgets import ZoomWidget
 # - [medium] Set max zoom value to something big enough for FitWidth/Window
 
 # TODO(unknown):
-# - [high] Add polygon movement with arrow keys
 # - [low,maybe] Preview images on file dialogs.
 # - Zoom is too "steppy".
 

+ 63 - 17
labelme/widgets/canvas.py

@@ -17,6 +17,8 @@ CURSOR_DRAW = QtCore.Qt.CrossCursor
 CURSOR_MOVE = QtCore.Qt.ClosedHandCursor
 CURSOR_GRAB = QtCore.Qt.OpenHandCursor
 
+MOVE_SPEED = 5.0
+
 
 class Canvas(QtWidgets.QWidget):
 
@@ -490,7 +492,6 @@ class Canvas(QtWidgets.QWidget):
         else:
             for shape in reversed(self.shapes):
                 if self.isVisible(shape) and shape.containsPoint(point):
-                    self.calculateOffsets(shape, point)
                     self.setHiding()
                     if shape not in self.selectedShapes:
                         if multiple_selection_mode:
@@ -502,15 +503,30 @@ class Canvas(QtWidgets.QWidget):
                         self.hShapeIsSelected = False
                     else:
                         self.hShapeIsSelected = True
+                    self.calculateOffsets(point)
                     return
         self.deSelectShape()
 
-    def calculateOffsets(self, shape, point):
-        rect = shape.boundingRect()
-        x1 = rect.x() - point.x()
-        y1 = rect.y() - point.y()
-        x2 = (rect.x() + rect.width() - 1) - point.x()
-        y2 = (rect.y() + rect.height() - 1) - point.y()
+    def calculateOffsets(self, point):
+        left = self.pixmap.width() - 1
+        right = 0
+        top = self.pixmap.height() - 1
+        bottom = 0
+        for s in self.selectedShapes:
+            rect = s.boundingRect()
+            if rect.left() < left:
+                left = rect.left()
+            if rect.right() > right:
+                right = rect.right()
+            if rect.top() < top:
+                top = rect.top()
+            if rect.bottom() > bottom:
+                bottom = rect.bottom()
+
+        x1 = left - point.x()
+        y1 = top - point.y()
+        x2 = right - point.x()
+        y2 = bottom - point.y()
         self.offsets = QtCore.QPoint(x1, y1), QtCore.QPoint(x2, y2)
 
     def boundedMoveVertex(self, pos):
@@ -757,22 +773,52 @@ class Canvas(QtWidgets.QWidget):
                 self.scrollRequest.emit(ev.delta(), QtCore.Qt.Horizontal)
         ev.accept()
 
+    def moveByKeyboard(self, offset):
+        if self.selectedShapes:
+            self.boundedMoveShapes(
+                self.selectedShapes, self.prevPoint + offset
+            )
+            self.repaint()
+            self.movingShape = True
+
     def keyPressEvent(self, ev):
         modifiers = ev.modifiers()
         key = ev.key()
-        if key == QtCore.Qt.Key_Escape and self.current:
-            self.current = None
-            self.drawingPolygon.emit(False)
-            self.update()
-        elif key == QtCore.Qt.Key_Return and self.canCloseShape():
-            self.finalise()
-        elif modifiers == QtCore.Qt.AltModifier:
-            self.snapping = False
+        if self.drawing():
+            if key == QtCore.Qt.Key_Escape and self.current:
+                self.current = None
+                self.drawingPolygon.emit(False)
+                self.update()
+            elif key == QtCore.Qt.Key_Return and self.canCloseShape():
+                self.finalise()
+            elif modifiers == QtCore.Qt.AltModifier:
+                self.snapping = False
+        elif self.editing():
+            if key == QtCore.Qt.Key_Up:
+                self.moveByKeyboard(QtCore.QPoint(0.0, -MOVE_SPEED))
+            elif key == QtCore.Qt.Key_Down:
+                self.moveByKeyboard(QtCore.QPoint(0.0, MOVE_SPEED))
+            elif key == QtCore.Qt.Key_Left:
+                self.moveByKeyboard(QtCore.QPoint(-MOVE_SPEED, 0.0))
+            elif key == QtCore.Qt.Key_Right:
+                self.moveByKeyboard(QtCore.QPoint(MOVE_SPEED, 0.0))
 
     def keyReleaseEvent(self, ev):
         modifiers = ev.modifiers()
-        if int(modifiers) == 0:
-            self.snapping = True
+        if self.drawing():
+            if int(modifiers) == 0:
+                self.snapping = True
+        elif self.editing():
+            if self.movingShape and self.selectedShapes:
+                index = self.shapes.index(self.selectedShapes[0])
+                if (
+                    self.shapesBackups[-1][index].points
+                    != self.shapes[index].points
+                ):
+                    self.storeShapes()
+                    self.shapeMoved.emit()
+
+                self.movingShape = False
 
     def setLastLabel(self, text, flags):
         assert text