1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 from PyQt4 import QtGui, QtCore
29 from PyQt4.QtCore import Qt, SIGNAL
30 from PyQt4.QtGui import QItemDelegate
31 from camelot.core.utils import variant_to_pyobject
32
33 from camelot.view.controls import editors
34 from camelot.core.utils import create_constant_function
35 from camelot.view.proxy import ValueLoading
36
37
38 not_editable_background = QtGui.QColor(235, 233, 237)
39
40 not_editable_foreground = QtGui.QColor(Qt.darkGray)
41
42
59
60 if '__init__' in dct:
61 dct['__doc__'] = dct['__doc__'] + 'Field attributes supported by the delegate : \n'
62 args, _varargs, _varkw, _defaults = inspect.getargspec(dct['__init__'])
63 for arg in args:
64 if arg not in ['self', 'parent']:
65 add_field_attribute_item(arg)
66
67 if 'editor' in dct:
68 dct['__doc__'] = dct['__doc__'] + '\nBy default, creates a %s as its editor.\n'%dct['editor'].__name__
69 dct['__doc__'] = dct['__doc__'] + '\n.. image:: ../_static/editors/%s_editable.png'%dct['editor'].__name__ + '\n'
70 dct['__doc__'] = dct['__doc__'] + 'Field attributes supported by this editor : \n'
71 args, _varargs, _varkw, _defaults = inspect.getargspec(dct['editor'].__init__)
72 for arg in args:
73 if arg not in ['self', 'parent']:
74 add_field_attribute_item(arg)
75
76 return type(name, bases, dct)
77
78
80 """Base class for implementing custom delegates.
81
82 .. attribute:: editor
83
84 class attribute specifies the editor class that should be used
85 """
86
87 editor = None
88
89 - def __init__(self, parent=None, editable=True, **kwargs):
90 """:param parent: the parent object for the delegate
91 :param editable: a boolean indicating if the field associated to the delegate
92 is editable"""
93 QItemDelegate.__init__(self, parent)
94 self.editable = editable
95 self.kwargs = kwargs
96 self._font_metrics = QtGui.QFontMetrics(QtGui.QApplication.font())
97 self._height = self._font_metrics.lineSpacing() + 10
98 self._width = self._font_metrics.averageCharWidth() * 20
99
112
114 return QtCore.QSize(self._width, self._height)
115
117 editor = self.sender()
118 assert editor
119 assert isinstance(editor, (QtGui.QWidget,))
120 self.emit(SIGNAL('commitData(QWidget*)'), editor)
121 sig = SIGNAL('closeEditor(QWidget*, \
122 QAbstractItemDelegate::EndEditHint)')
123 self.emit(sig, editor, QtGui.QAbstractItemDelegate.NoHint)
124
139
146
147 - def paint_text(self, painter, option, index, text, margin_left=0, margin_right=0):
148 """Paint unicode text into the given rect defined by option, and fill the rect with
149 the background color
150 :arg margin_left: additional margin to the left, to be used for icons or others
151 :arg margin_right: additional margin to the right, to be used for icons or others"""
152
153 background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
154 rect = option.rect
155
156 if( option.state & QtGui.QStyle.State_Selected ):
157 painter.fillRect(option.rect, option.palette.highlight())
158 fontColor = QtGui.QColor()
159 if self.editable:
160 Color = option.palette.highlightedText().color()
161 fontColor.setRgb(Color.red(), Color.green(), Color.blue())
162 else:
163 fontColor.setRgb(130,130,130)
164 else:
165 if self.editable:
166 painter.fillRect(rect, background_color)
167 fontColor = QtGui.QColor()
168 fontColor.setRgb(0,0,0)
169 else:
170 painter.fillRect(rect, option.palette.window())
171 fontColor = QtGui.QColor()
172 fontColor.setRgb(130,130,130)
173
174
175 painter.setPen(fontColor.toRgb())
176 painter.drawText(rect.x() + 2 + margin_left,
177 rect.y(),
178 rect.width() - 4 - (margin_left + margin_right),
179 rect.height(),
180 Qt.AlignVCenter | Qt.AlignLeft,
181 text)
182