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

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

Attempt at realigning the tableview display and euqalising font. SASVIEW-889

  • Property mode set to 100644
File size: 10.6 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
131        self.poly_min = 2
132        self.poly_max = 3
133        self.poly_npts = 4
134        self.poly_nsigs = 5
135        self.poly_function = 6
[e43fc91]136        self.poly_filename = 7
[8eaa101]137
138    def editableParameters(self):
[7ffa5ee9]139        return [self.poly_pd, self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs]
[8eaa101]140
141    def columnDict(self):
142        return {self.poly_pd:    'width',
143                self.poly_min:   'min',
144                self.poly_max:   'max',
145                self.poly_npts:  'npts',
146                self.poly_nsigs: 'nsigmas'}
147
148    def addErrorColumn(self):
149        """
150        Modify local column pointers
151        Note: the reverse is never required!
152        """
153        self.poly_parameter = 0
154        self.poly_pd = 1
155        self.poly_min = 3
156        self.poly_max = 4
157        self.poly_npts = 5
158        self.poly_nsigs = 6
159        self.poly_function = 7
[e43fc91]160        self.poly_filename = 8
[8eaa101]161
[00b3b40]162    def createEditor(self, widget, option, index):
163        # Remember the current choice
[aca8418]164        if not index.isValid():
165            return 0
[e43fc91]166        elif index.column() == self.poly_filename:
167            # Notify the widget that we want to change the filename
168            self.filename_updated.emit(index.row())
169            return None
[8eaa101]170        elif index.column() in self.editableParameters():
[4992ff2]171            self.editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]172            validator = GuiUtils.DoubleValidator()
[e43fc91]173            self.editor.setValidator(validator)
174            return self.editor
[6011788]175        else:
[4992ff2]176            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[aca8418]177
178    def paint(self, painter, option, index):
179        """
180        Overwrite generic painter for certain columns
181        """
[8eaa101]182        if index.column() in (self.poly_min, self.poly_max):
[aca8418]183            # Units - present in nice HTML
[4992ff2]184            options = QtWidgets.QStyleOptionViewItem(option)
[aca8418]185            self.initStyleOption(options,index)
186
[7969b9c]187            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[b00414d]188
189            # Prepare document for inserting into cell
190            doc = QtGui.QTextDocument()
[cf8d6c9]191            current_font = painter.font()
192            doc.setDefaultFont(current_font)
[b00414d]193            # Convert the unit description into HTML
194            text_html = GuiUtils.convertUnitToHTML(str(options.text))
195            doc.setHtml(text_html)
196
197            # delete the original content
198            options.text = ""
[4992ff2]199            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[b00414d]200
201            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]202            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[b00414d]203            painter.save()
[cf8d6c9]204
205            rect = textRect.topLeft()
206            y = rect.y()
207            y += 5.0 # magic value for rendering nice display in the table
208            rect.setY(y)
209            painter.translate(rect)
210            painter.setClipRect(textRect.translated(-rect))
[b00414d]211            # Draw the QTextDocument in the cell
212            doc.documentLayout().draw(painter, context)
213            painter.restore()
214        else:
215            # Just the default paint
[4992ff2]216            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
[b00414d]217
[4992ff2]218class MagnetismViewDelegate(QtWidgets.QStyledItemDelegate):
[b00414d]219    """
220    Custom delegate for appearance and behavior control of the magnetism view
221    """
222    def __init__(self, parent=None):
223        """
224        Overwrite generic constructor to allow for some globals
225        """
[4992ff2]226        super(QtWidgets.QStyledItemDelegate, self).__init__()
[b00414d]227
228        self.mag_parameter = 0
229        self.mag_value = 1
230        self.mag_min = 2
231        self.mag_max = 3
232        self.mag_unit = 4
233
234    def editableParameters(self):
[02f1d12]235        return [self.mag_value, self.mag_min, self.mag_max]
[b00414d]236
237    def addErrorColumn(self):
238        """
239        Modify local column pointers
240        Note: the reverse is never required!
241        """
242        self.mag_parameter = 0
243        self.mag_value = 1
244        self.mag_min = 3
245        self.mag_max = 4
246        self.mag_unit = 5
247
248    def createEditor(self, widget, option, index):
249        # Remember the current choice
[fbfc488]250        current_text = index.data()
[b00414d]251        if not index.isValid():
252            return 0
253        if index.column() in self.editableParameters():
[4992ff2]254            editor = QtWidgets.QLineEdit(widget)
[d6b8a1d]255            validator = GuiUtils.DoubleValidator()
[b00414d]256            editor.setValidator(validator)
257            return editor
258        else:
[4992ff2]259            QtWidgets.QStyledItemDelegate.createEditor(self, widget, option, index)
[b00414d]260
261    def paint(self, painter, option, index):
262        """
263        Overwrite generic painter for certain columns
264        """
[0d13814]265        if index.column() in (self.mag_min, self.mag_max, self.mag_unit):
[b00414d]266            # Units - present in nice HTML
[4992ff2]267            options = QtWidgets.QStyleOptionViewItem(option)
[b00414d]268            self.initStyleOption(options,index)
269
[7969b9c]270            style = QtWidgets.QApplication.style() if options.widget is None else options.widget.style()
[aca8418]271
272            # Prepare document for inserting into cell
273            doc = QtGui.QTextDocument()
[cf8d6c9]274            current_font = painter.font()
275            doc.setDefaultFont(current_font)
[aca8418]276            # Convert the unit description into HTML
277            text_html = GuiUtils.convertUnitToHTML(str(options.text))
278            doc.setHtml(text_html)
279
280            # delete the original content
281            options.text = ""
[4992ff2]282            style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter, options.widget);
[aca8418]283
284            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
[4992ff2]285            textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options)
[aca8418]286
287            painter.save()
[cf8d6c9]288            rect = textRect.topLeft()
289            y = rect.y()
290            y += 5.0 # magic value for rendering nice display in the table
291            rect.setY(y)
292            painter.translate(rect)
293            painter.setClipRect(textRect.translated(-rect))
[aca8418]294            # Draw the QTextDocument in the cell
295            doc.documentLayout().draw(painter, context)
296            painter.restore()
297        else:
298            # Just the default paint
[4992ff2]299            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.