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