Package Camelot :: Package camelot :: Package view :: Package controls :: Package delegates :: Module integerdelegate |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.delegates.integerdelegate

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

     4  from customdelegate import CustomDelegate, DocumentationMetaclass
     5  from camelot.view.controls import editors
     6  from camelot.core.utils import variant_to_pyobject
     7  from camelot.core import constants
     8  from camelot.view.proxy import ValueLoading
     9

    10 -class IntegerDelegate(CustomDelegate):
    11      """Custom delegate for integer values"""
    12

    13      __metaclass__ = DocumentationMetaclass
    14

    15      editor = editors.IntegerEditor
    16

    17 -    def __init__(self,

    18                   minimum=constants.camelot_minint,

    19                   maximum=constants.camelot_maxint,

    20                   editable=True,

    21                   parent=None,

    22                   unicode_format = None,

    23                   **kwargs):
    24

    25          CustomDelegate.__init__(self, parent=parent, editable=editable, minimum=minimum, maximum=maximum, **kwargs)
    26          self.minimum = minimum
    27          self.maximum = maximum
    28          self.editable = editable
    29          self.unicode_format = unicode_format
    30

    31 -    def paint(self, painter, option, index):
    32          painter.save()
    33          self.drawBackground(painter, option, index)
    34          value = variant_to_pyobject(index.model().data(index, Qt.EditRole))
    35

    36          background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
    37

    38          rect = option.rect
    39          rect = QtCore.QRect(rect.left()+3, rect.top()+6, 16, 16)
    40          #fontColor = QtGui.QColor()

    41

    42          if( option.state & QtGui.QStyle.State_Selected ):
    43              painter.fillRect(option.rect, option.palette.highlight())
    44              fontColor = QtGui.QColor()
    45              if self.editable:
    46                  Color = option.palette.highlightedText().color()
    47                  fontColor.setRgb(Color.red(), Color.green(), Color.blue())
    48              else:
    49                  fontColor.setRgb(130,130,130)
    50          else:
    51              if self.editable:
    52                  painter.fillRect(option.rect, background_color)
    53                  fontColor = QtGui.QColor()
    54                  fontColor.setRgb(0,0,0)
    55              else:
    56                  painter.fillRect(option.rect, option.palette.window())
    57                  fontColor = QtGui.QColor()
    58                  fontColor.setRgb(130,130,130)
    59

    60          if value in (None, ValueLoading):
    61              value_str = ''
    62          else:
    63              value_str = '%i'%value
    64          if self.unicode_format != None:
    65              value_str = self.unicode_format(value)
    66

    67          #fontColor = fontColor.darker()

    68

    69

    70

    71          painter.setPen(fontColor.toRgb())
    72          rect = QtCore.QRect(option.rect.left()+23,

    73                              option.rect.top(),

    74                              option.rect.width()-23,

    75                              option.rect.height())
    76          painter.drawText(rect.x()+2,

    77                           rect.y(),

    78                           rect.width()-4,

    79                           rect.height(),

    80                           Qt.AlignVCenter | Qt.AlignRight,

    81                           value_str)
    82          painter.restore()
    83

    84 -    def setEditorData(self, editor, index):
    85          value = index.model().data(index, Qt.EditRole).toInt()[0]
    86          editor.set_value(value)
    87