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