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