瀏覽代碼

Add checkbox for label visibility

Implement shape visibility toggle with a mapping of shapes to a
visibility value. Missing values default to visible shapes.
Michael Pitidis 13 年之前
父節點
當前提交
d8751d8568
共有 2 個文件被更改,包括 15 次插入1 次删除
  1. 7 1
      canvas.py
  2. 8 0
      labelme.py

+ 7 - 1
canvas.py

@@ -29,6 +29,7 @@ class Canvas(QWidget):
         self.prevPoint=QPoint()
         self.prevPoint=QPoint()
         self.scale = 1.0
         self.scale = 1.0
         self.pixmap = None
         self.pixmap = None
+        self.visible = {}
         # Set widget options.
         # Set widget options.
         self.setMouseTracking(True)
         self.setMouseTracking(True)
         self.setFocusPolicy(Qt.WheelFocus)
         self.setFocusPolicy(Qt.WheelFocus)
@@ -152,7 +153,8 @@ class Canvas(QWidget):
         p.drawPixmap(0, 0, self.pixmap)
         p.drawPixmap(0, 0, self.pixmap)
         Shape.scale = self.scale
         Shape.scale = self.scale
         for shape in self.shapes:
         for shape in self.shapes:
-            shape.paint(p)
+            if self.visible.get(shape, True):
+                shape.paint(p)
         if self.current:
         if self.current:
             self.current.paint(p)
             self.current.paint(p)
             self.line.paint(p)
             self.line.paint(p)
@@ -295,6 +297,10 @@ class Canvas(QWidget):
         self.current = None
         self.current = None
         self.repaint()
         self.repaint()
 
 
+    def setShapeVisible(self, shape, value):
+        self.visible[shape] = value
+        self.repaint()
+
 
 
 def distance(p):
 def distance(p):
     return sqrt(p.x() * p.x() + p.y() * p.y())
     return sqrt(p.x() * p.x() + p.y() * p.y())

+ 8 - 0
labelme.py

@@ -72,6 +72,8 @@ class MainWindow(QMainWindow, WindowMixin):
         self.zoom_widget = ZoomWidget()
         self.zoom_widget = ZoomWidget()
 
 
         self.labelList.itemActivated.connect(self.highlightLabel)
         self.labelList.itemActivated.connect(self.highlightLabel)
+        # Connect to itemChanged to detect checkbox changes.
+        self.labelList.itemChanged.connect(self.labelItemChanged)
 
 
         self.canvas = Canvas()
         self.canvas = Canvas()
         #self.canvas.setAlignment(Qt.AlignCenter)
         #self.canvas.setAlignment(Qt.AlignCenter)
@@ -196,6 +198,8 @@ class MainWindow(QMainWindow, WindowMixin):
 
 
     def addLabel(self, label, shape):
     def addLabel(self, label, shape):
         item = QListWidgetItem(label)
         item = QListWidgetItem(label)
+        item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
+        item.setCheckState(Qt.Checked)
         self.labels[item] = shape
         self.labels[item] = shape
         self.labelList.addItem(item)
         self.labelList.addItem(item)
 
 
@@ -207,6 +211,10 @@ class MainWindow(QMainWindow, WindowMixin):
         self.highlighted = shape
         self.highlighted = shape
         self.canvas.repaint()
         self.canvas.repaint()
 
 
+    def labelItemChanged(self, item):
+        shape = self.labels[item]
+        self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked)
+
     ## Callback functions:
     ## Callback functions:
     def newShape(self, position):
     def newShape(self, position):
         """Pop-up and give focus to the label editor.
         """Pop-up and give focus to the label editor.