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

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

Fixing unit tests + removal of unnecessary files

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