1 | """ |
---|
2 | GUI for the data operation |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | import time |
---|
7 | import numpy |
---|
8 | from sans.dataloader.data_info import Data2D |
---|
9 | from sans.dataloader.data_info import Data1D |
---|
10 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
11 | from danse.common.plottools.plottables import Graph |
---|
12 | from danse.common.plottools.canvas import FigureCanvas |
---|
13 | from matplotlib.font_manager import FontProperties |
---|
14 | from matplotlib.figure import Figure |
---|
15 | from sans.guiframe.events import StatusEvent |
---|
16 | |
---|
17 | #Control panel width |
---|
18 | if sys.platform.count("win32") > 0: |
---|
19 | PANEL_WIDTH = 790 |
---|
20 | PANEL_HEIGTH = 370 |
---|
21 | FONT_VARIANT = 0 |
---|
22 | _BOX_WIDTH = 200 |
---|
23 | ON_MAC = False |
---|
24 | else: |
---|
25 | _BOX_WIDTH = 230 |
---|
26 | PANEL_WIDTH = 900 |
---|
27 | PANEL_HEIGTH = 430 |
---|
28 | FONT_VARIANT = 1 |
---|
29 | ON_MAC = True |
---|
30 | |
---|
31 | class DataOperPanel(wx.ScrolledWindow): |
---|
32 | """ |
---|
33 | :param data: when not empty the class can |
---|
34 | same information into a data object |
---|
35 | and post event containing the changed data object to some other frame |
---|
36 | """ |
---|
37 | def __init__(self, parent, *args, **kwds): |
---|
38 | kwds['name'] = "Data Operation" |
---|
39 | kwds["size"] = (PANEL_WIDTH, PANEL_HEIGTH) |
---|
40 | wx.ScrolledWindow.__init__(self, parent, *args, **kwds) |
---|
41 | self.parent = parent |
---|
42 | #sizers etc. |
---|
43 | self.main_sizer = None |
---|
44 | self.name_sizer = None |
---|
45 | self.button_sizer = None |
---|
46 | self.data_namectr = None |
---|
47 | self.numberctr = None |
---|
48 | self.data1_cbox = None |
---|
49 | self.operator_cbox = None |
---|
50 | self.data2_cbox = None |
---|
51 | self.data_title_tcl = None |
---|
52 | self.out_pic = None |
---|
53 | self.equal_pic = None |
---|
54 | self.data1_pic = None |
---|
55 | self.operator_pic = None |
---|
56 | self.data2_pic = None |
---|
57 | self.output = None |
---|
58 | self._notes = None |
---|
59 | #text grayed color |
---|
60 | self.color = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND) |
---|
61 | #data |
---|
62 | self._data = self.get_datalist() |
---|
63 | self._do_layout() |
---|
64 | self.fill_data_combox() |
---|
65 | self.fill_oprator_combox() |
---|
66 | self.Bind(wx.EVT_PAINT, self.set_panel_on_focus) |
---|
67 | |
---|
68 | def _define_structure(self): |
---|
69 | """ |
---|
70 | define initial sizer |
---|
71 | """ |
---|
72 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
73 | title = "Data Operation " |
---|
74 | title += "[ + (add); - (subtract); " |
---|
75 | title += "* (multiply); / (divide); " |
---|
76 | title += "| (append) ]" |
---|
77 | name_box = wx.StaticBox(self, -1, title) |
---|
78 | self.name_sizer = wx.StaticBoxSizer(name_box, wx.HORIZONTAL) |
---|
79 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
80 | |
---|
81 | def _layout_name(self): |
---|
82 | """ |
---|
83 | Do the layout for data name related widgets |
---|
84 | """ |
---|
85 | new_data_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
86 | equal_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
87 | old_data1_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
88 | operator_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
89 | old_data2_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
90 | data2_hori_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
91 | data_name = wx.StaticText(self, -1, 'Output Data Name') |
---|
92 | equal_name = wx.StaticText(self, -1, ' =', size=(50, 25)) |
---|
93 | data1_name = wx.StaticText(self, -1, 'Data1') |
---|
94 | operator_name = wx.StaticText(self, -1, 'Operator') |
---|
95 | data2_name = wx.StaticText(self, -1, 'Data2 (or Number)') |
---|
96 | self.data_namectr = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 25), style=wx.TE_PROCESS_ENTER) |
---|
97 | self.data_namectr.SetToolTipString("Hit 'Enter' key after typing.") |
---|
98 | self.data_namectr.SetValue(str('MyNewDataName')) |
---|
99 | self.numberctr = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/3, 25), style=wx.TE_PROCESS_ENTER) |
---|
100 | self.numberctr.SetToolTipString("Hit 'Enter' key after typing.") |
---|
101 | self.numberctr.SetValue(str(1.0)) |
---|
102 | self.data1_cbox = wx.ComboBox(self, -1, size=(_BOX_WIDTH, 25), |
---|
103 | style=wx.CB_READONLY) |
---|
104 | self.operator_cbox = wx.ComboBox(self, -1, size=(70, 25), |
---|
105 | style=wx.CB_READONLY) |
---|
106 | operation_tip = "Add: +, Subtract: -, " |
---|
107 | operation_tip += "Multiply: *, Divide: /, " |
---|
108 | operation_tip += "Append(Combine): | " |
---|
109 | self.operator_cbox.SetToolTipString(operation_tip) |
---|
110 | self.data2_cbox = wx.ComboBox(self, -1, size=(_BOX_WIDTH*2/3, 25), |
---|
111 | style=wx.CB_READONLY) |
---|
112 | |
---|
113 | self.out_pic = SmallPanel(self, -1, True, |
---|
114 | size=(_BOX_WIDTH, _BOX_WIDTH), |
---|
115 | style=wx.NO_BORDER) |
---|
116 | self.equal_pic = SmallPanel(self, -1, True, '=', |
---|
117 | size=(50, _BOX_WIDTH), |
---|
118 | style=wx.NO_BORDER) |
---|
119 | self.data1_pic = SmallPanel(self, -1, True, |
---|
120 | size=(_BOX_WIDTH, _BOX_WIDTH), |
---|
121 | style=wx.NO_BORDER) |
---|
122 | self.operator_pic = SmallPanel(self, -1, True, '+', |
---|
123 | size=(70, _BOX_WIDTH), |
---|
124 | style=wx.NO_BORDER) |
---|
125 | self.data2_pic = SmallPanel(self, -1, True, |
---|
126 | size=(_BOX_WIDTH, _BOX_WIDTH), |
---|
127 | style=wx.NO_BORDER) |
---|
128 | for ax in self.equal_pic.axes: |
---|
129 | ax.set_frame_on(False) |
---|
130 | for ax in self.operator_pic.axes: |
---|
131 | ax.set_frame_on(False) |
---|
132 | |
---|
133 | new_data_sizer.AddMany([(data_name, 0, wx.LEFT, 3), |
---|
134 | (self.data_namectr, 0, wx.LEFT, 3), |
---|
135 | (self.out_pic, 0, wx.LEFT, 3)]) |
---|
136 | equal_sizer.AddMany([(13, 13), (equal_name, 0, wx.LEFT, 3), |
---|
137 | (self.equal_pic, 0, wx.LEFT, 3)]) |
---|
138 | old_data1_sizer.AddMany([(data1_name, 0, wx.LEFT, 3), |
---|
139 | (self.data1_cbox, 0, wx.LEFT, 3), |
---|
140 | (self.data1_pic, 0, wx.LEFT, 3)]) |
---|
141 | operator_sizer.AddMany([(operator_name, 0, wx.LEFT, 3), |
---|
142 | (self.operator_cbox, 0, wx.LEFT, 3), |
---|
143 | (self.operator_pic, 0, wx.LEFT, 3)]) |
---|
144 | data2_hori_sizer.AddMany([(self.data2_cbox, 0, wx.LEFT, 0), |
---|
145 | (self.numberctr, 0, wx.RIGHT, 0)]) |
---|
146 | old_data2_sizer.AddMany([(data2_name, 0, wx.LEFT, 3), |
---|
147 | (data2_hori_sizer, 0, wx.LEFT, 3), |
---|
148 | (self.data2_pic, 0, wx.LEFT, 3)]) |
---|
149 | self.name_sizer.AddMany([(new_data_sizer, 0, wx.LEFT|wx.TOP, 5), |
---|
150 | (equal_sizer, 0, wx.TOP, 5), |
---|
151 | (old_data1_sizer, 0, wx.TOP, 5), |
---|
152 | (operator_sizer, 0, wx.TOP, 5), |
---|
153 | (old_data2_sizer, 0, wx.TOP, 5)]) |
---|
154 | self.data2_cbox.Show(True) |
---|
155 | |
---|
156 | self._show_numctrl(self.numberctr, False) |
---|
157 | |
---|
158 | wx.EVT_TEXT_ENTER(self.data_namectr, -1, self.on_name) |
---|
159 | wx.EVT_TEXT_ENTER(self.numberctr, -1, self.on_number) |
---|
160 | wx.EVT_COMBOBOX(self.data1_cbox, -1, self.on_select_data1) |
---|
161 | wx.EVT_COMBOBOX(self.operator_cbox, -1, self.on_select_operator) |
---|
162 | wx.EVT_COMBOBOX(self.data2_cbox, -1, self.on_select_data2) |
---|
163 | |
---|
164 | def _show_numctrl(self, ctrl, enable=True): |
---|
165 | """ |
---|
166 | Show/Hide on Win |
---|
167 | Enable/Disable on MAC |
---|
168 | """ |
---|
169 | if ON_MAC: |
---|
170 | ctrl.Enable(enable) |
---|
171 | children = ctrl.GetChildren() |
---|
172 | if len(children) > 0: |
---|
173 | ctrl.GetChildren()[0].SetBackGroundColour(self.color) |
---|
174 | if enable: |
---|
175 | wx.EVT_TEXT_ENTER(self.numberctr, -1, self.on_number) |
---|
176 | else: |
---|
177 | if not ctrl.IsEnabled(): |
---|
178 | ctrl.Enable(True) |
---|
179 | ctrl.Show(enable) |
---|
180 | |
---|
181 | def on_name(self, event=None): |
---|
182 | """ |
---|
183 | On data name typing |
---|
184 | """ |
---|
185 | if event != None: |
---|
186 | event.Skip() |
---|
187 | item = event.GetEventObject() |
---|
188 | if item.IsEnabled(): |
---|
189 | self._set_textctrl_color(item, 'white') |
---|
190 | else: |
---|
191 | self._set_textctrl_color(item, self.color) |
---|
192 | text = item.GetValue().strip() |
---|
193 | self._check_newname(text) |
---|
194 | |
---|
195 | def _check_newname(self, name=None): |
---|
196 | """ |
---|
197 | Check name ctr strings |
---|
198 | """ |
---|
199 | self.send_warnings('') |
---|
200 | msg = '' |
---|
201 | if name == None: |
---|
202 | text = self.data_namectr.GetValue().strip() |
---|
203 | else: |
---|
204 | text = name |
---|
205 | state_list = self.get_datalist().values() |
---|
206 | name_list = [] |
---|
207 | for state in state_list: |
---|
208 | if state.data == None: |
---|
209 | theory_list = state.get_theory() |
---|
210 | theory, _ = theory_list.values()[0] |
---|
211 | d_name = str(theory.name) |
---|
212 | else: |
---|
213 | d_name = str(state.data.name) |
---|
214 | name_list.append(d_name) |
---|
215 | if text in name_list: |
---|
216 | self._set_textctrl_color(self.data_namectr, 'pink') |
---|
217 | msg = "DataOperation: The name already exists." |
---|
218 | if len(text) == 0: |
---|
219 | self._set_textctrl_color(self.data_namectr, 'pink') |
---|
220 | msg = "DataOperation: Type the data name first." |
---|
221 | if self._notes: |
---|
222 | self.send_warnings(msg, 'error') |
---|
223 | self.name_sizer.Layout() |
---|
224 | self.Refresh() |
---|
225 | |
---|
226 | def _set_textctrl_color(self, ctrl, color): |
---|
227 | """ |
---|
228 | Set TextCtrl color |
---|
229 | """ |
---|
230 | if ON_MAC: |
---|
231 | children = ctrl.GetChildren() |
---|
232 | if len(children) > 0: |
---|
233 | children[0].SetBackgroundColour(color) |
---|
234 | else: |
---|
235 | ctrl.SetBackgroundColour(color) |
---|
236 | self.name_sizer.Layout() |
---|
237 | |
---|
238 | def on_number(self, event=None): |
---|
239 | """ |
---|
240 | On selecting Number for Data2 |
---|
241 | """ |
---|
242 | self.send_warnings('') |
---|
243 | item = event.GetEventObject() |
---|
244 | text = item.GetValue().strip() |
---|
245 | if self.numberctr.IsShown(): |
---|
246 | if self.numberctr.IsEnabled(): |
---|
247 | self._set_textctrl_color(self.numberctr, 'white') |
---|
248 | try: |
---|
249 | val = float(text) |
---|
250 | pos = self.data2_cbox.GetCurrentSelection() |
---|
251 | self.data2_cbox.SetClientData(pos, val) |
---|
252 | except: |
---|
253 | self._set_textctrl_color(self.numberctr, 'pink') |
---|
254 | msg = "DataOperation: Number requires a float number." |
---|
255 | self.send_warnings(msg, 'error') |
---|
256 | return |
---|
257 | else: |
---|
258 | self._set_textctrl_color(self.numberctr, self.color) |
---|
259 | |
---|
260 | self.put_text_pic(self.data2_pic, content=str(val)) |
---|
261 | self.check_data_inputs() |
---|
262 | if self.output != None: |
---|
263 | self.output.name = str(self.data_namectr.GetValue()) |
---|
264 | self.draw_output(self.output) |
---|
265 | self.Refresh() |
---|
266 | |
---|
267 | def on_select_data1(self, event=None): |
---|
268 | """ |
---|
269 | On select data1 |
---|
270 | """ |
---|
271 | self.send_warnings('') |
---|
272 | item = event.GetEventObject() |
---|
273 | pos = item.GetCurrentSelection() |
---|
274 | data = item.GetClientData(pos) |
---|
275 | if data == None: |
---|
276 | content = "?" |
---|
277 | self.put_text_pic(self.data1_pic, content) |
---|
278 | else: |
---|
279 | self.data1_pic.add_image(data) |
---|
280 | self.check_data_inputs() |
---|
281 | if self.output != None: |
---|
282 | self.output.name = str(self.data_namectr.GetValue()) |
---|
283 | self.draw_output(self.output) |
---|
284 | |
---|
285 | def on_select_operator(self, event=None): |
---|
286 | """ |
---|
287 | On Select an Operator |
---|
288 | """ |
---|
289 | self.send_warnings('') |
---|
290 | item = event.GetEventObject() |
---|
291 | text = item.GetValue().strip() |
---|
292 | self.put_text_pic(self.operator_pic, content=text) |
---|
293 | self.check_data_inputs() |
---|
294 | if self.output != None: |
---|
295 | self.output.name = str(self.data_namectr.GetValue()) |
---|
296 | self.draw_output(self.output) |
---|
297 | |
---|
298 | def on_select_data2(self, event=None): |
---|
299 | """ |
---|
300 | On Selecting Data2 |
---|
301 | """ |
---|
302 | self.send_warnings('') |
---|
303 | item = event.GetEventObject() |
---|
304 | text = item.GetValue().strip().lower() |
---|
305 | self._show_numctrl(self.numberctr, text=='number') |
---|
306 | pos = item.GetCurrentSelection() |
---|
307 | data = item.GetClientData(pos) |
---|
308 | content = "?" |
---|
309 | if not (self.numberctr.IsShown() and self.numberctr.IsEnabled()): |
---|
310 | if data == None: |
---|
311 | content = "?" |
---|
312 | self.put_text_pic(self.data2_pic, content) |
---|
313 | else: |
---|
314 | self.data2_pic.add_image(data) |
---|
315 | self.check_data_inputs() |
---|
316 | else: |
---|
317 | content = str(self.numberctr.GetValue().strip()) |
---|
318 | try: |
---|
319 | content = float(content) |
---|
320 | data = content |
---|
321 | except: |
---|
322 | self._set_textctrl_color(self.numberctr, 'pink') |
---|
323 | content = "?" |
---|
324 | data = None |
---|
325 | item.SetClientData(pos, data) |
---|
326 | if data != None: |
---|
327 | self.check_data_inputs() |
---|
328 | |
---|
329 | self.put_text_pic(self.data2_pic, content) |
---|
330 | |
---|
331 | if self.output != None: |
---|
332 | self.output.name = str(self.data_namectr.GetValue()) |
---|
333 | self.draw_output(self.output) |
---|
334 | |
---|
335 | def put_text_pic(self, pic=None, content=''): |
---|
336 | """ |
---|
337 | Put text to the pic |
---|
338 | """ |
---|
339 | pic.set_content(content) |
---|
340 | pic.add_text() |
---|
341 | pic.draw() |
---|
342 | |
---|
343 | def check_data_inputs(self): |
---|
344 | """ |
---|
345 | Check data1 and data2 whether or not they are ready for operation |
---|
346 | """ |
---|
347 | self._set_textctrl_color(self.data1_cbox, 'white') |
---|
348 | self._set_textctrl_color(self.data2_cbox, 'white') |
---|
349 | flag = False |
---|
350 | pos1 = self.data1_cbox.GetCurrentSelection() |
---|
351 | data1 = self.data1_cbox.GetClientData(pos1) |
---|
352 | if data1 == None: |
---|
353 | self.output = None |
---|
354 | return flag |
---|
355 | pos2 = self.data2_cbox.GetCurrentSelection() |
---|
356 | data2 = self.data2_cbox.GetClientData(pos2) |
---|
357 | |
---|
358 | if data2 == None: |
---|
359 | self.output = None |
---|
360 | return flag |
---|
361 | if self.numberctr.IsShown(): |
---|
362 | if self.numberctr.IsEnabled(): |
---|
363 | self._set_textctrl_color(self.numberctr, 'white') |
---|
364 | try: |
---|
365 | float(data2) |
---|
366 | if self.operator_cbox.GetValue().strip() == '|': |
---|
367 | msg = "DataOperation: This operation can not accept " |
---|
368 | msg += "a float number." |
---|
369 | self.send_warnings(msg, 'error') |
---|
370 | self._set_textctrl_color(self.numberctr, 'pink') |
---|
371 | self.output = None |
---|
372 | return flag |
---|
373 | except: |
---|
374 | msg = "DataOperation: Number requires a float number." |
---|
375 | self.send_warnings(msg, 'error') |
---|
376 | self._set_textctrl_color(self.numberctr, 'pink') |
---|
377 | self.output = None |
---|
378 | return flag |
---|
379 | else: |
---|
380 | self._set_textctrl_color(self.numberctr, self.color ) |
---|
381 | elif data1.__class__.__name__ != data2.__class__.__name__: |
---|
382 | self._set_textctrl_color(self.data1_cbox, 'pink') |
---|
383 | self._set_textctrl_color(self.data2_cbox, 'pink') |
---|
384 | msg = "DataOperation: Data types must be same." |
---|
385 | self.send_warnings(msg, 'error') |
---|
386 | self.output = None |
---|
387 | return flag |
---|
388 | try: |
---|
389 | self.output = self.make_data_out(data1, data2) |
---|
390 | except: |
---|
391 | self._check_newname() |
---|
392 | self._set_textctrl_color(self.data1_cbox, 'pink') |
---|
393 | self._set_textctrl_color(self.data2_cbox, 'pink') |
---|
394 | msg = "DataOperation: Data types must be same." |
---|
395 | self.send_warnings(msg, 'error') |
---|
396 | self.output = None |
---|
397 | return flag |
---|
398 | return True |
---|
399 | |
---|
400 | def make_data_out(self, data1, data2): |
---|
401 | """ |
---|
402 | Make a temp. data output set |
---|
403 | """ |
---|
404 | output = None |
---|
405 | pos = self.operator_cbox.GetCurrentSelection() |
---|
406 | operator = self.operator_cbox.GetClientData(pos) |
---|
407 | try: |
---|
408 | exec "output = data1 %s data2"% operator |
---|
409 | except: |
---|
410 | raise |
---|
411 | return output |
---|
412 | |
---|
413 | |
---|
414 | def draw_output(self, output): |
---|
415 | """ |
---|
416 | Draw output data(temp) |
---|
417 | """ |
---|
418 | out = self.out_pic |
---|
419 | if output == None: |
---|
420 | content = "?" |
---|
421 | self.put_text_pic(out, content) |
---|
422 | else: |
---|
423 | out.add_image(output) |
---|
424 | wx.CallAfter(self.name_sizer.Layout) |
---|
425 | self.Layout() |
---|
426 | self.Refresh() |
---|
427 | |
---|
428 | def _layout_button(self): |
---|
429 | """ |
---|
430 | Do the layout for the button widgets |
---|
431 | """ |
---|
432 | self.bt_apply = wx.Button(self, -1, "Apply", size=(_BOX_WIDTH/2, -1)) |
---|
433 | app_tip = "Generate the Data and send to Data Explorer." |
---|
434 | self.bt_apply.SetToolTipString(app_tip) |
---|
435 | self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) |
---|
436 | |
---|
437 | self.bt_close = wx.Button(self, -1, 'Close', size=(_BOX_WIDTH/2, -1)) |
---|
438 | self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
439 | self.bt_close.SetToolTipString("Close this panel.") |
---|
440 | |
---|
441 | self.button_sizer.AddMany([(PANEL_WIDTH/2, 25), |
---|
442 | (self.bt_apply, 0, wx.RIGHT, 10), |
---|
443 | (self.bt_close, 0, wx.RIGHT, 10)]) |
---|
444 | |
---|
445 | def _do_layout(self): |
---|
446 | """ |
---|
447 | Draw the current panel |
---|
448 | """ |
---|
449 | self._define_structure() |
---|
450 | self._layout_name() |
---|
451 | self._layout_button() |
---|
452 | self.main_sizer.AddMany([(self.name_sizer, 0, wx.EXPAND|wx.ALL, 10), |
---|
453 | (self.button_sizer, 0, |
---|
454 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
455 | self.SetSizer(self.main_sizer) |
---|
456 | self.SetScrollbars(20, 20, 25, 65) |
---|
457 | self.SetAutoLayout(True) |
---|
458 | |
---|
459 | def set_panel_on_focus(self, event): |
---|
460 | """ |
---|
461 | On Focus at this window |
---|
462 | """ |
---|
463 | if event != None: |
---|
464 | event.Skip() |
---|
465 | self._data = self.get_datalist() |
---|
466 | children = self.GetChildren() |
---|
467 | # update the list only when it is on the top |
---|
468 | if self.FindFocus() in children: |
---|
469 | self.fill_data_combox() |
---|
470 | |
---|
471 | def fill_oprator_combox(self): |
---|
472 | """ |
---|
473 | fill the current combobox with the operator |
---|
474 | """ |
---|
475 | operator_list = [' +', ' -', ' *', " /", " |"] |
---|
476 | for oper in operator_list: |
---|
477 | pos = self.operator_cbox.Append(str(oper)) |
---|
478 | self.operator_cbox.SetClientData(pos, str(oper.strip())) |
---|
479 | self.operator_cbox.SetSelection(0) |
---|
480 | |
---|
481 | |
---|
482 | def fill_data_combox(self): |
---|
483 | """ |
---|
484 | fill the current combobox with the available data |
---|
485 | """ |
---|
486 | pos_pre1 = self.data1_cbox.GetCurrentSelection() |
---|
487 | pos_pre2 = self.data2_cbox.GetCurrentSelection() |
---|
488 | current1 = self.data1_cbox.GetLabel() |
---|
489 | current2 = self.data2_cbox.GetLabel() |
---|
490 | if pos_pre1 < 0: |
---|
491 | pos_pre1 = 0 |
---|
492 | if pos_pre2 < 0: |
---|
493 | pos_pre2 = 0 |
---|
494 | self.data1_cbox.Clear() |
---|
495 | self.data2_cbox.Clear() |
---|
496 | |
---|
497 | if not self._data: |
---|
498 | pos = self.data1_cbox.Append('No Data Available') |
---|
499 | self.data1_cbox.SetSelection(pos) |
---|
500 | self.data1_cbox.SetClientData(pos, None) |
---|
501 | pos2 = self.data2_cbox.Append('No Data Available') |
---|
502 | self.data2_cbox.SetSelection(pos2) |
---|
503 | self.data2_cbox.SetClientData(pos2, None) |
---|
504 | return |
---|
505 | pos1 = self.data1_cbox.Append('Select Data') |
---|
506 | self.data1_cbox.SetSelection(pos1) |
---|
507 | self.data1_cbox.SetClientData(pos1, None) |
---|
508 | pos2 = self.data2_cbox.Append('Select Data') |
---|
509 | self.data2_cbox.SetSelection(pos2) |
---|
510 | self.data2_cbox.SetClientData(pos2, None) |
---|
511 | pos3 = self.data2_cbox.Append('Number') |
---|
512 | val = None |
---|
513 | if (self.numberctr.IsShown() and self.numberctr.IsEnabled()): |
---|
514 | try: |
---|
515 | val = float(self.numberctr.GetValue()) |
---|
516 | except: |
---|
517 | val = None |
---|
518 | self.data2_cbox.SetClientData(pos3, val) |
---|
519 | dnames = [] |
---|
520 | ids = self._data.keys() |
---|
521 | for id in ids: |
---|
522 | if id != None: |
---|
523 | if self._data[id].data != None: |
---|
524 | dnames.append(self._data[id].data.name) |
---|
525 | else: |
---|
526 | theory_list = self._data[id].get_theory() |
---|
527 | theory, _ = theory_list.values()[0] |
---|
528 | dnames.append(theory.name) |
---|
529 | ind = numpy.argsort(dnames) |
---|
530 | if len(ind) > 0: |
---|
531 | val_list = numpy.array(self._data.values())[ind] |
---|
532 | for datastate in val_list: |
---|
533 | data = datastate.data |
---|
534 | if data != None: |
---|
535 | name = data.name |
---|
536 | pos1 = self.data1_cbox.Append(str(name)) |
---|
537 | self.data1_cbox.SetClientData(pos1, data) |
---|
538 | pos2 = self.data2_cbox.Append(str(name)) |
---|
539 | self.data2_cbox.SetClientData(pos2, data) |
---|
540 | if str(current1) == str(name): |
---|
541 | pos_pre1 = pos1 |
---|
542 | if str(current2) == str(name): |
---|
543 | pos_pre2 = pos2 |
---|
544 | try: |
---|
545 | theory_list = datastate.get_theory() |
---|
546 | for theory, _ in theory_list.values(): |
---|
547 | th_name = theory.name |
---|
548 | posth1 = self.data1_cbox.Append(str(th_name)) |
---|
549 | self.data1_cbox.SetClientData(posth1, theory) |
---|
550 | posth2 = self.data2_cbox.Append(str(th_name)) |
---|
551 | self.data2_cbox.SetClientData(posth2, theory) |
---|
552 | if str(current1) == str(th_name): |
---|
553 | pos_pre1 = posth1 |
---|
554 | if str(current2) == str(th_name): |
---|
555 | pos_pre2 = posth2 |
---|
556 | except: |
---|
557 | continue |
---|
558 | self.data1_cbox.SetSelection(pos_pre1) |
---|
559 | self.data2_cbox.SetSelection(pos_pre2) |
---|
560 | |
---|
561 | def get_datalist(self): |
---|
562 | """ |
---|
563 | """ |
---|
564 | data_manager = self.parent.parent.get_data_manager() |
---|
565 | if data_manager != None: |
---|
566 | return data_manager.get_all_data() |
---|
567 | else: |
---|
568 | return {} |
---|
569 | |
---|
570 | def on_click_apply(self, event): |
---|
571 | """ |
---|
572 | changes are saved in data object imported to edit |
---|
573 | """ |
---|
574 | self.send_warnings('') |
---|
575 | self.data_namectr.SetBackgroundColour('white') |
---|
576 | state_list = self.get_datalist().values() |
---|
577 | name = self.data_namectr.GetValue().strip() |
---|
578 | name_list = [] |
---|
579 | for state in state_list: |
---|
580 | if state.data == None: |
---|
581 | theory_list = state.get_theory() |
---|
582 | theory, _ = theory_list.values()[0] |
---|
583 | d_name = str(theory.name) |
---|
584 | else: |
---|
585 | d_name = str(state.data.name) |
---|
586 | name_list.append(d_name) |
---|
587 | if name in name_list: |
---|
588 | self._set_textctrl_color(self.data_namectr, 'pink') |
---|
589 | msg = "The Output Data Name already exists... " |
---|
590 | wx.MessageBox(msg, 'Error') |
---|
591 | return |
---|
592 | if name == '': |
---|
593 | self._set_textctrl_color(self.data_namectr, 'pink') |
---|
594 | msg = "Please type the output data name first... " |
---|
595 | wx.MessageBox(msg, 'Error') |
---|
596 | return |
---|
597 | if self.output == None: |
---|
598 | msg = "No Output Data has been generated... " |
---|
599 | wx.MessageBox(msg, 'Error') |
---|
600 | return |
---|
601 | # send data to data manager |
---|
602 | self.output.name = name |
---|
603 | self.output.run = "Data Operation" |
---|
604 | self.output.instrument = "SansView" |
---|
605 | self.output.id = str(name) + str(time.time()) |
---|
606 | data = {self.output.id :self.output} |
---|
607 | self.parent.parent.add_data(data) |
---|
608 | self.name_sizer.Layout() |
---|
609 | self.Refresh() |
---|
610 | #must post event here |
---|
611 | event.Skip() |
---|
612 | |
---|
613 | def on_close(self, event): |
---|
614 | """ |
---|
615 | leave data as it is and close |
---|
616 | """ |
---|
617 | self.parent.OnClose() |
---|
618 | |
---|
619 | def set_plot_unfocus(self): |
---|
620 | """ |
---|
621 | Unfocus on right click |
---|
622 | """ |
---|
623 | |
---|
624 | def send_warnings(self, msg='', info='info'): |
---|
625 | """ |
---|
626 | Send warning to status bar |
---|
627 | """ |
---|
628 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info=info)) |
---|
629 | |
---|
630 | class SmallPanel(PlotPanel): |
---|
631 | """ |
---|
632 | PlotPanel for Quick plot and masking plot |
---|
633 | """ |
---|
634 | def __init__(self, parent, id=-1, is_number=False, content='?', **kwargs): |
---|
635 | """ |
---|
636 | """ |
---|
637 | PlotPanel.__init__(self, parent, id=id, **kwargs) |
---|
638 | self.is_number = is_number |
---|
639 | self.content = content |
---|
640 | self.position = (0.4, 0.5) |
---|
641 | self.scale = 'linear' |
---|
642 | self.subplot.set_xticks([]) |
---|
643 | self.subplot.set_yticks([]) |
---|
644 | self.add_text() |
---|
645 | self.figure.subplots_adjust(left=0.1, bottom=0.1) |
---|
646 | |
---|
647 | def set_content(self, content=''): |
---|
648 | """ |
---|
649 | Set text content |
---|
650 | """ |
---|
651 | self.content = str(content) |
---|
652 | |
---|
653 | def add_toolbar(self): |
---|
654 | """ |
---|
655 | Add toolbar |
---|
656 | """ |
---|
657 | # Not implemented |
---|
658 | pass |
---|
659 | |
---|
660 | def on_set_focus(self, event): |
---|
661 | """ |
---|
662 | send to the parenet the current panel on focus |
---|
663 | """ |
---|
664 | pass |
---|
665 | |
---|
666 | def add_image(self, plot): |
---|
667 | """ |
---|
668 | Add Image |
---|
669 | """ |
---|
670 | self.content = '' |
---|
671 | self.textList = [] |
---|
672 | self.plots = {} |
---|
673 | self.clear() |
---|
674 | try: |
---|
675 | self.figure.delaxes(self.figure.axes[0]) |
---|
676 | self.subplot = self.figure.add_subplot(111) |
---|
677 | #self.figure.delaxes(self.figure.axes[1]) |
---|
678 | except: |
---|
679 | pass |
---|
680 | try: |
---|
681 | name = plot.name |
---|
682 | except: |
---|
683 | name = plot.filename |
---|
684 | self.plots[name] = plot |
---|
685 | |
---|
686 | #init graph |
---|
687 | self.graph = Graph() |
---|
688 | |
---|
689 | #add plot |
---|
690 | self.graph.add(plot) |
---|
691 | #draw |
---|
692 | self.graph.render(self) |
---|
693 | |
---|
694 | try: |
---|
695 | self.figure.delaxes(self.figure.axes[1]) |
---|
696 | except: |
---|
697 | pass |
---|
698 | self.subplot.figure.canvas.resizing = False |
---|
699 | self.subplot.tick_params(axis='both', labelsize=9) |
---|
700 | # Draw zero axis lines |
---|
701 | self.subplot.axhline(linewidth = 1, color='r') |
---|
702 | self.subplot.axvline(linewidth = 1, color='r') |
---|
703 | |
---|
704 | self.erase_legend() |
---|
705 | try: |
---|
706 | # mpl >= 1.1.0 |
---|
707 | self.figure.tight_layout() |
---|
708 | except: |
---|
709 | self.figure.subplots_adjust(left=0.1, bottom=0.1) |
---|
710 | self.subplot.figure.canvas.draw() |
---|
711 | |
---|
712 | def add_text(self): |
---|
713 | """ |
---|
714 | Text in the plot |
---|
715 | """ |
---|
716 | if not self.is_number: |
---|
717 | return |
---|
718 | |
---|
719 | self.clear() |
---|
720 | try: |
---|
721 | self.figure.delaxes(self.figure.axes[0]) |
---|
722 | self.subplot = self.figure.add_subplot(111) |
---|
723 | self.figure.delaxes(self.figure.axes[1]) |
---|
724 | except: |
---|
725 | pass |
---|
726 | self.subplot.set_xticks([]) |
---|
727 | self.subplot.set_yticks([]) |
---|
728 | label = self.content |
---|
729 | FONT = FontProperties() |
---|
730 | xpos, ypos = (0.4, 0.5) |
---|
731 | font = FONT.copy() |
---|
732 | font.set_size(14) |
---|
733 | |
---|
734 | self.textList = [] |
---|
735 | self.subplot.set_xlim((0, 1)) |
---|
736 | self.subplot.set_ylim((0, 1)) |
---|
737 | |
---|
738 | try: |
---|
739 | if self.content != '?': |
---|
740 | float(label) |
---|
741 | except: |
---|
742 | self.subplot.set_frame_on(False) |
---|
743 | try: |
---|
744 | # mpl >= 1.1.0 |
---|
745 | self.figure.tight_layout() |
---|
746 | except: |
---|
747 | self.figure.subplots_adjust(left=0.1, bottom=0.1) |
---|
748 | if len(label) > 0 and xpos > 0 and ypos > 0: |
---|
749 | new_text = self.subplot.text(str(xpos), str(ypos), str(label), |
---|
750 | fontproperties=font) |
---|
751 | self.textList.append(new_text) |
---|
752 | |
---|
753 | def erase_legend(self): |
---|
754 | """ |
---|
755 | Remove Legend |
---|
756 | """ |
---|
757 | #for ax in self.axes: |
---|
758 | self.remove_legend(self.subplot) |
---|
759 | |
---|
760 | def onMouseMotion(self, event): |
---|
761 | """ |
---|
762 | Disable dragging 2D image |
---|
763 | """ |
---|
764 | |
---|
765 | def onWheel(self, event): |
---|
766 | """ |
---|
767 | """ |
---|
768 | |
---|
769 | def onLeftDown(self, event): |
---|
770 | """ |
---|
771 | Disables LeftDown |
---|
772 | """ |
---|
773 | |
---|
774 | def onPick(self, event): |
---|
775 | """ |
---|
776 | Remove Legend |
---|
777 | """ |
---|
778 | for ax in self.axes: |
---|
779 | self.remove_legend(ax) |
---|
780 | |
---|
781 | |
---|
782 | def draw(self): |
---|
783 | """ |
---|
784 | Draw |
---|
785 | """ |
---|
786 | if self.dimension == 3: |
---|
787 | pass |
---|
788 | else: |
---|
789 | self.subplot.figure.canvas.draw_idle() |
---|
790 | |
---|
791 | def onContextMenu(self, event): |
---|
792 | """ |
---|
793 | Default context menu for a plot panel |
---|
794 | """ |
---|
795 | |
---|
796 | class DataOperatorWindow(wx.Frame): |
---|
797 | def __init__(self, parent, *args, **kwds): |
---|
798 | kwds["size"] = (PANEL_WIDTH, PANEL_HEIGTH) |
---|
799 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
800 | self.parent = parent |
---|
801 | self.panel = DataOperPanel(parent=self) |
---|
802 | wx.EVT_CLOSE(self, self.OnClose) |
---|
803 | self.CenterOnParent() |
---|
804 | self.Show() |
---|
805 | |
---|
806 | def OnClose(self, event=None): |
---|
807 | """ |
---|
808 | On close event |
---|
809 | """ |
---|
810 | self.Show(False) |
---|
811 | |
---|
812 | |
---|
813 | if __name__ == "__main__": |
---|
814 | |
---|
815 | app = wx.App() |
---|
816 | window = DataOperatorWindow(parent=None, data=[], title="Data Editor") |
---|
817 | app.MainLoop() |
---|
818 | |
---|