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
11 """Custom delegate for float values"""
12
13 __metaclass__ = DocumentationMetaclass
14
15 editor = editors.FloatEditor
16
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
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
75
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