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 graph 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 | # Plot menu |
---|
38 | self.menu = None |
---|
39 | |
---|
40 | |
---|
41 | def set_panel_on_focus(self, panel): |
---|
42 | """ |
---|
43 | """ |
---|
44 | self._panel_on_focus = panel |
---|
45 | |
---|
46 | def is_always_active(self): |
---|
47 | """ |
---|
48 | return True is this plugin is always active even if the user is |
---|
49 | switching between perspectives |
---|
50 | """ |
---|
51 | return True |
---|
52 | |
---|
53 | def populate_menu(self, parent): |
---|
54 | """ |
---|
55 | Create a 'Plot' menu to list the panels |
---|
56 | available for displaying |
---|
57 | |
---|
58 | :param id: next available unique ID for wx events |
---|
59 | :param parent: parent window |
---|
60 | |
---|
61 | """ |
---|
62 | self.menu = wx.Menu() |
---|
63 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
64 | "No graph available") |
---|
65 | self.menu.FindItemByPosition(0).Enable(False) |
---|
66 | return [(self.menu, "Show")] |
---|
67 | |
---|
68 | def get_panels(self, parent): |
---|
69 | """ |
---|
70 | Create and return a list of panel objects |
---|
71 | """ |
---|
72 | ## Save a reference to the parent |
---|
73 | self.parent = parent |
---|
74 | # Connect to plotting events |
---|
75 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
76 | # We have no initial panels for this plug-in |
---|
77 | return [] |
---|
78 | |
---|
79 | def _on_show_panel(self, event): |
---|
80 | """show plug-in panel""" |
---|
81 | pass |
---|
82 | |
---|
83 | def remove_plot(self, group_id, id): |
---|
84 | """ |
---|
85 | remove plot of ID = id from a panel of group ID =group_id |
---|
86 | """ |
---|
87 | |
---|
88 | if group_id in self.plot_panels.keys(): |
---|
89 | panel = self.plot_panels[group_id] |
---|
90 | panel.remove_data_by_id(id=id) |
---|
91 | return True |
---|
92 | return False |
---|
93 | |
---|
94 | def clear_panel(self): |
---|
95 | """ |
---|
96 | Clear and Hide all plot panels, and remove them from menu |
---|
97 | """ |
---|
98 | for group_id in self.plot_panels.keys(): |
---|
99 | panel = self.plot_panels[group_id] |
---|
100 | panel.graph.reset() |
---|
101 | self.hide_panel(group_id) |
---|
102 | self.plot_panels = {} |
---|
103 | item = self.menu.FindItemByPosition(0) |
---|
104 | while item != None: |
---|
105 | self.menu.DeleteItem(item) |
---|
106 | try: |
---|
107 | item = self.menu.FindItemByPosition(0) |
---|
108 | except: |
---|
109 | item = None |
---|
110 | |
---|
111 | |
---|
112 | def clear_panel_by_id(self, group_id): |
---|
113 | """ |
---|
114 | clear the graph |
---|
115 | """ |
---|
116 | if group_id in self.plot_panels.keys(): |
---|
117 | panel = self.plot_panels[group_id] |
---|
118 | panel.graph.reset() |
---|
119 | return True |
---|
120 | return False |
---|
121 | |
---|
122 | |
---|
123 | def hide_panel(self, group_id): |
---|
124 | """ |
---|
125 | hide panel with group ID = group_id |
---|
126 | """ |
---|
127 | if group_id in self.plot_panels.keys(): |
---|
128 | panel = self.plot_panels[group_id] |
---|
129 | self.parent.hide_panel(panel.uid) |
---|
130 | return True |
---|
131 | return False |
---|
132 | |
---|
133 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
134 | """ |
---|
135 | """ |
---|
136 | ## Set group ID if available |
---|
137 | ## Assign data properties to the new create panel |
---|
138 | new_panel.set_manager(self) |
---|
139 | new_panel.group_id = group_id |
---|
140 | if group_id not in data.list_group_id: |
---|
141 | data.list_group_id.append(group_id) |
---|
142 | if title is None: |
---|
143 | title = data.title |
---|
144 | new_panel.window_caption = title |
---|
145 | new_panel.window_name = data.title |
---|
146 | event_id = self.parent.popup_panel(new_panel) |
---|
147 | #remove the default item in the menu |
---|
148 | if len(self.plot_panels) == 0: |
---|
149 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
150 | if pos != -1: |
---|
151 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
152 | |
---|
153 | self.menu.AppendCheckItem(event_id, new_panel.window_caption, |
---|
154 | "Show %s plot panel" % new_panel.window_caption) |
---|
155 | self.menu.Check(event_id, True) |
---|
156 | wx.EVT_MENU(self.parent, event_id, self._on_check_menu) |
---|
157 | wx.EVT_SHOW(new_panel, self._on_close_panel) |
---|
158 | # Set UID to allow us to reference the panel later |
---|
159 | new_panel.uid = event_id |
---|
160 | # Ship the plottable to its panel |
---|
161 | new_panel.plot_data(data) |
---|
162 | self.plot_panels[new_panel.group_id] = new_panel |
---|
163 | |
---|
164 | |
---|
165 | def create_1d_panel(self, data, group_id): |
---|
166 | """ |
---|
167 | """ |
---|
168 | # Create a new plot panel if none was available |
---|
169 | if issubclass(data.__class__, Data1D): |
---|
170 | from Plotter1D import ModelPanel1D |
---|
171 | ## get the data representation label of the data to plot |
---|
172 | ## when even the user select "change scale" |
---|
173 | xtransform = data.xtransform |
---|
174 | ytransform = data.ytransform |
---|
175 | ## create a plotpanel for 1D Data |
---|
176 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
177 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
178 | return new_panel |
---|
179 | |
---|
180 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
181 | raise ValueError, msg |
---|
182 | |
---|
183 | def create_2d_panel(self, data, group_id): |
---|
184 | """ |
---|
185 | """ |
---|
186 | if issubclass(data.__class__, Data2D): |
---|
187 | ##Create a new plotpanel for 2D data |
---|
188 | from Plotter2D import ModelPanel2D |
---|
189 | scale = data.scale |
---|
190 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
191 | data2d=data, scale = scale, |
---|
192 | style=wx.RAISED_BORDER) |
---|
193 | return new_panel |
---|
194 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
195 | raise ValueError, msg |
---|
196 | |
---|
197 | def update_panel(self, data, panel): |
---|
198 | """ |
---|
199 | update the graph of a given panel |
---|
200 | """ |
---|
201 | # Check whether we already have a graph with the same units |
---|
202 | # as the plottable we just received. |
---|
203 | _, x_unit = data.get_xaxis() |
---|
204 | _, y_unit = data.get_yaxis() |
---|
205 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
206 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
207 | (x_unit != panel.graph.prop["xunit"]) |
---|
208 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
209 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
210 | (x_unit != panel.graph.prop["yunit"]) |
---|
211 | if (flag_x and flag_y): |
---|
212 | msg = "Cannot add %s" % str(data.name) |
---|
213 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
214 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
215 | raise ValueError, msg |
---|
216 | else: |
---|
217 | if panel.group_id not in data.list_group_id: |
---|
218 | data.list_group_id.append(panel.group_id) |
---|
219 | panel.plot_data( data) |
---|
220 | self.parent.show_panel(panel.uid) |
---|
221 | |
---|
222 | def delete_menu_item(self, name, uid): |
---|
223 | """ |
---|
224 | """ |
---|
225 | #remove menu item |
---|
226 | pos = self.menu.FindItem(name) |
---|
227 | if pos != -1: |
---|
228 | self.menu.Delete(uid) |
---|
229 | if self.menu.GetMenuItemCount() == 0: |
---|
230 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
231 | "No graph available") |
---|
232 | self.menu.FindItemByPosition(0).Enable(False) |
---|
233 | |
---|
234 | def delete_panel(self, group_id): |
---|
235 | """ |
---|
236 | """ |
---|
237 | if group_id in self.plot_panels.keys(): |
---|
238 | panel = self.plot_panels[group_id] |
---|
239 | #remove menu item |
---|
240 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
241 | del self.plot_panels[group_id] |
---|
242 | return True |
---|
243 | return False |
---|
244 | |
---|
245 | def _on_plot_event(self, event): |
---|
246 | """ |
---|
247 | A new plottable is being shipped to the plotting plug-in. |
---|
248 | Check whether we have a panel to put in on, or create |
---|
249 | a new one |
---|
250 | |
---|
251 | :param event: EVT_NEW_PLOT event |
---|
252 | |
---|
253 | """ |
---|
254 | if hasattr(event, 'action'): |
---|
255 | group_id = event.group_id |
---|
256 | #remove data from panel |
---|
257 | if event.action.lower() == 'remove': |
---|
258 | id = event.id |
---|
259 | return self.remove_plot(group_id, id) |
---|
260 | if event.action.lower() == 'hide': |
---|
261 | return self.hide_panel(group_id) |
---|
262 | if event.action.lower() == 'delete': |
---|
263 | return self.delete_panel(group_id) |
---|
264 | if event.action.lower() == "clear": |
---|
265 | return self.clear_panel_by_id(group_id) |
---|
266 | |
---|
267 | title = None |
---|
268 | if hasattr(event, 'title'): |
---|
269 | title = event.title |
---|
270 | |
---|
271 | data = event.plot |
---|
272 | group_id = data.group_id |
---|
273 | |
---|
274 | if group_id in self.plot_panels.keys(): |
---|
275 | #update a panel graph |
---|
276 | panel = self.plot_panels[group_id] |
---|
277 | self.update_panel(data, panel) |
---|
278 | else: |
---|
279 | #create a new panel |
---|
280 | if issubclass(data.__class__, Data1D): |
---|
281 | new_panel = self.create_1d_panel(data, group_id) |
---|
282 | else: |
---|
283 | new_panel = self.create_2d_panel(data, group_id) |
---|
284 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
285 | |
---|
286 | return |
---|
287 | |
---|
288 | def _on_check_menu(self, event): |
---|
289 | """ |
---|
290 | Check mark on menu |
---|
291 | """ |
---|
292 | #event.Skip() |
---|
293 | event_id = event.GetId() |
---|
294 | |
---|
295 | if self.menu.IsChecked(event_id): |
---|
296 | self.parent.on_view(event) |
---|
297 | self.menu.Check(event_id, True) |
---|
298 | else: |
---|
299 | self.parent.hide_panel(event_id) |
---|
300 | self.menu.Check(event_id, False) |
---|
301 | |
---|
302 | def _on_close_panel(self, event): |
---|
303 | """ |
---|
304 | Update check mark on panel's show and hide event |
---|
305 | """ |
---|
306 | event.Skip() |
---|
307 | panel = event.GetEventObject() |
---|
308 | id = panel.uid |
---|
309 | if event.GetShow() and self.parent._mgr.GetPane(panel).IsShown(): |
---|
310 | if not self.menu.IsChecked(id): |
---|
311 | self.menu.Check(id, True) |
---|
312 | #self.menu.Enable(id, False) |
---|
313 | else: |
---|
314 | try: |
---|
315 | if self.menu.IsChecked(id): |
---|
316 | self.menu.Check(id, False) |
---|
317 | #self.menu.Enable(id, True) |
---|
318 | except: |
---|
319 | pass |
---|