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

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

     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.constants import camelot_small_icon_width
     7  from camelot.core.utils import variant_to_pyobject
     8  from camelot.view.proxy import ValueLoading
     9  from camelot.view.utils import local_date_format
    10

    11 -class DateDelegate(CustomDelegate):
    12      """Custom delegate for date values"""
    13

    14      __metaclass__ = DocumentationMetaclass
    15

    16      editor = editors.DateEditor
    17
    18 -    def __init__(self, parent=None, editable=True, **kwargs):
    19          CustomDelegate.__init__(self, parent, editable, **kwargs)
    20          self.date_format = local_date_format()
    21          self._width = self._font_metrics.averageCharWidth() * (len(self.date_format) + 4)  + (camelot_small_icon_width*2) * 2
    22

    23 -    def paint(self, painter, option, index):
    24          painter.save()
    25          self.drawBackground(painter, option, index)
    26

    27          dateObj = variant_to_pyobject(index.model().data(index, Qt.EditRole))
    28

    29          background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
    30

    31          if dateObj not in (None, ValueLoading):
    32              formattedDate = QtCore.QDate(dateObj).toString(self.date_format)
    33          else:
    34              formattedDate = "0/0/0"
    35

    36          rect = option.rect
    37          rect = QtCore.QRect(rect.left()+3, rect.top()+6, 16, 16)
    38

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

    57

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

    60                              option.rect.top(),

    61                              option.rect.width()-23,

    62                              option.rect.height())
    63

    64          painter.drawText(rect.x()+2,

    65                           rect.y(),

    66                           rect.width()-4,

    67                           rect.height(),

    68                           Qt.AlignVCenter | Qt.AlignRight,

    69                           str(formattedDate))
    70

    71          painter.restore()
    72

    73  #  def sizeHint(self, option, index):

    74  #    return editors.DateEditor().sizeHint()
    75