1 | """ |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | See the license text in license.txt |
---|
7 | |
---|
8 | copyright 2008, University of Tennessee |
---|
9 | """ |
---|
10 | |
---|
11 | |
---|
12 | import wx |
---|
13 | import sys |
---|
14 | from sans.guitools.PlotPanel import PlotPanel |
---|
15 | from sans.guitools.plottables import Graph |
---|
16 | from sans.guicomm.events import EVT_NEW_PLOT |
---|
17 | |
---|
18 | class PanelMenu(wx.Menu): |
---|
19 | plots = None |
---|
20 | |
---|
21 | def set_plots(self, plots): |
---|
22 | self.plots = plots |
---|
23 | |
---|
24 | |
---|
25 | class View1DPanel(PlotPanel): |
---|
26 | """ |
---|
27 | Plot panel for use with the GUI manager |
---|
28 | """ |
---|
29 | |
---|
30 | ## Internal name for the AUI manager |
---|
31 | window_name = "plotpanel" |
---|
32 | ## Title to appear on top of the window |
---|
33 | window_caption = "Plot Panel" |
---|
34 | ## Flag to tell the GUI manager that this panel is not |
---|
35 | # tied to any perspective |
---|
36 | ALWAYS_ON = True |
---|
37 | |
---|
38 | def __init__(self, parent, id = -1, color = None,\ |
---|
39 | dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
40 | """ |
---|
41 | Initialize the panel |
---|
42 | """ |
---|
43 | PlotPanel.__init__(self, parent, id = id, style = style, **kwargs) |
---|
44 | |
---|
45 | ## Reference to the parent window |
---|
46 | self.parent = parent |
---|
47 | ## Plottables |
---|
48 | self.plots = {} |
---|
49 | |
---|
50 | ## Unique ID (from gui_manager) |
---|
51 | self.uid = None |
---|
52 | |
---|
53 | ## Graph |
---|
54 | self.graph = Graph() |
---|
55 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
56 | self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
57 | self.graph.render(self) |
---|
58 | |
---|
59 | def _onEVT_1DREPLOT(self, event): |
---|
60 | """ |
---|
61 | Data is ready to be displayed |
---|
62 | @param event: data event |
---|
63 | """ |
---|
64 | #TODO: Check for existence of plot attribute |
---|
65 | |
---|
66 | is_new = True |
---|
67 | if event.plot.name in self.plots.keys(): |
---|
68 | is_new = False |
---|
69 | |
---|
70 | if is_new: |
---|
71 | self.plots[event.plot.name] = event.plot |
---|
72 | self.graph.add(self.plots[event.plot.name]) |
---|
73 | else: |
---|
74 | self.plots[event.plot.name].x = event.plot.x |
---|
75 | self.plots[event.plot.name].y = event.plot.y |
---|
76 | self.plots[event.plot.name].dy = event.plot.dy |
---|
77 | if hasattr(event.plot, 'dx') and hasattr(self.plots[event.plot.name], 'dx'): |
---|
78 | self.plots[event.plot.name].dx = event.plot.dx |
---|
79 | |
---|
80 | |
---|
81 | # Check axis labels |
---|
82 | #TODO: Should re-factor this |
---|
83 | if event.plot._xunit != self.graph.prop["xunit"]: |
---|
84 | self.graph.xaxis(event.plot._xaxis, event.plot._xunit) |
---|
85 | |
---|
86 | if event.plot._yunit != self.graph.prop["yunit"]: |
---|
87 | self.graph.yaxis(event.plot._yaxis, event.plot._yunit) |
---|
88 | |
---|
89 | self.graph.render(self) |
---|
90 | self.subplot.figure.canvas.draw_idle() |
---|
91 | |
---|
92 | def onContextMenu(self, event): |
---|
93 | """ |
---|
94 | 1D plot context menu |
---|
95 | @param event: wx context event |
---|
96 | """ |
---|
97 | #slicerpop = wx.Menu() |
---|
98 | slicerpop = PanelMenu() |
---|
99 | slicerpop.set_plots(self.plots) |
---|
100 | |
---|
101 | # Various plot options |
---|
102 | id = wx.NewId() |
---|
103 | slicerpop.Append(id,'&Save image', 'Save image as PNG') |
---|
104 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
105 | |
---|
106 | slicerpop.AppendSeparator() |
---|
107 | item_list = self.parent.get_context_menu() |
---|
108 | if not item_list==None: |
---|
109 | for item in item_list: |
---|
110 | try: |
---|
111 | id = wx.NewId() |
---|
112 | slicerpop.Append(id, item[0], item[1]) |
---|
113 | wx.EVT_MENU(self, id, item[2]) |
---|
114 | except: |
---|
115 | print sys.exc_value |
---|
116 | print RuntimeError, "View1DPanel.onContextMenu: bad menu item" |
---|
117 | |
---|
118 | slicerpop.AppendSeparator() |
---|
119 | |
---|
120 | id = wx.NewId() |
---|
121 | slicerpop.Append(id, '&Toggle Linear/Log scale') |
---|
122 | wx.EVT_MENU(self, id, self._onToggleScale) |
---|
123 | |
---|
124 | pos = event.GetPosition() |
---|
125 | pos = self.ScreenToClient(pos) |
---|
126 | self.PopupMenu(slicerpop, pos) |
---|
127 | |
---|
128 | def _onToggleScale(self, event): |
---|
129 | if self.get_yscale() == 'log': |
---|
130 | self.set_yscale('linear') |
---|
131 | else: |
---|
132 | self.set_yscale('log') |
---|
133 | self.subplot.figure.canvas.draw_idle() |
---|
134 | |
---|
135 | class Plugin: |
---|
136 | """ |
---|
137 | Plug-in class to be instantiated by the GUI manager |
---|
138 | """ |
---|
139 | |
---|
140 | def __init__(self): |
---|
141 | """ |
---|
142 | Initialize the plug-in |
---|
143 | """ |
---|
144 | ## Plug-in name |
---|
145 | self.sub_menu = "Plotting" |
---|
146 | |
---|
147 | ## Reference to the parent window |
---|
148 | self.parent = None |
---|
149 | |
---|
150 | ## List of panels for the simulation perspective (names) |
---|
151 | self.perspective = [] |
---|
152 | |
---|
153 | ## Plot panels |
---|
154 | self.plot_panels = [] |
---|
155 | |
---|
156 | |
---|
157 | def populate_menu(self, id, parent): |
---|
158 | """ |
---|
159 | Create a 'Plot' menu to list the panels |
---|
160 | available for displaying |
---|
161 | @param id: next available unique ID for wx events |
---|
162 | @param parent: parent window |
---|
163 | """ |
---|
164 | self.menu = wx.Menu() |
---|
165 | return [(id, self.menu, "Plot")] |
---|
166 | |
---|
167 | |
---|
168 | def get_panels(self, parent): |
---|
169 | """ |
---|
170 | Create and return a list of panel objects |
---|
171 | """ |
---|
172 | ## Save a reference to the parent |
---|
173 | self.parent = parent |
---|
174 | # Connect to plotting events |
---|
175 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
176 | |
---|
177 | # We have no initial panels for this plug-in |
---|
178 | return [] |
---|
179 | |
---|
180 | def get_perspective(self): |
---|
181 | """ |
---|
182 | Get the list of panel names for this perspective |
---|
183 | """ |
---|
184 | return self.perspective |
---|
185 | |
---|
186 | def on_perspective(self, event): |
---|
187 | """ |
---|
188 | Call back function for the perspective menu item. |
---|
189 | We notify the parent window that the perspective |
---|
190 | has changed. |
---|
191 | @param event: menu event |
---|
192 | """ |
---|
193 | self.parent.set_perspective(self.perspective) |
---|
194 | |
---|
195 | def post_init(self): |
---|
196 | """ |
---|
197 | Post initialization call back to close the loose ends |
---|
198 | [Somehow openGL needs this call] |
---|
199 | """ |
---|
200 | pass |
---|
201 | |
---|
202 | def _on_show_panel(self, event): |
---|
203 | print "_on_show_panel" |
---|
204 | |
---|
205 | def _on_plot_event(self, event): |
---|
206 | """ |
---|
207 | A new plottable is being shipped to the plotting plug-in. |
---|
208 | Check whether we have a panel to put in on, or create |
---|
209 | a new one |
---|
210 | @param event: EVT_NEW_PLOT event |
---|
211 | """ |
---|
212 | # Check whether we already have a graph with the same units |
---|
213 | # as the plottable we just received. |
---|
214 | is_available = False |
---|
215 | for panel in self.plot_panels: |
---|
216 | if event.plot._xunit == panel.graph.prop["xunit"] \ |
---|
217 | and event.plot._yunit == panel.graph.prop["yunit"]: |
---|
218 | is_available = True |
---|
219 | panel._onEVT_1DREPLOT(event) |
---|
220 | self.parent.show_panel(panel.uid) |
---|
221 | |
---|
222 | # Create a new plot panel if none was available |
---|
223 | if not is_available: |
---|
224 | new_panel = View1DPanel(self.parent, -1, style=wx.RAISED_BORDER) |
---|
225 | if hasattr(event, "title"): |
---|
226 | new_panel.window_caption = event.title |
---|
227 | new_panel.window_name = event.title |
---|
228 | |
---|
229 | event_id = self.parent.popup_panel(new_panel) |
---|
230 | self.menu.Append(event_id, new_panel.window_caption, |
---|
231 | "Show %s plot panel" % new_panel.window_caption) |
---|
232 | |
---|
233 | # Set UID to allow us to reference the panel later |
---|
234 | new_panel.uid = event_id |
---|
235 | |
---|
236 | # Ship the plottable to its panel |
---|
237 | new_panel._onEVT_1DREPLOT(event) |
---|
238 | self.plot_panels.append(new_panel) |
---|
239 | |
---|
240 | return |
---|
241 | |
---|