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
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)
[ee7e423]57            doc.setDocumentMargin(1)
[6011788]58
59            # delete the original content
60            options.text = ""
[4992ff2]61            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[6011788]62
63            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]64            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[6011788]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
[4992ff2]74            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[6011788]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
[8f2548c]82        if index.column() == self.param_value: #only in the value column
[4992ff2]83            editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]84            validator = GuiUtils.DoubleValidator()
[6011788]85            editor.setValidator(validator)
86            return editor
[fde5bcd]87        if index.column() in [self.param_property, self.param_error, self.param_unit]:
[0d13814]88            # Set some columns uneditable
89            return None
[6011788]90
91        return super(ModelViewDelegate, self).createEditor(widget, option, index)
92
[00b3b40]93    def setModelData(self, editor, model, index):
94        """
95        Overwrite generic model update method for certain columns
96        """
[8f2548c]97        if index.column() in (self.param_min, self.param_max):
[6011788]98            try:
[00b3b40]99                value_float = float(editor.text())
[6011788]100            except ValueError:
[00b3b40]101                # TODO: present the failure to the user
102                # balloon popup? tooltip? cell background colour flash?
[6011788]103                return
[4992ff2]104        QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index)
[6011788]105
106
[4992ff2]107class PolyViewDelegate(QtWidgets.QStyledItemDelegate):
[6011788]108    """
[00b3b40]109    Custom delegate for appearance and behavior control of the polydispersity view
[6011788]110    """
[e43fc91]111    POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz']
[06b0138]112
[aca8418]113    combo_updated = QtCore.pyqtSignal(str, int)
[e43fc91]114    filename_updated = QtCore.pyqtSignal(int)
[06b0138]115
[8eaa101]116    def __init__(self, parent=None):
117        """
118        Overwrite generic constructor to allow for some globals
119        """
[4992ff2]120        super(QtWidgets.QStyledItemDelegate, self).__init__()
[8eaa101]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
[e43fc91]129        self.poly_filename = 7
[8eaa101]130
131    def editableParameters(self):
[7ffa5ee9]132        return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs]
[8eaa101]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
[e43fc91]153        self.poly_filename = 8
[8eaa101]154
[00b3b40]155    def createEditor(self, widget, option, index):
156        # Remember the current choice
[aca8418]157        if not index.isValid():
158            return 0
[e43fc91]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
[8eaa101]163        elif index.column() in self.editableParameters():
[4992ff2]164            self.editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]165            validator = GuiUtils.DoubleValidator()
[e43fc91]166            self.editor.setValidator(validator)
167            return self.editor
[6011788]168        else:
[4992ff2]169            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[aca8418]170
171    def paint(self, painter, option, index):
172        """
173        Overwrite generic painter for certain columns
174        """
[8eaa101]175        if index.column() in (self.poly_min, self.poly_max):
[aca8418]176            # Units - present in nice HTML
[4992ff2]177            options = QtWidgets.QStyleOptionViewItem(option)
[aca8418]178            self.initStyleOption(options,index)
179
[7969b9c]180            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[b00414d]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 = ""
[4992ff2]191            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[b00414d]192
193            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]194            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[b00414d]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
[4992ff2]204            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[b00414d]205
[4992ff2]206class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate):
[b00414d]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        """
[4992ff2]214        super(QtWidgets.QStyledItemDelegate, self).__init__()
[b00414d]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):
[02f1d12]223        return [self.mag_value, self.mag_min, self.mag_max]
[b00414d]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
[fbfc488]238        current_text = index.data()
[b00414d]239        if not index.isValid():
240            return 0
241        if index.column() in self.editableParameters():
[4992ff2]242            editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]243            validator = GuiUtils.DoubleValidator()
[b00414d]244            editor.setValidator(validator)
245            return editor
246        else:
[4992ff2]247            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[b00414d]248
249    def paint(self, painter, option, index):
250        """
251        Overwrite generic painter for certain columns
252        """
[0d13814]253        if index.column() in (self.mag_min, self.mag_max, self.mag_unit):
[b00414d]254            # Units - present in nice HTML
[4992ff2]255            options = QtWidgets.QStyleOptionViewItem(option)
[b00414d]256            self.initStyleOption(options,index)
257
[7969b9c]258            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[aca8418]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 = ""
[4992ff2]269            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[aca8418]270
271            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]272            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[aca8418]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
[4992ff2]282            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.