1 | |
---|
2 | import wx |
---|
3 | import sys |
---|
4 | from copy import deepcopy |
---|
5 | from sas.sascalc.dataloader.data_info import Detector |
---|
6 | from sas.sasgui.guiframe.utils import check_float |
---|
7 | |
---|
8 | _BOX_WIDTH = 60 |
---|
9 | if sys.platform.count("win32") > 0: |
---|
10 | _STATICBOX_WIDTH = 465 |
---|
11 | PANEL_WIDTH = 500 |
---|
12 | PANEL_HEIGHT = 450 |
---|
13 | FONT_VARIANT = 0 |
---|
14 | else: |
---|
15 | _STATICBOX_WIDTH = 480 |
---|
16 | PANEL_WIDTH = 550 |
---|
17 | PANEL_HEIGHT = 480 |
---|
18 | FONT_VARIANT = 1 |
---|
19 | |
---|
20 | class DetectorDialog(wx.Dialog): |
---|
21 | def __init__(self, parent=None, manager=None, detector=None, |
---|
22 | title="Detector Editor", |
---|
23 | size=(PANEL_WIDTH, PANEL_HEIGHT)): |
---|
24 | wx.Dialog.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
25 | try: |
---|
26 | self.parent = parent |
---|
27 | self.manager = manager |
---|
28 | self._detector = detector |
---|
29 | |
---|
30 | self._reset_detector = deepcopy(detector) |
---|
31 | self._notes = "" |
---|
32 | self._description = "Edit Detector" |
---|
33 | self._do_layout() |
---|
34 | self.set_values() |
---|
35 | except: |
---|
36 | print "error", sys.exc_value |
---|
37 | |
---|
38 | def _define_structure(self): |
---|
39 | """ |
---|
40 | define initial sizer |
---|
41 | """ |
---|
42 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
43 | self.box_detector = wx.StaticBox(self, -1, str("Edit Selected Detector")) |
---|
44 | self.boxsizer_detector = wx.StaticBoxSizer(self.box_detector, |
---|
45 | wx.VERTICAL) |
---|
46 | detector_box = wx.StaticBox(self, -1, "Edit Number of Detectors") |
---|
47 | self.detector_sizer = wx.StaticBoxSizer(detector_box, wx.VERTICAL) |
---|
48 | self.detector_sizer.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
49 | self.detector_button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
50 | self.detector_hint_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
51 | |
---|
52 | self.detector_button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
53 | self.detector_hint_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
54 | self.instrument_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
55 | self.distance_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
56 | self.offset_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
57 | self.orientation_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
58 | self.beam_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
59 | self.pixel_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
60 | self.slit_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
61 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
62 | |
---|
63 | def _layout_detector(self): |
---|
64 | """ |
---|
65 | Do the layout for detector related widgets |
---|
66 | """ |
---|
67 | detector_name_txt = wx.StaticText(self, -1, "Detector:") |
---|
68 | hint_detector_txt = 'Current available detector.' |
---|
69 | detector_name_txt.SetToolTipString(hint_detector_txt) |
---|
70 | self.detector_cbox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
71 | hint_detector_name_txt = 'Name of detectors.' |
---|
72 | self.detector_cbox.SetToolTipString(hint_detector_name_txt) |
---|
73 | |
---|
74 | self.bt_add_detector = wx.Button(self, -1, "Add") |
---|
75 | self.bt_add_detector.SetToolTipString("Add data's detector.") |
---|
76 | self.bt_add_detector.Bind(wx.EVT_BUTTON, self.add_detector) |
---|
77 | |
---|
78 | self.bt_remove_detector = wx.Button(self, -1, "Remove") |
---|
79 | self.bt_remove_detector.SetToolTipString("Remove data's detector.") |
---|
80 | self.bt_remove_detector.Bind(wx.EVT_BUTTON, self.remove_detector) |
---|
81 | |
---|
82 | self.detector_button_sizer.AddMany([(detector_name_txt, 0, wx.LEFT, 15), |
---|
83 | (self.detector_cbox, 0, wx.LEFT, 5), |
---|
84 | (self.bt_add_detector, 0, wx.LEFT, 10), |
---|
85 | (self.bt_remove_detector, 0, wx.LEFT, 5)]) |
---|
86 | detector_hint_txt = 'No detector is available for this data.' |
---|
87 | self.detector_txt = wx.StaticText(self, -1, detector_hint_txt) |
---|
88 | self.detector_hint_sizer.Add(self.detector_txt, 0, wx.LEFT, 10) |
---|
89 | self.detector_sizer.AddMany([(self.detector_button_sizer, 0, |
---|
90 | wx.ALL, 10), |
---|
91 | (self.detector_hint_sizer, 0, wx.ALL, 10)]) |
---|
92 | |
---|
93 | self.fill_detector_combox() |
---|
94 | self.enable_detector() |
---|
95 | |
---|
96 | def _layout_instrument_sizer(self): |
---|
97 | """ |
---|
98 | Do the layout for instrument related widgets |
---|
99 | """ |
---|
100 | #Instrument |
---|
101 | instrument_name_txt = wx.StaticText(self, -1, 'Instrument Name : ') |
---|
102 | self.instrument_name_tcl = wx.TextCtrl(self, -1, |
---|
103 | size=(_BOX_WIDTH * 5, 20), style=0) |
---|
104 | self.instrument_sizer.AddMany([(instrument_name_txt, 0, |
---|
105 | wx.LEFT | wx.RIGHT, 10), |
---|
106 | (self.instrument_name_tcl, 0, wx.EXPAND)]) |
---|
107 | def _layout_distance(self): |
---|
108 | """ |
---|
109 | Do the layout for distance related widgets |
---|
110 | """ |
---|
111 | distance_txt = wx.StaticText(self, -1, 'Sample to Detector Distance : ') |
---|
112 | self.distance_tcl = wx.TextCtrl(self, -1, |
---|
113 | size=(_BOX_WIDTH, 20), style=0) |
---|
114 | distance_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
115 | self.distance_unit_tcl = wx.TextCtrl(self, -1, |
---|
116 | size=(_BOX_WIDTH, 20), style=0) |
---|
117 | self.distance_sizer.AddMany([(distance_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
118 | (self.distance_tcl, 0, wx.RIGHT, 10), |
---|
119 | (distance_unit_txt, 0, wx.EXPAND), |
---|
120 | (self.distance_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
121 | |
---|
122 | def _layout_offset(self): |
---|
123 | """ |
---|
124 | Do the layout for offset related widgets |
---|
125 | """ |
---|
126 | #Offset |
---|
127 | offset_txt = wx.StaticText(self, -1, 'Offset:') |
---|
128 | x_offset_txt = wx.StaticText(self, -1, 'x = ') |
---|
129 | self.x_offset_tcl = wx.TextCtrl(self, -1, |
---|
130 | size=(_BOX_WIDTH, 20), style=0) |
---|
131 | y_offset_txt = wx.StaticText(self, -1, 'y = ') |
---|
132 | self.y_offset_tcl = wx.TextCtrl(self, -1, |
---|
133 | size=(_BOX_WIDTH, 20), style=0) |
---|
134 | z_offset_txt = wx.StaticText(self, -1, 'z = ') |
---|
135 | self.z_offset_tcl = wx.TextCtrl(self, -1, |
---|
136 | size=(_BOX_WIDTH, 20), style=0) |
---|
137 | offset_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
138 | self.offset_unit_tcl = wx.TextCtrl(self, -1, |
---|
139 | size=(_BOX_WIDTH, 20), style=0) |
---|
140 | self.offset_sizer.AddMany([(offset_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
141 | (x_offset_txt, 0, wx.LEFT, 30), |
---|
142 | (self.x_offset_tcl, 0, wx.RIGHT, 10), |
---|
143 | (y_offset_txt, 0, wx.EXPAND), |
---|
144 | (self.y_offset_tcl, 0, wx.RIGHT, 10), |
---|
145 | (z_offset_txt, 0, wx.EXPAND), |
---|
146 | (self.z_offset_tcl, 0, wx.RIGHT, 10), |
---|
147 | (offset_unit_txt, 0, wx.EXPAND), |
---|
148 | (self.offset_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
149 | |
---|
150 | def _layout_orientation(self): |
---|
151 | """ |
---|
152 | Do the layout for orientation related widgets |
---|
153 | """ |
---|
154 | #Orientation |
---|
155 | orientation_txt = wx.StaticText(self, -1, 'Orientation:') |
---|
156 | x_orientation_txt = wx.StaticText(self, -1, 'x = ') |
---|
157 | self.x_orientation_tcl = wx.TextCtrl(self, -1, |
---|
158 | size=(_BOX_WIDTH, 20), style=0) |
---|
159 | y_orientation_txt = wx.StaticText(self, -1, 'y = ') |
---|
160 | self.y_orientation_tcl = wx.TextCtrl(self, -1, |
---|
161 | size=(_BOX_WIDTH, 20), style=0) |
---|
162 | z_orientation_txt = wx.StaticText(self, -1, 'z = ') |
---|
163 | self.z_orientation_tcl = wx.TextCtrl(self, -1, |
---|
164 | size=(_BOX_WIDTH, 20), style=0) |
---|
165 | orientation_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
166 | self.orientation_unit_tcl = wx.TextCtrl(self, -1, |
---|
167 | size=(_BOX_WIDTH, 20), style=0) |
---|
168 | self.orientation_sizer.AddMany([(orientation_txt, 0, |
---|
169 | wx.LEFT | wx.RIGHT, 10), |
---|
170 | (x_orientation_txt, 0, wx.LEFT, 7), |
---|
171 | (self.x_orientation_tcl, 0, wx.RIGHT, 10), |
---|
172 | (y_orientation_txt, 0, wx.EXPAND), |
---|
173 | (self.y_orientation_tcl, 0, wx.RIGHT, 10), |
---|
174 | (z_orientation_txt, 0, wx.EXPAND), |
---|
175 | (self.z_orientation_tcl, 0, wx.RIGHT, 10), |
---|
176 | (orientation_unit_txt, 0, wx.EXPAND), |
---|
177 | (self.orientation_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
178 | |
---|
179 | def _layout_beam_center(self): |
---|
180 | """ |
---|
181 | Do the layout for beam center related widgets |
---|
182 | """ |
---|
183 | #Beam center |
---|
184 | beam_center_txt = wx.StaticText(self, -1, 'Beam Center:') |
---|
185 | x_beam_center_txt = wx.StaticText(self, -1, 'x = ') |
---|
186 | self.x_beam_center_tcl = wx.TextCtrl(self, -1, |
---|
187 | size=(_BOX_WIDTH, 20), style=0) |
---|
188 | y_beam_center_txt = wx.StaticText(self, -1, 'y = ') |
---|
189 | self.y_beam_center_tcl = wx.TextCtrl(self, -1, |
---|
190 | size=(_BOX_WIDTH, 20), style=0) |
---|
191 | z_beam_center_txt = wx.StaticText(self, -1, 'z = ') |
---|
192 | self.z_beam_center_tcl = wx.TextCtrl(self, -1, |
---|
193 | size=(_BOX_WIDTH, 20), style=0) |
---|
194 | beam_center_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
195 | self.beam_center_unit_tcl = wx.TextCtrl(self, -1, |
---|
196 | size=(_BOX_WIDTH, 20), style=0) |
---|
197 | self.beam_sizer.AddMany([(beam_center_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
198 | (x_beam_center_txt, 0, wx.EXPAND), |
---|
199 | (self.x_beam_center_tcl, 0, wx.RIGHT, 10), |
---|
200 | (y_beam_center_txt, 0, wx.EXPAND), |
---|
201 | (self.y_beam_center_tcl, 0, wx.RIGHT, 10), |
---|
202 | (z_beam_center_txt, 0, wx.EXPAND), |
---|
203 | (self.z_beam_center_tcl, 0, wx.RIGHT, 10), |
---|
204 | (beam_center_unit_txt, 0, wx.EXPAND), |
---|
205 | (self.beam_center_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
206 | |
---|
207 | def _layout_pixel_size(self): |
---|
208 | """ |
---|
209 | Do the layout for pixel size related widgets |
---|
210 | """ |
---|
211 | #Pixel Size |
---|
212 | pixel_size_txt = wx.StaticText(self, -1, 'Pixel Size:') |
---|
213 | x_pixel_size_txt = wx.StaticText(self, -1, 'x = ') |
---|
214 | self.x_pixel_size_tcl = wx.TextCtrl(self, -1, |
---|
215 | size=(_BOX_WIDTH, 20), style=0) |
---|
216 | y_pixel_size_txt = wx.StaticText(self, -1, 'y = ') |
---|
217 | self.y_pixel_size_tcl = wx.TextCtrl(self, -1, |
---|
218 | size=(_BOX_WIDTH, 20), style=0) |
---|
219 | z_pixel_size_txt = wx.StaticText(self, -1, 'z = ') |
---|
220 | self.z_pixel_size_tcl = wx.TextCtrl(self, -1, |
---|
221 | size=(_BOX_WIDTH, 20), style=0) |
---|
222 | pixel_size_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
223 | self.pixel_size_unit_tcl = wx.TextCtrl(self, -1, |
---|
224 | size=(_BOX_WIDTH, 20), style=0) |
---|
225 | self.pixel_sizer.AddMany([(pixel_size_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
226 | (x_pixel_size_txt, 0, wx.LEFT, 17), |
---|
227 | (self.x_pixel_size_tcl, 0, wx.RIGHT, 10), |
---|
228 | (y_pixel_size_txt, 0, wx.EXPAND), |
---|
229 | (self.y_pixel_size_tcl, 0, wx.RIGHT, 10), |
---|
230 | (z_pixel_size_txt, 0, wx.EXPAND), |
---|
231 | (self.z_pixel_size_tcl, 0, wx.RIGHT, 10), |
---|
232 | (pixel_size_unit_txt, 0, wx.EXPAND), |
---|
233 | (self.pixel_size_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
234 | |
---|
235 | def _layout_slit_length(self): |
---|
236 | """ |
---|
237 | Do the layout for slit length related widgets |
---|
238 | """ |
---|
239 | #slit length |
---|
240 | slit_length_txt = wx.StaticText(self, -1, 'Slit Length: ') |
---|
241 | self.slit_length_tcl = wx.TextCtrl(self, -1, |
---|
242 | size=(_BOX_WIDTH, 20), style=0) |
---|
243 | slit_length_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
244 | self.slit_length_unit_tcl = wx.TextCtrl(self, -1, |
---|
245 | size=(_BOX_WIDTH, 20), style=0) |
---|
246 | self.slit_sizer.AddMany([(slit_length_txt, 0, wx.LEFT, 10), |
---|
247 | (self.slit_length_tcl, 0, wx.RIGHT, 10), |
---|
248 | (slit_length_unit_txt, 0, wx.EXPAND), |
---|
249 | (self.slit_length_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
250 | |
---|
251 | def _layout_button(self): |
---|
252 | """ |
---|
253 | Do the layout for the button widgets |
---|
254 | """ |
---|
255 | self.bt_apply = wx.Button(self, -1, 'Apply') |
---|
256 | self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) |
---|
257 | self.bt_apply.SetToolTipString("Apply current changes to the detector.") |
---|
258 | self.bt_cancel = wx.Button(self, -1, 'Cancel') |
---|
259 | self.bt_cancel.SetToolTipString("Cancel current changes.") |
---|
260 | self.bt_cancel.Bind(wx.EVT_BUTTON, self.on_click_cancel) |
---|
261 | self.bt_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
262 | self.bt_close.SetToolTipString("Close window.") |
---|
263 | self.button_sizer.AddMany([(self.bt_apply, 0, wx.LEFT, 200), |
---|
264 | (self.bt_cancel, 0, wx.LEFT, 10), |
---|
265 | (self.bt_close, 0, wx.LEFT, 10)]) |
---|
266 | |
---|
267 | def _do_layout(self, data=None): |
---|
268 | """ |
---|
269 | Draw the current panel |
---|
270 | """ |
---|
271 | self._define_structure() |
---|
272 | self._layout_detector() |
---|
273 | self._layout_instrument_sizer() |
---|
274 | self._layout_distance() |
---|
275 | self._layout_offset() |
---|
276 | self._layout_orientation() |
---|
277 | self._layout_beam_center() |
---|
278 | self._layout_pixel_size() |
---|
279 | self._layout_slit_length() |
---|
280 | self._layout_button() |
---|
281 | |
---|
282 | self.boxsizer_detector.AddMany([(self.instrument_sizer, 0, |
---|
283 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
284 | (self.distance_sizer, 0, |
---|
285 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
286 | (self.offset_sizer, 0, |
---|
287 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
288 | (self.orientation_sizer, 0, |
---|
289 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
290 | (self.beam_sizer, 0, |
---|
291 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
292 | (self.pixel_sizer, 0, |
---|
293 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
294 | (self.slit_sizer, 0, |
---|
295 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5)]) |
---|
296 | self.main_sizer.AddMany([(self.detector_sizer, 0, wx.ALL, 10), |
---|
297 | (self.boxsizer_detector, 0, wx.ALL, 10), |
---|
298 | (self.button_sizer, 0, |
---|
299 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5)]) |
---|
300 | |
---|
301 | self.SetSizer(self.main_sizer) |
---|
302 | self.SetAutoLayout(True) |
---|
303 | |
---|
304 | def reset_detector(self): |
---|
305 | """ |
---|
306 | put the default value of the detector back to the current detector |
---|
307 | """ |
---|
308 | self._detector = deepcopy(self._reset_detector) |
---|
309 | self.detector_cbox.Clear() |
---|
310 | self.fill_detector_combox() |
---|
311 | self.set_values() |
---|
312 | |
---|
313 | def set_manager(self, manager): |
---|
314 | """ |
---|
315 | Set manager of this window |
---|
316 | """ |
---|
317 | self.manager = manager |
---|
318 | |
---|
319 | def fill_detector_combox(self): |
---|
320 | """ |
---|
321 | fill the current combobox with the available detector |
---|
322 | """ |
---|
323 | for detector in self._detector: |
---|
324 | pos = self.detector_cbox.Append(str(detector.name)) |
---|
325 | self.detector_cbox.SetClientData(pos, detector) |
---|
326 | self.detector_cbox.SetSelection(pos) |
---|
327 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
328 | |
---|
329 | def reset_detector_combobox(self, edited_detector): |
---|
330 | """ |
---|
331 | take all edited editor and reset clientdata of detector combo box |
---|
332 | """ |
---|
333 | for position in range(self.detector_cbox.GetCount()): |
---|
334 | detector = self.detector_cbox.GetClientData(position) |
---|
335 | if detector == edited_detector: |
---|
336 | detector = edited_detector |
---|
337 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
338 | self.detector_cbox.SetClientData(position, detector) |
---|
339 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
340 | |
---|
341 | def add_detector(self, event): |
---|
342 | """ |
---|
343 | Append empty detector to data's list of detector |
---|
344 | """ |
---|
345 | |
---|
346 | if not self.detector_cbox.IsEnabled(): |
---|
347 | self.detector_cbox.Enable() |
---|
348 | detector = Detector() |
---|
349 | self._detector.append(detector) |
---|
350 | position = self.detector_cbox.Append(str(detector.name)) |
---|
351 | self.detector_cbox.SetClientData(position, detector) |
---|
352 | self.detector_cbox.SetSelection(position) |
---|
353 | self.enable_detector() |
---|
354 | self.set_values() |
---|
355 | |
---|
356 | def remove_detector(self, event): |
---|
357 | """ |
---|
358 | Remove detector to data's list of detector |
---|
359 | """ |
---|
360 | if self.detector_cbox.IsEnabled(): |
---|
361 | if self.detector_cbox.GetCount() > 1: |
---|
362 | position = self.detector_cbox.GetCurrentSelection() |
---|
363 | detector = self.detector_cbox.GetClientData(position) |
---|
364 | if detector in self._detector: |
---|
365 | self._detector.remove(detector) |
---|
366 | self.detector_cbox.Delete(position) |
---|
367 | #set the combo box box the next available item |
---|
368 | position = self.detector_cbox.GetCount() |
---|
369 | if position > 0: |
---|
370 | position -= 1 |
---|
371 | self.detector_cbox.SetSelection(position) |
---|
372 | self.set_values() |
---|
373 | #disable or enable the combo box when necessary |
---|
374 | self.enable_detector() |
---|
375 | |
---|
376 | def enable_detector(self): |
---|
377 | """ |
---|
378 | Enable /disable widgets crelated to detector |
---|
379 | """ |
---|
380 | if self._detector is not None and self.detector_cbox.GetCount() > 0: |
---|
381 | self.detector_cbox.Enable() |
---|
382 | self.bt_remove_detector.Enable() |
---|
383 | n_detector = self.detector_cbox.GetCount() |
---|
384 | detector_hint_txt = 'Detectors available: %s ' % str(n_detector) |
---|
385 | if len(self._detector) > 1: |
---|
386 | self.bt_remove_detector.Enable() |
---|
387 | else: |
---|
388 | self.bt_remove_detector.Disable() |
---|
389 | else: |
---|
390 | self.detector_cbox.Disable() |
---|
391 | self.bt_remove_detector.Disable() |
---|
392 | detector_hint_txt = 'No detector is available for this data.' |
---|
393 | self.detector_txt.SetLabel(detector_hint_txt) |
---|
394 | |
---|
395 | def set_detector(self, detector): |
---|
396 | """ |
---|
397 | set detector for data |
---|
398 | """ |
---|
399 | if self._detector is None: |
---|
400 | return |
---|
401 | if self._detector: |
---|
402 | for item in self._detector: |
---|
403 | if item == detector: |
---|
404 | item = detector |
---|
405 | self.reset_detector_combobox(edited_detector=detector) |
---|
406 | return |
---|
407 | |
---|
408 | def get_current_detector(self): |
---|
409 | """ |
---|
410 | """ |
---|
411 | if not self.detector_cbox.IsEnabled(): |
---|
412 | return None, None, None |
---|
413 | position = self.detector_cbox.GetSelection() |
---|
414 | if position == wx.NOT_FOUND: |
---|
415 | return None, None, None |
---|
416 | detector_name = self.detector_cbox.GetStringSelection() |
---|
417 | detector = self.detector_cbox.GetClientData(position) |
---|
418 | return detector, detector_name, position |
---|
419 | |
---|
420 | def set_values(self): |
---|
421 | """ |
---|
422 | take the detector values of the current data and display them |
---|
423 | through the panel |
---|
424 | """ |
---|
425 | detector, _, _ = self.get_current_detector() |
---|
426 | if detector is None: |
---|
427 | return |
---|
428 | self.instrument_name_tcl.SetValue(str(detector.name)) |
---|
429 | #Distance |
---|
430 | distance = detector.distance |
---|
431 | self.distance_tcl.SetValue(str(distance)) |
---|
432 | self.distance_unit_tcl.SetValue(str(detector.distance_unit)) |
---|
433 | #Offset |
---|
434 | x, y, z = detector.offset.x, detector.offset.y , detector.offset.z |
---|
435 | self.x_offset_tcl.SetValue(str(x)) |
---|
436 | self.y_offset_tcl.SetValue(str(y)) |
---|
437 | self.z_offset_tcl.SetValue(str(z)) |
---|
438 | self.offset_unit_tcl.SetValue(str(detector.offset_unit)) |
---|
439 | #Orientation |
---|
440 | x, y = detector.orientation.x, detector.orientation.y |
---|
441 | z = detector.orientation.z |
---|
442 | self.x_orientation_tcl.SetValue(str(x)) |
---|
443 | self.y_orientation_tcl.SetValue(str(y)) |
---|
444 | self.z_orientation_tcl.SetValue(str(z)) |
---|
445 | self.orientation_unit_tcl.SetValue(str(detector.orientation_unit)) |
---|
446 | #Beam center |
---|
447 | x, y = detector.beam_center.x, detector.beam_center.y |
---|
448 | z = detector.beam_center.z |
---|
449 | self.x_beam_center_tcl.SetValue(str(x)) |
---|
450 | self.y_beam_center_tcl.SetValue(str(y)) |
---|
451 | self.z_beam_center_tcl.SetValue(str(z)) |
---|
452 | self.beam_center_unit_tcl.SetValue(str(detector.beam_center_unit)) |
---|
453 | #Pixel size |
---|
454 | x, y = detector.pixel_size.x, detector.pixel_size.y |
---|
455 | z = detector.pixel_size.z |
---|
456 | self.x_pixel_size_tcl.SetValue(str(x)) |
---|
457 | self.y_pixel_size_tcl.SetValue(str(y)) |
---|
458 | self.z_pixel_size_tcl.SetValue(str(z)) |
---|
459 | self.pixel_size_unit_tcl.SetValue(str(detector.pixel_size_unit)) |
---|
460 | #Slit length |
---|
461 | slit_length = detector.slit_length |
---|
462 | self.slit_length_tcl.SetValue(str(detector.slit_length)) |
---|
463 | self.slit_length_unit_tcl.SetValue(str(detector.slit_length_unit)) |
---|
464 | |
---|
465 | def get_detector(self): |
---|
466 | """ |
---|
467 | return the current detector |
---|
468 | """ |
---|
469 | return self._detector |
---|
470 | |
---|
471 | def get_notes(self): |
---|
472 | """ |
---|
473 | return notes |
---|
474 | """ |
---|
475 | return self._notes |
---|
476 | |
---|
477 | def on_change_instrument(self): |
---|
478 | """ |
---|
479 | Change instrument |
---|
480 | """ |
---|
481 | detector, detector_name, position = self.get_current_detector() |
---|
482 | if detector is None: |
---|
483 | return |
---|
484 | #Change the name of the detector |
---|
485 | name = self.instrument_name_tcl.GetValue().lstrip().rstrip() |
---|
486 | if name == "" or name == str(None): |
---|
487 | name = None |
---|
488 | if detector_name != name: |
---|
489 | self._notes += " Instrument's " |
---|
490 | self._notes += "name from %s to %s \n" % (detector_name, name) |
---|
491 | detector.name = name |
---|
492 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
493 | self.detector_cbox.SetClientData(position, detector) |
---|
494 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
495 | |
---|
496 | def on_change_distance(self): |
---|
497 | """ |
---|
498 | Change distance of the sample to the detector |
---|
499 | """ |
---|
500 | detector, _, position = self.get_current_detector() |
---|
501 | if detector is None: |
---|
502 | return |
---|
503 | #Change the distance |
---|
504 | distance = self.distance_tcl.GetValue().lstrip().rstrip() |
---|
505 | if distance == "" or distance == str(None): |
---|
506 | distance = None |
---|
507 | detector.distance = distance |
---|
508 | else: |
---|
509 | if check_float(self.distance_tcl): |
---|
510 | if detector.distance != float(distance): |
---|
511 | self._notes += " Change Distance from" |
---|
512 | self._notes += " %s to %s \n" % (detector.distance, distance) |
---|
513 | detector.distance = float(distance) |
---|
514 | else: |
---|
515 | self._notes += "Error: Expected a float for " |
---|
516 | self._notes += " the distance won't changes " |
---|
517 | self._notes += "%s to %s" % (detector.distance, distance) |
---|
518 | #change the distance unit |
---|
519 | unit = self.distance_unit_tcl.GetValue().lstrip().rstrip() |
---|
520 | if detector.distance_unit != unit: |
---|
521 | self._notes += " Change distance's unit from " |
---|
522 | self._notes += "%s to %s" % (detector.distance_unit, unit) |
---|
523 | |
---|
524 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
525 | self.detector_cbox.SetClientData(position, detector) |
---|
526 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
527 | |
---|
528 | def on_change_offset(self): |
---|
529 | """ |
---|
530 | Change the detector offset |
---|
531 | """ |
---|
532 | detector, _, position = self.get_current_detector() |
---|
533 | if detector is None: |
---|
534 | return |
---|
535 | #Change x coordinate |
---|
536 | x_offset = self.x_offset_tcl.GetValue().lstrip().rstrip() |
---|
537 | if x_offset == "" or x_offset == str(None): |
---|
538 | x_offset = None |
---|
539 | detector.offset.x = x_offset |
---|
540 | else: |
---|
541 | if check_float(self.x_offset_tcl): |
---|
542 | if detector.offset.x != float(x_offset): |
---|
543 | self._notes += "Change x of offset from" |
---|
544 | self._notes += " %s to %s \n" % (detector.offset.x, |
---|
545 | x_offset) |
---|
546 | detector.offset.x = float(x_offset) |
---|
547 | else: |
---|
548 | self._notes += "Error: Expected a float for the offset 's x " |
---|
549 | self._notes += "won't changes x offset" |
---|
550 | self._notes += " from %s to %s" % (detector.offset.x, x_offset) |
---|
551 | #Change y coordinate |
---|
552 | y_offset = self.y_offset_tcl.GetValue().lstrip().rstrip() |
---|
553 | if y_offset == "" or y_offset == str(None): |
---|
554 | y_offset = None |
---|
555 | detector.offset.y = y_offset |
---|
556 | else: |
---|
557 | if check_float(self.y_offset_tcl): |
---|
558 | if detector.offset.y != float(y_offset): |
---|
559 | self._notes += "Change y of offset from " |
---|
560 | self._notes += "%s to %s \n" % (detector.offset.y, y_offset) |
---|
561 | detector.offset.y = float(y_offset) |
---|
562 | else: |
---|
563 | self._notes += "Error: Expected a float for the offset 's y " |
---|
564 | self._notes += "won't changes y " |
---|
565 | self._notes += "offset from %s to %s" % (detector.offset.y, |
---|
566 | y_offset) |
---|
567 | #Change z coordinate |
---|
568 | z_offset = self.z_offset_tcl.GetValue().lstrip().rstrip() |
---|
569 | if z_offset == "" or z_offset == str(None): |
---|
570 | z_offset = None |
---|
571 | detector.offset.z = z_offset |
---|
572 | else: |
---|
573 | if check_float(self.z_offset_tcl): |
---|
574 | if detector.offset.z != float(z_offset): |
---|
575 | self._notes += "Change z of offset from" |
---|
576 | self._notes += " %s to %s \n" % (detector.offset.z, |
---|
577 | z_offset) |
---|
578 | detector.offset.z = float(z_offset) |
---|
579 | else: |
---|
580 | self._notes += "Error: Expected a float for the offset 's x " |
---|
581 | self._notes += "won't changes z" |
---|
582 | self._notes += "offset from %s to %s" % (detector.offset.z, |
---|
583 | z_offset) |
---|
584 | #change the offset unit |
---|
585 | unit = self.offset_unit_tcl.GetValue().lstrip().rstrip() |
---|
586 | if detector.offset_unit != unit: |
---|
587 | self._notes += " Change Offset's" |
---|
588 | self._notes += "unit from %s to %s" % (detector.offset_unit, unit) |
---|
589 | |
---|
590 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
591 | self.detector_cbox.SetClientData(position, detector) |
---|
592 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
593 | |
---|
594 | def on_change_orientation(self): |
---|
595 | """ |
---|
596 | Change the detector orientation |
---|
597 | """ |
---|
598 | detector, _, position = self.get_current_detector() |
---|
599 | if detector is None: |
---|
600 | return |
---|
601 | #Change x coordinate |
---|
602 | x_orientation = self.x_orientation_tcl.GetValue().lstrip().rstrip() |
---|
603 | if x_orientation == "" or x_orientation == str(None): |
---|
604 | x_orientation = None |
---|
605 | detector.orientation.x = x_orientation |
---|
606 | else: |
---|
607 | if check_float(self.x_orientation_tcl): |
---|
608 | if detector.orientation.x != float(x_orientation): |
---|
609 | self._notes += "Change x of orientation from " |
---|
610 | self._notes += "%s to %s \n" % (detector.orientation.x, |
---|
611 | x_orientation) |
---|
612 | detector.orientation.x = float(x_orientation) |
---|
613 | else: |
---|
614 | self._notes += "Error: Expected a float for the orientation " |
---|
615 | self._notes += "'s x won't changes x orientation from " |
---|
616 | self._notes += "%s to %s" % (detector.orientation.x, |
---|
617 | x_orientation) |
---|
618 | #Change y coordinate |
---|
619 | y_orientation = self.y_orientation_tcl.GetValue().lstrip().rstrip() |
---|
620 | if y_orientation == "" or y_orientation == str(None): |
---|
621 | y_orientation = None |
---|
622 | detector.orientation.y = y_orientation |
---|
623 | else: |
---|
624 | if check_float(self.y_orientation_tcl): |
---|
625 | if detector.orientation.y != float(y_orientation): |
---|
626 | self._notes += "Change y of orientation from " |
---|
627 | self._notes += "%s to %s \n" % (detector.orientation.y, |
---|
628 | y_orientation) |
---|
629 | detector.orientation.y = float(y_orientation) |
---|
630 | else: |
---|
631 | self._notes += "Error: Expected a float for the orientation's " |
---|
632 | self._notes += " y won't changes y orientation from " |
---|
633 | self._notes += "%s to %s" % (detector.orientation.y, |
---|
634 | y_orientation) |
---|
635 | #Change z coordinate |
---|
636 | z_orientation = self.z_orientation_tcl.GetValue().lstrip().rstrip() |
---|
637 | if z_orientation == "" or z_orientation == str(None): |
---|
638 | z_orientation = None |
---|
639 | detector.orientation.z = z_orientation |
---|
640 | else: |
---|
641 | if check_float(self.z_orientation_tcl): |
---|
642 | if detector.orientation.z != float(z_orientation): |
---|
643 | self._notes += "Change z of offset from " |
---|
644 | self._notes += "%s to %s \n" % (detector.orientation.z, |
---|
645 | z_orientation) |
---|
646 | detector.orientation.z = float(z_orientation) |
---|
647 | else: |
---|
648 | self._notes += "Error: Expected a float for the orientation 's" |
---|
649 | self._notes += " x won't changes z orientation from " |
---|
650 | self._notes += "%s to %s" % (detector.orientation.z, |
---|
651 | z_orientation) |
---|
652 | #change the orientation unit |
---|
653 | unit = self.orientation_unit_tcl.GetValue().lstrip().rstrip() |
---|
654 | if detector.orientation_unit != unit: |
---|
655 | self._notes += " Change orientation's unit from " |
---|
656 | self._notes += "%s to %s" % (detector.orientation_unit, unit) |
---|
657 | |
---|
658 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
659 | self.detector_cbox.SetClientData(position, detector) |
---|
660 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
661 | |
---|
662 | def on_change_beam_center(self): |
---|
663 | """ |
---|
664 | Change the detector beam center |
---|
665 | """ |
---|
666 | |
---|
667 | detector, _, position = self.get_current_detector() |
---|
668 | if detector is None: |
---|
669 | return |
---|
670 | #Change x coordinate |
---|
671 | x_beam_center = self.x_beam_center_tcl.GetValue().lstrip().rstrip() |
---|
672 | if x_beam_center == "" or x_beam_center == str(None): |
---|
673 | x_beam_center = None |
---|
674 | detector.beam_center.x = x_beam_center |
---|
675 | else: |
---|
676 | if check_float(self.x_beam_center_tcl): |
---|
677 | if detector.beam_center.x != float(x_beam_center): |
---|
678 | self._notes += "Change x of offset from " |
---|
679 | self._notes += "%s to %s \n" % (detector.beam_center.x, |
---|
680 | x_beam_center) |
---|
681 | detector.beam_center.x = float(x_beam_center) |
---|
682 | else: |
---|
683 | self._notes += "Error: Expected a float for the beam " |
---|
684 | self._notes += "center 's x won't changes x beam center from " |
---|
685 | self._notes += "%s to %s" % (detector.beam_center.x, |
---|
686 | x_beam_center) |
---|
687 | #Change y coordinate |
---|
688 | y_beam_center = self.y_beam_center_tcl.GetValue().lstrip().rstrip() |
---|
689 | if y_beam_center == "" or y_beam_center == str(None): |
---|
690 | y_beam_center = None |
---|
691 | detector.beam_center.y = y_beam_center |
---|
692 | else: |
---|
693 | if check_float(self.y_beam_center_tcl): |
---|
694 | if detector.beam_center.y != float(y_beam_center): |
---|
695 | self._notes += "Change y of beam center from " |
---|
696 | self._notes += "%s to %s \n" % (detector.beam_center.y, |
---|
697 | y_beam_center) |
---|
698 | detector.beam_center.y = float(y_beam_center) |
---|
699 | else: |
---|
700 | self._notes += "Error: Expected a float for the beam " |
---|
701 | self._notes += "center 's y won't changes y beam center from " |
---|
702 | self._notes += "%s to %s" % (detector.beam_center.y, |
---|
703 | y_beam_center) |
---|
704 | #Change z coordinate |
---|
705 | z_beam_center = self.z_beam_center_tcl.GetValue().lstrip().rstrip() |
---|
706 | if z_beam_center == "" or z_beam_center == str(None): |
---|
707 | z_beam_center = None |
---|
708 | detector.beam_center.z = z_beam_center |
---|
709 | else: |
---|
710 | if check_float(self.z_beam_center_tcl): |
---|
711 | if detector.beam_center.z != float(z_beam_center): |
---|
712 | self._notes += "Change z of beam center from " |
---|
713 | self._notes += "%s to %s \n" % (detector.beam_center.z, |
---|
714 | z_beam_center) |
---|
715 | detector.beam_center.z = float(z_beam_center) |
---|
716 | else: |
---|
717 | self._notes += "Error: Expected a float for the offset 's x " |
---|
718 | self._notes += "won't changes z beam center from " |
---|
719 | self._notes += "%s to %s" % (detector.beam_center.z, |
---|
720 | z_beam_center) |
---|
721 | #change the beam center unit |
---|
722 | unit = self.beam_center_unit_tcl.GetValue().lstrip().rstrip() |
---|
723 | if detector.beam_center_unit != unit: |
---|
724 | self._notes += " Change beam center's unit from " |
---|
725 | self._notes += "%s to %s" % (detector.beam_center_unit, unit) |
---|
726 | |
---|
727 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
728 | self.detector_cbox.SetClientData(position, detector) |
---|
729 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
730 | |
---|
731 | def on_change_pixel_size(self): |
---|
732 | """ |
---|
733 | Change the detector pixel size |
---|
734 | """ |
---|
735 | detector, _, position = self.get_current_detector() |
---|
736 | if detector is None: |
---|
737 | return |
---|
738 | #Change x coordinate |
---|
739 | x_pixel_size = self.x_pixel_size_tcl.GetValue().lstrip().rstrip() |
---|
740 | if x_pixel_size == "" or x_pixel_size == str(None): |
---|
741 | x_pixel_size = None |
---|
742 | else: |
---|
743 | if check_float(self.x_pixel_size_tcl): |
---|
744 | if detector.pixel_size.x != float(x_pixel_size) : |
---|
745 | self._notes += "Change x of pixel size from " |
---|
746 | self._notes += "%s to %s \n" % (detector.pixel_size.x, |
---|
747 | x_pixel_size) |
---|
748 | detector.pixel_size.x = float(x_pixel_size) |
---|
749 | else: |
---|
750 | self._notes += "Error: Expected a float for the pixel" |
---|
751 | self._notes += " size 's x won't changes x pixel size from " |
---|
752 | self._notes += "%s to %s" % (detector.pixel_size.x, |
---|
753 | x_pixel_size) |
---|
754 | #Change y coordinate |
---|
755 | y_pixel_size = self.y_pixel_size_tcl.GetValue().lstrip().rstrip() |
---|
756 | if y_pixel_size == "" or y_pixel_size == str(None): |
---|
757 | y_pixel_size = None |
---|
758 | detector.pixel_size.y = y_pixel_size |
---|
759 | else: |
---|
760 | if check_float(self.y_pixel_size_tcl): |
---|
761 | if detector.pixel_size.y != float(y_pixel_size): |
---|
762 | self._notes += "Change y of pixel size from " |
---|
763 | self._notes += "%s to %s \n" % (detector.pixel_size.y, |
---|
764 | y_pixel_size) |
---|
765 | detector.pixel_size.y = float(y_pixel_size) |
---|
766 | else: |
---|
767 | self._notes += "Error: Expected a float for the pixel " |
---|
768 | self._notes += "size's y won't changes y pixel size from " |
---|
769 | self._notes += "%s to %s" % (detector.pixel_size.y, |
---|
770 | y_pixel_size) |
---|
771 | #Change z coordinate |
---|
772 | z_pixel_size = self.z_pixel_size_tcl.GetValue().lstrip().rstrip() |
---|
773 | if z_pixel_size == "" or z_pixel_size == str(None): |
---|
774 | z_pixel_size = None |
---|
775 | detector.pixel_size.z = z_pixel_size |
---|
776 | else: |
---|
777 | if check_float(self.z_pixel_size_tcl): |
---|
778 | if detector.pixel_size.z != float(z_pixel_size): |
---|
779 | self._notes += "Change z of pixel size from " |
---|
780 | self._notes += "%s to %s \n" % (detector.pixel_size.z, |
---|
781 | z_pixel_size) |
---|
782 | detector.pixel_size.z = float(z_pixel_size) |
---|
783 | else: |
---|
784 | self._notes += "Error: Expected a float for the offset 's x " |
---|
785 | self._notes += "won't changes z pixel size from " |
---|
786 | self._notes += "%s to %s" % (detector.pixel_size.z, z_pixel_size) |
---|
787 | #change the beam center unit |
---|
788 | unit = self.pixel_size_unit_tcl.GetValue().lstrip().rstrip() |
---|
789 | if detector.pixel_size_unit != unit: |
---|
790 | self._notes += " Change pixel size's unit from " |
---|
791 | self._notes += "%s to %s" % (detector.pixel_size_unit, unit) |
---|
792 | |
---|
793 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
794 | self.detector_cbox.SetClientData(position, detector) |
---|
795 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
796 | |
---|
797 | def on_change_slit_length(self): |
---|
798 | """ |
---|
799 | Change slit length of the detector |
---|
800 | """ |
---|
801 | detector, _, position = self.get_current_detector() |
---|
802 | if detector is None: |
---|
803 | return |
---|
804 | #Change the distance |
---|
805 | slit_length = self.slit_length_tcl.GetValue().lstrip().rstrip() |
---|
806 | if slit_length == "" or slit_length == str(None): |
---|
807 | slit_length = None |
---|
808 | detector.slit_length = slit_length |
---|
809 | else: |
---|
810 | if check_float(self.slit_length_tcl): |
---|
811 | if detector.slit_length != float(slit_length): |
---|
812 | self._notes += " Change slit length from" |
---|
813 | self._notes += " %s to %s \n" % (detector.slit_length, |
---|
814 | slit_length) |
---|
815 | detector.slit_length = float(slit_length) |
---|
816 | else: |
---|
817 | self._notes += "Error: Expected a float" |
---|
818 | self._notes += " for the slit length won't changes " |
---|
819 | self._notes += "%s to %s" % (detector.slit_length, slit_length) |
---|
820 | #change the distance unit |
---|
821 | unit = self.slit_length_unit_tcl.GetValue().lstrip().rstrip() |
---|
822 | if detector.slit_length_unit != unit: |
---|
823 | self._notes += " Change slit length's unit from " |
---|
824 | self._notes += "%s to %s" % (detector.slit_length_unit_tcl, unit) |
---|
825 | |
---|
826 | self.detector_cbox.SetString(position, str(detector.name)) |
---|
827 | self.detector_cbox.SetClientData(position, detector) |
---|
828 | self.detector_cbox.SetStringSelection(str(detector.name)) |
---|
829 | |
---|
830 | def on_click_cancel(self, event): |
---|
831 | """ |
---|
832 | reset the current detector to its initial values |
---|
833 | """ |
---|
834 | self.reset_detector() |
---|
835 | self.set_values() |
---|
836 | if self.manager is not None: |
---|
837 | self.manager.set_detector(self._detector) |
---|
838 | |
---|
839 | def on_click_apply(self, event): |
---|
840 | """ |
---|
841 | Apply user values to the detector |
---|
842 | """ |
---|
843 | self.on_change_instrument() |
---|
844 | self.on_change_distance() |
---|
845 | self.on_change_instrument() |
---|
846 | self.on_change_beam_center() |
---|
847 | self.on_change_offset() |
---|
848 | self.on_change_orientation() |
---|
849 | self.on_change_pixel_size() |
---|
850 | self.on_change_slit_length() |
---|
851 | for detector in self._detector: |
---|
852 | self.manager.set_detector(self._detector, self._notes) |
---|
853 | |
---|
854 | if __name__ == "__main__": |
---|
855 | app = wx.App() |
---|
856 | test_detector = Detector() |
---|
857 | dlg = DetectorDialog(detector=[test_detector]) |
---|
858 | dlg.ShowModal() |
---|
859 | app.MainLoop() |
---|