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

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

Minor refactoring + improvements to fitting views

  • Property mode set to 100755
File size: 3.7 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
38            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
39
40            # Prepare document for inserting into cell
41            doc = QtGui.QTextDocument()
42
43            # Convert the unit description into HTML
44            text_html = GuiUtils.convertUnitToHTML(str(options.text))
45            doc.setHtml(text_html)
46
47            # delete the original content
48            options.text = ""
49            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
50
51            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
52            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
53
54            painter.save()
55            painter.translate(textRect.topLeft())
56            painter.setClipRect(textRect.translated(-textRect.topLeft()))
57            # Draw the QTextDocument in the cell
58            doc.documentLayout().draw(painter, context)
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.