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