1 | from PyQt4 import QtGui |
---|
2 | from PyQt4 import QtCore |
---|
3 | |
---|
4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
5 | |
---|
6 | # Main parameter table view columns |
---|
7 | PARAM_PROPERTY=0 |
---|
8 | PARAM_VALUE=1 |
---|
9 | PARAM_MIN=2 |
---|
10 | PARAM_MAX=3 |
---|
11 | PARAM_UNIT=4 |
---|
12 | |
---|
13 | # polydispersity functions |
---|
14 | POLYDISPERSE_FUNCTIONS=['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
15 | # polydispersity columns |
---|
16 | POLY_PARAMETER=0 |
---|
17 | POLY_PD=1 |
---|
18 | POLY_MIN=2 |
---|
19 | POLY_MAX=3 |
---|
20 | POLY_NPTS=4 |
---|
21 | POLY_NSIGS=5 |
---|
22 | POLY_FUNCTION=6 |
---|
23 | |
---|
24 | |
---|
25 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
26 | """ |
---|
27 | Custom delegate for appearance and behavior control of the model view |
---|
28 | """ |
---|
29 | def paint(self, painter, option, index): |
---|
30 | """ |
---|
31 | Overwrite generic painter for certain columns |
---|
32 | """ |
---|
33 | if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX): |
---|
34 | # Units - present in nice HTML |
---|
35 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
36 | self.initStyleOption(options,index) |
---|
37 | |
---|
38 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
39 | |
---|
40 | # Prepare document for inserting into cell |
---|
41 | doc = QtGui.QTextDocument() |
---|
42 | |
---|
43 | # Convert the unit description into HTML |
---|
44 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
45 | doc.setHtml(text_html) |
---|
46 | |
---|
47 | # delete the original content |
---|
48 | options.text = "" |
---|
49 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
50 | |
---|
51 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
52 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
53 | |
---|
54 | painter.save() |
---|
55 | painter.translate(textRect.topLeft()) |
---|
56 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
57 | # Draw the QTextDocument in the cell |
---|
58 | doc.documentLayout().draw(painter, context) |
---|
59 | painter.restore() |
---|
60 | else: |
---|
61 | # Just the default paint |
---|
62 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
63 | |
---|
64 | def createEditor(self, widget, option, index): |
---|
65 | """ |
---|
66 | Overwrite generic editor for certain columns |
---|
67 | """ |
---|
68 | if not index.isValid(): |
---|
69 | return 0 |
---|
70 | if index.column() == PARAM_VALUE: #only in the value column |
---|
71 | editor = QtGui.QLineEdit(widget) |
---|
72 | validator = QtGui.QDoubleValidator() |
---|
73 | editor.setValidator(validator) |
---|
74 | return editor |
---|
75 | |
---|
76 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
77 | |
---|
78 | def setModelData(self, editor, model, index): |
---|
79 | """ |
---|
80 | Overwrite generic model update method for certain columns |
---|
81 | """ |
---|
82 | if index.column() in (PARAM_MIN, PARAM_MAX): |
---|
83 | try: |
---|
84 | value_float = float(editor.text()) |
---|
85 | except ValueError: |
---|
86 | # TODO: present the failure to the user |
---|
87 | # balloon popup? tooltip? cell background colour flash? |
---|
88 | return |
---|
89 | QtGui.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
90 | |
---|
91 | |
---|
92 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
93 | """ |
---|
94 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
95 | """ |
---|
96 | def createEditor(self, widget, option, index): |
---|
97 | # Remember the current choice |
---|
98 | current_text = index.data().toString() |
---|
99 | if index.column() == POLY_FUNCTION: |
---|
100 | editor = QtGui.QComboBox(widget) |
---|
101 | for function in POLYDISPERSE_FUNCTIONS: |
---|
102 | editor.addItem(function) |
---|
103 | current_index = editor.findText(current_text) |
---|
104 | editor.setCurrentIndex(current_index if current_index>-1 else 0) |
---|
105 | return editor |
---|
106 | else: |
---|
107 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|