source: sasview/src/sas/qtgui/Perspectives/Fitting/ViewDelegate.py @ 87dfca4

ESS_GUIESS_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 87dfca4 was fde5bcd, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Fixed issue with unwanted headers

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