1 | from PyQt4 import QtGui |
---|
2 | from PyQt4 import QtCore |
---|
3 | |
---|
4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
5 | |
---|
6 | # Table view columns |
---|
7 | PROPERTY=0 |
---|
8 | VALUE=1 |
---|
9 | MIN=2 |
---|
10 | MAX=3 |
---|
11 | UNIT=4 |
---|
12 | |
---|
13 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
14 | """ |
---|
15 | Custom delegate for appearance and behavior control of the model view |
---|
16 | """ |
---|
17 | def paint(self, painter, option, index): |
---|
18 | """ |
---|
19 | Overwrite generic painter for certain columns |
---|
20 | """ |
---|
21 | if index.column() == UNIT or index.column() == MIN or index.column() == MAX: |
---|
22 | # Units - present in nice HTML |
---|
23 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
24 | self.initStyleOption(options,index) |
---|
25 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
26 | |
---|
27 | # Prepare document for inserting into cell |
---|
28 | doc = QtGui.QTextDocument() |
---|
29 | |
---|
30 | # Convert the unit description into HTML |
---|
31 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
32 | doc.setHtml(text_html) |
---|
33 | |
---|
34 | # delete the original content |
---|
35 | options.text = "" |
---|
36 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter); |
---|
37 | |
---|
38 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
39 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
40 | |
---|
41 | painter.save() |
---|
42 | painter.translate(textRect.topLeft()) |
---|
43 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
44 | # Draw the QTextDocument in the cell |
---|
45 | doc.documentLayout().draw(painter, context) |
---|
46 | |
---|
47 | painter.restore() |
---|
48 | else: |
---|
49 | # Just the default paint |
---|
50 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
51 | |
---|
52 | #def sizeHint(self, option, index): |
---|
53 | # options = QtGui.QStyleOptionViewItemV4(option) |
---|
54 | # self.initStyleOption(options,index) |
---|
55 | |
---|
56 | # doc = QtGui.QTextDocument() |
---|
57 | # doc.setHtml(options.text) |
---|
58 | # doc.setTextWidth(options.rect.width()) |
---|
59 | # return QtCore.QSize(doc.idealWidth(), doc.size().height()) |
---|