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

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

More Qt5 related fixes

  • Property mode set to 100644
File size: 9.9 KB
RevLine 
[4992ff2]1from PyQt5 import QtCore
2from PyQt5 import QtGui
3from PyQt5 import QtWidgets
[ad6b4e2]4
5import sas.qtgui.Utilities.GuiUtils as GuiUtils
[6011788]6
[4992ff2]7class ModelViewDelegate(QtWidgets.QStyledItemDelegate):
[6011788]8    """
9    Custom delegate for appearance and behavior control of the model view
10    """
[8f2548c]11    def __init__(self, parent=None):
12        """
13        Overwrite generic constructor to allow for some globals
14        """
[4992ff2]15        super(QtWidgets.QStyledItemDelegate, self).__init__()
[8f2548c]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
[16afe01]22        self.param_max=3
23        self.param_unit=4
[8f2548c]24
[16afe01]25    def fancyColumns(self):
26        return [self.param_value, self.param_min, self.param_max, self.param_unit]
[8f2548c]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
[06b0138]39
[6011788]40    def paint(self, painter, option, index):
41        """
42        Overwrite generic painter for certain columns
43        """
[16afe01]44        if index.column() in self.fancyColumns():
[6011788]45            # Units - present in nice HTML
[4992ff2]46            options = QtWidgets.QStyleOptionViewItem(option)
[6011788]47            self.initStyleOption(options,index)
[2a432e7]48
[7969b9c]49            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[6011788]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 = ""
[4992ff2]60            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[6011788]61
62            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]63            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[6011788]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
[4992ff2]73            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[6011788]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
[8f2548c]81        if index.column() == self.param_value: #only in the value column
[4992ff2]82            editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]83            validator = GuiUtils.DoubleValidator()
[6011788]84            editor.setValidator(validator)
85            return editor
[8f2548c]86        if index.column() in [self.param_property, self.param_error]:
[0d13814]87            # Set some columns uneditable
88            return None
[6011788]89
90        return super(ModelViewDelegate, self).createEditor(widget, option, index)
91
[00b3b40]92    def setModelData(self, editor, model, index):
93        """
94        Overwrite generic model update method for certain columns
95        """
[8f2548c]96        if index.column() in (self.param_min, self.param_max):
[6011788]97            try:
[00b3b40]98                value_float = float(editor.text())
[6011788]99            except ValueError:
[00b3b40]100                # TODO: present the failure to the user
101                # balloon popup? tooltip? cell background colour flash?
[6011788]102                return
[4992ff2]103        QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index)
[6011788]104
105
[4992ff2]106class PolyViewDelegate(QtWidgets.QStyledItemDelegate):
[6011788]107    """
[00b3b40]108    Custom delegate for appearance and behavior control of the polydispersity view
[6011788]109    """
[e43fc91]110    POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz']
[06b0138]111
[aca8418]112    combo_updated = QtCore.pyqtSignal(str, int)
[e43fc91]113    filename_updated = QtCore.pyqtSignal(int)
[06b0138]114
[8eaa101]115    def __init__(self, parent=None):
116        """
117        Overwrite generic constructor to allow for some globals
118        """
[4992ff2]119        super(QtWidgets.QStyledItemDelegate, self).__init__()
[8eaa101]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
[e43fc91]128        self.poly_filename = 7
[8eaa101]129
130    def editableParameters(self):
[7ffa5ee9]131        return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs]
[8eaa101]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
[e43fc91]152        self.poly_filename = 8
[8eaa101]153
[00b3b40]154    def createEditor(self, widget, option, index):
155        # Remember the current choice
[aca8418]156        if not index.isValid():
157            return 0
[e43fc91]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
[8eaa101]162        elif index.column() in self.editableParameters():
[4992ff2]163            self.editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]164            validator = GuiUtils.DoubleValidator()
[e43fc91]165            self.editor.setValidator(validator)
166            return self.editor
[6011788]167        else:
[4992ff2]168            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[aca8418]169
170    def paint(self, painter, option, index):
171        """
172        Overwrite generic painter for certain columns
173        """
[8eaa101]174        if index.column() in (self.poly_min, self.poly_max):
[aca8418]175            # Units - present in nice HTML
[4992ff2]176            options = QtWidgets.QStyleOptionViewItem(option)
[aca8418]177            self.initStyleOption(options,index)
178
[7969b9c]179            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[b00414d]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 = ""
[4992ff2]190            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[b00414d]191
192            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]193            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[b00414d]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
[4992ff2]203            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[b00414d]204
[4992ff2]205class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate):
[b00414d]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        """
[4992ff2]213        super(QtWidgets.QStyledItemDelegate, self).__init__()
[b00414d]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):
[02f1d12]222        return [self.mag_value, self.mag_min, self.mag_max]
[b00414d]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
[fbfc488]237        current_text = index.data()
[b00414d]238        if not index.isValid():
239            return 0
240        if index.column() in self.editableParameters():
[4992ff2]241            editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]242            validator = GuiUtils.DoubleValidator()
[b00414d]243            editor.setValidator(validator)
244            return editor
245        else:
[4992ff2]246            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[b00414d]247
248    def paint(self, painter, option, index):
249        """
250        Overwrite generic painter for certain columns
251        """
[0d13814]252        if index.column() in (self.mag_min, self.mag_max, self.mag_unit):
[b00414d]253            # Units - present in nice HTML
[4992ff2]254            options = QtWidgets.QStyleOptionViewItem(option)
[b00414d]255            self.initStyleOption(options,index)
256
[7969b9c]257            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[aca8418]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 = ""
[4992ff2]268            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[aca8418]269
270            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]271            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[aca8418]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
[4992ff2]281            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.