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