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