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

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

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

    10 -class FloatDelegate( 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                   unicode_format = None,

    24                   prefix = '',

    25                   suffix = '',

    26                   **kwargs ):
    27          """

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

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

    30      """
    31          CustomDelegate.__init__( self, parent = parent, editable = editable,

    32                                  suffix = suffix, prefix = prefix,

    33                                  precision = precision, **kwargs )
    34          self.precision = precision
    35          self.editable = editable
    36          self.unicode_format = unicode_format
    37          self.prefix = prefix
    38          self.suffix = suffix
    39

    40 -    def paint( self, painter, option, index ):
    41          painter.save()
    42          self.drawBackground( painter, option, index )
    43          value = variant_to_pyobject( index.model().data( index, Qt.EditRole ) )
    44

    45          background_color = QtGui.QColor( index.model().data( index, Qt.BackgroundRole ) )
    46

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

    65          value_str = u''
    66          if value != None and value != ValueLoading:
    67              #

    68              # we need to convert value explicitely to a float, since it might be of some

    69              # other type when using ColumnProperty (eg Decimal, int), and then another

    70              # arg method will be called with a different signature (this is C++ remember)

    71              #

    72              value_str = QtCore.QString("%L1").arg(float(value),0,'f',self.precision)
    73

    74          value_str = unicode( self.prefix ) + u' ' + unicode( value_str ) + u' ' + unicode( self.suffix )
    75          value_str = value_str.strip()
    76          if self.unicode_format != None and value != ValueLoading:
    77              value_str = self.unicode_format( value )
    78

    79          painter.setPen( fontColor.toRgb() )
    80          painter.drawText( option.rect.left() + 3,

    81                           option.rect.top(),

    82                           option.rect.width() - 6,

    83                           option.rect.height(),

    84                           Qt.AlignVCenter | Qt.AlignRight,

    85                           value_str )
    86          painter.restore()
    87