[3bdbfcc] | 1 | """ |
---|
| 2 | Allows users to modify the box slicer parameters. |
---|
| 3 | """ |
---|
[161713c] | 4 | import numpy |
---|
[57b7ee2] | 5 | import functools |
---|
[4992ff2] | 6 | from PyQt5 import QtCore |
---|
| 7 | from PyQt5 import QtGui |
---|
| 8 | from PyQt5 import QtWidgets |
---|
[3bdbfcc] | 9 | |
---|
[d6b8a1d] | 10 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 11 | |
---|
[3bdbfcc] | 12 | # Local UI |
---|
[cd2cc745] | 13 | from sas.qtgui.UI import main_resources_rc |
---|
[83eb5208] | 14 | from sas.qtgui.Plotting.UI.SlicerParametersUI import Ui_SlicerParametersUI |
---|
[3bdbfcc] | 15 | |
---|
[4992ff2] | 16 | class SlicerParameters(QtWidgets.QDialog, Ui_SlicerParametersUI): |
---|
[3bdbfcc] | 17 | """ |
---|
| 18 | Interaction between the QTableView and the underlying model, |
---|
| 19 | passed from a slicer instance. |
---|
| 20 | """ |
---|
[57b7ee2] | 21 | close_signal = QtCore.pyqtSignal() |
---|
[161713c] | 22 | def __init__(self, model=None, validate_method=None): |
---|
[3bdbfcc] | 23 | super(SlicerParameters, self).__init__() |
---|
| 24 | |
---|
| 25 | self.setupUi(self) |
---|
[161713c] | 26 | |
---|
[3bdbfcc] | 27 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
| 28 | |
---|
| 29 | self.model = model |
---|
[161713c] | 30 | self.validate_method = validate_method |
---|
[3bdbfcc] | 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 | |
---|
[57b7ee2] | 42 | # Specify the validator on the parameter value column. |
---|
[161713c] | 43 | self.delegate = EditDelegate(self, validate_method=self.validate_method) |
---|
| 44 | self.lstParams.setItemDelegate(self.delegate) |
---|
| 45 | self.delegate.refocus_signal.connect(self.onFocus) |
---|
[57b7ee2] | 46 | |
---|
| 47 | # Display Help on clicking the button |
---|
[4992ff2] | 48 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect(self.onHelp) |
---|
[57b7ee2] | 49 | |
---|
| 50 | # Close doesn't trigger closeEvent automatically, so force it |
---|
[4992ff2] | 51 | self.buttonBox.button(QtWidgets.QDialogButtonBox.Close).clicked.connect(functools.partial(self.closeEvent, None)) |
---|
[57b7ee2] | 52 | |
---|
[b789967] | 53 | # Disable row number display |
---|
| 54 | self.lstParams.verticalHeader().setVisible(False) |
---|
[57b7ee2] | 55 | self.lstParams.setAlternatingRowColors(True) |
---|
[4992ff2] | 56 | self.lstParams.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding) |
---|
[57b7ee2] | 57 | |
---|
| 58 | # Header properties for nicer display |
---|
| 59 | header = self.lstParams.horizontalHeader() |
---|
[4992ff2] | 60 | header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch) |
---|
[57b7ee2] | 61 | header.setStretchLastSection(True) |
---|
| 62 | |
---|
[161713c] | 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)) |
---|
[57b7ee2] | 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 | """ |
---|
[aed0532] | 88 | location = "/user/qtgui/MainWindow/graph_help.html#d-data-averaging" |
---|
[e90988c] | 89 | self.parent.showHelp(location) |
---|
[b789967] | 90 | |
---|
[3bdbfcc] | 91 | |
---|
[4992ff2] | 92 | class ProxyModel(QtCore.QIdentityProxyModel): |
---|
[3bdbfcc] | 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): |
---|
[9a05a8d5] | 101 | '''Returns True if column is read only, false otherwise''' |
---|
[3bdbfcc] | 102 | return column in self._columns |
---|
| 103 | |
---|
| 104 | def setColumnReadOnly(self, column, readonly=True): |
---|
[9a05a8d5] | 105 | '''Add/removes a column from the readonly list''' |
---|
[3bdbfcc] | 106 | if readonly: |
---|
| 107 | self._columns.add(column) |
---|
| 108 | else: |
---|
| 109 | self._columns.discard(column) |
---|
| 110 | |
---|
| 111 | def flags(self, index): |
---|
[9a05a8d5] | 112 | '''Sets column flags''' |
---|
[3bdbfcc] | 113 | flags = super(ProxyModel, self).flags(index) |
---|
| 114 | if self.columnReadOnly(index.column()): |
---|
| 115 | flags &= ~QtCore.Qt.ItemIsEditable |
---|
| 116 | return flags |
---|
| 117 | |
---|
[4992ff2] | 118 | class PositiveDoubleEditor(QtWidgets.QLineEdit): |
---|
[161713c] | 119 | # a signal to tell the delegate when we have finished editing |
---|
| 120 | editingFinished = QtCore.Signal() |
---|
[3bdbfcc] | 121 | |
---|
[161713c] | 122 | def __init__(self, parent=None): |
---|
| 123 | # Initialize the editor object |
---|
| 124 | super(PositiveDoubleEditor, self).__init__(parent) |
---|
| 125 | self.setAutoFillBackground(True) |
---|
[d6b8a1d] | 126 | validator = GuiUtils.DoubleValidator() |
---|
[b789967] | 127 | # Don't use the scientific notation, cause 'e'. |
---|
[d6b8a1d] | 128 | validator.setNotation(GuiUtils.DoubleValidator.StandardNotation) |
---|
[161713c] | 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 | |
---|
[4992ff2] | 137 | class EditDelegate(QtWidgets.QStyledItemDelegate): |
---|
[161713c] | 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: |
---|
[4992ff2] | 156 | return QtWidgets.QStyledItemDelegate.createEditor(self, parent, option, index) |
---|
[161713c] | 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) |
---|