فهرست منبع

Merge branch 'feature/colors'

Michael Pitidis 13 سال پیش
والد
کامیت
5404279ebe
3فایلهای تغییر یافته به همراه72 افزوده شده و 19 حذف شده
  1. 30 0
      colorDialog.py
  2. 35 16
      labelme.py
  3. 7 3
      shape.py

+ 30 - 0
colorDialog.py

@@ -0,0 +1,30 @@
+
+from PyQt4.QtGui import *
+from PyQt4.QtCore import *
+
+BB = QDialogButtonBox
+
+class ColorDialog(QColorDialog):
+    def __init__(self, parent=None):
+        super(ColorDialog, self).__init__(parent)
+        self.setOption(QColorDialog.ShowAlphaChannel)
+        ## Add a restore defaults button.
+        # The default is set at invocation time, so that it
+        # works across dialogs for different elements.
+        self.default = None
+        self.bb = self.layout().itemAt(1).widget()
+        self.bb.addButton(BB.RestoreDefaults)
+        self.bb.clicked.connect(self.checkRestore)
+
+    def getColor(self, value=None, title=None, default=None):
+        self.default = default
+        if title:
+            self.setWindowTitle(title)
+        if value:
+            self.setCurrentColor(value)
+        return self.currentColor() if self.exec_() else None
+
+    def checkRestore(self, button):
+        if self.bb.buttonRole(button) & BB.ResetRole and self.default:
+            self.setCurrentColor(self.default)
+

+ 35 - 16
labelme.py

@@ -14,10 +14,11 @@ from PyQt4.QtCore import *
 import resources
 
 from lib import struct, newAction, addActions, labelValidator
-from shape import Shape
+from shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR
 from canvas import Canvas
 from zoomWidget import ZoomWidget
 from labelDialog import LabelDialog
+from colorDialog import ColorDialog
 from labelFile import LabelFile, LabelFileError
 from toolBar import ToolBar
 
@@ -86,6 +87,7 @@ class MainWindow(QMainWindow, WindowMixin):
         self.dock.setObjectName(u'Labels')
         self.dock.setWidget(self.labelList)
         self.zoomWidget = ZoomWidget()
+        self.colorDialog = ColorDialog(parent=self)
 
         self.labelList.setItemDelegate(LabelDelegate())
         self.labelList.itemActivated.connect(self.highlightLabel)
@@ -119,8 +121,10 @@ class MainWindow(QMainWindow, WindowMixin):
                 'Ctrl+O', 'open', u'Open file')
         save = action('&Save', self.saveFile,
                 'Ctrl+S', 'save', u'Save file')
