Package Camelot :: Package camelot :: Package view :: Package controls :: Package editors :: Module coloreditor |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.editors.coloreditor

     1  from PyQt4 import QtGui, QtCore
     2  from PyQt4.QtCore import Qt
     3

     4  from customeditor import CustomEditor
     5

     6 -class ColorEditor(CustomEditor):
     7

     8 -    def __init__(self, parent=None, editable=True, **kwargs):
     9          CustomEditor.__init__(self, parent)
    10          layout = QtGui.QVBoxLayout(self)
    11          layout.setSpacing(0)
    12          layout.setMargin(0)
    13          self.color_button = QtGui.QPushButton(parent)
    14          self.color_button.setMaximumSize(QtCore.QSize(20, 20))
    15          layout.addWidget(self.color_button)
    16          if editable:
    17              self.connect(self.color_button,

    18                           QtCore.SIGNAL('clicked(bool)'),

    19                           self.buttonClicked)
    20          self.setLayout(layout)
    21          self._color = None
    22

    23 -    def get_value(self):
    24          color = self.getColor()
    25          if color:
    26              value = (color.red(), color.green(), color.blue(), color.alpha())
    27          else:
    28              value = None
    29          return CustomEditor.get_value(self) or value
    30

    31 -    def set_value(self, value):
    32          value = CustomEditor.set_value(self, value)
    33          if value:
    34              color = QtGui.QColor()
    35              color.setRgb(*value)
    36              self.setColor(color)
    37          else:
    38              self.setColor(value)
    39

    40 -    def getColor(self):
    41          return self._color
    42

    43 -    def set_enabled(self, editable=True):
    44          self.color_button.setEnabled(editable)
    45

    46 -    def setColor(self, color):
    47          pixmap = QtGui.QPixmap(16, 16)
    48          if color:
    49              pixmap.fill(color)
    50          else:
    51              pixmap.fill(Qt.transparent)
    52          self.color_button.setIcon(QtGui.QIcon(pixmap))
    53          self._color = color
    54

    55 -    def buttonClicked(self, raised):
    56          if self._color:
    57              color = QtGui.QColorDialog.getColor(self._color)
    58          else:
    59              color = QtGui.QColorDialog.getColor()
    60          if color.isValid() and color!=self._color:
    61              self.setColor(color)
    62              self.emit(QtCore.SIGNAL('editingFinished()'))
    63