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 copy import deepcopy |
---|
17 | from sas.sasgui.guiframe.events import EVT_NEW_PLOT |
---|
18 | from sas.sasgui.guiframe.events import EVT_PLOT_QRANGE |
---|
19 | from sas.sasgui.guiframe.events import EVT_PLOT_LIM |
---|
20 | from sas.sasgui.guiframe.events import DeletePlotPanelEvent |
---|
21 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
22 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
23 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
24 | from sas.sasgui.guiframe.gui_manager import MDIFrame |
---|
25 | DEFAULT_MENU_ITEM_LABEL = "No graph available" |
---|
26 | DEFAULT_MENU_ITEM_ID = wx.NewId() |
---|
27 | |
---|
28 | IS_WIN = True |
---|
29 | if sys.platform.count("win32") == 0: |
---|
30 | if int(str(wx.__version__).split('.')[0]) == 2: |
---|
31 | if int(str(wx.__version__).split('.')[1]) < 9: |
---|
32 | IS_WIN = False |
---|
33 | |
---|
34 | |
---|
35 | class Plugin(PluginBase): |
---|
36 | """ |
---|
37 | Plug-in class to be instantiated by the GUI manager |
---|
38 | """ |
---|
39 | |
---|
40 | def __init__(self): |
---|
41 | PluginBase.__init__(self, name="Plotting") |
---|
42 | |
---|
43 | ## Plot panels |
---|
44 | self.plot_panels = {} |
---|
45 | self._panel_on_focus = None |
---|
46 | self.menu_default_id = None |
---|
47 | # Plot menu |
---|
48 | self.menu = None |
---|
49 | |
---|
50 | |
---|
51 | def set_panel_on_focus(self, panel): |
---|
52 | """ |
---|
53 | """ |
---|
54 | self._panel_on_focus = panel |
---|
55 | |
---|
56 | def is_always_active(self): |
---|
57 | """ |
---|
58 | return True is this plugin is always active even if the user is |
---|
59 | switching between perspectives |
---|
60 | """ |
---|
61 | return True |
---|
62 | |
---|
63 | def populate_menu(self, parent): |
---|
64 | """ |
---|
65 | Create a 'Plot' menu to list the panels |
---|
66 | available for displaying |
---|
67 | |
---|
68 | :param id: next available unique ID for wx events |
---|
69 | :param parent: parent window |
---|
70 | |
---|
71 | """ |
---|
72 | return [] |
---|
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 | self.parent.Bind(EVT_PLOT_QRANGE, self._on_plot_qrange) |
---|
83 | self.parent.Bind(EVT_PLOT_LIM, self._on_plot_lim) |
---|
84 | # We have no initial panels for this plug-in |
---|
85 | return [] |
---|
86 | |
---|
87 | def _on_plot_qrange(self, event=None): |
---|
88 | """ |
---|
89 | On Qmin Qmax vertical line event |
---|
90 | """ |
---|
91 | if event is None: |
---|
92 | return |
---|
93 | if event.id in self.plot_panels.keys(): |
---|
94 | panel = self.plot_panels[event.id] |
---|
95 | elif event.group_id in self.plot_panels.keys(): |
---|
96 | panel = self.plot_panels[event.group_id] |
---|
97 | else: |
---|
98 | return |
---|
99 | panel.on_plot_qrange(event) |
---|
100 | |
---|
101 | def _on_plot_lim(self, event=None): |
---|
102 | if event is None: |
---|
103 | return |
---|
104 | if event.id in self.plot_panels.keys(): |
---|
105 | panel = self.plot_panels[event.id] |
---|
106 | elif event.group_id in self.plot_panels.keys(): |
---|
107 | panel = self.plot_panels[event.group_id] |
---|
108 | else: |
---|
109 | return |
---|
110 | if hasattr(event, 'xlim'): |
---|
111 | panel.subplot.set_xlim(event.xlim) |
---|
112 | if hasattr(event, 'ylim'): |
---|
113 | panel.subplot.set_ylim(event.ylim) |
---|
114 | |
---|
115 | |
---|
116 | def _on_show_panel(self, event): |
---|
117 | """show plug-in panel""" |
---|
118 | pass |
---|
119 | |
---|
120 | def remove_plot(self, group_id, id): |
---|
121 | """ |
---|
122 | remove plot of ID = id from a panel of group ID =group_id |
---|
123 | """ |
---|
124 | |
---|
125 | if group_id in self.plot_panels.keys(): |
---|
126 | panel = self.plot_panels[group_id] |
---|
127 | panel.remove_data_by_id(id=id) |
---|
128 | |
---|
129 | return True |
---|
130 | return False |
---|
131 | |
---|
132 | def clear_panel(self): |
---|
133 | """ |
---|
134 | Clear and Hide all plot panels, and remove them from menu |
---|
135 | """ |
---|
136 | for group_id in self.plot_panels.keys(): |
---|
137 | self.clear_panel_by_id(group_id) |
---|
138 | self.plot_panels = {} |
---|
139 | |
---|
140 | def clear_panel_by_id(self, group_id): |
---|
141 | """ |
---|
142 | clear the graph |
---|
143 | """ |
---|
144 | if group_id in self.plot_panels.keys(): |
---|
145 | panel = self.plot_panels[group_id] |
---|
146 | for plottable in panel.graph.plottables.keys(): |
---|
147 | self.remove_plot(group_id, plottable.id) |
---|
148 | panel.graph.reset() |
---|
149 | return True |
---|
150 | return False |
---|
151 | |
---|
152 | def hide_panel(self, group_id): |
---|
153 | """ |
---|
154 | hide panel with group ID = group_id |
---|
155 | """ |
---|
156 | # Not implemeted |
---|
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 | |
---|
174 | # Set UID to allow us to reference the panel later |
---|
175 | new_panel.uid = event_id |
---|
176 | # Ship the plottable to its panel |
---|
177 | wx.CallAfter(new_panel.plot_data, data) |
---|
178 | #new_panel.canvas.set_resizing(new_panel.resizing) |
---|
179 | self.plot_panels[new_panel.group_id] = new_panel |
---|
180 | |
---|
181 | def create_1d_panel(self, data, group_id): |
---|
182 | """ |
---|
183 | """ |
---|
184 | # Create a new plot panel if none was available |
---|
185 | if issubclass(data.__class__, Data1D): |
---|
186 | from Plotter1D import ModelPanel1D |
---|
187 | ## get the data representation label of the data to plot |
---|
188 | ## when even the user select "change scale" |
---|
189 | xtransform = data.xtransform |
---|
190 | ytransform = data.ytransform |
---|
191 | ## create a plotpanel for 1D Data |
---|
192 | win = MDIFrame(self.parent, None, 'None', (100, 200)) |
---|
193 | new_panel = ModelPanel1D(win, -1, xtransform=xtransform, |
---|
194 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
195 | win.set_panel(new_panel) |
---|
196 | win.Show(False) |
---|
197 | new_panel.frame = win |
---|
198 | #win.Show(True) |
---|
199 | return new_panel |
---|
200 | |
---|
201 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
202 | raise ValueError, msg |
---|
203 | |
---|
204 | def create_2d_panel(self, data, group_id): |
---|
205 | """ |
---|
206 | """ |
---|
207 | if issubclass(data.__class__, Data2D): |
---|
208 | ##Create a new plotpanel for 2D data |
---|
209 | from Plotter2D import ModelPanel2D |
---|
210 | scale = data.scale |
---|
211 | win = MDIFrame(self.parent, None, 'None', (200, 150)) |
---|
212 | win.Show(False) |
---|
213 | new_panel = ModelPanel2D(win, id=-1, |
---|
214 | data2d=data, scale=scale, |
---|
215 | style=wx.RAISED_BORDER) |
---|
216 | win.set_panel(new_panel) |
---|
217 | new_panel.frame = win |
---|
218 | return new_panel |
---|
219 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
220 | raise ValueError, msg |
---|
221 | |
---|
222 | def update_panel(self, data, panel): |
---|
223 | """ |
---|
224 | update the graph of a given panel |
---|
225 | """ |
---|
226 | # Check whether we already have a graph with the same units |
---|
227 | # as the plottable we just received. |
---|
228 | _, x_unit = data.get_xaxis() |
---|
229 | _, y_unit = data.get_yaxis() |
---|
230 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
231 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
232 | (x_unit != panel.graph.prop["xunit"]) and False |
---|
233 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
234 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
235 | (y_unit != panel.graph.prop["yunit"]) and False |
---|
236 | if flag_x and flag_y: |
---|
237 | msg = "Cannot add %s" % str(data.name) |
---|
238 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
239 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
240 | raise ValueError, msg |
---|
241 | else: |
---|
242 | if panel.group_id not in data.list_group_id: |
---|
243 | data.list_group_id.append(panel.group_id) |
---|
244 | wx.CallAfter(panel.plot_data, data) |
---|
245 | |
---|
246 | def delete_panel(self, group_id): |
---|
247 | """ |
---|
248 | """ |
---|
249 | if group_id in self.plot_panels.keys(): |
---|
250 | panel = self.plot_panels[group_id] |
---|
251 | uid = panel.uid |
---|
252 | wx.PostEvent(self.parent, |
---|
253 | DeletePlotPanelEvent(name=panel.window_caption, |
---|
254 | caption=panel.window_caption)) |
---|
255 | del self.plot_panels[group_id] |
---|
256 | if uid in self.parent.plot_panels.keys(): |
---|
257 | del self.parent.plot_panels[uid] |
---|
258 | panel.frame.Destroy() |
---|
259 | return True |
---|
260 | |
---|
261 | return False |
---|
262 | |
---|
263 | def _on_plot_event(self, event): |
---|
264 | """ |
---|
265 | A new plottable is being shipped to the plotting plug-in. |
---|
266 | Check whether we have a panel to put in on, or create |
---|
267 | a new one |
---|
268 | |
---|
269 | :param event: EVT_NEW_PLOT event |
---|
270 | |
---|
271 | """ |
---|
272 | action_check = False |
---|
273 | if hasattr(event, 'action'): |
---|
274 | action_string = event.action.lower().strip() |
---|
275 | if action_string == 'check': |
---|
276 | action_check = True |
---|
277 | else: |
---|
278 | if action_string == 'update': |
---|
279 | # Update all existing plots of data with this ID |
---|
280 | for data in event.plots: |
---|
281 | for panel in self.plot_panels.values(): |
---|
282 | if data.id in panel.plots.keys(): |
---|
283 | plot_exists = True |
---|
284 | # Pass each panel it's own copy of the data |
---|
285 | # that's being updated, otherwise things like |
---|
286 | # colour and line thickness are unintentionally |
---|
287 | # synced across panels |
---|
288 | self.update_panel(deepcopy(data), panel) |
---|
289 | return |
---|
290 | |
---|
291 | group_id = event.group_id |
---|
292 | if group_id in self.plot_panels: |
---|
293 | #remove data from panel |
---|
294 | if action_string == 'remove': |
---|
295 | return self.remove_plot(group_id, event.id) |
---|
296 | if action_string == 'hide': |
---|
297 | return self.hide_panel(group_id) |
---|
298 | if action_string == 'delete': |
---|
299 | panel = self.plot_panels[group_id] |
---|
300 | uid = panel.uid |
---|
301 | return self.parent.delete_panel(uid) |
---|
302 | if action_string == "clear": |
---|
303 | return self.clear_panel_by_id(group_id) |
---|
304 | |
---|
305 | if not hasattr(event, 'plot'): |
---|
306 | return |
---|
307 | title = None |
---|
308 | if hasattr(event, 'title'): |
---|
309 | title = 'Graph' #event.title |
---|
310 | data = event.plot |
---|
311 | group_id = data.group_id |
---|
312 | if group_id in self.plot_panels.keys(): |
---|
313 | if action_check: |
---|
314 | # Check if the plot already exist. if it does, do nothing. |
---|
315 | if data.id in self.plot_panels[group_id].plots.keys(): |
---|
316 | return |
---|
317 | #update a panel graph |
---|
318 | panel = self.plot_panels[group_id] |
---|
319 | self.update_panel(data, panel) |
---|
320 | else: |
---|
321 | #create a new panel |
---|
322 | if issubclass(data.__class__, Data1D): |
---|
323 | new_panel = self.create_1d_panel(data, group_id) |
---|
324 | else: |
---|
325 | # Need to make the group_id consistent with 1D thus no if below |
---|
326 | if len(self.plot_panels.values()) > 0: |
---|
327 | for p_group_id in self.plot_panels.keys(): |
---|
328 | p_plot = self.plot_panels[p_group_id] |
---|
329 | if data.id in p_plot.plots.keys(): |
---|
330 | p_plot.plots[data.id] = data |
---|
331 | self.plot_panels[group_id] = p_plot |
---|
332 | if group_id != p_group_id: |
---|
333 | del self.plot_panels[p_group_id] |
---|
334 | if p_group_id in data.list_group_id: |
---|
335 | data.list_group_id.remove(p_group_id) |
---|
336 | if group_id not in data.list_group_id: |
---|
337 | data.list_group_id.append(group_id) |
---|
338 | p_plot.group_id = group_id |
---|
339 | return |
---|
340 | |
---|
341 | new_panel = self.create_2d_panel(data, group_id) |
---|
342 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
343 | if hasattr(event, 'xlim'): |
---|
344 | new_panel.subplot.set_xlim(event.xlim) |
---|
345 | if hasattr(event, 'ylim'): |
---|
346 | new_panel.subplot.set_ylim(event.ylim) |
---|
347 | return |
---|