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 | POLY_EDITABLE_PARAMS = [POLY_MIN, POLY_MAX, POLY_NPTS, POLY_NSIGS] |
---|
24 | |
---|
25 | |
---|
26 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
27 | """ |
---|
28 | Custom delegate for appearance and behavior control of the model view |
---|
29 | """ |
---|
30 | def paint(self, painter, option, index): |
---|
31 | """ |
---|
32 | Overwrite generic painter for certain columns |
---|
33 | """ |
---|
34 | if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX): |
---|
35 | # Units - present in nice HTML |
---|
36 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
37 | self.initStyleOption(options,index) |
---|
38 | |
---|
39 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
40 | |
---|
41 | # Prepare document for inserting into cell |
---|
42 | doc = QtGui.QTextDocument() |
---|
43 | |
---|
44 | # Convert the unit description into HTML |
---|
45 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
46 | doc.setHtml(text_html) |
---|
47 | |
---|
48 | # delete the original content |
---|
49 | options.text = "" |
---|
50 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
51 | |
---|
52 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
53 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
54 | |
---|
55 | painter.save() |
---|
56 | painter.translate(textRect.topLeft()) |
---|
57 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
58 | # Draw the QTextDocument in the cell |
---|
59 | doc.documentLayout().draw(painter, context) |
---|
60 | painter.restore() |
---|
61 | else: |
---|
62 | # Just the default paint |
---|
63 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
64 | |
---|
65 | def createEditor(self, widget, option, index): |
---|
66 | """ |
---|
67 | Overwrite generic editor for certain columns |
---|
68 | """ |
---|
69 | if not index.isValid(): |
---|
70 | return 0 |
---|
71 | if index.column() == PARAM_VALUE: #only in the value column |
---|
72 | editor = QtGui.QLineEdit(widget) |
---|
73 | validator = QtGui.QDoubleValidator() |
---|
74 | editor.setValidator(validator) |
---|
75 | return editor |
---|
76 | |
---|
77 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
78 | |
---|
79 | def setModelData(self, editor, model, index): |
---|
80 | """ |
---|
81 | Overwrite generic model update method for certain columns |
---|
82 | """ |
---|
83 | if index.column() in (PARAM_MIN, PARAM_MAX): |
---|
84 | try: |
---|
85 | value_float = float(editor.text()) |
---|
86 | except ValueError: |
---|
87 | # TODO: present the failure to the user |
---|
88 | # balloon popup? tooltip? cell background colour flash? |
---|
89 | return |
---|
90 | QtGui.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
91 | |
---|
92 | |
---|
93 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
94 | """ |
---|
95 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
96 | """ |
---|
97 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
98 | def createEditor(self, widget, option, index): |
---|
99 | # Remember the current choice |
---|
100 | current_text = index.data().toString() |
---|
101 | if not index.isValid(): |
---|
102 | return 0 |
---|
103 | if index.column() == POLY_FUNCTION: |
---|
104 | editor = QtGui.QComboBox(widget) |
---|
105 | for function in POLYDISPERSE_FUNCTIONS: |
---|
106 | editor.addItem(function) |
---|
107 | current_index = editor.findText(current_text) |
---|
108 | editor.setCurrentIndex(current_index if current_index>-1 else 3) |
---|
109 | editor.currentIndexChanged.connect(lambda: self.combo_updated.emit(str(editor.currentText()), index.row())) |
---|
110 | return editor |
---|
111 | elif index.column() in POLY_EDITABLE_PARAMS: |
---|
112 | editor = QtGui.QLineEdit(widget) |
---|
113 | validator = QtGui.QDoubleValidator() |
---|
114 | editor.setValidator(validator) |
---|
115 | return editor |
---|
116 | else: |
---|
117 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
118 | |
---|
119 | def paint(self, painter, option, index): |
---|
120 | """ |
---|
121 | Overwrite generic painter for certain columns |
---|
122 | """ |
---|
123 | if index.column() in (POLY_MIN, POLY_MAX): |
---|
124 | # Units - present in nice HTML |
---|
125 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
126 | self.initStyleOption(options,index) |
---|
127 | |
---|
128 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
129 | |
---|
130 | # Prepare document for inserting into cell |
---|
131 | doc = QtGui.QTextDocument() |
---|
132 | |
---|
133 | # Convert the unit description into HTML |
---|
134 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
135 | doc.setHtml(text_html) |
---|
136 | |
---|
137 | # delete the original content |
---|
138 | options.text = "" |
---|
139 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
140 | |
---|
141 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
142 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
143 | |
---|
144 | painter.save() |
---|
145 | painter.translate(textRect.topLeft()) |
---|
146 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
147 | # Draw the QTextDocument in the cell |
---|
148 | doc.documentLayout().draw(painter, context) |
---|
149 | painter.restore() |
---|
150 | else: |
---|
151 | # Just the default paint |
---|
152 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|