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 | # Disable row number display |
---|
34 | self.lstParams.verticalHeader().setVisible(False) |
---|
35 | |
---|
36 | # Specify the validator on the parameter value column. |
---|
37 | self.lstParams.setItemDelegate(ValidatedItemDelegate()) |
---|
38 | |
---|
39 | class ProxyModel(QtGui.QIdentityProxyModel): |
---|
40 | """ |
---|
41 | Trivial proxy model with custom column edit flag |
---|
42 | """ |
---|
43 | def __init__(self, parent=None): |
---|
44 | super(ProxyModel, self).__init__(parent) |
---|
45 | self._columns = set() |
---|
46 | |
---|
47 | def columnReadOnly(self, column): |
---|
48 | return column in self._columns |
---|
49 | |
---|
50 | def setColumnReadOnly(self, column, readonly=True): |
---|
51 | if readonly: |
---|
52 | self._columns.add(column) |
---|
53 | else: |
---|
54 | self._columns.discard(column) |
---|
55 | |
---|
56 | def flags(self, index): |
---|
57 | flags = super(ProxyModel, self).flags(index) |
---|
58 | if self.columnReadOnly(index.column()): |
---|
59 | flags &= ~QtCore.Qt.ItemIsEditable |
---|
60 | return flags |
---|
61 | |
---|
62 | |
---|
63 | class ValidatedItemDelegate(QtGui.QStyledItemDelegate): |
---|
64 | """ |
---|
65 | Simple delegate enabling adding a validator to a cell editor. |
---|
66 | """ |
---|
67 | def createEditor(self, widget, option, index): |
---|
68 | if not index.isValid(): |
---|
69 | return 0 |
---|
70 | if index.column() == 1: # Edir only cells in the second column |
---|
71 | editor = QtGui.QLineEdit(widget) |
---|
72 | validator = QtGui.QDoubleValidator() |
---|
73 | # Don't use the scientific notation, cause 'e'. |
---|
74 | validator.setNotation(QtGui.QDoubleValidator.StandardNotation) |
---|
75 | editor.setValidator(validator) |
---|
76 | return editor |
---|
77 | return super(ValidatedItemDelegate, self).createEditor(widget, option, index) |
---|