1 | """ |
---|
2 | Allows users to modify the box slicer parameters. |
---|
3 | """ |
---|
4 | import functools |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtCore |
---|
7 | from PyQt4 import QtWebKit |
---|
8 | |
---|
9 | # Local UI |
---|
10 | from sas.qtgui.UI.SlicerParametersUI import Ui_SlicerParametersUI |
---|
11 | |
---|
12 | class SlicerParameters(QtGui.QDialog, Ui_SlicerParametersUI): |
---|
13 | """ |
---|
14 | Interaction between the QTableView and the underlying model, |
---|
15 | passed from a slicer instance. |
---|
16 | """ |
---|
17 | close_signal = QtCore.pyqtSignal() |
---|
18 | def __init__(self, parent=None, model=None): |
---|
19 | super(SlicerParameters, self).__init__() |
---|
20 | |
---|
21 | self.setupUi(self) |
---|
22 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
23 | |
---|
24 | self.model = model |
---|
25 | |
---|
26 | # Define a proxy model so cell enablement can be finegrained. |
---|
27 | self.proxy = ProxyModel(self) |
---|
28 | self.proxy.setSourceModel(self.model) |
---|
29 | |
---|
30 | # Set the proxy model for display in the Table View. |
---|
31 | self.lstParams.setModel(self.proxy) |
---|
32 | |
---|
33 | # Disallow edit of the parameter name column. |
---|
34 | self.lstParams.model().setColumnReadOnly(0, True) |
---|
35 | |
---|
36 | # Specify the validator on the parameter value column. |
---|
37 | self.lstParams.setItemDelegate(ValidatedItemDelegate()) |
---|
38 | |
---|
39 | # Display Help on clicking the button |
---|
40 | self.buttonBox.button(QtGui.QDialogButtonBox.Help).clicked.connect(self.onHelp) |
---|
41 | |
---|
42 | # Close doesn't trigger closeEvent automatically, so force it |
---|
43 | self.buttonBox.button(QtGui.QDialogButtonBox.Close).clicked.connect(functools.partial(self.closeEvent,None)) |
---|
44 | |
---|
45 | # Disable row number display |
---|
46 | self.lstParams.verticalHeader().setVisible(False) |
---|
47 | self.lstParams.setAlternatingRowColors(True) |
---|
48 | self.lstParams.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Expanding) |
---|
49 | |
---|
50 | # Header properties for nicer display |
---|
51 | header = self.lstParams.horizontalHeader() |
---|
52 | header.setResizeMode(QtGui.QHeaderView.Stretch) |
---|
53 | header.setStretchLastSection(True) |
---|
54 | |
---|
55 | |
---|
56 | def setModel(self, model): |
---|
57 | """ Model setter """ |
---|
58 | self.model = model |
---|
59 | self.proxy.setSourceModel(self.model) |
---|
60 | |
---|
61 | def closeEvent(self, event): |
---|
62 | """ |
---|
63 | Overwritten close widget method in order to send the close |
---|
64 | signal to the parent. |
---|
65 | """ |
---|
66 | self.close_signal.emit() |
---|
67 | if event: |
---|
68 | event.accept() |
---|
69 | |
---|
70 | def onHelp(self): |
---|
71 | """ |
---|
72 | Display generic data averaging help |
---|
73 | """ |
---|
74 | location = "docs/sphinx-docs/build/html" + \ |
---|
75 | "/user/sasgui/guiframe/graph_help.html#d-data-averaging" |
---|
76 | self._helpView = QtWebKit.QWebView() |
---|
77 | self._helpView.load(QtCore.QUrl(location)) |
---|
78 | self._helpView.show() |
---|
79 | |
---|
80 | |
---|
81 | class ProxyModel(QtGui.QIdentityProxyModel): |
---|
82 | """ |
---|
83 | Trivial proxy model with custom column edit flag |
---|
84 | """ |
---|
85 | def __init__(self, parent=None): |
---|
86 | super(ProxyModel, self).__init__(parent) |
---|
87 | self._columns = set() |
---|
88 | |
---|
89 | def columnReadOnly(self, column): |
---|
90 | return column in self._columns |
---|
91 | |
---|
92 | def setColumnReadOnly(self, column, readonly=True): |
---|
93 | if readonly: |
---|
94 | self._columns.add(column) |
---|
95 | else: |
---|
96 | self._columns.discard(column) |
---|
97 | |
---|
98 | def flags(self, index): |
---|
99 | flags = super(ProxyModel, self).flags(index) |
---|
100 | if self.columnReadOnly(index.column()): |
---|
101 | flags &= ~QtCore.Qt.ItemIsEditable |
---|
102 | return flags |
---|
103 | |
---|
104 | |
---|
105 | class ValidatedItemDelegate(QtGui.QStyledItemDelegate): |
---|
106 | """ |
---|
107 | Simple delegate enabling adding a validator to a cell editor. |
---|
108 | """ |
---|
109 | def createEditor(self, widget, option, index): |
---|
110 | if not index.isValid(): |
---|
111 | return 0 |
---|
112 | if index.column() == 1: # Edir only cells in the second column |
---|
113 | editor = QtGui.QLineEdit(widget) |
---|
114 | validator = QtGui.QDoubleValidator() |
---|
115 | # Don't use the scientific notation, cause 'e'. |
---|
116 | validator.setNotation(QtGui.QDoubleValidator.StandardNotation) |
---|
117 | editor.setValidator(validator) |
---|
118 | return editor |
---|
119 | return super(ValidatedItemDelegate, self).createEditor(widget, option, index) |
---|