source: sasview/src/sas/qtgui/Plotting/SlicerParameters.py @ 9c0ce68

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

Show help pages in default browser. Fixed some help links and modified unit tests. SASVIEW-800

  • Property mode set to 100644
File size: 6.1 KB
Line 
1"""
2Allows users to modify the box slicer parameters.
3"""
4import numpy
5import functools
6from PyQt5 import QtCore
7from PyQt5 import QtGui
8from PyQt5 import QtWidgets
9
10import sas.qtgui.Utilities.GuiUtils as GuiUtils
11
12# Local UI
13from sas.qtgui.UI import main_resources_rc
14from sas.qtgui.Plotting.UI.SlicerParametersUI import Ui_SlicerParametersUI
15
16class SlicerParameters(QtWidgets.QDialog, Ui_SlicerParametersUI):
17    """
18    Interaction between the QTableView and the underlying model,
19    passed from a slicer instance.
20    """
21    close_signal = QtCore.pyqtSignal()
22    def __init__(self, model=None, validate_method=None):
23        super(SlicerParameters, self).__init__()
24
25        self.setupUi(self)
26
27        assert isinstance(model, QtGui.QStandardItemModel)
28
29        self.model = model
30        self.validate_method = validate_method
31
32        # Define a proxy model so cell enablement can be finegrained.
33        self.proxy = ProxyModel(self)
34        self.proxy.setSourceModel(self.model)
35
36        # Set the proxy model for display in the Table View.
37        self.lstParams.setModel(self.proxy)
38
39        # Disallow edit of the parameter name column.
40        self.lstParams.model().setColumnReadOnly(0, True)
41
42        # Specify the validator on the parameter value column.
43        self.delegate = EditDelegate(self, validate_method=self.validate_method)
44        self.lstParams.setItemDelegate(self.delegate)
45        self.delegate.refocus_signal.connect(self.onFocus)
46
47        # Display Help on clicking the button
48        self.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect(self.onHelp)
49
50        # Close doesn't trigger closeEvent automatically, so force it
51        self.buttonBox.button(QtWidgets.QDialogButtonBox.Close).clicked.connect(functools.partial(self.closeEvent, None))
52
53        # Disable row number display
54        self.lstParams.verticalHeader().setVisible(False)
55        self.lstParams.setAlternatingRowColors(True)
56        self.lstParams.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding)
57
58        # Header properties for nicer display
59        header = self.lstParams.horizontalHeader()
60        header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
61        header.setStretchLastSection(True)
62
63    def onFocus(self, row, column):
64        """ Set the focus on the cell (row, column) """
65        selection_model = self.lstParams.selectionModel()
66        selection_model.select(self.model.index(row, column), QtGui.QItemSelectionModel.Select)
67        self.lstParams.setSelectionModel(selection_model)
68        self.lstParams.setCurrentIndex(self.model.index(row, column))
69
70    def setModel(self, model):
71        """ Model setter """
72        self.model = model
73        self.proxy.setSourceModel(self.model)
74
75    def closeEvent(self, event):
76        """
77        Overwritten close widget method in order to send the close
78        signal to the parent.
79        """
80        self.close_signal.emit()
81        if event:
82            event.accept()
83
84    def onHelp(self):
85        """
86        Display generic data averaging help
87        """
88        location = "/user/sasgui/guiframe/graph_help.html#d-data-averaging"
89        self.parent.showHelp(location)
90
91
92class ProxyModel(QtCore.QIdentityProxyModel):
93    """
94    Trivial proxy model with custom column edit flag
95    """
96    def __init__(self, parent=None):
97        super(ProxyModel, self).__init__(parent)
98        self._columns = set()
99
100    def columnReadOnly(self, column):
101        '''Returns True if column is read only, false otherwise'''
102        return column in self._columns
103
104    def setColumnReadOnly(self, column, readonly=True):
105        '''Add/removes a column from the readonly list'''
106        if readonly:
107            self._columns.add(column)
108        else:
109            self._columns.discard(column)
110
111    def flags(self, index):
112        '''Sets column flags'''
113        flags = super(ProxyModel, self).flags(index)
114        if self.columnReadOnly(index.column()):
115            flags &= ~QtCore.Qt.ItemIsEditable
116        return flags
117
118class PositiveDoubleEditor(QtWidgets.QLineEdit):
119    # a signal to tell the delegate when we have finished editing
120    editingFinished = QtCore.Signal()
121
122    def __init__(self, parent=None):
123            # Initialize the editor object
124            super(PositiveDoubleEditor, self).__init__(parent)
125            self.setAutoFillBackground(True)
126            validator = GuiUtils.DoubleValidator()
127            # Don't use the scientific notation, cause 'e'.
128            validator.setNotation(GuiUtils.DoubleValidator.StandardNotation)
129
130            self.setValidator(validator)
131
132    def focusOutEvent(self, event):
133            # Once focus is lost, tell the delegate we're done editing
134            self.editingFinished.emit()
135
136
137class EditDelegate(QtWidgets.QStyledItemDelegate):
138    refocus_signal = QtCore.pyqtSignal(int, int)
139    def __init__(self, parent=None, validate_method=None):
140        super(EditDelegate, self).__init__(parent)
141        self.editor = None
142        self.index = None
143        self.validate_method = validate_method
144
145    def createEditor(self, parent, option, index):
146        # Creates and returns the custom editor object we will use to edit the cell
147        if not index.isValid():
148            return 0
149
150        result = index.column()
151        if result==1:
152                self.editor = PositiveDoubleEditor(parent)
153                self.index = index
154                return self.editor
155        else:
156                return QtWidgets.QStyledItemDelegate.createEditor(self, parent, option, index)
157
158    def setModelData(self, editor, model, index):
159        """
160        Custom version of the model update, rejecting bad values
161        """
162        self.index = index
163
164        # Find out the changed parameter name and proposed value
165        new_value = self.editor.text().toFloat()[0]
166        param_name = str(model.sourceModel().item(index.row(),0).text())
167
168        validated = True
169        if self.validate_method:
170            # Validate the proposed value in the slicer
171            value_accepted = self.validate_method(param_name, new_value)
172
173        if value_accepted:
174            # Update the model only if value accepted
175            return super(EditDelegate, self).setModelData(editor, model, index)         
Note: See TracBrowser for help on using the repository browser.