1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2008, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | |
---|
13 | import wx |
---|
14 | import sys |
---|
15 | import math |
---|
16 | import numpy |
---|
17 | |
---|
18 | from sans.plottools.PlotPanel import PlotPanel |
---|
19 | from sans.guiframe.events import StatusEvent |
---|
20 | from sans.guiframe.events import PanelOnFocusEvent |
---|
21 | from sans.guiframe.utils import PanelMenu |
---|
22 | from sans.guiframe.panel_base import PanelBase |
---|
23 | from sans.guiframe.gui_style import GUIFRAME_ICON |
---|
24 | from appearanceDialog import appearanceDialog |
---|
25 | from graphAppearance import graphAppearance |
---|
26 | |
---|
27 | DEFAULT_QMAX = 0.05 |
---|
28 | DEFAULT_QSTEP = 0.001 |
---|
29 | DEFAULT_BEAM = 0.005 |
---|
30 | BIN_WIDTH = 1 |
---|
31 | IS_MAC = (sys.platform == 'darwin') |
---|
32 | |
---|
33 | |
---|
34 | def find_key(dic, val): |
---|
35 | """return the key of dictionary dic given the value""" |
---|
36 | return [k for k, v in dic.iteritems() if v == val][0] |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | class ModelPanel1D(PlotPanel, PanelBase): |
---|
41 | """ |
---|
42 | Plot panel for use with the GUI manager |
---|
43 | """ |
---|
44 | |
---|
45 | ## Internal name for the AUI manager |
---|
46 | window_name = "plotpanel" |
---|
47 | ## Title to appear on top of the window |
---|
48 | window_caption = "Graph" |
---|
49 | ## Flag to tell the GUI manager that this panel is not |
---|
50 | # tied to any perspective |
---|
51 | ALWAYS_ON = True |
---|
52 | ## Group ID |
---|
53 | group_id = None |
---|
54 | |
---|
55 | def __init__(self, parent, id=-1, color = None, |
---|
56 | dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
57 | PlotPanel.__init__(self, parent, id=id, style=style, **kwargs) |
---|
58 | PanelBase.__init__(self, parent) |
---|
59 | ## Reference to the parent window |
---|
60 | self.parent = parent |
---|
61 | if hasattr(parent, "parent"): |
---|
62 | self.parent = self.parent.parent |
---|
63 | ## Plottables |
---|
64 | self.plots = {} |
---|
65 | self.frame = None |
---|
66 | #context menu |
---|
67 | self._slicerpop = None |
---|
68 | |
---|
69 | self._available_data = [] |
---|
70 | self._menu_add_ids = [] |
---|
71 | self._symbol_labels = self.get_symbol_label() |
---|
72 | self._color_labels = self.get_color_label() |
---|
73 | self.currColorIndex = "" |
---|
74 | self._is_changed_legend_label = False |
---|
75 | self.is_xtick = False |
---|
76 | self.is_ytick = False |
---|
77 | |
---|
78 | self.hide_menu = None |
---|
79 | ## Unique ID (from gui_manager) |
---|
80 | self.uid = None |
---|
81 | self.x_size = None |
---|
82 | ## Default locations |
---|
83 | #self._default_save_location = os.getcwd() |
---|
84 | self.size = None |
---|
85 | self.vl_ind = 0 |
---|
86 | ## Graph |
---|
87 | #self.graph = Graph() |
---|
88 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
89 | self.graph.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
90 | self.graph.render(self) |
---|
91 | self.cursor_id = None |
---|
92 | |
---|
93 | # In resizing event |
---|
94 | self.resizing = False |
---|
95 | self.canvas.set_resizing(self.resizing) |
---|
96 | self.Bind(wx.EVT_SIZE, self._OnReSize) |
---|
97 | self._add_more_tool() |
---|
98 | self.parent.SetFocus() |
---|
99 | |
---|
100 | |
---|
101 | def get_symbol_label(self): |
---|
102 | """ |
---|
103 | Associates label to symbol |
---|
104 | """ |
---|
105 | _labels = {} |
---|
106 | i = 0 |
---|
107 | _labels['Circle'] = i |
---|
108 | i += 1 |
---|
109 | _labels['Cross X '] = i |
---|
110 | i += 1 |
---|
111 | _labels['Triangle Down'] = i |
---|
112 | i += 1 |
---|
113 | _labels['Triangle Up'] = i |
---|
114 | i += 1 |
---|
115 | _labels['Triangle Left'] = i |
---|
116 | i += 1 |
---|
117 | _labels['Triangle Right'] = i |
---|
118 | i += 1 |
---|
119 | _labels['Cross +'] = i |
---|
120 | i += 1 |
---|
121 | _labels['Square'] = i |
---|
122 | i += 1 |
---|
123 | _labels['diamond'] = i |
---|
124 | i += 1 |
---|
125 | _labels['Diamond'] = i |
---|
126 | i += 1 |
---|
127 | _labels['Hexagon1'] = i |
---|
128 | i += 1 |
---|
129 | _labels['Hexagon2'] = i |
---|
130 | i += 1 |
---|
131 | _labels['Pentagon'] = i |
---|
132 | i += 1 |
---|
133 | _labels['Line'] = i |
---|
134 | i += 1 |
---|
135 | _labels['Dash'] = i |
---|
136 | i += 1 |
---|
137 | _labels['Vline'] = i |
---|
138 | i += 1 |
---|
139 | _labels['Step'] = i |
---|
140 | return _labels |
---|
141 | |
---|
142 | def get_color_label(self): |
---|
143 | """ |
---|
144 | Associates label to a specific color |
---|
145 | """ |
---|
146 | _labels = {} |
---|
147 | i = 0 |
---|
148 | _labels['Blue'] = i |
---|
149 | i += 1 |
---|
150 | _labels['Green'] = i |
---|
151 | i += 1 |
---|
152 | _labels['Red'] = i |
---|
153 | i += 1 |
---|
154 | _labels['Cyan'] = i |
---|
155 | i += 1 |
---|
156 | _labels['Magenta'] = i |
---|
157 | i += 1 |
---|
158 | _labels['Yellow'] = i |
---|
159 | i += 1 |
---|
160 | _labels['Black'] = i |
---|
161 | return _labels |
---|
162 | |
---|
163 | |
---|
164 | def set_data(self, list=None): |
---|
165 | """ |
---|
166 | """ |
---|
167 | pass |
---|
168 | |
---|
169 | def _reset(self): |
---|
170 | """ |
---|
171 | Resets internal data and graph |
---|
172 | """ |
---|
173 | self.graph.reset() |
---|
174 | self.plots = {} |
---|
175 | self.is_zoomed = False |
---|
176 | |
---|
177 | def _OnReSize(self, event): |
---|
178 | """ |
---|
179 | On response of the resize of a panel, set axes_visiable False |
---|
180 | """ |
---|
181 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
182 | # So manually recode the size (=x_size) and compare here. |
---|
183 | # Massy code to work around:< |
---|
184 | if self.parent._mgr != None: |
---|
185 | max_panel = self.parent._mgr.GetPane(self) |
---|
186 | if max_panel.IsMaximized(): |
---|
187 | self.parent._mgr.RestorePane(max_panel) |
---|
188 | max_panel.Maximize() |
---|
189 | if self.x_size != None: |
---|
190 | if self.x_size == self.GetSize(): |
---|
191 | self.resizing = False |
---|
192 | self.canvas.set_resizing(self.resizing) |
---|
193 | return |
---|
194 | self.x_size = self.GetSize() |
---|
195 | |
---|
196 | # Ready for another event |
---|
197 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
198 | event.Skip() |
---|
199 | # set the resizing flag |
---|
200 | self.resizing = True |
---|
201 | self.canvas.set_resizing(self.resizing) |
---|
202 | self.parent.set_schedule(True) |
---|
203 | pos_x, pos_y = self.GetPositionTuple() |
---|
204 | if pos_x != 0 and pos_y != 0: |
---|
205 | self.size, _ = self.GetClientSizeTuple() |
---|
206 | self.SetSizer(self.sizer) |
---|
207 | wx.CallAfter(self.parent.disable_app_menu,self) |
---|
208 | |
---|
209 | def on_plot_qrange(self, event=None): |
---|
210 | """ |
---|
211 | On Qmin Qmax vertical line event |
---|
212 | """ |
---|
213 | if event == None: |
---|
214 | return |
---|
215 | event.Skip() |
---|
216 | active_ctrl = event.active |
---|
217 | if active_ctrl == None: |
---|
218 | return |
---|
219 | if event.id in self.plots.keys(): |
---|
220 | # Set line position and color |
---|
221 | colors = ['red', 'purple'] |
---|
222 | self.cursor_id = event.id |
---|
223 | ctrl = event.ctrl |
---|
224 | if self.ly == None: |
---|
225 | self.ly = [] |
---|
226 | for ind_ly in range(len(colors)): |
---|
227 | self.ly.append(self.subplot.axvline(color=colors[ind_ly], |
---|
228 | lw=2.5, alpha=0.7)) |
---|
229 | self.ly[ind_ly].set_rasterized(True) |
---|
230 | try: |
---|
231 | # Display x,y in the status bar if possible |
---|
232 | xval = float(active_ctrl.GetValue()) |
---|
233 | position = self.get_data_xy_vals(xval) |
---|
234 | if position != None: |
---|
235 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
236 | except: |
---|
237 | pass |
---|
238 | if not event.leftdown: |
---|
239 | # text event |
---|
240 | try: |
---|
241 | is_moved = False |
---|
242 | for idx in range(len(self.ly)): |
---|
243 | val = float(ctrl[idx].GetValue()) |
---|
244 | # check if vline moved |
---|
245 | if self.ly[idx].get_xdata() != val: |
---|
246 | self.ly[idx].set_xdata(val) |
---|
247 | is_moved = True |
---|
248 | if is_moved: |
---|
249 | self.canvas.draw() |
---|
250 | except: |
---|
251 | pass |
---|
252 | event.Skip() |
---|
253 | return |
---|
254 | self.q_ctrl = ctrl |
---|
255 | try: |
---|
256 | pos_x_min = float(self.q_ctrl[0].GetValue()) |
---|
257 | except: |
---|
258 | pos_x_min = xmin |
---|
259 | try: |
---|
260 | pos_x_max = float(self.q_ctrl[1].GetValue()) |
---|
261 | except: |
---|
262 | pos_x_max = xmax |
---|
263 | pos_x = [pos_x_min, pos_x_max] |
---|
264 | for ind_ly in range(len(colors)): |
---|
265 | self.ly[ind_ly].set_color(colors[ind_ly]) |
---|
266 | self.ly[ind_ly].set_xdata(pos_x[ind_ly]) |
---|
267 | self.canvas.draw() |
---|
268 | else: |
---|
269 | self.q_ctrl = None |
---|
270 | |
---|
271 | def get_data_xy_vals(self, xval): |
---|
272 | """ |
---|
273 | Get x, y data values near x = x_val |
---|
274 | """ |
---|
275 | try: |
---|
276 | x_data = self.plots[self.cursor_id].x |
---|
277 | y_data = self.plots[self.cursor_id].y |
---|
278 | indx = self._find_nearest(x_data, xval) |
---|
279 | pos_x = x_data[indx] |
---|
280 | pos_y = y_data[indx] |
---|
281 | position = str(pos_x), str(pos_y) |
---|
282 | return position |
---|
283 | except: |
---|
284 | return None |
---|
285 | |
---|
286 | def _find_nearest(self, array, value): |
---|
287 | """ |
---|
288 | Find and return the nearest value in array to the value. |
---|
289 | Used in cusor_line() |
---|
290 | :Param array: numpy array |
---|
291 | :Param value: float |
---|
292 | """ |
---|
293 | idx = (numpy.abs(array - value)).argmin() |
---|
294 | return int(idx)#array.flat[idx] |
---|
295 | |
---|
296 | def _check_line_positions(self, pos_x=None, nop=None): |
---|
297 | """ |
---|
298 | Check vertical line positions |
---|
299 | :Param pos_x: position of the current line [float] |
---|
300 | :Param nop: number of plots [int] |
---|
301 | """ |
---|
302 | ly = self.ly |
---|
303 | ly0x = ly[0].get_xdata() |
---|
304 | ly1x = ly[1].get_xdata() |
---|
305 | self.q_ctrl[0].SetBackgroundColour('white') |
---|
306 | self.q_ctrl[1].SetBackgroundColour('white') |
---|
307 | if ly0x >= ly1x: |
---|
308 | if self.vl_ind == 0: |
---|
309 | ly[1].set_xdata(pos_x) |
---|
310 | ly[1].set_zorder(nop) |
---|
311 | self.q_ctrl[1].SetValue(str(pos_x)) |
---|
312 | self.q_ctrl[0].SetBackgroundColour('pink') |
---|
313 | elif self.vl_ind == 1: |
---|
314 | ly[0].set_xdata(pos_x) |
---|
315 | ly[0].set_zorder(nop) |
---|
316 | self.q_ctrl[0].SetValue(str(pos_x)) |
---|
317 | self.q_ctrl[1].SetBackgroundColour('pink') |
---|
318 | |
---|
319 | def _get_cusor_lines(self, event): |
---|
320 | """ |
---|
321 | Revmove or switch cursor line if drawn |
---|
322 | :Param event: LeftClick mouse event |
---|
323 | """ |
---|
324 | ax = event.inaxes |
---|
325 | if hasattr(event, "action"): |
---|
326 | dclick = event.action == 'dclick' |
---|
327 | if ax == None or dclick: |
---|
328 | # remove the vline |
---|
329 | self._check_zoom_plot() |
---|
330 | self.canvas.draw() |
---|
331 | self.q_ctrl = None |
---|
332 | return |
---|
333 | if self.ly != None and event.xdata != None: |
---|
334 | # Selecting a new line if cursor lines are displayed already |
---|
335 | dqmin = math.fabs(event.xdata - self.ly[0].get_xdata()) |
---|
336 | dqmax = math.fabs(event.xdata - self.ly[1].get_xdata()) |
---|
337 | is_qmax = dqmin > dqmax |
---|
338 | if is_qmax: |
---|
339 | self.vl_ind = 1 |
---|
340 | else: |
---|
341 | self.vl_ind = 0 |
---|
342 | |
---|
343 | def cusor_line(self, event): |
---|
344 | """ |
---|
345 | Move the cursor line to write Q range |
---|
346 | """ |
---|
347 | if self.q_ctrl == None: |
---|
348 | return |
---|
349 | #release a q range vline |
---|
350 | if self.ly != None and not self.leftdown: |
---|
351 | for ly in self.ly: |
---|
352 | ly.set_alpha(0.7) |
---|
353 | self.canvas.draw() |
---|
354 | return |
---|
355 | ax = event.inaxes |
---|
356 | if ax == None or not hasattr(event, 'action'): |
---|
357 | return |
---|
358 | end_drag = event.action != 'drag' and event.xdata != None |
---|
359 | nop = len(self.plots) |
---|
360 | pos_x, pos_y = float(event.xdata), float(event.ydata) |
---|
361 | try: |
---|
362 | ly = self.ly |
---|
363 | ly0x = ly[0].get_xdata() |
---|
364 | ly1x = ly[1].get_xdata() |
---|
365 | if ly0x == ly1x: |
---|
366 | if ly[0].get_zorder() > ly[1].get_zorder(): |
---|
367 | self.vl_ind = 0 |
---|
368 | else: |
---|
369 | self.vl_ind = 1 |
---|
370 | vl_ind = self.vl_ind |
---|
371 | x_data = self.plots[self.cursor_id].x |
---|
372 | y_data = self.plots[self.cursor_id].y |
---|
373 | xmin = x_data.min() |
---|
374 | xmax = x_data.max() |
---|
375 | indx = self._find_nearest(x_data, pos_x) |
---|
376 | #pos_x = self._find_nearest(x_data, pos_x) |
---|
377 | #indx = int(numpy.searchsorted(x_data, [pos_x])[0]) |
---|
378 | # Need to hold LeftButton to drag |
---|
379 | if end_drag: |
---|
380 | if event.button: |
---|
381 | self._check_line_positions(pos_x, nop) |
---|
382 | return |
---|
383 | if indx >= len(x_data): |
---|
384 | indx = len(x_data) - 1 |
---|
385 | pos_x = x_data[indx] |
---|
386 | pos_y = y_data[indx] |
---|
387 | if xmin == ly1x: |
---|
388 | vl_ind = 1 |
---|
389 | elif xmax == ly0x: |
---|
390 | vl_ind = 0 |
---|
391 | else: |
---|
392 | ly[vl_ind].set_xdata(pos_x) |
---|
393 | ly[vl_ind].set_zorder(nop + 1) |
---|
394 | self._check_line_positions(pos_x, nop) |
---|
395 | ly[vl_ind].set_xdata(pos_x) |
---|
396 | ly[vl_ind].set_alpha(1.0) |
---|
397 | ly[vl_ind].set_zorder(nop + 1) |
---|
398 | self.canvas.draw() |
---|
399 | self.q_ctrl[vl_ind].SetValue(str(pos_x)) |
---|
400 | except: |
---|
401 | pass |
---|
402 | |
---|
403 | def set_resizing(self, resizing=False): |
---|
404 | """ |
---|
405 | Set the resizing (True/False) |
---|
406 | """ |
---|
407 | self.resizing = resizing |
---|
408 | #self.canvas.set_resizing(resizing) |
---|
409 | |
---|
410 | def schedule_full_draw(self, func='append'): |
---|
411 | """ |
---|
412 | Put self in schedule to full redraw list |
---|
413 | """ |
---|
414 | # append/del this panel in the schedule list |
---|
415 | self.parent.set_schedule_full_draw(self, func) |
---|
416 | |
---|
417 | |
---|
418 | def remove_data_by_id(self, id): |
---|
419 | """' |
---|
420 | remove data from plot |
---|
421 | """ |
---|
422 | if id in self.plots.keys(): |
---|
423 | data = self.plots[id] |
---|
424 | self.graph.delete(data) |
---|
425 | data_manager = self._manager.parent.get_data_manager() |
---|
426 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
427 | |
---|
428 | if id in data_list.keys(): |
---|
429 | data = data_list[id] |
---|
430 | if id in theory_list.keys(): |
---|
431 | data = theory_list[id] |
---|
432 | # Update Graph menu and help string |
---|
433 | #h_id = self.parent._window_menu.FindItem(self.window_caption) |
---|
434 | if data != None: |
---|
435 | if data.__class__.__name__ == 'list': |
---|
436 | label = data[0].label |
---|
437 | else: |
---|
438 | label = data.label |
---|
439 | else: |
---|
440 | label = '???' |
---|
441 | #helpString = self.parent._window_menu.GetHelpString(h_id) |
---|
442 | d_string = (' ' + str(label) +';') |
---|
443 | #new_tip = helpString.replace(d_string, '') |
---|
444 | #self.parent._window_menu.SetHelpString(h_id, new_tip) |
---|
445 | |
---|
446 | del self.plots[id] |
---|
447 | self.graph.render(self) |
---|
448 | self.subplot.figure.canvas.draw_idle() |
---|
449 | if len(self.graph.plottables) == 0: |
---|
450 | #onRemove: graph is empty must be the panel must be destroyed |
---|
451 | self.parent.delete_panel(self.uid) |
---|
452 | |
---|
453 | |
---|
454 | def plot_data(self, data): |
---|
455 | """ |
---|
456 | Data is ready to be displayed |
---|
457 | |
---|
458 | :param event: data event |
---|
459 | """ |
---|
460 | if data.__class__.__name__ == 'Data2D': |
---|
461 | return |
---|
462 | plot_keys = self.plots.keys() |
---|
463 | if data.id in plot_keys: |
---|
464 | #Recover panel prop.s |
---|
465 | xlo, xhi = self.subplot.get_xlim() |
---|
466 | ylo, yhi = self.subplot.get_ylim() |
---|
467 | old_data = self.plots[data.id] |
---|
468 | if self._is_changed_legend_label: |
---|
469 | data.label = old_data.label |
---|
470 | if old_data.__class__.__name__ == 'Data1D': |
---|
471 | data.custom_color = old_data.custom_color |
---|
472 | data.symbol = old_data.symbol |
---|
473 | data.markersize = old_data.markersize |
---|
474 | data.zorder = len(plot_keys) |
---|
475 | # Replace data |
---|
476 | self.graph.replace(data) |
---|
477 | self.plots[data.id] = data |
---|
478 | ## Set the view scale for all plots |
---|
479 | try: |
---|
480 | self._onEVT_FUNC_PROPERTY() |
---|
481 | except Exception, exc: |
---|
482 | msg=" Encountered singular points..." |
---|
483 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
484 | "Plotting Error: %s"% str(exc), info="error")) |
---|
485 | if self.is_zoomed: |
---|
486 | # Recover the x,y limits |
---|
487 | self.subplot.set_xlim((xlo, xhi)) |
---|
488 | self.subplot.set_ylim((ylo, yhi)) |
---|
489 | else: |
---|
490 | self.plots[data.id] = data |
---|
491 | self.graph.add(self.plots[data.id]) |
---|
492 | data.zorder = len(plot_keys) |
---|
493 | ## Set the view scale for all plots |
---|
494 | try: |
---|
495 | self._onEVT_FUNC_PROPERTY() |
---|
496 | if IS_MAC: |
---|
497 | # MAC: forcing to plot 2D avg |
---|
498 | self.canvas._onDrawIdle() |
---|
499 | except Exception,exc: |
---|
500 | msg=" Encountered singular points..." |
---|
501 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
502 | "Plotting Error: %s"% str(exc), info="error")) |
---|
503 | self.toolbar.update() |
---|
504 | self.is_zoomed = False |
---|
505 | # Update Graph menu and help string |
---|
506 | #pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
507 | helpString = 'Show/Hide Graph: ' |
---|
508 | for plot in self.plots.itervalues(): |
---|
509 | helpString += (' ' + str(plot.label) +';') |
---|
510 | #self.parent._window_menu.SetHelpString(pos, helpString) |
---|
511 | |
---|
512 | def draw_plot(self): |
---|
513 | """ |
---|
514 | Draw plot |
---|
515 | """ |
---|
516 | self.draw() |
---|
517 | |
---|
518 | def onLeftDown(self,event): |
---|
519 | """ |
---|
520 | left button down and ready to drag |
---|
521 | Display the position of the mouse on the statusbar |
---|
522 | """ |
---|
523 | #self.parent.set_plot_unfocus() |
---|
524 | self._get_cusor_lines(event) |
---|
525 | ax = event.inaxes |
---|
526 | PlotPanel.onLeftDown(self, event) |
---|
527 | if ax != None: |
---|
528 | try: |
---|
529 | pos_x = float(event.xdata)# / size_x |
---|
530 | pos_y = float(event.ydata)# / size_y |
---|
531 | pos_x = "%8.3g"% pos_x |
---|
532 | pos_y = "%8.3g"% pos_y |
---|
533 | self.position = str(pos_x), str(pos_y) |
---|
534 | wx.PostEvent(self.parent, StatusEvent(status=self.position)) |
---|
535 | except: |
---|
536 | self.position = None |
---|
537 | # unfocus all |
---|
538 | self.parent.set_plot_unfocus() |
---|
539 | #post nd event to notify guiframe that this panel is on focus |
---|
540 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
541 | |
---|
542 | |
---|
543 | def _ontoggle_hide_error(self, event): |
---|
544 | """ |
---|
545 | Toggle error display to hide or show |
---|
546 | """ |
---|
547 | menu = event.GetEventObject() |
---|
548 | id = event.GetId() |
---|
549 | self.set_selected_from_menu(menu, id) |
---|
550 | # Check zoom |
---|
551 | xlo, xhi = self.subplot.get_xlim() |
---|
552 | ylo, yhi = self.subplot.get_ylim() |
---|
553 | |
---|
554 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
555 | if self.hide_menu.GetText() == "Hide Error Bar": |
---|
556 | selected_plot.hide_error = True |
---|
557 | else: |
---|
558 | selected_plot.hide_error = False |
---|
559 | ## increment graph color |
---|
560 | self.graph.render(self) |
---|
561 | self.subplot.figure.canvas.draw_idle() |
---|
562 | if self.is_zoomed: |
---|
563 | # Recover the x,y limits |
---|
564 | self.subplot.set_xlim((xlo, xhi)) |
---|
565 | self.subplot.set_ylim((ylo, yhi)) |
---|
566 | |
---|
567 | |
---|
568 | def _onRemove(self, event): |
---|
569 | """ |
---|
570 | Remove a plottable from the graph and render the graph |
---|
571 | |
---|
572 | :param event: Menu event |
---|
573 | |
---|
574 | """ |
---|
575 | menu = event.GetEventObject() |
---|
576 | id = event.GetId() |
---|
577 | self.set_selected_from_menu(menu, id) |
---|
578 | ## Check if there is a selected graph to remove |
---|
579 | if self.graph.selected_plottable in self.plots.keys(): |
---|
580 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
581 | id = self.graph.selected_plottable |
---|
582 | self.remove_data_by_id(id) |
---|
583 | |
---|
584 | def onContextMenu(self, event): |
---|
585 | """ |
---|
586 | 1D plot context menu |
---|
587 | |
---|
588 | :param event: wx context event |
---|
589 | |
---|
590 | """ |
---|
591 | self._slicerpop = PanelMenu() |
---|
592 | self._slicerpop.set_plots(self.plots) |
---|
593 | self._slicerpop.set_graph(self.graph) |
---|
594 | if not self.graph.selected_plottable in self.plots: |
---|
595 | # Various plot options |
---|
596 | id = wx.NewId() |
---|
597 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
598 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
599 | id = wx.NewId() |
---|
600 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
601 | wx.EVT_MENU(self, id, self.onPrint) |
---|
602 | id = wx.NewId() |
---|
603 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
604 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
605 | |
---|
606 | id = wx.NewId() |
---|
607 | self._slicerpop.Append(id, '&Copy to Clipboard', |
---|
608 | 'Copy to the clipboard') |
---|
609 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
610 | |
---|
611 | self._slicerpop.AppendSeparator() |
---|
612 | |
---|
613 | for plot in self.plots.values(): |
---|
614 | #title = plot.title |
---|
615 | name = plot.name |
---|
616 | plot_menu = wx.Menu() |
---|
617 | if self.graph.selected_plottable: |
---|
618 | if not self.graph.selected_plottable in self.plots.keys(): |
---|
619 | continue |
---|
620 | if plot != self.plots[self.graph.selected_plottable]: |
---|
621 | continue |
---|
622 | |
---|
623 | id = wx.NewId() |
---|
624 | plot_menu.Append(id, "&DataInfo", name) |
---|
625 | wx.EVT_MENU(self, id, self. _onDataShow) |
---|
626 | id = wx.NewId() |
---|
627 | plot_menu.Append(id, "&Save Points as a File", name) |
---|
628 | wx.EVT_MENU(self, id, self._onSave) |
---|
629 | plot_menu.AppendSeparator() |
---|
630 | |
---|
631 | #add menu of other plugins |
---|
632 | item_list = self.parent.get_current_context_menu(self) |
---|
633 | |
---|
634 | if (not item_list == None) and (not len(item_list) == 0): |
---|
635 | for item in item_list: |
---|
636 | |
---|
637 | try: |
---|
638 | id = wx.NewId() |
---|
639 | plot_menu.Append(id, item[0], name) |
---|
640 | wx.EVT_MENU(self, id, item[2]) |
---|
641 | except: |
---|
642 | msg = "ModelPanel1D.onContextMenu: " |
---|
643 | msg += "bad menu item %s" % sys.exc_value |
---|
644 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
645 | pass |
---|
646 | plot_menu.AppendSeparator() |
---|
647 | |
---|
648 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
649 | id = wx.NewId() |
---|
650 | plot_menu.Append(id, '&Linear Fit', name) |
---|
651 | wx.EVT_MENU(self, id, self.onFitting) |
---|
652 | plot_menu.AppendSeparator() |
---|
653 | |
---|
654 | id = wx.NewId() |
---|
655 | plot_menu.Append(id, "Remove", name) |
---|
656 | wx.EVT_MENU(self, id, self._onRemove) |
---|
657 | if not plot.is_data: |
---|
658 | id = wx.NewId() |
---|
659 | plot_menu.Append(id, '&Freeze', name) |
---|
660 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
661 | plot_menu.AppendSeparator() |
---|
662 | symbol_menu = wx.Menu() |
---|
663 | |
---|
664 | if plot.is_data: |
---|
665 | id = wx.NewId() |
---|
666 | self.hide_menu = plot_menu.Append(id, |
---|
667 | "Hide Error Bar", name) |
---|
668 | |
---|
669 | if plot.dy is not None and plot.dy != []: |
---|
670 | if plot.hide_error : |
---|
671 | self.hide_menu.SetText('Show Error Bar') |
---|
672 | else: |
---|
673 | self.hide_menu.SetText('Hide Error Bar') |
---|
674 | else: |
---|
675 | self.hide_menu.Enable(False) |
---|
676 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
677 | |
---|
678 | plot_menu.AppendSeparator() |
---|
679 | |
---|
680 | id = wx.NewId() |
---|
681 | plot_menu.Append(id, '&Modify Plot Property', name) |
---|
682 | wx.EVT_MENU(self, id, self.createAppDialog) |
---|
683 | |
---|
684 | |
---|
685 | |
---|
686 | id = wx.NewId() |
---|
687 | #plot_menu.SetTitle(name) |
---|
688 | self._slicerpop.AppendMenu(id, '&%s'% name, plot_menu) |
---|
689 | # Option to hide |
---|
690 | #TODO: implement functionality to hide a plottable (legend click) |
---|
691 | if not self.graph.selected_plottable in self.plots: |
---|
692 | self._slicerpop.AppendSeparator() |
---|
693 | loc_menu = wx.Menu() |
---|
694 | for label in self._loc_labels: |
---|
695 | id = wx.NewId() |
---|
696 | loc_menu.Append(id, str(label), str(label)) |
---|
697 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
698 | |
---|
699 | id = wx.NewId() |
---|
700 | self._slicerpop.Append(id, '&Modify Graph Appearance', |
---|
701 | 'Modify graph appearance') |
---|
702 | wx.EVT_MENU(self, id, self.modifyGraphAppearance) |
---|
703 | self._slicerpop.AppendSeparator() |
---|
704 | |
---|
705 | |
---|
706 | if self.position != None: |
---|
707 | id = wx.NewId() |
---|
708 | self._slicerpop.Append(id, '&Add Text') |
---|
709 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
710 | id = wx.NewId() |
---|
711 | self._slicerpop.Append(id, '&Remove Text') |
---|
712 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
713 | self._slicerpop.AppendSeparator() |
---|
714 | id = wx.NewId() |
---|
715 | self._slicerpop.Append(id, '&Change Scale') |
---|
716 | wx.EVT_MENU(self, id, self._onProperties) |
---|
717 | self._slicerpop.AppendSeparator() |
---|
718 | id = wx.NewId() |
---|
719 | self._slicerpop.Append(id, '&Reset Graph Range') |
---|
720 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
721 | |
---|
722 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
723 | self._slicerpop.AppendSeparator() |
---|
724 | id = wx.NewId() |
---|
725 | self._slicerpop.Append(id, '&Window Title') |
---|
726 | wx.EVT_MENU(self, id, self.onChangeCaption) |
---|
727 | try: |
---|
728 | pos_evt = event.GetPosition() |
---|
729 | pos = self.ScreenToClient(pos_evt) |
---|
730 | except: |
---|
731 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
732 | pos = (pos_x, pos_y + 5) |
---|
733 | self.PopupMenu(self._slicerpop, pos) |
---|
734 | |
---|
735 | def onFreeze(self, event): |
---|
736 | """ |
---|
737 | on Freeze data |
---|
738 | """ |
---|
739 | menu = event.GetEventObject() |
---|
740 | id = event.GetId() |
---|
741 | self.set_selected_from_menu(menu, id) |
---|
742 | plot = self.plots[self.graph.selected_plottable] |
---|
743 | self.parent.onfreeze([plot.id]) |
---|
744 | |
---|
745 | |
---|
746 | def _onSave(self, evt): |
---|
747 | """ |
---|
748 | Save a data set to a text file |
---|
749 | |
---|
750 | :param evt: Menu event |
---|
751 | |
---|
752 | """ |
---|
753 | menu = evt.GetEventObject() |
---|
754 | id = evt.GetId() |
---|
755 | self.set_selected_from_menu(menu, id) |
---|
756 | data = self.plots[self.graph.selected_plottable] |
---|
757 | default_name = data.label |
---|
758 | if default_name.count('.') > 0: |
---|
759 | default_name = default_name.split('.')[0] |
---|
760 | default_name += "_out" |
---|
761 | if self.parent != None: |
---|
762 | self.parent.save_data1d(data, default_name) |
---|
763 | |
---|
764 | |
---|
765 | def _onDataShow(self, evt): |
---|
766 | """ |
---|
767 | Show the data set in text |
---|
768 | |
---|
769 | :param evt: Menu event |
---|
770 | |
---|
771 | """ |
---|
772 | menu = evt.GetEventObject() |
---|
773 | id = evt.GetId() |
---|
774 | self.set_selected_from_menu(menu, id) |
---|
775 | data = self.plots[self.graph.selected_plottable] |
---|
776 | default_name = data.label |
---|
777 | if default_name.count('.') > 0: |
---|
778 | default_name = default_name.split('.')[0] |
---|
779 | #default_name += "_out" |
---|
780 | if self.parent != None: |
---|
781 | self.parent.show_data1d(data, default_name) |
---|
782 | |
---|
783 | def _add_more_tool(self): |
---|
784 | """ |
---|
785 | Add refresh, add/hide button in the tool bar |
---|
786 | """ |
---|
787 | return |
---|
788 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
789 | return |
---|
790 | self.toolbar.AddSeparator() |
---|
791 | id_hide = wx.NewId() |
---|
792 | hide = wx.Bitmap(GUIFRAME_ICON.HIDE_ID_PATH, wx.BITMAP_TYPE_PNG) |
---|
793 | self.toolbar.AddSimpleTool(id_hide, hide, 'Hide', 'Hide') |
---|
794 | self.toolbar.Realize() |
---|
795 | wx.EVT_TOOL(self, id_hide, self._on_hide) |
---|
796 | |
---|
797 | def _on_hide(self, event): |
---|
798 | """ |
---|
799 | Hides the plot when button is pressed |
---|
800 | """ |
---|
801 | if self.parent is not None: |
---|
802 | self.parent.hide_panel(self.uid) |
---|
803 | |
---|
804 | def on_close(self, event): |
---|
805 | """ |
---|
806 | On Close Event |
---|
807 | """ |
---|
808 | ID = self.uid |
---|
809 | self.parent.delete_panel(ID) |
---|
810 | |
---|
811 | def createAppDialog(self, event): |
---|
812 | """ |
---|
813 | Create the custom dialog for fit appearance modification |
---|
814 | """ |
---|
815 | menu = event.GetEventObject() |
---|
816 | id = event.GetId() |
---|
817 | self.set_selected_from_menu(menu, id) |
---|
818 | self.appearance_selected_plot = \ |
---|
819 | self.plots[self.graph.selected_plottable] |
---|
820 | # find current properties |
---|
821 | curr_color = self.appearance_selected_plot.custom_color |
---|
822 | curr_symbol = self.appearance_selected_plot.symbol |
---|
823 | curr_size = self.appearance_selected_plot.markersize |
---|
824 | curr_label = self.appearance_selected_plot.label |
---|
825 | |
---|
826 | if curr_color == None: |
---|
827 | curr_color = self._color_labels['Blue'] |
---|
828 | curr_symbol = 13 |
---|
829 | |
---|
830 | self.appD = appearanceDialog(self, 'Modify Plot Property') |
---|
831 | icon = self.parent.GetIcon() |
---|
832 | self.appD.SetIcon(icon) |
---|
833 | self.appD.set_defaults(float(curr_size), int(curr_color), |
---|
834 | str(appearanceDialog.find_key(self.get_symbol_label(), |
---|
835 | int(curr_symbol))), curr_label) |
---|
836 | self.appD.Bind(wx.EVT_CLOSE, self.on_AppDialog_close) |
---|
837 | |
---|
838 | def on_AppDialog_close(self, event): |
---|
839 | """ |
---|
840 | on_Modify Plot Property_close |
---|
841 | """ |
---|
842 | if(self.appD.okay_clicked == True): |
---|
843 | # returns (size,color,symbol,datalabel) |
---|
844 | info = self.appD.get_current_values() |
---|
845 | self.appearance_selected_plot.custom_color = \ |
---|
846 | self._color_labels[info[1].encode('ascii', 'ignore')] |
---|
847 | |
---|
848 | self.appearance_selected_plot.markersize = float(info[0]) |
---|
849 | self.appearance_selected_plot.symbol = \ |
---|
850 | self.get_symbol_label()[info[2]] |
---|
851 | self.appearance_selected_plot.label = str(info[3]) |
---|
852 | |
---|
853 | #pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
854 | #helpString = 'Show/Hide Graph: ' |
---|
855 | #for plot in self.plots.itervalues(): |
---|
856 | # helpString += (' ' + str(plot.label) + ';') |
---|
857 | # self.parent._window_menu.SetHelpString(pos, helpString) |
---|
858 | # self._is_changed_legend_label = True |
---|
859 | |
---|
860 | self.appD.Destroy() |
---|
861 | self._check_zoom_plot() |
---|
862 | |
---|
863 | |
---|
864 | def modifyGraphAppearance(self, event): |
---|
865 | """ |
---|
866 | On Modify Graph Appearance |
---|
867 | """ |
---|
868 | self.graphApp = graphAppearance(self, 'Modify Graph Appearance') |
---|
869 | icon = self.parent.GetIcon() |
---|
870 | self.graphApp.SetIcon(icon) |
---|
871 | self.graphApp.setDefaults(self.grid_on, self.legend_on, |
---|
872 | self.xaxis_label, self.yaxis_label, |
---|
873 | self.xaxis_unit, self.yaxis_unit, |
---|
874 | self.xaxis_font, self.yaxis_font, |
---|
875 | find_key(self.get_loc_label(), |
---|
876 | self.legendLoc), |
---|
877 | self.xcolor, self.ycolor, |
---|
878 | self.is_xtick, self.is_ytick) |
---|
879 | self.graphApp.Bind(wx.EVT_CLOSE, self.on_graphApp_close) |
---|
880 | |
---|
881 | |
---|
882 | def on_graphApp_close(self, event): |
---|
883 | """ |
---|
884 | Gets values from graph appearance dialog and sends them off |
---|
885 | to modify the plot |
---|
886 | """ |
---|
887 | graph_app = self.graphApp |
---|
888 | toggle_grid = graph_app.get_togglegrid() |
---|
889 | legend_loc = graph_app.get_legend_loc() |
---|
890 | toggle_legend = graph_app.get_togglelegend() |
---|
891 | |
---|
892 | self.onGridOnOff(toggle_grid ) |
---|
893 | self.ChangeLegendLoc(legend_loc) |
---|
894 | self.onLegend(toggle_legend) |
---|
895 | |
---|
896 | self.xaxis_label = graph_app.get_xlab() |
---|
897 | self.yaxis_label = graph_app.get_ylab() |
---|
898 | self.xaxis_unit = graph_app.get_xunit() |
---|
899 | self.yaxis_unit = graph_app.get_yunit() |
---|
900 | self.xaxis_font = graph_app.get_xfont() |
---|
901 | self.yaxis_font = graph_app.get_yfont() |
---|
902 | self.is_xtick = graph_app.get_xtick_check() |
---|
903 | self.is_ytick = graph_app.get_ytick_check() |
---|
904 | if self.is_xtick: |
---|
905 | self.xaxis_tick = self.xaxis_font |
---|
906 | if self.is_ytick: |
---|
907 | self.yaxis_tick = self.yaxis_font |
---|
908 | |
---|
909 | self.xaxis(self.xaxis_label, self.xaxis_unit, |
---|
910 | graph_app.get_xfont(), graph_app.get_xcolor(), |
---|
911 | self.xaxis_tick) |
---|
912 | self.yaxis(self.yaxis_label, self.yaxis_unit, |
---|
913 | graph_app.get_yfont(), graph_app.get_ycolor(), |
---|
914 | self.yaxis_tick) |
---|
915 | |
---|
916 | graph_app.Destroy() |
---|