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

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

SASVIEW-272: initial implementation of magnetic model/view

  • Property mode set to 100755
File size: 9.1 KB
Line 
1from PyQt4 import QtGui
2from PyQt4 import QtCore
3
4import sas.qtgui.Utilities.GuiUtils as GuiUtils
5
6class ModelViewDelegate(QtGui.QStyledItemDelegate):
7    """
8    Custom delegate for appearance and behavior control of the model view
9    """
10    # Main parameter table view columns
11    PARAM_PROPERTY=0
12    PARAM_VALUE=1
13    PARAM_MIN=2
14    PARAM_MAX=3
15    PARAM_UNIT=4
16
17    def paint(self, painter, option, index):
18        """
19        Overwrite generic painter for certain columns
20        """
21        if index.column() in (self.PARAM_UNIT, self.PARAM_MIN, self.PARAM_MAX):
22            # Units - present in nice HTML
23            options = QtGui.QStyleOptionViewItemV4(option)
24            self.initStyleOption(options,index)
25
26            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
27
28            # Prepare document for inserting into cell
29            doc = QtGui.QTextDocument()
30
31            # Convert the unit description into HTML
32            text_html = GuiUtils.convertUnitToHTML(str(options.text))
33            doc.setHtml(text_html)
34
35            # delete the original content
36            options.text = ""
37            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
38
39            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
40            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
41
42            painter.save()
43            painter.translate(textRect.topLeft())
44            painter.setClipRect(textRect.translated(-textRect.topLeft()))
45            # Draw the QTextDocument in the cell
46            doc.documentLayout().draw(painter, context)
47            painter.restore()
48        else:
49            # Just the default paint
50            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
51
52    def createEditor(self, widget, option, index):
53        """
54        Overwrite generic editor for certain columns
55        """
56        if not index.isValid():
57            return 0
58        if index.column() == self.PARAM_VALUE: #only in the value column
59            editor = QtGui.QLineEdit(widget)
60            validator = QtGui.QDoubleValidator()
61            editor.setValidator(validator)
62            return editor
63
64        return super(ModelViewDelegate, self).createEditor(widget, option, index)
65
66    def setModelData(self, editor, model, index):
67        """
68        Overwrite generic model update method for certain columns
69        """
70        if index.column() in (self.PARAM_MIN, self.PARAM_MAX):
71            try:
72                value_float = float(editor.text())
73            except ValueError:
74                # TODO: present the failure to the user
75                # balloon popup? tooltip? cell background colour flash?
76                return
77        QtGui.QStyledItemDelegate.setModelData(self, editor, model, index)
78
79
80class PolyViewDelegate(QtGui.QStyledItemDelegate):
81    """
82    Custom delegate for appearance and behavior control of the polydispersity view
83    """
84    POLYDISPERSE_FUNCTIONS = ['rectangle', 'array', 'lognormal', 'gaussian', 'schulz']
85
86    combo_updated = QtCore.pyqtSignal(str, int)
87
88    def __init__(self, parent=None):
89        """
90        Overwrite generic constructor to allow for some globals
91        """
92        super(QtGui.QStyledItemDelegate, self).__init__()
93
94        self.poly_parameter = 0
95        self.poly_pd = 1
96        self.poly_min = 2
97        self.poly_max = 3
98        self.poly_npts = 4
99        self.poly_nsigs = 5
100        self.poly_function = 6
101
102    def editableParameters(self):
103        return [self.poly_min, self.poly_max, self.poly_npts, self.poly_nsigs]
104
105    def columnDict(self):
106        return {self.poly_pd:    'width',
107                self.poly_min:   'min',
108                self.poly_max:   'max',
109                self.poly_npts:  'npts',
110                self.poly_nsigs: 'nsigmas'}
111
112    def addErrorColumn(self):
113        """
114        Modify local column pointers
115        Note: the reverse is never required!
116        """
117        self.poly_parameter = 0
118        self.poly_pd = 1
119        self.poly_min = 3
120        self.poly_max = 4
121        self.poly_npts = 5
122        self.poly_nsigs = 6
123        self.poly_function = 7
124
125    def createEditor(self, widget, option, index):
126        # Remember the current choice
127        current_text = index.data().toString()
128        if not index.isValid():
129            return 0
130        if index.column() == self.poly_function:
131            editor = QtGui.QComboBox(widget)
132            for function in self.POLYDISPERSE_FUNCTIONS:
133                editor.addItem(function)
134            current_index = editor.findText(current_text)
135            editor.setCurrentIndex(current_index if current_index>-1 else 3)
136            editor.currentIndexChanged.connect(lambda: self.combo_updated.emit(str(editor.currentText()), index.row()))
137            return editor
138        elif index.column() in self.editableParameters():
139            editor = QtGui.QLineEdit(widget)
140            validator = QtGui.QDoubleValidator()
141            editor.setValidator(validator)
142            return editor
143        else:
144            QtGui.QStyledItemDelegate.createEditor(self, widget, option, index)
145
146    def paint(self, painter, option, index):
147        """
148        Overwrite generic painter for certain columns
149        """
150        if index.column() in (self.poly_min, self.poly_max):
151            # Units - present in nice HTML
152            options = QtGui.QStyleOptionViewItemV4(option)
153            self.initStyleOption(options,index)
154
155            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
156
157            # Prepare document for inserting into cell
158            doc = QtGui.QTextDocument()
159
160            # Convert the unit description into HTML
161            text_html = GuiUtils.convertUnitToHTML(str(options.text))
162            doc.setHtml(text_html)
163
164            # delete the original content
165            options.text = ""
166            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
167
168            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
169            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
170
171            painter.save()
172            painter.translate(textRect.topLeft())
173            painter.setClipRect(textRect.translated(-textRect.topLeft()))
174            # Draw the QTextDocument in the cell
175            doc.documentLayout().draw(painter, context)
176            painter.restore()
177        else:
178            # Just the default paint
179            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
180
181class MagnetismViewDelegate(QtGui.QStyledItemDelegate):
182    """
183    Custom delegate for appearance and behavior control of the magnetism view
184    """
185    def __init__(self, parent=None):
186        """
187        Overwrite generic constructor to allow for some globals
188        """
189        super(QtGui.QStyledItemDelegate, self).__init__()
190
191        self.mag_parameter = 0
192        self.mag_value = 1
193        self.mag_min = 2
194        self.mag_max = 3
195        self.mag_unit = 4
196
197    def editableParameters(self):
198        return [self.mag_min, self.mag_max]
199
200    def addErrorColumn(self):
201        """
202        Modify local column pointers
203        Note: the reverse is never required!
204        """
205        self.mag_parameter = 0
206        self.mag_value = 1
207        self.mag_min = 3
208        self.mag_max = 4
209        self.mag_unit = 5
210
211    def createEditor(self, widget, option, index):
212        # Remember the current choice
213        current_text = index.data().toString()
214        if not index.isValid():
215            return 0
216        if index.column() in self.editableParameters():
217            editor = QtGui.QLineEdit(widget)
218            validator = QtGui.QDoubleValidator()
219            editor.setValidator(validator)
220            return editor
221        else:
222            QtGui.QStyledItemDelegate.createEditor(self, widget, option, index)
223
224    def paint(self, painter, option, index):
225        """
226        Overwrite generic painter for certain columns
227        """
228        if index.column() in (self.mag_min, self.mag_max):
229            # Units - present in nice HTML
230            options = QtGui.QStyleOptionViewItemV4(option)
231            self.initStyleOption(options,index)
232
233            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
234
235            # Prepare document for inserting into cell
236            doc = QtGui.QTextDocument()
237
238            # Convert the unit description into HTML
239            text_html = GuiUtils.convertUnitToHTML(str(options.text))
240            doc.setHtml(text_html)
241
242            # delete the original content
243            options.text = ""
244            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter, options.widget);
245
246            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
247            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
248
249            painter.save()
250            painter.translate(textRect.topLeft())
251            painter.setClipRect(textRect.translated(-textRect.topLeft()))
252            # Draw the QTextDocument in the cell
253            doc.documentLayout().draw(painter, context)
254            painter.restore()
255        else:
256            # Just the default paint
257            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
Note: See TracBrowser for help on using the repository browser.