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

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

More polydisp. functionality SASVIEW-601

  • Property mode set to 100755
File size: 5.5 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
23POLY_EDITABLE_PARAMS = [POLY_MIN, POLY_MAX, POLY_NPTS, POLY_NSIGS]
24
25
26class ModelViewDelegate(QtGui.QStyledItemDelegate):
27    """
28    Custom delegate for appearance and behavior control of the model view
29    """
30    def paint(self, painter, option, index):
31        """
32        Overwrite generic painter for certain columns
33        """
34        if index.column() in (PARAM_UNIT, PARAM_MIN, PARAM_MAX):
35            # Units - present in nice HTML
36            options = QtGui.QStyleOptionViewItemV4(option)
37            self.initStyleOption(options,index)
38
39            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
40
41            # Prepare document for inserting into cell
42            doc = QtGui.QTextDocument()
43
44            # Convert the unit description into HTML
45            text_html = GuiUtils.convertUnitToHTML(str(options.text))
46            doc.setHtml(text_html)
47
48            # delete the original content
49            options.text = ""
50            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
51
52            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
53            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
54
55            painter.save()
56            painter.translate(textRect.topLeft())
57            painter.setClipRect(textRect.translated(-textRect.topLeft()))
58            # Draw the QTextDocument in the cell
59            doc.documentLayout().draw(painter, context)
60            painter.restore()
61        else:
62            # Just the default paint
63            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
64
65    def createEditor(self, widget, option, index):
66        """
67        Overwrite generic editor for certain columns
68        """
69        if not index.isValid():
70            return 0
71        if index.column() == PARAM_VALUE: #only in the value column
72            editor = QtGui.QLineEdit(widget)
73            validator = QtGui.QDoubleValidator()
74            editor.setValidator(validator)
75            return editor
76
77        return super(ModelViewDelegate, self).createEditor(widget, option, index)
78
79    def setModelData(self, editor, model, index):
80        """
81        Overwrite generic model update method for certain columns
82        """
83        if index.column() in (PARAM_MIN, PARAM_MAX):
84            try:
85                value_float = float(editor.text())
86            except ValueError:
87                # TODO: present the failure to the user
88                # balloon popup? tooltip? cell background colour flash?
89                return
90        QtGui.QStyledItemDelegate.setModelData(self, editor, model, index)
91
92
93class PolyViewDelegate(QtGui.QStyledItemDelegate):
94    """
95    Custom delegate for appearance and behavior control of the polydispersity view
96    """
97    combo_updated = QtCore.pyqtSignal(str, int)
98    def createEditor(self, widget, option, index):
99        # Remember the current choice
100        current_text = index.data().toString()
101        if not index.isValid():
102            return 0
103        if index.column() == POLY_FUNCTION:
104            editor = QtGui.QComboBox(widget)
105            for function in POLYDISPERSE_FUNCTIONS:
106                editor.addItem(function)
107            current_index = editor.findText(current_text)
108            editor.setCurrentIndex(current_index if current_index>-1 else 3)
109            editor.currentIndexChanged.connect(lambda: self.combo_updated.emit(str(editor.currentText()), index.row()))
110            return editor
111        elif index.column() in POLY_EDITABLE_PARAMS:
112            editor = QtGui.QLineEdit(widget)
113            validator = QtGui.QDoubleValidator()
114            editor.setValidator(validator)
115            return editor
116        else:
117            QtGui.QStyledItemDelegate.createEditor(self, widget, option, index)
118
119    def paint(self, painter, option, index):
120        """
121        Overwrite generic painter for certain columns
122        """
123        if index.column() in (POLY_MIN, POLY_MAX):
124            # Units - present in nice HTML
125            options = QtGui.QStyleOptionViewItemV4(option)
126            self.initStyleOption(options,index)
127
128            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
129
130            # Prepare document for inserting into cell
131            doc = QtGui.QTextDocument()
132
133            # Convert the unit description into HTML
134            text_html = GuiUtils.convertUnitToHTML(str(options.text))
135            doc.setHtml(text_html)
136
137            # delete the original content
138            options.text = ""
139            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
140
141            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
142            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
143
144            painter.save()
145            painter.translate(textRect.topLeft())
146            painter.setClipRect(textRect.translated(-textRect.topLeft()))
147            # Draw the QTextDocument in the cell
148            doc.documentLayout().draw(painter, context)
149            painter.restore()
150        else:
151            # Just the default paint
152            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.