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

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

     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 CurrencyDelegate(CustomDelegate):
    11      """Custom delegate for float values"""
    12

    13      __metaclass__ = DocumentationMetaclass
    14

    15      editor = editors.FloatEditor
    16

    17 -    def __init__(self,

    18                   minimum=constants.camelot_minfloat,

    19                   maximum=constants.camelot_maxfloat,

    20                   precision=2,

    21                   editable=True,

    22                   parent=None,

    23                   prefix="",

    24                   suffix="",

    25                   **kwargs):
    26          """

    27      :param precision:  The number of digits after the decimal point displayed.  This defaults

    28      to the precision specified in the definition of the Field.

    29      """
    30          CustomDelegate.__init__(self, parent=parent, editable=editable, minimum=minimum, maximum=maximum,

    31                                  precision=precision, prefix=prefix, suffix=suffix, **kwargs)
    32          self.minimum = minimum
    33          self.maximum = maximum
    34          self.precision = precision
    35          self.editable = editable
    36          self.prefix = prefix
    37          self.suffix = suffix
    38

    39 -    def setEditorData(self, editor, index):
    40          value = variant_to_pyobject(index.model().data(index, Qt.EditRole))
    41          editor.set_value(value)
    42

    43 -    def paint(self, painter, option, index):
    44          painter.save()
    45          self.drawBackground(painter, option, index)
    46          value = variant_to_pyobject(index.model().data(index, Qt.EditRole))
    47          if value in (None, ValueLoading):
    48              value = 0.0
    49          rect = option.rect
    50          rect = QtCore.QRect(rect.left()+3, rect.top()+6, 16, 16)
    51

    52

    53          background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
    54

    55          if( option.state & QtGui.QStyle.State_Selected ):
    56              painter.fillRect(option.rect, option.palette.highlight())
    57              fontColor = QtGui.QColor()
    58              if self.editable:
    59                  Color = option.palette.highlightedText().color()
    60                  fontColor.setRgb(Color.red(), Color.green(), Color.blue())
    61              else:
    62                  fontColor.setRgb(130,130,130)
    63          else:
    64              if self.editable:
    65                  painter.fillRect(option.rect, background_color)
    66                  fontColor = QtGui.QColor()
    67                  fontColor.setRgb(0,0,0)
    68              else:
    69                  painter.fillRect(option.rect, option.palette.window())
    70                  fontColor = QtGui.QColor()
    71                  fontColor.setRgb(130,130,130)
    72

    73          #

    74          # explicit conversion of value to float needed, see floatdelegate.py for

    75          # explanation

    76          #

    77          value_str_formatted  = QtCore.QString("%L1").arg(float(value),0,'f',2)
    78

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

    81                              option.rect.top(),

    82                              option.rect.width()-23,

    83                              option.rect.height())
    84

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

    86                           rect.y(),

    87                           rect.width()-4,

    88                           rect.height(),

    89                           Qt.AlignVCenter | Qt.AlignRight,

    90                           value_str_formatted)
    91

    92          painter.restore()
    93