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