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