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