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 | else: |
---|
90 | msg = "Panel with GROUP_ID: %s cannot be located" % str(group_id) |
---|
91 | raise ValueError, msg |
---|
92 | |
---|
93 | def hide_panel(self, group_id): |
---|
94 | """ |
---|
95 | hide panel with group ID = group_id |
---|
96 | """ |
---|
97 | if group_id in self.plot_panels.keys(): |
---|
98 | panel = self.plot_panels[group_id] |
---|
99 | self.parent.hide_panel(panel.uid) |
---|
100 | return True |
---|
101 | return False |
---|
102 | |
---|
103 | def create_panel_helper(self, new_panel, data, group_id): |
---|
104 | """ |
---|
105 | """ |
---|
106 | ## Set group ID if available |
---|
107 | ## Assign data properties to the new create panel |
---|
108 | new_panel.set_manager(self) |
---|
109 | new_panel.group_id = group_id |
---|
110 | title = data.title |
---|
111 | new_panel.window_caption = title |
---|
112 | new_panel.window_name = title |
---|
113 | event_id = self.parent.popup_panel(new_panel) |
---|
114 | #remove the default item in the menu |
---|
115 | if len(self.plot_panels) == 0: |
---|
116 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
117 | if pos != -1: |
---|
118 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
119 | |
---|
120 | self.menu.Append(event_id, new_panel.window_caption, |
---|
121 | "Show %s plot panel" % new_panel.window_caption) |
---|
122 | # Set UID to allow us to reference the panel later |
---|
123 | new_panel.uid = event_id |
---|
124 | # Ship the plottable to its panel |
---|
125 | new_panel.plot_data(data) |
---|
126 | self.plot_panels[new_panel.group_id] = new_panel |
---|
127 | |
---|
128 | |
---|
129 | def create_1d_panel(self, data, group_id): |
---|
130 | """ |
---|
131 | """ |
---|
132 | # Create a new plot panel if none was available |
---|
133 | if issubclass(data.__class__, Data1D): |
---|
134 | from Plotter1D import ModelPanel1D |
---|
135 | ## get the data representation label of the data to plot |
---|
136 | ## when even the user select "change scale" |
---|
137 | xtransform = data.xtransform |
---|
138 | ytransform = data.ytransform |
---|
139 | ## create a plotpanel for 1D Data |
---|
140 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
141 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
142 | return new_panel |
---|
143 | |
---|
144 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
145 | raise ValueError, msg |
---|
146 | |
---|
147 | def create_2d_panel(self, data, group_id): |
---|
148 | """ |
---|
149 | """ |
---|
150 | if issubclass(data.__class__, Data2D): |
---|
151 | ##Create a new plotpanel for 2D data |
---|
152 | from Plotter2D import ModelPanel2D |
---|
153 | scale = data.scale |
---|
154 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
155 | data2d=data, scale = scale, |
---|
156 | style=wx.RAISED_BORDER) |
---|
157 | return new_panel |
---|
158 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
159 | raise ValueError, msg |
---|
160 | |
---|
161 | def update_panel(self, data, panel): |
---|
162 | """ |
---|
163 | update the graph of a given panel |
---|
164 | """ |
---|
165 | # Check whether we already have a graph with the same units |
---|
166 | # as the plottable we just received. |
---|
167 | _, x_unit = data.get_xaxis() |
---|
168 | _, y_unit = data.get_yaxis() |
---|
169 | if x_unit != panel.graph.prop["xunit"] \ |
---|
170 | or y_unit != panel.graph.prop["yunit"]: |
---|
171 | msg = "Cannot add %s" % str(data.name) |
---|
172 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
173 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
174 | raise ValueError, msg |
---|
175 | else: |
---|
176 | panel.plot_data( data) |
---|
177 | self.parent.show_panel(panel.uid) |
---|
178 | |
---|
179 | def delete_panel(self, group_id): |
---|
180 | """ |
---|
181 | """ |
---|
182 | if group_id in self.plot_panels.keys(): |
---|
183 | panel = self.plot_panels[group_id] |
---|
184 | #remove menu item |
---|
185 | self.menu.Delete(panel.uid) |
---|
186 | self.parent.delete_panel(panel.uid) |
---|
187 | del self.plot_panels[group_id] |
---|
188 | return True |
---|
189 | return False |
---|
190 | |
---|
191 | def _on_plot_event(self, event): |
---|
192 | """ |
---|
193 | A new plottable is being shipped to the plotting plug-in. |
---|
194 | Check whether we have a panel to put in on, or create |
---|
195 | a new one |
---|
196 | |
---|
197 | :param event: EVT_NEW_PLOT event |
---|
198 | |
---|
199 | """ |
---|
200 | if hasattr(event, 'action'): |
---|
201 | group_id = event.group_id |
---|
202 | #remove data from panel |
---|
203 | if event.action.lower() == 'remove': |
---|
204 | id = event.id |
---|
205 | return self.remove_plot(group_id, id) |
---|
206 | if event.action.lower() == 'hide': |
---|
207 | return self.hide_panel(group_id) |
---|
208 | if event.action.lower() == 'delete': |
---|
209 | return self.delete_panel(group_id) |
---|
210 | |
---|
211 | data = event.plot |
---|
212 | group_id_list = data.group_id |
---|
213 | group_id = None |
---|
214 | if group_id_list: |
---|
215 | group_id = group_id_list[len(group_id_list)-1] |
---|
216 | |
---|
217 | if group_id in self.plot_panels.keys(): |
---|
218 | #update a panel graph |
---|
219 | panel = self.plot_panels[group_id] |
---|
220 | self.update_panel(data, panel) |
---|
221 | else: |
---|
222 | #create a new panel |
---|
223 | if issubclass(data.__class__, Data1D): |
---|
224 | new_panel = self.create_1d_panel(data, group_id) |
---|
225 | else: |
---|
226 | new_panel = self.create_2d_panel(data, group_id) |
---|
227 | self.create_panel_helper(new_panel, data, group_id) |
---|
228 | |
---|
229 | return |
---|
230 | |
---|