Ver código fonte

Select shapes when the user clicks on a label

Michael Pitidis 13 anos atrás
pai
commit
bd1dfc8da5
2 arquivos alterados com 29 adições e 7 exclusões
  1. 11 3
      canvas.py
  2. 18 4
      labelme.py

+ 11 - 3
canvas.py

@@ -141,11 +141,11 @@ class Canvas(QWidget):
                     self.setHiding()
                     self.repaint()
             else:
-                self.selectShape(pos)
+                self.selectShapePoint(pos)
                 self.prevPoint = pos
                 self.repaint()
         elif ev.button() == Qt.RightButton and not self.editing():
-            self.selectShape(pos)
+            self.selectShapePoint(pos)
             self.prevPoint = pos
             self.repaint()
 
@@ -200,7 +200,15 @@ class Canvas(QWidget):
             self.current[-1] = self.current[0]
             self.finalise(ev)
 
-    def selectShape(self, point):
+    def selectShape(self, shape):
+        self.deSelectShape()
+        shape.selected = True
+        self.selectedShape = shape
+        self.setHiding()
+        self.selectionChanged.emit(True)
+        self.update()
+
+    def selectShapePoint(self, point):
         """Select the first shape created which contains this point."""
         self.deSelectShape()
         for shape in reversed(self.shapes):

+ 18 - 4
labelme.py

@@ -74,6 +74,8 @@ class MainWindow(QMainWindow, WindowMixin):
         # Whether we need to save or not.
         self.dirty = False
 
+        self._noSelectionSlot = False
+
         # Main widgets.
         self.label = LabelDialog(parent=self)
         self.labels = {}
@@ -90,6 +92,7 @@ class MainWindow(QMainWindow, WindowMixin):
         self.labelList.itemActivated.connect(self.highlightLabel)
         # Connect to itemChanged to detect checkbox changes.
         self.labelList.itemChanged.connect(self.labelItemChanged)
+        self.labelList.itemSelectionChanged.connect(self.labelSelectionChanged)
 
         self.canvas = Canvas()
         self.canvas.zoomRequest.connect(self.zoomRequest)
@@ -252,11 +255,14 @@ class MainWindow(QMainWindow, WindowMixin):
 
     # React to canvas signals.
     def shapeSelectionChanged(self, selected=False):
-        shape = self.canvas.selectedShape
-        if shape:
-            self.labelList.setItemSelected(self.items[shape], True)
+        if self._noSelectionSlot:
+            self._noSelectionSlot = False
         else:
-            self.labelList.clearSelection()
+            shape = self.canvas.selectedShape
+            if shape:
+                self.labelList.setItemSelected(self.items[shape], True)
+            else:
+                self.labelList.clearSelection()
         self.actions.delete.setEnabled(selected)
         self.actions.copy.setEnabled(selected)
 
@@ -308,6 +314,14 @@ class MainWindow(QMainWindow, WindowMixin):
         self.highlighted = shape
         self.canvas.repaint()
 
+    def labelSelectionChanged(self):
+        items = self.labelList.selectedItems()
+        if not items:
+            return
+        shape = self.labels[items[0]]
+        self._noSelectionSlot = True
+        self.canvas.selectShape(shape)
+
     def labelItemChanged(self, item):
         shape = self.labels[item]
         label = unicode(item.text())