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