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

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

More Qt5 related fixes.

  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[0849aec]1from PyQt5 import QtCore
2from PyQt5 import QtGui
3from PyQt5 import QtWidgets
[ad6b4e2]4
5import sas.qtgui.Utilities.GuiUtils as GuiUtils
[6011788]6
[6280464]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
[0849aec]13class ModelViewDelegate(QtWidgets.QStyledItemDelegate):
[6011788]14    """
15    Custom delegate for appearance and behavior control of the model view
16    """
[8f2548c]17    def __init__(self, parent=None):
18        """
19        Overwrite generic constructor to allow for some globals
20        """
[0849aec]21        super(QtWidgets.QStyledItemDelegate, self).__init__()
[8f2548c]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
[16afe01]28        self.param_max=3
29        self.param_unit=4
[8f2548c]30
[16afe01]31    def fancyColumns(self):
32        return [self.param_value, self.param_min, self.param_max, self.param_unit]
[8f2548c]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
[06b0138]45
[6011788]46    def paint(self, painter, option, index):
47        """
48        Overwrite generic painter for certain columns
49        """
[16afe01]50        if index.column() in self.fancyColumns():
[6011788]51            # Units - present in nice HTML
[0849aec]52            options = QtWidgets.QStyleOptionViewItem(option)
[6011788]53            self.initStyleOption(options,index)
[2a432e7]54
[304d082]55            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[6011788]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 = ""
[0849aec]66            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[6011788]67
68            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[0849aec]69            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[6011788]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
[0849aec]79            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[6011788]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
[8f2548c]87        if index.column() == self.param_value: #only in the value column
[0849aec]88            editor = QtWidgets.QLineEdit(widget)
[6011788]89            validator = QtGui.QDoubleValidator()
90            editor.setValidator(validator)
91            return editor
[8f2548c]92        if index.column() in [self.param_property, self.param_error]:
[0d13814]93            # Set some columns uneditable
94            return None
[6011788]95
96        return super(ModelViewDelegate, self).createEditor(widget, option, index)
97
[00b3b40]98    def setModelData(self, editor, model, index):
99        """
100        Overwrite generic model update method for certain columns
101        """
[8f2548c]102        if index.column() in (self.param_min, self.param_max):
[6011788]103            try:
[00b3b40]104                value_float = float(editor.text())
[6011788]105            except ValueError:
[00b3b40]106                # TODO: present the failure to the user
107                # balloon popup? tooltip? cell background colour flash?
[6011788]108                return
[0849aec]109        QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index)
[6011788]110
111
[0849aec]112class PolyViewDelegate(QtWidgets.QStyledItemDelegate):
[6011788]113    """
[00b3b40]114    Custom delegate for appearance and behavior control of the polydispersity view
[6011788]115    """
[e43fc91]116    POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz']
[06b0138]117
[aca8418]118    combo_updated = QtCore.pyqtSignal(str, int)
[e43fc91]119    filename_updated = QtCore.pyqtSignal(int)
[06b0138]120
[8eaa101]121    def __init__(self, parent=None):
122        """
123        Overwrite generic constructor to allow for some globals
124        """
[0849aec]125        super(QtWidgets.QStyledItemDelegate, self).__init__()
[8eaa101]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
[e43fc91]134        self.poly_filename = 7
[8eaa101]135
136    def editableParameters(self):
[7ffa5ee9]137        return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs]
[8eaa101]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
[e43fc91]158        self.poly_filename = 8
[8eaa101]159
[00b3b40]160    def createEditor(self, widget, option, index):
161        # Remember the current choice
[aca8418]162        if not index.isValid():
163            return 0
[e43fc91]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
[8eaa101]168        elif index.column() in self.editableParameters():
[0849aec]169            self.editor = QtWidgets.QLineEdit(widget)
[aca8418]170            validator = QtGui.QDoubleValidator()
[e43fc91]171            self.editor.setValidator(validator)
172            return self.editor
[6011788]173        else:
[0849aec]174            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[aca8418]175
176    def paint(self, painter, option, index):
177        """
178        Overwrite generic painter for certain columns
179        """
[8eaa101]180        if index.column() in (self.poly_min, self.poly_max):
[aca8418]181            # Units - present in nice HTML
[0849aec]182            options = QtWidgets.QStyleOptionViewItem(option)
[aca8418]183            self.initStyleOption(options,index)
184
[304d082]185            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[b00414d]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 = ""
[0849aec]196            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[b00414d]197
198            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[0849aec]199            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[b00414d]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
[0849aec]209            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[b00414d]210
[0849aec]211class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate):
[b00414d]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        """
[0849aec]219        super(QtWidgets.QStyledItemDelegate, self).__init__()
[b00414d]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_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
[6280464]243        current_text = index.data()
[b00414d]244        if not index.isValid():
245            return 0
246        if index.column() in self.editableParameters():
[0849aec]247            editor = QtWidgets.QLineEdit(widget)
[b00414d]248            validator = QtGui.QDoubleValidator()
249            editor.setValidator(validator)
250            return editor
251        else:
[0849aec]252            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[b00414d]253
254    def paint(self, painter, option, index):
255        """
256        Overwrite generic painter for certain columns
257        """
[0d13814]258        if index.column() in (self.mag_min, self.mag_max, self.mag_unit):
[b00414d]259            # Units - present in nice HTML
[0849aec]260            options = QtWidgets.QStyleOptionViewItem(option)
[b00414d]261            self.initStyleOption(options,index)
262
[304d082]263            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[aca8418]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 = ""
[0849aec]274            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[aca8418]275
276            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[0849aec]277            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[aca8418]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
[0849aec]287            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.