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