1 | """ |
---|
2 | Widget/logic for smearing data. |
---|
3 | """ |
---|
4 | from PyQt4 import QtGui |
---|
5 | from PyQt4 import QtCore |
---|
6 | |
---|
7 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
8 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
9 | |
---|
10 | # Local UI |
---|
11 | from sas.qtgui.Perspectives.Fitting.UI.SmearingWidgetUI import Ui_SmearingWidgetUI |
---|
12 | |
---|
13 | class DataWidgetMapper(QtGui.QDataWidgetMapper): |
---|
14 | """ |
---|
15 | Custom version of the standard QDataWidgetMapper allowing for proper |
---|
16 | response to index change in comboboxes |
---|
17 | """ |
---|
18 | def addMapping(self, widget, section, propertyName=None): |
---|
19 | if propertyName is None: |
---|
20 | super(DataWidgetMapper, self).addMapping(widget, section) |
---|
21 | else: |
---|
22 | super(DataWidgetMapper, self).addMapping(widget, section, propertyName) |
---|
23 | |
---|
24 | if isinstance(widget, QtGui.QComboBox): |
---|
25 | delegate = self.itemDelegate() |
---|
26 | widget.currentIndexChanged.connect(lambda: delegate.commitData.emit(widget)) |
---|
27 | |
---|
28 | SMEARING_1D = ["Custom Pinhole Smear", "Custom Slit Smear"] |
---|
29 | SMEARING_2D = ["Custom Pinhole Smear"] |
---|
30 | |
---|
31 | MODEL = [ |
---|
32 | 'SMEARING', |
---|
33 | 'PINHOLE_MIN', |
---|
34 | 'PINHOLE_MAX', |
---|
35 | 'ACCURACY'] |
---|
36 | |
---|
37 | class SmearingWidget(QtGui.QWidget, Ui_SmearingWidgetUI): |
---|
38 | def __init__(self, parent=None): |
---|
39 | super(SmearingWidget, self).__init__() |
---|
40 | |
---|
41 | self.setupUi(self) |
---|
42 | |
---|
43 | # Have we loaded data yet? If so, what kind |
---|
44 | self.have_data = None |
---|
45 | # Local model for holding data |
---|
46 | self.model = None |
---|
47 | # Mapper for model update |
---|
48 | self.mapper = None |
---|
49 | |
---|
50 | self.parent = parent |
---|
51 | # Let only floats in the line edits |
---|
52 | self.txtSmearDown.setValidator(QtGui.QDoubleValidator()) |
---|
53 | self.txtSmearUp.setValidator(QtGui.QDoubleValidator()) |
---|
54 | |
---|
55 | # Attach slots |
---|
56 | self.cbSmearing.currentIndexChanged.connect(self.onIndexChange) |
---|
57 | self.cbSmearing.setCurrentIndex(0) |
---|
58 | |
---|
59 | self.initModel() |
---|
60 | self.initMapper() |
---|
61 | |
---|
62 | def initModel(self): |
---|
63 | """ |
---|
64 | Initialize the state |
---|
65 | """ |
---|
66 | self.model = QtGui.QStandardItemModel() |
---|
67 | for model_item in xrange(len(MODEL)): |
---|
68 | self.model.setItem(model_item, QtGui.QStandardItem()) |
---|
69 | # Attach slot |
---|
70 | self.model.dataChanged.connect(self.onModelChange) |
---|
71 | |
---|
72 | def initMapper(self): |
---|
73 | """ |
---|
74 | Initialize model item <-> UI element mapping |
---|
75 | """ |
---|
76 | self.mapper = DataWidgetMapper(self) |
---|
77 | |
---|
78 | self.mapper.setModel(self.model) |
---|
79 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
80 | |
---|
81 | self.mapper.addMapping(self.txtSmearUp, MODEL.index('PINHOLE_MIN')) |
---|
82 | self.mapper.addMapping(self.txtSmearDown, MODEL.index('PINHOLE_MAX')) |
---|
83 | self.mapper.addMapping(self.cbSmearing, MODEL.index('SMEARING')) |
---|
84 | self.mapper.addMapping(self.cbAccuracy, MODEL.index('ACCURACY')) |
---|
85 | self.mapper.toFirst() |
---|
86 | |
---|
87 | def updateSmearing(self, data=None): |
---|
88 | """ |
---|
89 | Update control elements based on data passed |
---|
90 | """ |
---|
91 | self.cbSmearing.clear() |
---|
92 | self.cbSmearing.addItem("None") |
---|
93 | self.cbAccuracy.setVisible(False) |
---|
94 | |
---|
95 | if data is None: |
---|
96 | self.setElementsVisibility(False) |
---|
97 | elif isinstance(data, Data1D): |
---|
98 | self.cbSmearing.addItems(SMEARING_1D) |
---|
99 | self.have_data = Data1D |
---|
100 | else: |
---|
101 | self.cbSmearing.addItems(SMEARING_2D) |
---|
102 | self.have_data = Data2D |
---|
103 | self.cbSmearing.setCurrentIndex(0) |
---|
104 | |
---|
105 | def onIndexChange(self, index): |
---|
106 | """ |
---|
107 | Callback for smearing combobox index change |
---|
108 | """ |
---|
109 | if index == 0: |
---|
110 | self.setElementsVisibility(False) |
---|
111 | return |
---|
112 | elif index == 1: |
---|
113 | self.setElementsVisibility(True) |
---|
114 | self.setPinholeLabels() |
---|
115 | elif index == 2: |
---|
116 | self.setElementsVisibility(True) |
---|
117 | self.setSlitLabels() |
---|
118 | |
---|
119 | def onModelChange(self, top, bottom): |
---|
120 | """ |
---|
121 | Respond to model change by updating |
---|
122 | """ |
---|
123 | #print "MODEL CHANGED for property: %s. The value is now: %s" % \ |
---|
124 | # (MODEL[top.row()], str(self.model.item(top.row()).text())) |
---|
125 | pass |
---|
126 | |
---|
127 | def setElementsVisibility(self, visible): |
---|
128 | """ |
---|
129 | Labels and linedits visibility control |
---|
130 | """ |
---|
131 | self.lblSmearDown.setVisible(visible) |
---|
132 | self.lblSmearUp.setVisible(visible) |
---|
133 | self.txtSmearDown.setVisible(visible) |
---|
134 | self.txtSmearUp.setVisible(visible) |
---|
135 | self.label_14.setVisible(visible) |
---|
136 | self.label_15.setVisible(visible) |
---|
137 | self.setAccuracyVisibility() |
---|
138 | |
---|
139 | def setAccuracyVisibility(self): |
---|
140 | """ |
---|
141 | Accuracy combobox visibility |
---|
142 | """ |
---|
143 | if self.have_data == Data2D and self.cbSmearing.currentIndex() == 1: |
---|
144 | self.cbAccuracy.setVisible(True) |
---|
145 | else: |
---|
146 | self.cbAccuracy.setVisible(False) |
---|
147 | |
---|
148 | def setPinholeLabels(self): |
---|
149 | """ |
---|
150 | Use pinhole labels |
---|
151 | """ |
---|
152 | self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">low</span></p></body></html>') |
---|
153 | self.lblSmearDown.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">high</span></p></body></html>') |
---|
154 | |
---|
155 | def setSlitLabels(self): |
---|
156 | """ |
---|
157 | Use pinhole labels |
---|
158 | """ |
---|
159 | self.lblSmearUp.setText('Slit height') |
---|
160 | self.lblSmearDown.setText('Slit width') |
---|
161 | |
---|
162 | def state(self): |
---|
163 | """ |
---|
164 | Returns current state of controls |
---|
165 | """ |
---|
166 | # or model-held values |
---|
167 | smearing = str(self.model.item(MODEL.index('SMEARING')).text()) |
---|
168 | accuracy = "" |
---|
169 | d_down = None |
---|
170 | d_up = None |
---|
171 | if smearing != "None": |
---|
172 | accuracy = str(self.model.item(MODEL.index('ACCURACY')).text()) |
---|
173 | try: |
---|
174 | d_down = float(self.model.item(MODEL.index('PINHOLE_MIN')).text()) |
---|
175 | except ValueError: |
---|
176 | d_down = None |
---|
177 | try: |
---|
178 | d_up = float(self.model.item(MODEL.index('PINHOLE_MAX')).text()) |
---|
179 | except ValueError: |
---|
180 | d_up = None |
---|
181 | |
---|
182 | return (smearing, accuracy, d_down, d_up) |
---|
183 | |
---|
184 | def setState(self, smearing, accuracy, d_down, d_up): |
---|
185 | """ |
---|
186 | Sets new values for the controls |
---|
187 | """ |
---|
188 | # Update the model -> controls update automatically |
---|
189 | if smearing is not None: |
---|
190 | self.model.item(MODEL.index('SMEARING')).setText(smearing) |
---|
191 | if accuracy is not None: |
---|
192 | self.model.item(MODEL.index('ACCURACY')).setText(accuracy) |
---|
193 | if d_down is not None: |
---|
194 | self.model.item(MODEL.index('PINHOLE_MIN')).setText(d_down) |
---|
195 | if d_up is not None: |
---|
196 | self.model.item(MODEL.index('PINHOLE_MAX')).setText(d_up) |
---|
197 | |
---|