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