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

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

      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.view.art import Icon
      7

      8 -class ColoredFloatDelegate(CustomDelegate):
      9      """Custom delegate for float values, representing them in green when they are

     10    positive and in red when they are negative.

     11    """
     12

     13      __metaclass__ = DocumentationMetaclass
     14

     15      editor = editors.ColoredFloatEditor
     16

     17 -    def __init__(self,

     18                   parent=None,

     19                   minimum=-1e15,

     20                   maximum=1e15,

     21                   precision=2,

     22                   editable=True,

     23                   reverse=False,

     24                   neutral=False,

     25                   unicode_format=None,

     26                   **kwargs):
     27          CustomDelegate.__init__(self,

     28                                  parent=parent,

     29                                  editable=editable,

     30                                  minimum=minimum,

     31                                  maximum=maximum,

     32                                  reverse=reverse,

     33                                  neutral=neutral,

     34                                  precision=precision,

     35                                  unicode_format=unicode_format,

     36                                  **kwargs)
     37          self.minimum = minimum
     38          self.maximum = maximum
     39          self.precision = precision
     40          self.editable = editable
     41          self.reverse = reverse
     42          self.neutral = neutral
     43          self.unicode_format = unicode_format
     44          if not self.reverse:
     45              if not self.neutral:
     46                  self.icons = {

     47                      1:QtGui.QIcon(Icon('tango/16x16/actions/go-up.png').getQPixmap()),

     48                      -1:QtGui.QIcon(Icon('tango/16x16/actions/go-down-red.png').getQPixmap()),

     49                      0:QtGui.QIcon(Icon('tango/16x16/actions/zero.png').getQPixmap())

     50                  }
     51              else:
     52                  self.icons = {

     53                      1:QtGui.QIcon(Icon('tango/16x16/actions/go-up-blue.png').getQPixmap()),

     54                      -1:QtGui.QIcon(Icon('tango/16x16/actions/go-down-blue.png').getQPixmap()),

     55                      0:QtGui.QIcon(Icon('tango/16x16/actions/zero.png').getQPixmap())

     56                  }
     57          else:
     58              self.icons = {

     59                  -1:QtGui.QIcon(Icon('tango/16x16/actions/go-up.png').getQPixmap()),

     60                  1:QtGui.QIcon(Icon('tango/16x16/actions/go-down-red.png').getQPixmap()),

     61                  0:QtGui.QIcon(Icon('tango/16x16/actions/zero.png').getQPixmap())

     62              }
     63

     64 -    def paint(self, painter, option, index):
     65          painter.save()
     66          self.drawBackground(painter, option, index)
     67          value = index.model().data(index, Qt.EditRole).toDouble()[0]
     68          color = index.model().data(index, Qt.BackgroundRole)
     69          background_color = QtGui.QColor(color)
     70          fontColor = QtGui.QColor()
     71

     72          if( option.state & QtGui.QStyle.State_Selected ):
     73              painter.fillRect(option.rect, option.palette.highlight())
     74          else:
     75              if not self.editable:
     76                  painter.fillRect(option.rect, option.palette.window())
     77              else:
     78                  painter.fillRect(option.rect, background_color)
     79

     80          self.icons[cmp(value,0)].paint(painter, option.rect.left(), option.rect.top()+1, option.rect.height(), option.rect.height(), Qt.AlignVCenter)
     81

     82          value_str = '%.*f'%(self.precision, value)
     83          if self.unicode_format != None:
     84              value_str = self.unicode_format(value)
     85

     86          fontColor = fontColor.darker()
     87          painter.setPen(fontColor.toRgb())
     88          rect = QtCore.QRect(option.rect.left()+23,

     89                              option.rect.top(),

     90                              option.rect.width()-23,

     91                              option.rect.height())
     92

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

     94                           rect.y(),

     95                           rect.width()-4,

     96                           rect.height(),

     97                           Qt.AlignVCenter | Qt.AlignRight,

     98                           value_str)
     99

    100          painter.restore()
    101