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 wx.__version__.split('.')[0] == 2: |
---|
29 | if 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 | return True |
---|
100 | return False |
---|
101 | |
---|
102 | def clear_panel(self): |
---|
103 | """ |
---|
104 | Clear and Hide all plot panels, and remove them from menu |
---|
105 | """ |
---|
106 | for group_id in self.plot_panels.keys(): |
---|
107 | panel = self.plot_panels[group_id] |
---|
108 | panel.graph.reset() |
---|
109 | self.hide_panel(group_id) |
---|
110 | self.plot_panels = {} |
---|
111 | item = self.menu.FindItemByPosition(0) |
---|
112 | while item != None: |
---|
113 | self.menu.DeleteItem(item) |
---|
114 | try: |
---|
115 | item = self.menu.FindItemByPosition(0) |
---|
116 | except: |
---|
117 | item = None |
---|
118 | |
---|
119 | |
---|
120 | def clear_panel_by_id(self, group_id): |
---|
121 | """ |
---|
122 | clear the graph |
---|
123 | """ |
---|
124 | if group_id in self.plot_panels.keys(): |
---|
125 | panel = self.plot_panels[group_id] |
---|
126 | panel.graph.reset() |
---|
127 | return True |
---|
128 | return False |
---|
129 | |
---|
130 | |
---|
131 | def hide_panel(self, group_id): |
---|
132 | """ |
---|
133 | hide panel with group ID = group_id |
---|
134 | """ |
---|
135 | if group_id in self.plot_panels.keys(): |
---|
136 | panel = self.plot_panels[group_id] |
---|
137 | self.parent.hide_panel(panel.uid) |
---|
138 | return True |
---|
139 | return False |
---|
140 | |
---|
141 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
142 | """ |
---|
143 | """ |
---|
144 | ## Set group ID if available |
---|
145 | ## Assign data properties to the new create panel |
---|
146 | new_panel.set_manager(self) |
---|
147 | new_panel.group_id = group_id |
---|
148 | if group_id not in data.list_group_id: |
---|
149 | data.list_group_id.append(group_id) |
---|
150 | if title is None: |
---|
151 | title = data.title |
---|
152 | new_panel.window_caption = title |
---|
153 | new_panel.window_name = data.title |
---|
154 | event_id = self.parent.popup_panel(new_panel) |
---|
155 | #remove the default item in the menu |
---|
156 | if len(self.plot_panels) == 0: |
---|
157 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
158 | if pos != -1: |
---|
159 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
160 | |
---|
161 | self.menu.AppendCheckItem(event_id, new_panel.window_caption, |
---|
162 | "Show %s plot panel" % new_panel.window_caption) |
---|
163 | self.menu.Check(event_id, IS_WIN) |
---|
164 | wx.EVT_MENU(self.parent, event_id, self._on_check_menu) |
---|
165 | |
---|
166 | wx.EVT_SHOW(new_panel, self._on_close_panel) |
---|
167 | |
---|
168 | # Set UID to allow us to reference the panel later |
---|
169 | new_panel.uid = event_id |
---|
170 | # Ship the plottable to its panel |
---|
171 | new_panel.plot_data(data) |
---|
172 | self.plot_panels[new_panel.group_id] = new_panel |
---|
173 | |
---|
174 | |
---|
175 | def create_1d_panel(self, data, group_id): |
---|
176 | """ |
---|
177 | """ |
---|
178 | # Create a new plot panel if none was available |
---|
179 | if issubclass(data.__class__, Data1D): |
---|
180 | from Plotter1D import ModelPanel1D |
---|
181 | ## get the data representation label of the data to plot |
---|
182 | ## when even the user select "change scale" |
---|
183 | xtransform = data.xtransform |
---|
184 | ytransform = data.ytransform |
---|
185 | ## create a plotpanel for 1D Data |
---|
186 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
187 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
188 | return new_panel |
---|
189 | |
---|
190 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
191 | raise ValueError, msg |
---|
192 | |
---|
193 | def create_2d_panel(self, data, group_id): |
---|
194 | """ |
---|
195 | """ |
---|
196 | if issubclass(data.__class__, Data2D): |
---|
197 | ##Create a new plotpanel for 2D data |
---|
198 | from Plotter2D import ModelPanel2D |
---|
199 | scale = data.scale |
---|
200 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
201 | data2d=data, scale = scale, |
---|
202 | style=wx.RAISED_BORDER) |
---|
203 | return new_panel |
---|
204 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
205 | raise ValueError, msg |
---|
206 | |
---|
207 | def update_panel(self, data, panel): |
---|
208 | """ |
---|
209 | update the graph of a given panel |
---|
210 | """ |
---|
211 | # Check whether we already have a graph with the same units |
---|
212 | # as the plottable we just received. |
---|
213 | _, x_unit = data.get_xaxis() |
---|
214 | _, y_unit = data.get_yaxis() |
---|
215 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
216 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
217 | (x_unit != panel.graph.prop["xunit"]) |
---|
218 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
219 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
220 | (y_unit != panel.graph.prop["yunit"]) |
---|
221 | if (flag_x and flag_y): |
---|
222 | msg = "Cannot add %s" % str(data.name) |
---|
223 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
224 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
225 | raise ValueError, msg |
---|
226 | else: |
---|
227 | if panel.group_id not in data.list_group_id: |
---|
228 | data.list_group_id.append(panel.group_id) |
---|
229 | panel.plot_data( data) |
---|
230 | self.parent.show_panel(panel.uid) |
---|
231 | |
---|
232 | def delete_menu_item(self, name, uid): |
---|
233 | """ |
---|
234 | """ |
---|
235 | #remove menu item |
---|
236 | pos = self.menu.FindItem(name) |
---|
237 | if pos != -1: |
---|
238 | self.menu.Delete(uid) |
---|
239 | if self.menu.GetMenuItemCount() == 0: |
---|
240 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
241 | "No graph available") |
---|
242 | self.menu.FindItemByPosition(0).Enable(False) |
---|
243 | |
---|
244 | def delete_panel(self, group_id): |
---|
245 | """ |
---|
246 | """ |
---|
247 | if group_id in self.plot_panels.keys(): |
---|
248 | panel = self.plot_panels[group_id] |
---|
249 | uid = panel.uid |
---|
250 | wx.PostEvent(self.parent, |
---|
251 | DeletePlotPanelEvent(name=panel.window_caption, |
---|
252 | caption=panel.window_caption)) |
---|
253 | #remove menu item |
---|
254 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
255 | del self.plot_panels[group_id] |
---|
256 | self.parent.delete_panel(uid) |
---|
257 | return True |
---|
258 | return False |
---|
259 | |
---|
260 | def _on_plot_event(self, event): |
---|
261 | """ |
---|
262 | A new plottable is being shipped to the plotting plug-in. |
---|
263 | Check whether we have a panel to put in on, or create |
---|
264 | a new one |
---|
265 | |
---|
266 | :param event: EVT_NEW_PLOT event |
---|
267 | |
---|
268 | """ |
---|
269 | if hasattr(event, 'action'): |
---|
270 | group_id = event.group_id |
---|
271 | #remove data from panel |
---|
272 | if event.action.lower() == 'remove': |
---|
273 | id = event.id |
---|
274 | return self.remove_plot(group_id, id) |
---|
275 | if event.action.lower() == 'hide': |
---|
276 | return self.hide_panel(group_id) |
---|
277 | if event.action.lower() == 'delete': |
---|
278 | return self.delete_panel(group_id) |
---|
279 | if event.action.lower() == "clear": |
---|
280 | return self.clear_panel_by_id(group_id) |
---|
281 | |
---|
282 | title = None |
---|
283 | if hasattr(event, 'title'): |
---|
284 | title = event.title |
---|
285 | |
---|
286 | data = event.plot |
---|
287 | group_id = data.group_id |
---|
288 | |
---|
289 | if group_id in self.plot_panels.keys(): |
---|
290 | #update a panel graph |
---|
291 | panel = self.plot_panels[group_id] |
---|
292 | self.update_panel(data, panel) |
---|
293 | else: |
---|
294 | #create a new panel |
---|
295 | if issubclass(data.__class__, Data1D): |
---|
296 | new_panel = self.create_1d_panel(data, group_id) |
---|
297 | else: |
---|
298 | new_panel = self.create_2d_panel(data, group_id) |
---|
299 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
300 | |
---|
301 | return |
---|
302 | |
---|
303 | def _on_check_menu(self, event): |
---|
304 | """ |
---|
305 | Check mark on menu |
---|
306 | """ |
---|
307 | #event.Skip() |
---|
308 | event_id = event.GetId() |
---|
309 | |
---|
310 | if self.menu.IsChecked(event_id): |
---|
311 | self.parent.on_view(event) |
---|
312 | self.menu.Check(event_id, IS_WIN) |
---|
313 | else: |
---|
314 | self.parent.hide_panel(event_id) |
---|
315 | self.menu.Check(event_id, False) |
---|
316 | |
---|
317 | def help(self, evt): |
---|
318 | """ |
---|
319 | Show a general help dialog. |
---|
320 | """ |
---|
321 | from help_panel import HelpWindow |
---|
322 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
323 | frame.Show(True) |
---|
324 | |
---|
325 | def _on_close_panel(self, event): |
---|
326 | """ |
---|
327 | Update check mark on panel's show and hide event |
---|
328 | """ |
---|
329 | event.Skip() |
---|
330 | panel = event.GetEventObject() |
---|
331 | id = panel.uid |
---|
332 | if event.GetShow() and self.parent._mgr.GetPane(panel).IsShown(): |
---|
333 | try: |
---|
334 | if self.menu.FindItemById(id) != None: |
---|
335 | if not self.menu.IsChecked(id): |
---|
336 | self.menu.Check(id, True) |
---|
337 | #self.menu.Enable(id, False) |
---|
338 | except: |
---|
339 | # when no such item; may happens after delete |
---|
340 | pass |
---|
341 | else: |
---|
342 | try: |
---|
343 | if self.menu.FindItemById(id) != None: |
---|
344 | if self.menu.IsChecked(id): |
---|
345 | self.menu.Check(id, False) |
---|
346 | #self.menu.Enable(id, True) |
---|
347 | except: |
---|
348 | pass |
---|