1 | |
---|
2 | import wx |
---|
3 | import sys |
---|
4 | from copy import deepcopy |
---|
5 | from sas.sasgui.guiframe.utils import check_float |
---|
6 | |
---|
7 | _BOX_WIDTH = 60 |
---|
8 | if sys.platform.count("win32") > 0: |
---|
9 | _STATICBOX_WIDTH = 450 |
---|
10 | PANEL_WIDTH = 500 |
---|
11 | PANEL_HEIGHT = 290 |
---|
12 | FONT_VARIANT = 0 |
---|
13 | else: |
---|
14 | _STATICBOX_WIDTH = 480 |
---|
15 | PANEL_WIDTH = 530 |
---|
16 | PANEL_HEIGHT = 320 |
---|
17 | FONT_VARIANT = 1 |
---|
18 | |
---|
19 | class ApertureDialog(wx.Dialog): |
---|
20 | def __init__(self, parent=None, manager=None, aperture=None, *args, **kwds): |
---|
21 | """ |
---|
22 | Dialog allows to enter values for aperture |
---|
23 | """ |
---|
24 | kwds['size'] = (PANEL_WIDTH, PANEL_HEIGHT) |
---|
25 | kwds['title'] = "Aperture Editor" |
---|
26 | wx.Dialog.__init__(self, parent=parent, *args, **kwds) |
---|
27 | self.parent = parent |
---|
28 | self.manager = manager |
---|
29 | self._aperture = aperture |
---|
30 | self._reset_aperture = deepcopy(aperture) |
---|
31 | self._notes = "" |
---|
32 | |
---|
33 | #Attributes for panel |
---|
34 | self.aperture_name_tcl = None |
---|
35 | self.main_sizer = None |
---|
36 | self.box_aperture = None |
---|
37 | self.boxsizer_aperture = None |
---|
38 | self.name_sizer = None |
---|
39 | self.name_sizer = None |
---|
40 | self.size_name_tcl = None |
---|
41 | self.type_sizer = None |
---|
42 | self.distance_sizer = None |
---|
43 | self.size_name_sizer = None |
---|
44 | self.aperture_size_unit_tcl = None |
---|
45 | self.aperture_size_sizer = None |
---|
46 | self.button_sizer = None |
---|
47 | self.aperture_name_tcl = None |
---|
48 | self.type_tcl = None |
---|
49 | self.distance_tcl = None |
---|
50 | self.distance_unit_tcl = None |
---|
51 | self.x_aperture_size_tcl = None |
---|
52 | self.y_aperture_size_tcl = None |
---|
53 | self.z_aperture_size_tcl = None |
---|
54 | self.bt_apply = None |
---|
55 | self.bt_cancel = None |
---|
56 | self.bt_close = None |
---|
57 | |
---|
58 | self._do_layout() |
---|
59 | self.set_values() |
---|
60 | |
---|
61 | def _define_structure(self): |
---|
62 | """ |
---|
63 | define initial sizer |
---|
64 | """ |
---|
65 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
66 | self.box_aperture = wx.StaticBox(self, -1, str("Aperture")) |
---|
67 | self.boxsizer_aperture = wx.StaticBoxSizer(self.box_aperture, wx.VERTICAL) |
---|
68 | self.name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
69 | self.type_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
70 | self.distance_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
71 | self.size_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
72 | self.aperture_size_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
73 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
74 | |
---|
75 | def _layout_name(self): |
---|
76 | """ |
---|
77 | Do the layout for aperture name related widgets |
---|
78 | """ |
---|
79 | #Aperture name [string] |
---|
80 | aperture_name_txt = wx.StaticText(self, -1, 'Aperture Name : ') |
---|
81 | self.aperture_name_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 5, 20), style=0) |
---|
82 | self.name_sizer.AddMany([(aperture_name_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
83 | (self.aperture_name_tcl, 0, wx.EXPAND)]) |
---|
84 | def _layout_type(self): |
---|
85 | """ |
---|
86 | Do the layout for aperture type related widgets |
---|
87 | """ |
---|
88 | #Aperture type [string] |
---|
89 | type_txt = wx.StaticText(self, -1, 'Type: ') |
---|
90 | self.type_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
91 | self.type_sizer.AddMany([(type_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
92 | (self.type_tcl, 0, wx.LEFT, 20)]) |
---|
93 | |
---|
94 | def _layout_distance(self): |
---|
95 | """ |
---|
96 | Do the layout for aperture distance related widgets |
---|
97 | """ |
---|
98 | #Aperture distance [float] |
---|
99 | distance_txt = wx.StaticText(self, -1, 'Distance:') |
---|
100 | self.distance_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
101 | style=0) |
---|
102 | distance_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
103 | self.distance_unit_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
104 | self.distance_sizer.AddMany([(distance_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
105 | (self.distance_tcl, 0, wx.LEFT, 10), |
---|
106 | (distance_unit_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
107 | (self.distance_unit_tcl, 0, wx.EXPAND)]) |
---|
108 | def _layout_size_name(self): |
---|
109 | """ |
---|
110 | Do the layout for size name related widgets |
---|
111 | """ |
---|
112 | # Size name [string] |
---|
113 | size_name_txt = wx.StaticText(self, -1, 'Size Name : ') |
---|
114 | self.size_name_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 5, 20), style=0) |
---|
115 | self.size_name_sizer.AddMany([(size_name_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
116 | (self.size_name_tcl, 0, wx.EXPAND)]) |
---|
117 | |
---|
118 | def _layout_size(self): |
---|
119 | """ |
---|
120 | Do the layout for aperture size related widgets |
---|
121 | """ |
---|
122 | #Aperture size [Vector] |
---|
123 | aperture_size_txt = wx.StaticText(self, -1, 'Size:') |
---|
124 | x_aperture_size_txt = wx.StaticText(self, -1, 'x = ') |
---|
125 | self.x_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
126 | y_aperture_size_txt = wx.StaticText(self, -1, 'y = ') |
---|
127 | self.y_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
128 | z_aperture_size_txt = wx.StaticText(self, -1, 'z = ') |
---|
129 | self.z_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
130 | aperture_size_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
131 | self.aperture_size_unit_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
132 | self.aperture_size_sizer.AddMany([(aperture_size_txt, 0, wx.LEFT | wx.RIGHT, 10), |
---|
133 | (x_aperture_size_txt, 0, wx.LEFT, 17), |
---|
134 | (self.x_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
135 | (y_aperture_size_txt, 0, wx.EXPAND), |
---|
136 | (self.y_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
137 | (z_aperture_size_txt, 0, wx.EXPAND), |
---|
138 | (self.z_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
139 | (aperture_size_unit_txt, 0, wx.EXPAND), |
---|
140 | (self.aperture_size_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
141 | |
---|
142 | def _layout_button(self): |
---|
143 | """ |
---|
144 | Do the layout for the button widgets |
---|
145 | """ |
---|
146 | self.bt_apply = wx.Button(self, -1, 'Apply') |
---|
147 | self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) |
---|
148 | self.bt_apply.SetToolTipString("Apply current changes to aperture.") |
---|
149 | self.bt_cancel = wx.Button(self, -1, 'Cancel') |
---|
150 | self.bt_cancel.SetToolTipString("Cancel current changes.") |
---|
151 | self.bt_cancel.Bind(wx.EVT_BUTTON, self.on_click_cancel) |
---|
152 | self.bt_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
153 | self.bt_close.SetToolTipString("Close window.") |
---|
154 | self.button_sizer.AddMany([(self.bt_apply, 0, wx.LEFT, 200), |
---|
155 | (self.bt_cancel, 0, wx.LEFT, 10), |
---|
156 | (self.bt_close, 0, wx.LEFT, 10)]) |
---|
157 | |
---|
158 | def _do_layout(self):#, data=None): |
---|
159 | """ |
---|
160 | Draw the current panel |
---|
161 | """ |
---|
162 | self._define_structure() |
---|
163 | self._layout_name() |
---|
164 | self._layout_type() |
---|
165 | self._layout_distance() |
---|
166 | self._layout_size_name() |
---|
167 | self._layout_size() |
---|
168 | self._layout_button() |
---|
169 | self.boxsizer_aperture.AddMany([(self.name_sizer, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
170 | (self.type_sizer, 0, |
---|
171 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
172 | (self.distance_sizer, 0, |
---|
173 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
174 | (self.size_name_sizer, 0, |
---|
175 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5), |
---|
176 | (self.aperture_size_sizer, 0, |
---|
177 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5)]) |
---|
178 | self.main_sizer.AddMany([(self.boxsizer_aperture, 0, wx.ALL, 10), |
---|
179 | (self.button_sizer, 0, |
---|
180 | wx.EXPAND | wx.TOP | wx.BOTTOM, 5)]) |
---|
181 | self.SetSizer(self.main_sizer) |
---|
182 | self.SetAutoLayout(True) |
---|
183 | |
---|
184 | def set_manager(self, manager): |
---|
185 | """ |
---|
186 | Set manager of this window |
---|
187 | """ |
---|
188 | self.manager = manager |
---|
189 | |
---|
190 | def reset_aperture(self): |
---|
191 | """ |
---|
192 | put the default value of the detector back to the current aperture |
---|
193 | """ |
---|
194 | self._aperture.name = self._reset_aperture.name |
---|
195 | self._aperture.type = self._reset_aperture.type |
---|
196 | self._aperture.size_name = self._reset_aperture.size_name |
---|
197 | self._aperture.size.x = self._reset_aperture.size.x |
---|
198 | self._aperture.size.y = self._reset_aperture.size.y |
---|
199 | self._aperture.size.z = self._reset_aperture.size.z |
---|
200 | self._aperture.size_unit = self._reset_aperture.size_unit |
---|
201 | self._aperture.distance = self._reset_aperture.distance |
---|
202 | self._aperture.distance_unit = self._reset_aperture.distance_unit |
---|
203 | |
---|
204 | def set_values(self): |
---|
205 | """ |
---|
206 | take the aperture values of the current data and display them |
---|
207 | through the panel |
---|
208 | """ |
---|
209 | aperture = self._aperture |
---|
210 | #Name |
---|
211 | self.aperture_name_tcl.SetValue(str(aperture.name)) |
---|
212 | #Type |
---|
213 | self.type_tcl.SetValue(str(aperture.type)) |
---|
214 | #distance |
---|
215 | self.distance_tcl.SetValue(str(aperture.distance)) |
---|
216 | #distance unit |
---|
217 | self.distance_unit_tcl.SetValue(str(aperture.distance_unit)) |
---|
218 | #Size name |
---|
219 | self.size_name_tcl.SetValue(str(aperture.size_name)) |
---|
220 | #Aperture size as a vector |
---|
221 | x, y, z = aperture.size.x, aperture.size.y, aperture.size.z |
---|
222 | self.x_aperture_size_tcl.SetValue(str(x)) |
---|
223 | self.y_aperture_size_tcl.SetValue(str(y)) |
---|
224 | self.z_aperture_size_tcl.SetValue(str(z)) |
---|
225 | self.aperture_size_unit_tcl.SetValue(str(aperture.size_unit)) |
---|
226 | |
---|
227 | def get_aperture(self): |
---|
228 | """ |
---|
229 | return the current aperture |
---|
230 | """ |
---|
231 | return self._aperture |
---|
232 | |
---|
233 | def get_notes(self): |
---|
234 | """ |
---|
235 | return notes |
---|
236 | """ |
---|
237 | return self._notes |
---|
238 | |
---|
239 | def on_change_name(self): |
---|
240 | """ |
---|
241 | Change name |
---|
242 | """ |
---|
243 | #Change the name of the aperture |
---|
244 | name = self.aperture_name_tcl.GetValue().lstrip().rstrip() |
---|
245 | if name == "": |
---|
246 | name = str(None) |
---|
247 | if self._aperture.name != name: |
---|
248 | self._notes += "Change sample 's " |
---|
249 | self._notes += "name from %s to %s \n" % (self._aperture.name, name) |
---|
250 | self._aperture.name = name |
---|
251 | |
---|
252 | def on_change_type(self): |
---|
253 | """ |
---|
254 | Change aperture type |
---|
255 | """ |
---|
256 | #Change type |
---|
257 | type_value = self.type_tcl.GetValue().lstrip().rstrip() |
---|
258 | self._aperture.type = type_value |
---|
259 | self._notes += " Change type from" |
---|
260 | self._notes += " %s to %s \n" % (self._aperture.type, type_value) |
---|
261 | |
---|
262 | def on_change_distance(self): |
---|
263 | """ |
---|
264 | Change distance of the aperture |
---|
265 | """ |
---|
266 | #Change distance |
---|
267 | distance = self.distance_tcl.GetValue().lstrip().rstrip() |
---|
268 | if distance == "" or distance == str(None): |
---|
269 | distance = None |
---|
270 | self._aperture.distance = distance |
---|
271 | else: |
---|
272 | if check_float(self.distance_tcl): |
---|
273 | if self._aperture.distance != float(distance): |
---|
274 | self._notes += "Change distance from " |
---|
275 | self._notes += "%s to %s \n" % (self._aperture.distance, distance) |
---|
276 | self._aperture.distance = float(distance) |
---|
277 | else: |
---|
278 | self._notes += "Error: Expected a float for distance " |
---|
279 | self._notes += "won't changes distance from " |
---|
280 | self._notes += "%s to %s" % (self._aperture.distance, distance) |
---|
281 | #change the distance unit |
---|
282 | unit = self.distance_unit_tcl.GetValue().lstrip().rstrip() |
---|
283 | if self._aperture.distance_unit != unit: |
---|
284 | self._notes += " Change distance 's unit from " |
---|
285 | self._notes += "%s to %s" % (self._aperture.distance_unit, unit) |
---|
286 | |
---|
287 | def on_change_size_name(self): |
---|
288 | """ |
---|
289 | Change the size's name |
---|
290 | """ |
---|
291 | #Change size name |
---|
292 | size_name = self.size_name_tcl.GetValue().lstrip().rstrip() |
---|
293 | self._aperture.size_name = size_name |
---|
294 | self._notes += " Change size name from" |
---|
295 | self._notes += " %s to %s \n" % (self._aperture.size_name, size_name) |
---|
296 | |
---|
297 | def on_change_size(self): |
---|
298 | """ |
---|
299 | Change aperture size |
---|
300 | """ |
---|
301 | #Change x coordinate |
---|
302 | x_aperture_size = self.x_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
303 | if x_aperture_size == "" or x_aperture_size == str(None): |
---|
304 | x_aperture_size = None |
---|
305 | else: |
---|
306 | if check_float(self.x_aperture_size_tcl): |
---|
307 | if self._aperture.size.x != float(x_aperture_size): |
---|
308 | self._notes += "Change x of aperture size from " |
---|
309 | self._notes += "%s to %s \n" % (self._aperture.size.x, x_aperture_size) |
---|
310 | self._aperture.aperture_size.x = float(x_aperture_size) |
---|
311 | else: |
---|
312 | self._notes += "Error: Expected a" |
---|
313 | self._notes += " float for the aperture size 's x " |
---|
314 | self._notes += "won't changes x aperture size from " |
---|
315 | self._notes += "%s to %s" % (self._aperture.size.x, x_aperture_size) |
---|
316 | #Change y coordinate |
---|
317 | y_aperture_size = self.y_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
318 | if y_aperture_size == "" or y_aperture_size == str(None): |
---|
319 | y_aperture_size = None |
---|
320 | self._aperture.size.y = y_aperture_size |
---|
321 | else: |
---|
322 | if check_float(self.y_aperture_size_tcl): |
---|
323 | if self._aperture.size.y != float(y_aperture_size): |
---|
324 | self._notes += "Change y of aperture size from " |
---|
325 | self._notes += "%s to %s \n" % (self._aperture.size.y, y_aperture_size) |
---|
326 | self._aperture.size.y = float(y_aperture_size) |
---|
327 | else: |
---|
328 | self._notes += "Error: Expected a float for the" |
---|
329 | self._notes += " aperture size's y " |
---|
330 | self._notes += "won't changes y aperture size from " |
---|
331 | self._notes += "%s to %s" % (self._aperture.size.y, y_aperture_size) |
---|
332 | #Change z coordinate |
---|
333 | z_aperture_size = self.z_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
334 | if z_aperture_size == "" or z_aperture_size == str(None): |
---|
335 | z_aperture_size = None |
---|
336 | self._aperture.size.z = z_aperture_size |
---|
337 | else: |
---|
338 | if check_float(self.z_aperture_size_tcl): |
---|
339 | if self._aperture.size.z != float(z_aperture_size): |
---|
340 | self._notes += "Change z of aperture size from " |
---|
341 | self._notes += "%s to %s \n" % (self._aperture.size.z, z_aperture_size) |
---|
342 | self._aperture.size.z = float(z_aperture_size) |
---|
343 | else: |
---|
344 | self._notes += "Error: Expected a float for the offset 's x " |
---|
345 | self._notes += "won't changes z aperture size from " |
---|
346 | self._notes += "%s to %s" % (self._aperture.size.z, z_aperture_size) |
---|
347 | #change the aperture center unit |
---|
348 | unit = self.aperture_size_unit_tcl.GetValue().lstrip().rstrip() |
---|
349 | if self._aperture.size_unit != unit: |
---|
350 | self._notes += " Change aperture size's unit from " |
---|
351 | self._notes += "%s to %s" % (self._aperture.size_unit, unit) |
---|
352 | self._aperture.size_unit = unit |
---|
353 | |
---|
354 | def on_click_apply(self, event): |
---|
355 | """ |
---|
356 | Apply user values to the aperture |
---|
357 | """ |
---|
358 | self.on_change_name() |
---|
359 | self.on_change_type() |
---|
360 | self.on_change_distance() |
---|
361 | self.on_change_size_name() |
---|
362 | self.on_change_size() |
---|
363 | self.set_values() |
---|
364 | if self.manager is not None: |
---|
365 | self.manager.set_aperture(self._aperture) |
---|
366 | if event is not None: |
---|
367 | event.Skip() |
---|
368 | |
---|
369 | def on_click_cancel(self, event): |
---|
370 | """ |
---|
371 | reset the current aperture to its initial values |
---|
372 | """ |
---|
373 | self.reset_aperture() |
---|
374 | self.set_values() |
---|
375 | if self.manager is not None: |
---|
376 | self.manager.set_aperture(self._aperture) |
---|
377 | if event is not None: |
---|
378 | event.Skip() |
---|