1 | |
---|
2 | |
---|
3 | |
---|
4 | ################################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | # |
---|
9 | #See the license text in license.txt |
---|
10 | # |
---|
11 | #copyright 2008, University of Tennessee |
---|
12 | ################################################################################ |
---|
13 | |
---|
14 | import wx |
---|
15 | import sys |
---|
16 | from sans.guiframe.events import EVT_NEW_PLOT |
---|
17 | from sans.guiframe.events import StatusEvent |
---|
18 | from sans.guiframe.plugin_base import PluginBase |
---|
19 | |
---|
20 | class Plugin(PluginBase): |
---|
21 | """ |
---|
22 | Plug-in class to be instantiated by the GUI manager |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, standalone=False): |
---|
26 | PluginBase.__init__(self, name="Plotting", standalone=standalone) |
---|
27 | |
---|
28 | ## Plot panels |
---|
29 | self.plot_panels = [] |
---|
30 | |
---|
31 | def is_always_active(self): |
---|
32 | """ |
---|
33 | return True is this plugin is always active even if the user is |
---|
34 | switching between perspectives |
---|
35 | """ |
---|
36 | return True |
---|
37 | |
---|
38 | def populate_menu(self, parent): |
---|
39 | """ |
---|
40 | Create a 'Plot' menu to list the panels |
---|
41 | available for displaying |
---|
42 | |
---|
43 | :param id: next available unique ID for wx events |
---|
44 | :param parent: parent window |
---|
45 | |
---|
46 | """ |
---|
47 | self.menu = wx.Menu() |
---|
48 | |
---|
49 | self.menu.Append(wx.NewId(), "No plot available", |
---|
50 | "No plot available") |
---|
51 | self.menu.FindItemByPosition(0).Enable(False) |
---|
52 | return [(self.menu, "Plot")] |
---|
53 | |
---|
54 | def get_panels(self, parent): |
---|
55 | """ |
---|
56 | Create and return a list of panel objects |
---|
57 | """ |
---|
58 | ## Save a reference to the parent |
---|
59 | self.parent = parent |
---|
60 | # Connect to plotting events |
---|
61 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
62 | # We have no initial panels for this plug-in |
---|
63 | return [] |
---|
64 | |
---|
65 | def _on_show_panel(self, event): |
---|
66 | """show plug-in panel""" |
---|
67 | pass |
---|
68 | |
---|
69 | def _on_plot_event(self, event): |
---|
70 | """ |
---|
71 | A new plottable is being shipped to the plotting plug-in. |
---|
72 | Check whether we have a panel to put in on, or create |
---|
73 | a new one |
---|
74 | |
---|
75 | :param event: EVT_NEW_PLOT event |
---|
76 | |
---|
77 | """ |
---|
78 | # Check whether we already have a graph with the same units |
---|
79 | # as the plottable we just received. |
---|
80 | is_available = False |
---|
81 | for panel in self.plot_panels: |
---|
82 | if event.plot._xunit == panel.graph.prop["xunit_base"] \ |
---|
83 | and event.plot._yunit == panel.graph.prop["yunit_base"]: |
---|
84 | if hasattr(event.plot, "group_id"): |
---|
85 | ## if same group_id used the same panel to plot |
---|
86 | if not event.plot.group_id == None \ |
---|
87 | and event.plot.group_id == panel.group_id: |
---|
88 | is_available = True |
---|
89 | panel._onEVT_1DREPLOT(event) |
---|
90 | self.parent.show_panel(panel.uid) |
---|
91 | else: |
---|
92 | # Check that the plot panel has no group ID |
---|
93 | ## Use a panel with group_id ==None to plot |
---|
94 | if panel.group_id == None: |
---|
95 | is_available = True |
---|
96 | panel._onEVT_1DREPLOT(event) |
---|
97 | self.parent.show_panel(panel.uid) |
---|
98 | # Create a new plot panel if none was available |
---|
99 | if not is_available: |
---|
100 | #print"event.plot",hasattr(event.plot,'data') |
---|
101 | if not hasattr(event.plot, 'data'): |
---|
102 | from Plotter1D import ModelPanel1D |
---|
103 | ## get the data representation label of the data to plot |
---|
104 | ## when even the user select "change scale" |
---|
105 | if hasattr(event.plot, "xtransform"): |
---|
106 | xtransform = event.plot.xtransform |
---|
107 | else: |
---|
108 | xtransform = None |
---|
109 | |
---|
110 | if hasattr(event.plot, "ytransform"): |
---|
111 | ytransform = event.plot.ytransform |
---|
112 | else: |
---|
113 | ytransform = None |
---|
114 | ## create a plotpanel for 1D Data |
---|
115 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
116 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
117 | else: |
---|
118 | ##Create a new plotpanel for 2D data |
---|
119 | from Plotter2D import ModelPanel2D |
---|
120 | if hasattr(event.plot, "scale"): |
---|
121 | scale = event.plot.scale |
---|
122 | else: |
---|
123 | scale = 'log' |
---|
124 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
125 | data2d=event.plot, scale = scale, |
---|
126 | style=wx.RAISED_BORDER) |
---|
127 | ## Set group ID if available |
---|
128 | ## Assign data properties to the new create panel |
---|
129 | group_id_str = '' |
---|
130 | if hasattr(event.plot, "group_id"): |
---|
131 | if not event.plot.group_id == None: |
---|
132 | new_panel.group_id = event.plot.group_id |
---|
133 | group_id_str = ' [%s]' % event.plot.group_id |
---|
134 | if hasattr(event, "title"): |
---|
135 | new_panel.window_caption = event.title |
---|
136 | new_panel.window_name = event.title |
---|
137 | event_id = self.parent.popup_panel(new_panel) |
---|
138 | #remove the default item in the menu |
---|
139 | if len(self.plot_panels) == 0: |
---|
140 | self.menu.RemoveItem(self.menu.FindItemByPosition(0)) |
---|
141 | self.menu.Append(event_id, new_panel.window_caption, |
---|
142 | "Show %s plot panel" % new_panel.window_caption) |
---|
143 | # Set UID to allow us to reference the panel later |
---|
144 | new_panel.uid = event_id |
---|
145 | # Ship the plottable to its panel |
---|
146 | new_panel._onEVT_1DREPLOT(event) |
---|
147 | self.plot_panels.append(new_panel) |
---|
148 | |
---|
149 | return |
---|
150 | |
---|