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

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

Improvements to view delegates and model updates.

  • Property mode set to 100755
File size: 3.6 KB
Line 
1from PyQt4 import QtGui
2from PyQt4 import QtCore
3
4import sas.qtgui.Utilities.GuiUtils as GuiUtils
5
6# Main parameter table view columns
7PARAM_PROPERTY=0
8PARAM_VALUE=1
9PARAM_MIN=2
10PARAM_MAX=3
11PARAM_UNIT=4
12
13# polydispersity functions
14POLYDISPERSE_FUNCTIONS=['rectangle', 'array', 'lognormal', 'gaussian', 'schulz']
15# polydispersity columns
16POLY_PARAMETER=0
17POLY_PD=1
18POLY_MIN=2
19POLY_MAX=3
20POLY_NPTS=4
21POLY_NSIGS=5
22POLY_FUNCTION=6
23
24
25class ModelViewDelegate(QtGui.QStyledItemDelegate):
26    """
27    Custom delegate for appearance and behavior control of the model view
28    """
29    def paint(self, painter, option, index):
30        """
31        Overwrite generic painter for certain columns
32        """
33        if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX):
34            # Units - present in nice HTML
35            options = QtGui.QStyleOptionViewItemV4(option)
36            self.initStyleOption(options,index)
37            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
38
39            # Prepare document for inserting into cell
40            doc = QtGui.QTextDocument()
41
42            # Convert the unit description into HTML
43            text_html = GuiUtils.convertUnitToHTML(str(options.text))
44            doc.setHtml(text_html)
45
46            # delete the original content
47            options.text = ""
48            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter);
49
50            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
51            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
52
53            painter.save()
54            painter.translate(textRect.topLeft())
55            painter.setClipRect(textRect.translated(-textRect.topLeft()))
56            # Draw the QTextDocument in the cell
57            doc.documentLayout().draw(painter, context)
58
59            painter.restore()
60        else:
61            # Just the default paint
62            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
63
64    def createEditor(self, widget, option, index):
65        """
66        Overwrite generic editor for certain columns
67        """
68        if not index.isValid():
69            return 0
70        if index.column() == PARAM_VALUE: #only in the value column
71            editor = QtGui.QLineEdit(widget)
72            validator = QtGui.QDoubleValidator()
73            editor.setValidator(validator)
74            return editor
75
76        return super(ModelViewDelegate, self).createEditor(widget, option, index)
77
78    def setModelData(self, editor, model, index):
79        """
80        Overwrite generic model update method for certain columns
81        """
82        if index.column() in (PARAM_MIN, PARAM_MAX):
83            try:
84                value_float = float(editor.text())
85            except ValueError:
86                # TODO: present the failure to the user
87                # balloon popup? tooltip? cell background colour flash?
88                return
89        QtGui.QStyledItemDelegate.setModelData(self, editor, model, index)
90
91
92class PolyViewDelegate(QtGui.QStyledItemDelegate):
93    """
94    Custom delegate for appearance and behavior control of the polydispersity view
95    """
96    def createEditor(self, widget, option, index):
97        # Remember the current choice
98        current_text = index.data().toString()
99        if index.column() == POLY_FUNCTION:
100            editor = QtGui.QComboBox(widget)
101            for function in POLYDISPERSE_FUNCTIONS:
102                editor.addItem(function)
103            current_index = editor.findText(current_text)
104            editor.setCurrentIndex(current_index if current_index>-1 else 0)
105            return editor
106        else:
107            QtGui.QStyledItemDelegate.createEditor(self, widget, option, index)
Note: See TracBrowser for help on using the repository browser.