1 | from PyQt4 import QtGui |
---|
2 | from PyQt4 import QtCore |
---|
3 | |
---|
4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
5 | |
---|
6 | class ModelViewDelegate(QtGui.QStyledItemDelegate): |
---|
7 | """ |
---|
8 | Custom delegate for appearance and behavior control of the model view |
---|
9 | """ |
---|
10 | # Main parameter table view columns |
---|
11 | PARAM_PROPERTY=0 |
---|
12 | PARAM_VALUE=1 |
---|
13 | PARAM_MIN=2 |
---|
14 | PARAM_MAX=3 |
---|
15 | PARAM_UNIT=4 |
---|
16 | |
---|
17 | def paint(self, painter, option, index): |
---|
18 | """ |
---|
19 | Overwrite generic painter for certain columns |
---|
20 | """ |
---|
21 | if index.column() in (self.PARAM_UNIT, self.PARAM_MIN, self.PARAM_MAX): |
---|
22 | # Units - present in nice HTML |
---|
23 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
24 | self.initStyleOption(options,index) |
---|
25 | |
---|
26 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
27 | |
---|
28 | # Prepare document for inserting into cell |
---|
29 | doc = QtGui.QTextDocument() |
---|
30 | |
---|
31 | # Convert the unit description into HTML |
---|
32 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
33 | doc.setHtml(text_html) |
---|
34 | |
---|
35 | # delete the original content |
---|
36 | options.text = "" |
---|
37 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
38 | |
---|
39 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
40 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
41 | |
---|
42 | painter.save() |
---|
43 | painter.translate(textRect.topLeft()) |
---|
44 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
45 | # Draw the QTextDocument in the cell |
---|
46 | doc.documentLayout().draw(painter, context) |
---|
47 | painter.restore() |
---|
48 | else: |
---|
49 | # Just the default paint |
---|
50 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
51 | |
---|
52 | def createEditor(self, widget, option, index): |
---|
53 | """ |
---|
54 | Overwrite generic editor for certain columns |
---|
55 | """ |
---|
56 | if not index.isValid(): |
---|
57 | return 0 |
---|
58 | if index.column() == self.PARAM_VALUE: #only in the value column |
---|
59 | editor = QtGui.QLineEdit(widget) |
---|
60 | validator = QtGui.QDoubleValidator() |
---|
61 | editor.setValidator(validator) |
---|
62 | return editor |
---|
63 | if index.column() in [self.PARAM_PROPERTY, self.PARAM_UNIT]: |
---|
64 | # Set some columns uneditable |
---|
65 | return None |
---|
66 | |
---|
67 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
68 | |
---|
69 | def setModelData(self, editor, model, index): |
---|
70 | """ |
---|
71 | Overwrite generic model update method for certain columns |
---|
72 | """ |
---|
73 | if index.column() in (self.PARAM_MIN, self.PARAM_MAX): |
---|
74 | try: |
---|
75 | value_float = float(editor.text()) |
---|
76 | except ValueError: |
---|
77 | # TODO: present the failure to the user |
---|
78 | # balloon popup? tooltip? cell background colour flash? |
---|
79 | return |
---|
80 | QtGui.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
81 | |
---|
82 | |
---|
83 | class PolyViewDelegate(QtGui.QStyledItemDelegate): |
---|
84 | """ |
---|
85 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
86 | """ |
---|
87 | POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
88 | |
---|
89 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
90 | |
---|
91 | def __init__(self, parent=None): |
---|
92 | """ |
---|
93 | Overwrite generic constructor to allow for some globals |
---|
94 | """ |
---|
95 | super(QtGui.QStyledItemDelegate, self).__init__() |
---|
96 | |
---|
97 | self.poly_parameter = 0 |
---|
98 | self.poly_pd = 1 |
---|
99 | self.poly_min = 2 |
---|
100 | self.poly_max = 3 |
---|
101 | self.poly_npts = 4 |
---|
102 | self.poly_nsigs = 5 |
---|
103 | self.poly_function = 6 |
---|
104 | |
---|
105 | def editableParameters(self): |
---|
106 | return [self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs] |
---|
107 | |
---|
108 | def columnDict(self): |
---|
109 | return {self.poly_pd: 'width', |
---|
110 | self.poly_min: 'min', |
---|
111 | self.poly_max: 'max', |
---|
112 | self.poly_npts: 'npts', |
---|
113 | self.poly_nsigs: 'nsigmas'} |
---|
114 | |
---|
115 | def addErrorColumn(self): |
---|
116 | """ |
---|
117 | Modify local column pointers |
---|
118 | Note: the reverse is never required! |
---|
119 | """ |
---|
120 | self.poly_parameter = 0 |
---|
121 | self.poly_pd = 1 |
---|
122 | self.poly_min = 3 |
---|
123 | self.poly_max = 4 |
---|
124 | self.poly_npts = 5 |
---|
125 | self.poly_nsigs = 6 |
---|
126 | self.poly_function = 7 |
---|
127 | |
---|
128 | def createEditor(self, widget, option, index): |
---|
129 | # Remember the current choice |
---|
130 | current_text = index.data().toString() |
---|
131 | if not index.isValid(): |
---|
132 | return 0 |
---|
133 | if index.column() == self.poly_function: |
---|
134 | editor = QtGui.QComboBox(widget) |
---|
135 | for function in self.POLYDISPERSE_FUNCTIONS: |
---|
136 | editor.addItem(function) |
---|
137 | current_index = editor.findText(current_text) |
---|
138 | editor.setCurrentIndex(current_index if current_index>-1 else 3) |
---|
139 | editor.currentIndexChanged.connect(lambda: self.combo_updated.emit(str(editor.currentText()), index.row())) |
---|
140 | return editor |
---|
141 | elif index.column() in self.editableParameters(): |
---|
142 | editor = QtGui.QLineEdit(widget) |
---|
143 | validator = QtGui.QDoubleValidator() |
---|
144 | editor.setValidator(validator) |
---|
145 | return editor |
---|
146 | else: |
---|
147 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
148 | |
---|
149 | def paint(self, painter, option, index): |
---|
150 | """ |
---|
151 | Overwrite generic painter for certain columns |
---|
152 | """ |
---|
153 | if index.column() in (self.poly_min, self.poly_max): |
---|
154 | # Units - present in nice HTML |
---|
155 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
156 | self.initStyleOption(options,index) |
---|
157 | |
---|
158 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
159 | |
---|
160 | # Prepare document for inserting into cell |
---|
161 | doc = QtGui.QTextDocument() |
---|
162 | |
---|
163 | # Convert the unit description into HTML |
---|
164 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
165 | doc.setHtml(text_html) |
---|
166 | |
---|
167 | # delete the original content |
---|
168 | options.text = "" |
---|
169 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
170 | |
---|
171 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
172 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
173 | |
---|
174 | painter.save() |
---|
175 | painter.translate(textRect.topLeft()) |
---|
176 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
177 | # Draw the QTextDocument in the cell |
---|
178 | doc.documentLayout().draw(painter, context) |
---|
179 | painter.restore() |
---|
180 | else: |
---|
181 | # Just the default paint |
---|
182 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
183 | |
---|
184 | class MagnetismViewDelegate(QtGui.QStyledItemDelegate): |
---|
185 | """ |
---|
186 | Custom delegate for appearance and behavior control of the magnetism view |
---|
187 | """ |
---|
188 | def __init__(self, parent=None): |
---|
189 | """ |
---|
190 | Overwrite generic constructor to allow for some globals |
---|
191 | """ |
---|
192 | super(QtGui.QStyledItemDelegate, self).__init__() |
---|
193 | |
---|
194 | self.mag_parameter = 0 |
---|
195 | self.mag_value = 1 |
---|
196 | self.mag_min = 2 |
---|
197 | self.mag_max = 3 |
---|
198 | self.mag_unit = 4 |
---|
199 | |
---|
200 | def editableParameters(self): |
---|
201 | return [self.mag_min, self.mag_max] |
---|
202 | |
---|
203 | def addErrorColumn(self): |
---|
204 | """ |
---|
205 | Modify local column pointers |
---|
206 | Note: the reverse is never required! |
---|
207 | """ |
---|
208 | self.mag_parameter = 0 |
---|
209 | self.mag_value = 1 |
---|
210 | self.mag_min = 3 |
---|
211 | self.mag_max = 4 |
---|
212 | self.mag_unit = 5 |
---|
213 | |
---|
214 | def createEditor(self, widget, option, index): |
---|
215 | # Remember the current choice |
---|
216 | current_text = index.data().toString() |
---|
217 | if not index.isValid(): |
---|
218 | return 0 |
---|
219 | if index.column() in self.editableParameters(): |
---|
220 | editor = QtGui.QLineEdit(widget) |
---|
221 | validator = QtGui.QDoubleValidator() |
---|
222 | editor.setValidator(validator) |
---|
223 | return editor |
---|
224 | else: |
---|
225 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
226 | |
---|
227 | def paint(self, painter, option, index): |
---|
228 | """ |
---|
229 | Overwrite generic painter for certain columns |
---|
230 | """ |
---|
231 | if index.column() in (self.mag_min, self.mag_max, self.mag_unit): |
---|
232 | # Units - present in nice HTML |
---|
233 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
234 | self.initStyleOption(options,index) |
---|
235 | |
---|
236 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
237 | |
---|
238 | # Prepare document for inserting into cell |
---|
239 | doc = QtGui.QTextDocument() |
---|
240 | |
---|
241 | # Convert the unit description into HTML |
---|
242 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
243 | doc.setHtml(text_html) |
---|
244 | |
---|
245 | # delete the original content |
---|
246 | options.text = "" |
---|
247 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
248 | |
---|
249 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
250 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
251 | |
---|
252 | painter.save() |
---|
253 | painter.translate(textRect.topLeft()) |
---|
254 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
255 | # Draw the QTextDocument in the cell |
---|
256 | doc.documentLayout().draw(painter, context) |
---|
257 | painter.restore() |
---|
258 | else: |
---|
259 | # Just the default paint |
---|
260 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|