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