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

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

misc fixes to polydispersity table: updated values enter main model correctly, including upon fit

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