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 | if index.column() in (self.param_min, self.param_max): |
---|
98 | # Check if the edit role is set |
---|
99 | if not (index.flags() & QtCore.Qt.ItemIsEditable): |
---|
100 | return None |
---|
101 | |
---|
102 | return super(ModelViewDelegate, self).createEditor(widget, option, index) |
---|
103 | |
---|
104 | def setModelData(self, editor, model, index): |
---|
105 | """ |
---|
106 | Overwrite generic model update method for certain columns |
---|
107 | """ |
---|
108 | if index.column() in (self.param_min, self.param_max): |
---|
109 | try: |
---|
110 | value_float = float(editor.text()) |
---|
111 | except ValueError: |
---|
112 | # TODO: present the failure to the user |
---|
113 | # balloon popup? tooltip? cell background colour flash? |
---|
114 | return |
---|
115 | QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index) |
---|
116 | |
---|
117 | |
---|
118 | class PolyViewDelegate(QtWidgets.QStyledItemDelegate): |
---|
119 | """ |
---|
120 | Custom delegate for appearance and behavior control of the polydispersity view |
---|
121 | """ |
---|
122 | POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz'] |
---|
123 | |
---|
124 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
125 | filename_updated = QtCore.pyqtSignal(int) |
---|
126 | |
---|
127 | def __init__(self, parent=None): |
---|
128 | """ |
---|
129 | Overwrite generic constructor to allow for some globals |
---|
130 | """ |
---|
131 | super(PolyViewDelegate, self).__init__() |
---|
132 | |
---|
133 | self.poly_parameter = 0 |
---|
134 | self.poly_pd = 1 |
---|
135 | self.poly_error = None |
---|
136 | self.poly_min = 2 |
---|
137 | self.poly_max = 3 |
---|
138 | self.poly_npts = 4 |
---|
139 | self.poly_nsigs = 5 |
---|
140 | self.poly_function = 6 |
---|
141 | self.poly_filename = 7 |
---|
142 | |
---|
143 | def editableParameters(self): |
---|
144 | return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs] |
---|
145 | |
---|
146 | def columnDict(self): |
---|
147 | return {self.poly_pd: 'width', |
---|
148 | self.poly_min: 'min', |
---|
149 | self.poly_max: 'max', |
---|
150 | self.poly_npts: 'npts', |
---|
151 | self.poly_nsigs: 'nsigmas'} |
---|
152 | |
---|
153 | def addErrorColumn(self): |
---|
154 | """ |
---|
155 | Modify local column pointers |
---|
156 | Note: the reverse is never required! |
---|
157 | """ |
---|
158 | self.poly_parameter = 0 |
---|
159 | self.poly_pd = 1 |
---|
160 | self.poly_error = 2 |
---|
161 | self.poly_min = 3 |
---|
162 | self.poly_max = 4 |
---|
163 | self.poly_npts = 5 |
---|
164 | self.poly_nsigs = 6 |
---|
165 | self.poly_function = 7 |
---|
166 | self.poly_filename = 8 |
---|
167 | |
---|
168 | def createEditor(self, widget, option, index): |
---|
169 | # Remember the current choice |
---|
170 | if not index.isValid(): |
---|
171 | return 0 |
---|
172 | elif index.column() == self.poly_filename: |
---|
173 | # Notify the widget that we want to change the filename |
---|
174 | self.filename_updated.emit(index.row()) |
---|
175 | return None |
---|
176 | elif index.column() in self.editableParameters(): |
---|
177 | self.editor = QtWidgets.QLineEdit(widget) |
---|
178 | validator = GuiUtils.DoubleValidator() |
---|
179 | self.editor.setValidator(validator) |
---|
180 | return self.editor |
---|
181 | else: |
---|
182 | QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
183 | |
---|
184 | def paint(self, painter, option, index): |
---|
185 | """ |
---|
186 | Overwrite generic painter for certain columns |
---|
187 | """ |
---|
188 | if index.column() in (self.poly_pd, self.poly_min, self.poly_max): |
---|
189 | # Units - present in nice HTML |
---|
190 | options = QtWidgets.QStyleOptionViewItem(option) |
---|
191 | self.initStyleOption(options,index) |
---|
192 | |
---|
193 | style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style() |
---|
194 | |
---|
195 | # Prepare document for inserting into cell |
---|
196 | doc = QtGui.QTextDocument() |
---|
197 | current_font = painter.font() |
---|
198 | doc.setDefaultFont(current_font) |
---|
199 | # Convert the unit description into HTML |
---|
200 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
201 | doc.setHtml(text_html) |
---|
202 | |
---|
203 | # delete the original content |
---|
204 | options.text = "" |
---|
205 | style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
206 | |
---|
207 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
208 | textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options) |
---|
209 | painter.save() |
---|
210 | |
---|
211 | rect = textRect.topLeft() |
---|
212 | y = rect.y() |
---|
213 | y += 5.0 # magic value for rendering nice display in the table |
---|
214 | rect.setY(y) |
---|
215 | painter.translate(rect) |
---|
216 | painter.setClipRect(textRect.translated(-rect)) |
---|
217 | # Draw the QTextDocument in the cell |
---|
218 | doc.documentLayout().draw(painter, context) |
---|
219 | painter.restore() |
---|
220 | else: |
---|
221 | # Just the default paint |
---|
222 | QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) |
---|
223 | |
---|
224 | class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate): |
---|
225 | """ |
---|
226 | Custom delegate for appearance and behavior control of the magnetism view |
---|
227 | """ |
---|
228 | def __init__(self, parent=None): |
---|
229 | """ |
---|
230 | Overwrite generic constructor to allow for some globals |
---|
231 | """ |
---|
232 | super(MagnetismViewDelegate, self).__init__() |
---|
233 | |
---|
234 | self.mag_parameter = 0 |
---|
235 | self.mag_value = 1 |
---|
236 | self.mag_min = 2 |
---|
237 | self.mag_max = 3 |
---|
238 | self.mag_unit = 4 |
---|
239 | |
---|
240 | def editableParameters(self): |
---|
241 | return [self.mag_value, self.mag_min, self.mag_max] |
---|
242 | |
---|
243 | def addErrorColumn(self): |
---|
244 | """ |
---|
245 | Modify local column pointers |
---|
246 | Note: the reverse is never required! |
---|
247 | """ |
---|
248 | self.mag_parameter = 0 |
---|
249 | self.mag_value = 1 |
---|
250 | self.mag_min = 3 |
---|
251 | self.mag_max = 4 |
---|
252 | self.mag_unit = 5 |
---|
253 | |
---|
254 | def createEditor(self, widget, option, index): |
---|
255 | # Remember the current choice |
---|
256 | current_text = index.data() |
---|
257 | if not index.isValid(): |
---|
258 | return 0 |
---|
259 | if index.column() in self.editableParameters(): |
---|
260 | editor = QtWidgets.QLineEdit(widget) |
---|
261 | validator = GuiUtils.DoubleValidator() |
---|
262 | editor.setValidator(validator) |
---|
263 | return editor |
---|
264 | else: |
---|
265 | QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
266 | |
---|
267 | def paint(self, painter, option, index): |
---|
268 | """ |
---|
269 | Overwrite generic painter for certain columns |
---|
270 | """ |
---|
271 | if index.column() in (self.mag_value, self.mag_min, self.mag_max, self.mag_unit): |
---|
272 | # Units - present in nice HTML |
---|
273 | options = QtWidgets.QStyleOptionViewItem(option) |
---|
274 | self.initStyleOption(options,index) |
---|
275 | |
---|
276 | style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style() |
---|
277 | |
---|
278 | # Prepare document for inserting into cell |
---|
279 | doc = QtGui.QTextDocument() |
---|
280 | current_font = painter.font() |
---|
281 | doc.setDefaultFont(current_font) |
---|
282 | # Convert the unit description into HTML |
---|
283 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
284 | doc.setHtml(text_html) |
---|
285 | |
---|
286 | # delete the original content |
---|
287 | options.text = "" |
---|
288 | style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
289 | |
---|
290 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
291 | textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options) |
---|
292 | |
---|
293 | painter.save() |
---|
294 | rect = textRect.topLeft() |
---|
295 | y = rect.y() |
---|
296 | y += 6.0 # magic value for rendering nice display in the table |
---|
297 | rect.setY(y) |
---|
298 | painter.translate(rect) |
---|
299 | painter.setClipRect(textRect.translated(-rect)) |
---|
300 | # Draw the QTextDocument in the cell |
---|
301 | doc.documentLayout().draw(painter, context) |
---|
302 | painter.restore() |
---|
303 | else: |
---|
304 | # Just the default paint |
---|
305 | QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) |
---|