source: sasview/src/sas/qtgui/Perspectives/Fitting/ViewDelegate.py @ 7ffa5ee9

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 7ffa5ee9 was 7ffa5ee9, checked in by Tim Snow <tim.snow@…>, 6 years ago

Fixing ticket SVCC-91

Minor modification in the ViewDelegate? to make the polydispersity editable

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