[3bdbfcc] | 1 | """ |
---|
| 2 | Allows users to modify the box slicer parameters. |
---|
| 3 | """ |
---|
| 4 | from PyQt4 import QtGui |
---|
| 5 | from PyQt4 import QtCore |
---|
| 6 | |
---|
| 7 | # Local UI |
---|
| 8 | from sas.qtgui.UI.SlicerParametersUI import Ui_SlicerParametersUI |
---|
| 9 | |
---|
| 10 | class SlicerParameters(QtGui.QDialog, Ui_SlicerParametersUI): |
---|
| 11 | """ |
---|
| 12 | Interaction between the QTableView and the underlying model, |
---|
| 13 | passed from a slicer instance. |
---|
| 14 | """ |
---|
| 15 | def __init__(self, parent=None, model=None): |
---|
| 16 | super(SlicerParameters, self).__init__() |
---|
| 17 | |
---|
| 18 | self.setupUi(self) |
---|
| 19 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
| 20 | |
---|
| 21 | self.model = model |
---|
| 22 | |
---|
| 23 | # Define a proxy model so cell enablement can be finegrained. |
---|
| 24 | self.proxy = ProxyModel(self) |
---|
| 25 | self.proxy.setSourceModel(self.model) |
---|
| 26 | |
---|
| 27 | # Set the proxy model for display in the Table View. |
---|
| 28 | self.lstParams.setModel(self.proxy) |
---|
| 29 | |
---|
| 30 | # Disallow edit of the parameter name column. |
---|
| 31 | self.lstParams.model().setColumnReadOnly(0, True) |
---|
| 32 | |
---|
| 33 | # Specify the validator on the parameter value column. |
---|
| 34 | self.lstParams.setItemDelegate(ValidatedItemDelegate()) |
---|
| 35 | |
---|
| 36 | class ProxyModel(QtGui.QIdentityProxyModel): |
---|
| 37 | """ |
---|
| 38 | Trivial proxy model with custom column edit flag |
---|
| 39 | """ |
---|
| 40 | def __init__(self, parent=None): |
---|
| 41 | super(ProxyModel, self).__init__(parent) |
---|
| 42 | self._columns = set() |
---|
| 43 | |
---|
| 44 | def columnReadOnly(self, column): |
---|
| 45 | return column in self._columns |
---|
| 46 | |
---|
| 47 | def setColumnReadOnly(self, column, readonly=True): |
---|
| 48 | if readonly: |
---|
| 49 | self._columns.add(column) |
---|
| 50 | else: |
---|
| 51 | self._columns.discard(column) |
---|
| 52 | |
---|
| 53 | def flags(self, index): |
---|
| 54 | flags = super(ProxyModel, self).flags(index) |
---|
| 55 | if self.columnReadOnly(index.column()): |
---|
| 56 | flags &= ~QtCore.Qt.ItemIsEditable |
---|
| 57 | return flags |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | class ValidatedItemDelegate(QtGui.QStyledItemDelegate): |
---|
| 61 | """ |
---|
| 62 | Simple delegate enabling adding a validator to a cell editor. |
---|
| 63 | """ |
---|
| 64 | def createEditor(self, widget, option, index): |
---|
| 65 | if not index.isValid(): |
---|
| 66 | return 0 |
---|
| 67 | if index.column() == 1: # Edir only cells in the second column |
---|
| 68 | editor = QtGui.QLineEdit(widget) |
---|
| 69 | validator = QtGui.QDoubleValidator() |
---|
| 70 | editor.setValidator(validator) |
---|
| 71 | return editor |
---|
| 72 | return super(ValidatedItemDelegate, self).createEditor(widget, option, index) |
---|