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

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

Fixed a crash when the file open dialog would lose reference to the underlying listview cell on cell losing focus SASVIEW-625

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