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