1 | """ |
---|
2 | Widget/logic for smearing data. |
---|
3 | """ |
---|
4 | import numpy as np |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtCore |
---|
7 | |
---|
8 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
9 | |
---|
10 | # Local UI |
---|
11 | from sas.qtgui.Perspectives.Fitting.UI.OptionsWidgetUI import Ui_tabOptions |
---|
12 | |
---|
13 | QMIN_DEFAULT = 0.0005 |
---|
14 | QMAX_DEFAULT = 0.5 |
---|
15 | NPTS_DEFAULT = 50 |
---|
16 | |
---|
17 | MODEL = [ |
---|
18 | 'MIN_RANGE', |
---|
19 | 'MAX_RANGE', |
---|
20 | 'NPTS', |
---|
21 | 'LOG_SPACED'] |
---|
22 | |
---|
23 | class DataWidgetMapper(QtGui.QDataWidgetMapper): |
---|
24 | """ |
---|
25 | Custom version of the standard QDataWidgetMapper allowing for proper |
---|
26 | response to index change in comboboxes |
---|
27 | """ |
---|
28 | def addMapping(self, widget, section, propertyName=None): |
---|
29 | if propertyName is None: |
---|
30 | super(DataWidgetMapper, self).addMapping(widget, section) |
---|
31 | else: |
---|
32 | super(DataWidgetMapper, self).addMapping(widget, section, propertyName) |
---|
33 | |
---|
34 | if isinstance(widget, QtGui.QComboBox): |
---|
35 | delegate = self.itemDelegate() |
---|
36 | widget.currentIndexChanged.connect(lambda: delegate.commitData.emit(widget)) |
---|
37 | |
---|
38 | elif isinstance(widget, QtGui.QCheckBox): |
---|
39 | delegate = self.itemDelegate() |
---|
40 | widget.stateChanged.connect(lambda: delegate.commitData.emit(widget)) |
---|
41 | |
---|
42 | class OptionsWidget(QtGui.QWidget, Ui_tabOptions): |
---|
43 | plot_signal = QtCore.pyqtSignal() |
---|
44 | def __init__(self, parent=None, logic=None): |
---|
45 | super(OptionsWidget, self).__init__() |
---|
46 | |
---|
47 | self.setupUi(self) |
---|
48 | |
---|
49 | # Logic component |
---|
50 | self.logic = logic |
---|
51 | self.parent = parent |
---|
52 | |
---|
53 | # Weight radio box group |
---|
54 | self.weightingGroup = QtGui.QButtonGroup() |
---|
55 | self.weighting = 0 |
---|
56 | |
---|
57 | # Group boxes |
---|
58 | self.boxWeighting.setEnabled(False) |
---|
59 | self.cmdMaskEdit.setEnabled(False) |
---|
60 | # Button groups |
---|
61 | self.weightingGroup.addButton(self.rbWeighting1) |
---|
62 | self.weightingGroup.addButton(self.rbWeighting2) |
---|
63 | self.weightingGroup.addButton(self.rbWeighting3) |
---|
64 | self.weightingGroup.addButton(self.rbWeighting4) |
---|
65 | |
---|
66 | # Let only floats in the range edits |
---|
67 | self.txtMinRange.setValidator(QtGui.QDoubleValidator()) |
---|
68 | self.txtMaxRange.setValidator(QtGui.QDoubleValidator()) |
---|
69 | # Let only ints in the number of points edit |
---|
70 | self.txtNpts.setValidator(QtGui.QIntValidator()) |
---|
71 | |
---|
72 | # Attach slots |
---|
73 | self.cmdReset.clicked.connect(self.onRangeReset) |
---|
74 | self.cmdMaskEdit.clicked.connect(self.onMaskEdit) |
---|
75 | self.chkLogData.stateChanged.connect(self.toggleLogData) |
---|
76 | # Button groups |
---|
77 | self.weightingGroup.buttonClicked.connect(self.onWeightingChoice) |
---|
78 | |
---|
79 | self.initModel() |
---|
80 | self.initMapper() |
---|
81 | self.model.blockSignals(True) |
---|
82 | self.updateQRange(QMIN_DEFAULT, QMAX_DEFAULT, NPTS_DEFAULT) |
---|
83 | self.txtMaxRange.setText(str(QMAX_DEFAULT)) |
---|
84 | self.txtMinRange.setText(str(QMIN_DEFAULT)) |
---|
85 | self.txtNpts.setText(str(NPTS_DEFAULT)) |
---|
86 | self.model.blockSignals(False) |
---|
87 | |
---|
88 | def initModel(self): |
---|
89 | """ |
---|
90 | Initialize the state |
---|
91 | """ |
---|
92 | self.model = QtGui.QStandardItemModel() |
---|
93 | for model_item in xrange(len(MODEL)): |
---|
94 | self.model.setItem(model_item, QtGui.QStandardItem()) |
---|
95 | # Attach slot |
---|
96 | self.model.dataChanged.connect(self.onModelChange) |
---|
97 | |
---|
98 | def initMapper(self): |
---|
99 | """ |
---|
100 | Initialize model item <-> UI element mapping |
---|
101 | """ |
---|
102 | self.mapper = DataWidgetMapper(self) |
---|
103 | |
---|
104 | self.mapper.setModel(self.model) |
---|
105 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
106 | |
---|
107 | self.mapper.addMapping(self.txtMinRange, MODEL.index('MIN_RANGE')) |
---|
108 | self.mapper.addMapping(self.txtMaxRange, MODEL.index('MAX_RANGE')) |
---|
109 | self.mapper.addMapping(self.txtNpts, MODEL.index('NPTS')) |
---|
110 | self.mapper.addMapping(self.chkLogData, MODEL.index('LOG_SPACED')) |
---|
111 | self.mapper.toFirst() |
---|
112 | |
---|
113 | def toggleLogData(self, isChecked): |
---|
114 | """ Toggles between log and linear data sets """ |
---|
115 | pass |
---|
116 | |
---|
117 | def onMaskEdit(self): |
---|
118 | """ |
---|
119 | Callback for running the mask editor |
---|
120 | """ |
---|
121 | pass |
---|
122 | |
---|
123 | def onRangeReset(self): |
---|
124 | """ |
---|
125 | Callback for resetting qmin/qmax |
---|
126 | """ |
---|
127 | pass |
---|
128 | |
---|
129 | def onWeightingChoice(self, button): |
---|
130 | """ |
---|
131 | Update weighting in the fit state |
---|
132 | """ |
---|
133 | button_id = button.group().checkedId() |
---|
134 | self.weighting = abs(button_id + 2) |
---|
135 | #self.fitPage.weighting = button_id |
---|
136 | |
---|
137 | def onModelChange(self, top, bottom): |
---|
138 | """ |
---|
139 | Respond to model change by updating the plot |
---|
140 | """ |
---|
141 | # "bottom" is unused |
---|
142 | # update if there's something to update |
---|
143 | if str(self.model.item(top.row()).text()): |
---|
144 | self.plot_signal.emit() |
---|
145 | |
---|
146 | def setEnablementOnDataLoad(self): |
---|
147 | """ |
---|
148 | Enable/disable various UI elements based on data loaded |
---|
149 | """ |
---|
150 | self.boxWeighting.setEnabled(True) |
---|
151 | self.cmdMaskEdit.setEnabled(True) |
---|
152 | # Switch off txtNpts related controls |
---|
153 | self.txtNpts.setEnabled(False) |
---|
154 | self.txtNptsFit.setEnabled(False) |
---|
155 | self.chkLogData.setEnabled(False) |
---|
156 | # Weighting controls |
---|
157 | if isinstance(self.logic.data, Data2D): |
---|
158 | if self.logic.data.err_data is None or\ |
---|
159 | np.all(err == 1 for err in self.logic.data.err_data) or \ |
---|
160 | not np.any(self.logic.data.err_data): |
---|
161 | self.rbWeighting2.setEnabled(False) |
---|
162 | self.rbWeighting1.setChecked(True) |
---|
163 | else: |
---|
164 | self.rbWeighting2.setEnabled(True) |
---|
165 | self.rbWeighting2.setChecked(True) |
---|
166 | else: |
---|
167 | if self.logic.data.dy is None or\ |
---|
168 | np.all(self.logic.data.dy == 1) or\ |
---|
169 | not np.any(self.logic.data.dy): |
---|
170 | self.rbWeighting2.setEnabled(False) |
---|
171 | self.rbWeighting1.setChecked(True) |
---|
172 | else: |
---|
173 | self.rbWeighting2.setEnabled(True) |
---|
174 | self.rbWeighting2.setChecked(True) |
---|
175 | |
---|
176 | def updateQRange(self, q_range_min, q_range_max, npts): |
---|
177 | """ |
---|
178 | Update the local model based on calculated values |
---|
179 | """ |
---|
180 | self.model.item(MODEL.index('MIN_RANGE')).setText(str(q_range_min)) |
---|
181 | self.model.item(MODEL.index('MAX_RANGE')).setText(str(q_range_max)) |
---|
182 | self.model.item(MODEL.index('NPTS')).setText(str(npts)) |
---|
183 | |
---|
184 | def state(self): |
---|
185 | """ |
---|
186 | Returns current state of controls |
---|
187 | """ |
---|
188 | q_range_min = float(self.model.item(MODEL.index('MIN_RANGE')).text()) |
---|
189 | q_range_max = float(self.model.item(MODEL.index('MAX_RANGE')).text()) |
---|
190 | npts = int(self.model.item(MODEL.index('NPTS')).text()) |
---|
191 | log_points = str(self.model.item(MODEL.index('LOG_SPACED')).text()) == 'true' |
---|
192 | |
---|
193 | return (q_range_min, q_range_max, npts, log_points, self.weighting) |
---|