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 | #POLYDISPERSE_FUNCTIONS = ['rectangle', 'lognormal', 'gaussian', 'schulz'] |
---|
89 | |
---|
90 | combo_updated = QtCore.pyqtSignal(str, int) |
---|
91 | filename_updated = QtCore.pyqtSignal(int) |
---|
92 | |
---|
93 | def __init__(self, parent=None): |
---|
94 | """ |
---|
95 | Overwrite generic constructor to allow for some globals |
---|
96 | """ |
---|
97 | super(QtGui.QStyledItemDelegate, self).__init__() |
---|
98 | |
---|
99 | self.poly_parameter = 0 |
---|
100 | self.poly_pd = 1 |
---|
101 | self.poly_min = 2 |
---|
102 | self.poly_max = 3 |
---|
103 | self.poly_npts = 4 |
---|
104 | self.poly_nsigs = 5 |
---|
105 | self.poly_function = 6 |
---|
106 | self.poly_filename = 7 |
---|
107 | |
---|
108 | def editableParameters(self): |
---|
109 | return [self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs] |
---|
110 | |
---|
111 | def columnDict(self): |
---|
112 | return {self.poly_pd: 'width', |
---|
113 | self.poly_min: 'min', |
---|
114 | self.poly_max: 'max', |
---|
115 | self.poly_npts: 'npts', |
---|
116 | self.poly_nsigs: 'nsigmas'} |
---|
117 | |
---|
118 | def addErrorColumn(self): |
---|
119 | """ |
---|
120 | Modify local column pointers |
---|
121 | Note: the reverse is never required! |
---|
122 | """ |
---|
123 | self.poly_parameter = 0 |
---|
124 | self.poly_pd = 1 |
---|
125 | self.poly_min = 3 |
---|
126 | self.poly_max = 4 |
---|
127 | self.poly_npts = 5 |
---|
128 | self.poly_nsigs = 6 |
---|
129 | self.poly_function = 7 |
---|
130 | self.poly_filename = 8 |
---|
131 | |
---|
132 | def createEditor(self, widget, option, index): |
---|
133 | # Remember the current choice |
---|
134 | if not index.isValid(): |
---|
135 | return 0 |
---|
136 | elif index.column() == self.poly_filename: |
---|
137 | # Notify the widget that we want to change the filename |
---|
138 | self.filename_updated.emit(index.row()) |
---|
139 | return None |
---|
140 | elif index.column() in self.editableParameters(): |
---|
141 | self.editor = QtGui.QLineEdit(widget) |
---|
142 | validator = QtGui.QDoubleValidator() |
---|
143 | self.editor.setValidator(validator) |
---|
144 | return self.editor |
---|
145 | else: |
---|
146 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
147 | |
---|
148 | def paint(self, painter, option, index): |
---|
149 | """ |
---|
150 | Overwrite generic painter for certain columns |
---|
151 | """ |
---|
152 | if index.column() in (self.poly_min, self.poly_max): |
---|
153 | # Units - present in nice HTML |
---|
154 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
155 | self.initStyleOption(options,index) |
---|
156 | |
---|
157 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
158 | |
---|
159 | # Prepare document for inserting into cell |
---|
160 | doc = QtGui.QTextDocument() |
---|
161 | |
---|
162 | # Convert the unit description into HTML |
---|
163 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
164 | doc.setHtml(text_html) |
---|
165 | |
---|
166 | # delete the original content |
---|
167 | options.text = "" |
---|
168 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
169 | |
---|
170 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
171 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
172 | |
---|
173 | painter.save() |
---|
174 | painter.translate(textRect.topLeft()) |
---|
175 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
176 | # Draw the QTextDocument in the cell |
---|
177 | doc.documentLayout().draw(painter, context) |
---|
178 | painter.restore() |
---|
179 | else: |
---|
180 | # Just the default paint |
---|
181 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|
182 | |
---|
183 | class MagnetismViewDelegate(QtGui.QStyledItemDelegate): |
---|
184 | """ |
---|
185 | Custom delegate for appearance and behavior control of the magnetism view |
---|
186 | """ |
---|
187 | def __init__(self, parent=None): |
---|
188 | """ |
---|
189 | Overwrite generic constructor to allow for some globals |
---|
190 | """ |
---|
191 | super(QtGui.QStyledItemDelegate, self).__init__() |
---|
192 | |
---|
193 | self.mag_parameter = 0 |
---|
194 | self.mag_value = 1 |
---|
195 | self.mag_min = 2 |
---|
196 | self.mag_max = 3 |
---|
197 | self.mag_unit = 4 |
---|
198 | |
---|
199 | def editableParameters(self): |
---|
200 | return [self.mag_min, self.mag_max] |
---|
201 | |
---|
202 | def addErrorColumn(self): |
---|
203 | """ |
---|
204 | Modify local column pointers |
---|
205 | Note: the reverse is never required! |
---|
206 | """ |
---|
207 | self.mag_parameter = 0 |
---|
208 | self.mag_value = 1 |
---|
209 | self.mag_min = 3 |
---|
210 | self.mag_max = 4 |
---|
211 | self.mag_unit = 5 |
---|
212 | |
---|
213 | def createEditor(self, widget, option, index): |
---|
214 | # Remember the current choice |
---|
215 | current_text = index.data().toString() |
---|
216 | if not index.isValid(): |
---|
217 | return 0 |
---|
218 | if index.column() in self.editableParameters(): |
---|
219 | editor = QtGui.QLineEdit(widget) |
---|
220 | validator = QtGui.QDoubleValidator() |
---|
221 | editor.setValidator(validator) |
---|
222 | return editor |
---|
223 | else: |
---|
224 | QtGui.QStyledItemDelegate.createEditor(self, widget, option, index) |
---|
225 | |
---|
226 | def paint(self, painter, option, index): |
---|
227 | """ |
---|
228 | Overwrite generic painter for certain columns |
---|
229 | """ |
---|
230 | if index.column() in (self.mag_min, self.mag_max, self.mag_unit): |
---|
231 | # Units - present in nice HTML |
---|
232 | options = QtGui.QStyleOptionViewItemV4(option) |
---|
233 | self.initStyleOption(options,index) |
---|
234 | |
---|
235 | style = QtGui.QApplication.style() if options.widget is None else options.widget.style() |
---|
236 | |
---|
237 | # Prepare document for inserting into cell |
---|
238 | doc = QtGui.QTextDocument() |
---|
239 | |
---|
240 | # Convert the unit description into HTML |
---|
241 | text_html = GuiUtils.convertUnitToHTML(str(options.text)) |
---|
242 | doc.setHtml(text_html) |
---|
243 | |
---|
244 | # delete the original content |
---|
245 | options.text = "" |
---|
246 | style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget); |
---|
247 | |
---|
248 | context = QtGui.QAbstractTextDocumentLayout.PaintContext() |
---|
249 | textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) |
---|
250 | |
---|
251 | painter.save() |
---|
252 | painter.translate(textRect.topLeft()) |
---|
253 | painter.setClipRect(textRect.translated(-textRect.topLeft())) |
---|
254 | # Draw the QTextDocument in the cell |
---|
255 | doc.documentLayout().draw(painter, context) |
---|
256 | painter.restore() |
---|
257 | else: |
---|
258 | # Just the default paint |
---|
259 | QtGui.QStyledItemDelegate.paint(self, painter, option, index) |
---|