瀏覽代碼

Clean up and refactor slightly

Michael Pitidis 13 年之前
父節點
當前提交
ee894fdcb4
共有 3 個文件被更改,包括 27 次插入42 次删除
  1. 14 22
      canvas.py
  2. 6 7
      labelme.py
  3. 7 13
      shape.py

+ 14 - 22
canvas.py

@@ -17,13 +17,10 @@ class Canvas(QLabel):
         self.selectedShape=None # save the selected shape here
         self.line_color = QColor(0, 0, 255)
         self.line = Shape(line_color=self.line_color)
-        self.mouseButtonIsPressed=False #when it is true and shape is selected , move the shape with the mouse move event
-        
-        
 
     def mouseMoveEvent(self, ev):
         """Update line with last point and current coordinates."""
-        if self.current and self.startLabeling: 
+        if self.current and self.startLabeling:
             if len(self.current) > 1 and self.closeEnough(ev.pos(), self.current[0]):
                 self.line[1] = self.current[0]
                 self.line.line_color = self.current.line_color
@@ -31,16 +28,11 @@ class Canvas(QLabel):
                 self.line[1] = ev.pos()
                 self.line.line_color = self.line_color
             self.repaint()
-            return
-            
-        if self.mouseButtonIsPressed:
-            print ev.x()
-            
+
     def mousePressEvent(self, ev):
-        if ev.button() == 1: 
+        if ev.button() == 1:
             if self.startLabeling:
-                
-                if self.current :
+                if self.current:
                     self.current.addPoint(self.line[1])
                     self.line[0] = self.current[-1]
                     if self.current.isClosed():
@@ -61,20 +53,20 @@ class Canvas(QLabel):
             self.current.addPoint(self.current[0])
             self.finalise()
 
-    def selectShape(self,point):
+    def selectShape(self, point):
+        """Select the first shape created which contains this point."""
         self.deSelectShape()
         for shape in self.shapes:
-            if shape.containPoint(point):
-                shape.selected=True
-                self.selectedShape=shape
-                self.repaint()
-                return
-        
-                #shape.select()
+            if shape.containsPoint(point):
+                shape.selected = True
+                self.selectedShape = shape
+                return self.repaint()
+
     def deSelectShape(self):
         if self.selectedShape:
-            self.selectedShape.selected=False
+            self.selectedShape.selected = False
             self.repaint()
+
     def paintEvent(self, event):
         super(Canvas, self).paintEvent(event)
         qp = QPainter()
@@ -93,7 +85,7 @@ class Canvas(QLabel):
         self.current.fill = True
         self.shapes.append(self.current)
         self.current = None
-        self.startLabeling=False
+        self.startLabeling = False
         # TODO: Mouse tracking is still useful for selecting shapes!
         self.setMouseTracking(False)
         self.repaint()

+ 6 - 7
labelme.py

@@ -94,7 +94,7 @@ class MainWindow(QMainWindow, WindowMixin):
         quit = action(self, '&Quit', self.close, 'Ctrl+Q', u'Exit application')
         open = action(self, '&Open', self.openFile, 'Ctrl+O', u'Open file')
         color = action(self, '&Color', self.chooseColor, 'Ctrl+C', u'Choose line color')
-        new_Label=action(self,'&New Label',self.newlabel,'Ctrl+N',u'Add new label')
+        newl = action(self, '&New Label', self.newLabel, 'Ctrl+N', u'Add new label')
         labl = self.dock.toggleViewAction()
         labl.setShortcut('Ctrl+L')
 
@@ -108,13 +108,12 @@ class MainWindow(QMainWindow, WindowMixin):
                 edit=self.menu('&Image'),
                 view=self.menu('&View'))
         add_actions(self.menus.file, (open, quit))
-        add_actions(self.menus.edit, (new_Label,color, fit_window))
+        add_actions(self.menus.edit, (newl, color, fit_window))
 
         add_actions(self.menus.view, (labl,))
-        
 
         self.tools = self.toolbar('Tools')
-        add_actions(self.tools, (open, color, None,new_Label,None, zoom, fit_window, None, quit))
+        add_actions(self.tools, (open, color, None, newl,None, zoom, fit_window, None, quit))
 
 
         self.statusBar().showMessage('%s started.' % __appname__)
@@ -243,10 +242,10 @@ class MainWindow(QMainWindow, WindowMixin):
         # Change the color for all shape lines:
         Shape.line_color = self.color
         self.canvas.repaint()
-        
-    def newlabel(self):
+
+    def newLabel(self):
         self.canvas.deSelectShape()
-        self.canvas.startLabeling=True
+        self.canvas.startLabeling = True
 
 class Settings(object):
     """Convenience dict-like wrapper around QSettings."""

+ 7 - 13
shape.py

@@ -11,7 +11,7 @@ class Shape(object):
     ## of _all_ shape objects.
     line_color = QColor(0, 255, 0, 128)
     fill_color = QColor(255, 0, 0, 128)
-    select_color=QColor(255,255,255,255)
+    select_color = QColor(255, 255, 255)
     point_type = P_SQUARE
     point_size = 8
 
@@ -19,7 +19,7 @@ class Shape(object):
         self.label = label
         self.points = []
         self.fill = False
-        self.selected=False
+        self.selected = False
         if line_color is not None:
             # Override the class line_color attribute
             # with an object attribute. Currently this
@@ -41,12 +41,7 @@ class Shape(object):
     # The paths could be stored and elements added directly to them.
     def paint(self, painter):
         if self.points:
-            if self.selected:
-                pen = QPen(self.select_color)
-            else:
-                pen = QPen(self.line_color)
-
-            
+            pen = QPen(self.select_color if self.selected else self.line_color)
             painter.setPen(pen)
 
             line_path = QPainterPath()
@@ -71,14 +66,13 @@ class Shape(object):
             path.addRect(point.x() - d/2, point.y() - d/2, d, d)
         else:
             path.addEllipse(QPointF(point), d/2.0, d/2.0)
-    
-    
-    def containPoint(self,point):
-        path=QPainterPath(QPointF(self.points[0]))
+
+    def containsPoint(self, point):
+        path = QPainterPath(QPointF(self.points[0]))
         for p in self.points[1:]:
             path.lineTo(QPointF(p))
         return path.contains(QPointF(point))
-        
+
     def __len__(self):
         return len(self.points)