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

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 fbfc488 was fbfc488, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

More Qt5 related fixes.

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