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
7 -class TextEditDelegate(CustomDelegate):
8 """Custom delegate for simple string values"""
9
10 __metaclass__ = DocumentationMetaclass
11
12 editor = editors.TextEditEditor
13
14 - def __init__(self, parent=None, editable=True, **kwargs):
15 CustomDelegate.__init__(self, parent, editable)
16
17 self.editable = editable
18
19
20 - def paint(self, painter, option, index):
21 painter.save()
22 self.drawBackground(painter, option, index)
23
24 text = index.model().data(index, Qt.EditRole).toString()
25
26 background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
27
28 rect = option.rect
29 rect = QtCore.QRect(rect.left(), rect.top(), rect.width(), rect.height())
30
31 if( option.state & QtGui.QStyle.State_Selected ):
32 painter.fillRect(option.rect, option.palette.highlight())
33 fontColor = QtGui.QColor()
34 if self.editable:
35 Color = option.palette.highlightedText().color()
36 fontColor.setRgb(Color.red(), Color.green(), Color.blue())
37 else:
38 fontColor.setRgb(130,130,130)
39 else:
40 if self.editable:
41 painter.fillRect(option.rect, background_color)
42 fontColor = QtGui.QColor()
43 fontColor.setRgb(0,0,0)
44 else:
45 painter.fillRect(option.rect, option.palette.window())
46 fontColor = QtGui.QColor()
47 fontColor.setRgb(130,130,130)
48
49
50 painter.setPen(fontColor.toRgb())
51 rect = QtCore.QRect(option.rect.left(),
52 option.rect.top(),
53 option.rect.width(),
54 option.rect.height())
55 painter.drawText(rect.x() + 2,
56 rect.y(),
57 rect.width() - 4,
58 rect.height(),
59 Qt.AlignVCenter | Qt.AlignLeft,
60 text)
61 painter.restore()
62