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 | from sans.guiframe.dataFitting import Data1D |
---|
20 | from sans.guiframe.dataFitting import Data2D |
---|
21 | |
---|
22 | DEFAULT_MENU_ITEM_LABEL = "No plot available" |
---|
23 | DEFAULT_MENU_ITEM_ID = wx.NewId() |
---|
24 | |
---|
25 | class Plugin(PluginBase): |
---|
26 | """ |
---|
27 | Plug-in class to be instantiated by the GUI manager |
---|
28 | """ |
---|
29 | |
---|
30 | def __init__(self, standalone=False): |
---|
31 | PluginBase.__init__(self, name="Plotting", standalone=standalone) |
---|
32 | |
---|
33 | ## Plot panels |
---|
34 | self.plot_panels = {} |
---|
35 | self._panel_on_focus = None |
---|
36 | self.menu_default_id = None |
---|
37 | |
---|
38 | def set_panel_on_focus(self, panel): |
---|
39 | """ |
---|
40 | """ |
---|
41 | self._panel_on_focus = panel |
---|
42 | |
---|
43 | def is_always_active(self): |
---|
44 | """ |
---|
45 | return True is this plugin is always active even if the user is |
---|
46 | switching between perspectives |
---|
47 | """ |
---|
48 | return True |
---|
49 | |
---|
50 | def populate_menu(self, parent): |
---|
51 | """ |
---|
52 | Create a 'Plot' menu to list the panels |
---|
53 | available for displaying |
---|
54 | |
---|
55 | :param id: next available unique ID for wx events |
---|
56 | :param parent: parent window |
---|
57 | |
---|
58 | """ |
---|
59 | self.menu = wx.Menu() |
---|
60 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
61 | "No plot available") |
---|
62 | self.menu.FindItemByPosition(0).Enable(False) |
---|
63 | return [(self.menu, "Plot")] |
---|
64 | |
---|
65 | def get_panels(self, parent): |
---|
66 | """ |
---|
67 | Create and return a list of panel objects |
---|
68 | """ |
---|
69 | ## Save a reference to the parent |
---|
70 | self.parent = parent |
---|
71 | # Connect to plotting events |
---|
72 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
73 | # We have no initial panels for this plug-in |
---|
74 | return [] |
---|
75 | |
---|
76 | def _on_show_panel(self, event): |
---|
77 | """show plug-in panel""" |
---|
78 | pass |
---|
79 | |
---|
80 | def remove_plot(self, group_id, id): |
---|
81 | """ |
---|
82 | remove plot of ID = id from a panel of group ID =group_id |
---|
83 | """ |
---|
84 | |
---|
85 | if group_id in self.plot_panels.keys(): |
---|
86 | panel = self.plot_panels[group_id] |
---|
87 | panel.remove_data_by_id(id=id) |
---|
88 | return True |
---|
89 | else: |
---|
90 | msg = "Panel with GROUP_ID: %s cannot be located" % str(group_id) |
---|
91 | raise ValueError, msg |
---|
92 | |
---|
93 | def hide_panel(self, group_id): |
---|
94 | """ |
---|
95 | hide panel with group ID = group_id |
---|
96 | """ |
---|
97 | if group_id in self.plot_panels.keys(): |
---|
98 | panel = self.plot_panels[group_id] |
---|
99 | self.parent.hide_panel(panel.uid) |
---|
100 | return True |
---|
101 | return False |
---|
102 | |
---|
103 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
104 | """ |
---|
105 | """ |
---|
106 | ## Set group ID if available |
---|
107 | ## Assign data properties to the new create panel |
---|
108 | new_panel.set_manager(self) |
---|
109 | new_panel.group_id = group_id |
---|
110 | if group_id not in data.list_group_id: |
---|
111 | data.list_group_id.append(group_id) |
---|
112 | if title is None: |
---|
113 | title = data.title |
---|
114 | new_panel.window_caption = title |
---|
115 | new_panel.window_name = data.title |
---|
116 | event_id = self.parent.popup_panel(new_panel) |
---|
117 | #remove the default item in the menu |
---|
118 | if len(self.plot_panels) == 0: |
---|
119 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
120 | if pos != -1: |
---|
121 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
122 | |
---|
123 | self.menu.Append(event_id, new_panel.window_caption, |
---|
124 | "Show %s plot panel" % new_panel.window_caption) |
---|
125 | # Set UID to allow us to reference the panel later |
---|
126 | new_panel.uid = event_id |
---|
127 | # Ship the plottable to its panel |
---|
128 | new_panel.plot_data(data) |
---|
129 | self.plot_panels[new_panel.group_id] = new_panel |
---|
130 | |
---|
131 | |
---|
132 | def create_1d_panel(self, data, group_id): |
---|
133 | """ |
---|
134 | """ |
---|
135 | # Create a new plot panel if none was available |
---|
136 | if issubclass(data.__class__, Data1D): |
---|
137 | from Plotter1D import ModelPanel1D |
---|
138 | ## get the data representation label of the data to plot |
---|
139 | ## when even the user select "change scale" |
---|
140 | xtransform = data.xtransform |
---|
141 | ytransform = data.ytransform |
---|
142 | ## create a plotpanel for 1D Data |
---|
143 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
144 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
145 | return new_panel |
---|
146 | |
---|
147 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
148 | raise ValueError, msg |
---|
149 | |
---|
150 | def create_2d_panel(self, data, group_id): |
---|
151 | """ |
---|
152 | """ |
---|
153 | if issubclass(data.__class__, Data2D): |
---|
154 | ##Create a new plotpanel for 2D data |
---|
155 | from Plotter2D import ModelPanel2D |
---|
156 | scale = data.scale |
---|
157 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
158 | data2d=data, scale = scale, |
---|
159 | style=wx.RAISED_BORDER) |
---|
160 | return new_panel |
---|
161 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
162 | raise ValueError, msg |
---|
163 | |
---|
164 | def update_panel(self, data, panel): |
---|
165 | """ |
---|
166 | update the graph of a given panel |
---|
167 | """ |
---|
168 | # Check whether we already have a graph with the same units |
---|
169 | # as the plottable we just received. |
---|
170 | _, x_unit = data.get_xaxis() |
---|
171 | _, y_unit = data.get_yaxis() |
---|
172 | if x_unit != panel.graph.prop["xunit"] \ |
---|
173 | or y_unit != panel.graph.prop["yunit"]: |
---|
174 | msg = "Cannot add %s" % str(data.name) |
---|
175 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
176 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
177 | raise ValueError, msg |
---|
178 | else: |
---|
179 | if panel.group_id not in data.list_group_id: |
---|
180 | data.list_group_id.append(panel.group_id) |
---|
181 | panel.plot_data( data) |
---|
182 | self.parent.show_panel(panel.uid) |
---|
183 | |
---|
184 | def delete_menu_item(self, name, uid): |
---|
185 | """ |
---|
186 | """ |
---|
187 | #remove menu item |
---|
188 | pos = self.menu.FindItem(name) |
---|
189 | if pos != -1: |
---|
190 | self.menu.Delete(uid) |
---|
191 | if self.menu.GetMenuItemCount() == 0: |
---|
192 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
193 | "No plot available") |
---|
194 | self.menu.FindItemByPosition(0).Enable(False) |
---|
195 | |
---|
196 | def delete_panel(self, group_id): |
---|
197 | """ |
---|
198 | """ |
---|
199 | if group_id in self.plot_panels.keys(): |
---|
200 | panel = self.plot_panels[group_id] |
---|
201 | #remove menu item |
---|
202 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
203 | del self.plot_panels[group_id] |
---|
204 | return True |
---|
205 | return False |
---|
206 | |
---|
207 | def _on_plot_event(self, event): |
---|
208 | """ |
---|
209 | A new plottable is being shipped to the plotting plug-in. |
---|
210 | Check whether we have a panel to put in on, or create |
---|
211 | a new one |
---|
212 | |
---|
213 | :param event: EVT_NEW_PLOT event |
---|
214 | |
---|
215 | """ |
---|
216 | if hasattr(event, 'action'): |
---|
217 | group_id = event.group_id |
---|
218 | #remove data from panel |
---|
219 | if event.action.lower() == 'remove': |
---|
220 | id = event.id |
---|
221 | return self.remove_plot(group_id, id) |
---|
222 | if event.action.lower() == 'hide': |
---|
223 | return self.hide_panel(group_id) |
---|
224 | if event.action.lower() == 'delete': |
---|
225 | return self.delete_panel(group_id) |
---|
226 | title = None |
---|
227 | if hasattr(event, 'title'): |
---|
228 | title = event.title |
---|
229 | |
---|
230 | data = event.plot |
---|
231 | group_id = data.group_id |
---|
232 | |
---|
233 | if group_id in self.plot_panels.keys(): |
---|
234 | #update a panel graph |
---|
235 | panel = self.plot_panels[group_id] |
---|
236 | self.update_panel(data, panel) |
---|
237 | else: |
---|
238 | #create a new panel |
---|
239 | if issubclass(data.__class__, Data1D): |
---|
240 | new_panel = self.create_1d_panel(data, group_id) |
---|
241 | else: |
---|
242 | new_panel = self.create_2d_panel(data, group_id) |
---|
243 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
244 | |
---|
245 | return |
---|
246 | |
---|