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