-        color = action('&Color', self.chooseColor,
+        color1 = action('&Line Color', self.chooseColor1,
                 'Ctrl+C', 'color', u'Choose line color')
+        color2 = action('&Fill Color', self.chooseColor2,
+                'Ctrl+Shift+C', 'color', u'Choose fill color')
         label = action('&New Item', self.newLabel,
                 'Ctrl+N', 'new', u'Add new label', enabled=False)
         copy = action('&Copy', self.copySelectedShape,
@@ -161,7 +165,8 @@ class MainWindow(QMainWindow, WindowMixin):
         labels.setShortcut('Ctrl+L')
 
         # Store actions for further handling.
-        self.actions = struct(save=save, open=open, color=color,
+        self.actions = struct(save=save, open=open,
+                lineColor=color1, fillColor=color2,
                 label=label, delete=delete, zoom=zoom, copy=copy,
                 zoomIn=zoomIn, zoomOut=zoomOut, zoomOrg=zoomOrg,
                 fitWindow=fitWindow, fitWidth=fitWidth)
@@ -172,7 +177,7 @@ class MainWindow(QMainWindow, WindowMixin):
                 edit=self.menu('&Image'),
                 view=self.menu('&View'))
         addActions(self.menus.file, (open, save, quit))
-        addActions(self.menus.edit, (label, color, fitWindow, fitWidth))
+        addActions(self.menus.edit, (label, color1, color2))
         addActions(self.menus.view, (
             labels, None,
             zoomIn, zoomOut, zoomOrg, None,
@@ -181,9 +186,8 @@ class MainWindow(QMainWindow, WindowMixin):
         self.tools = self.toolbar('Tools')
         addActions(self.tools, (
             open, save, None,
-            label, delete, color, hide, None,
-            zoomIn, zoom, zoomOut, fitWindow, fitWidth, None,
-            quit))
+            label, delete, hide, None,
+            zoomIn, zoom, zoomOut, fitWindow, fitWidth))
 
         self.statusBar().showMessage('%s started.' % __appname__)
         self.statusBar().show()
@@ -192,7 +196,8 @@ class MainWindow(QMainWindow, WindowMixin):
         self.image = QImage()
         self.filename = filename
         self.recent_files = []
-        self.color = None
+        self.lineColor = None
+        self.fillColor = None
         self.zoom_level = 100
         self.fit_window = False
 
@@ -216,7 +221,10 @@ class MainWindow(QMainWindow, WindowMixin):
         # or simply:
         #self.restoreGeometry(settings['window/geometry']
         self.restoreState(settings['window/state'])
-        self.color = QColor(settings.get('line/color', QColor(0, 255, 0, 128)))
+        self.lineColor = QColor(settings.get('line/color', Shape.line_color))
+        self.fillColor = QColor(settings.get('fill/color', Shape.fill_color))
+        Shape.line_color = self.lineColor
+        Shape.fill_color = self.fillColor
 
         # The file menu has default dynamically generated entries.
         self.updateFileMenu()
@@ -440,7 +448,8 @@ class MainWindow(QMainWindow, WindowMixin):
         s['window/size'] = self.size()
         s['window/position'] = self.pos()
         s['window/state'] = self.saveState()
-        s['line/color'] = self.color
+        s['line/color'] = self.lineColor
+        s['fill/color'] = self.fillColor
         # ask the use for where to save the labels
         #s['window/geometry'] = self.saveGeometry()
 
@@ -490,12 +499,22 @@ class MainWindow(QMainWindow, WindowMixin):
     def currentPath(self):
         return os.path.dirname(unicode(self.filename)) if self.filename else '.'
 
-    def chooseColor(self):
-        self.color = QColorDialog.getColor(self.color, self,
-                u'Choose line color', QColorDialog.ShowAlphaChannel)
-        # Change the color for all shape lines:
-        Shape.line_color = self.color
-        self.canvas.repaint()
+    def chooseColor1(self):
+        color = self.colorDialog.getColor(self.lineColor, u'Choose line color',
+                default=DEFAULT_LINE_COLOR)
+        if color:
+            self.lineColor = color
+            # Change the color for all shape lines:
+            Shape.line_color = self.lineColor
+            self.canvas.repaint()
+
+    def chooseColor2(self):
+        color = self.colorDialog.getColor(self.fillColor, u'Choose fill color',
+                default=DEFAULT_FILL_COLOR)
+        if color:
+            self.fillColor = color
+            Shape.fill_color = self.fillColor
+            self.canvas.repaint()
 
     def newLabel(self):
         self.canvas.deSelectShape()

+ 7 - 3
shape.py

@@ -10,14 +10,18 @@ from PyQt4.QtCore import *
 # TODO:
 # - [opt] Store paths instead of creating new ones at each paint.
 
+DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
+DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
+DEFAULT_SELECT_COLOR = QColor(255, 255, 255)
+
 class Shape(object):
     P_SQUARE, P_ROUND = range(2)
 
     ## The following class variables influence the drawing
     ## of _all_ shape objects.
-    line_color = QColor(0, 255, 0, 128)
-    fill_color = QColor(255, 0, 0, 128)
-    select_color = QColor(255, 255, 255)
+    line_color = DEFAULT_LINE_COLOR
+    fill_color = DEFAULT_FILL_COLOR
+    select_color = DEFAULT_SELECT_COLOR
     point_type = P_SQUARE
     point_size = 8
     scale = 1.0