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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since ee7e423 was ee7e423, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

fix displaced text in parameter table (SASVIEW-889; ticket-1114)

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