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.guicomm.events import EVT_NEW_PLOT |
---|
17 | from sans.guicomm.events import NewPlotEvent |
---|
18 | from sans.guicomm.events import EVT_NEW_LOADED_DATA |
---|
19 | from sans.guicomm.events import StatusEvent |
---|
20 | |
---|
21 | |
---|
22 | class PlottingDialog(wx.Dialog): |
---|
23 | """ |
---|
24 | Dialog to display plotting option |
---|
25 | """ |
---|
26 | def __init__(self, parent=None, panel_on_focus=None, list_of_data=[]): |
---|
27 | """ |
---|
28 | """ |
---|
29 | wx.Dialog.__init__(self, parent=parent,title="Plotting", size=(300, 280)) |
---|
30 | self.parent = parent |
---|
31 | self.panel_on_focus = panel_on_focus |
---|
32 | self.list_of_data = list_of_data |
---|
33 | self.define_structure() |
---|
34 | self.layout_plot_on_panel(list_of_data=self.list_of_data) |
---|
35 | self.layout_data_name(list_of_data=self.list_of_data) |
---|
36 | self.layout_button() |
---|
37 | |
---|
38 | def define_structure(self): |
---|
39 | """ |
---|
40 | """ |
---|
41 | #Dialog interface |
---|
42 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
43 | self.sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
44 | |
---|
45 | self.sizer_selection = wx.BoxSizer(wx.VERTICAL) |
---|
46 | self.sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
47 | vbox.Add(self.sizer_data) |
---|
48 | vbox.Add(self.sizer_selection) |
---|
49 | vbox.Add(self.sizer_button) |
---|
50 | self.SetSizer(vbox) |
---|
51 | self.Centre() |
---|
52 | |
---|
53 | def layout_button(self): |
---|
54 | """ |
---|
55 | """ |
---|
56 | self.bt_ok = wx.Button(self, wx.NewId(), "Ok", (30, 10)) |
---|
57 | self.bt_ok.SetToolTipString("plot data") |
---|
58 | wx.EVT_BUTTON(self, self.bt_ok.GetId(), self.on_ok) |
---|
59 | |
---|
60 | self.sizer_button.AddMany([((40,40), 0, |
---|
61 | wx.LEFT|wx.ADJUST_MINSIZE, 100 ), |
---|
62 | (self.bt_ok, 0, wx.ALL,10)]) |
---|
63 | |
---|
64 | def layout_data_name(self, list_of_data=[]): |
---|
65 | """ |
---|
66 | """ |
---|
67 | self.data_tcl = wx.TextCtrl(self, -1,size=(260,80), style=wx.TE_MULTILINE) |
---|
68 | hint_data = "Data to plot." |
---|
69 | self.data_tcl.SetToolTipString(hint_data) |
---|
70 | self.data_tcl.SetEditable(False) |
---|
71 | for item in list_of_data: |
---|
72 | self.data_tcl.AppendText(item.name+"\n") |
---|
73 | |
---|
74 | self.sizer_data.AddMany([(self.data_tcl, 1, wx.ALL, 10)]) |
---|
75 | |
---|
76 | def layout_plot_on_panel(self, list_of_data=[]): |
---|
77 | """ |
---|
78 | """ |
---|
79 | if len(list_of_data) == 0: |
---|
80 | return |
---|
81 | elif len(list_of_data) ==1: |
---|
82 | self.layout_single_data(list_of_data=list_of_data) |
---|
83 | else: |
---|
84 | self.layout_multiple(list_of_data=list_of_data) |
---|
85 | |
---|
86 | def layout_single_data(self, list_of_data=[]): |
---|
87 | """ |
---|
88 | """ |
---|
89 | self.sizer_selection.Clear(True) |
---|
90 | if self.panel_on_focus is None and list_of_data < 1: |
---|
91 | return |
---|
92 | else: |
---|
93 | name = "Plot data on new panel" |
---|
94 | self.rb_single_data_panel = wx.RadioButton(self, -1,name, |
---|
95 | style=wx.RB_GROUP) |
---|
96 | msg = "Each data will be plotted separately on a new panel" |
---|
97 | self.rb_single_data_panel.SetToolTipString(msg) |
---|
98 | self.rb_single_data_panel.SetValue(True) |
---|
99 | self.Bind(wx.EVT_RADIOBUTTON, self.on_choose_panel, |
---|
100 | id=self.rb_single_data_panel.GetId()) |
---|
101 | self.rb_panel_on_focus = wx.RadioButton(self, -1,"No Panel on Focus") |
---|
102 | msg = "All Data will be appended to panel on focus" |
---|
103 | self.rb_panel_on_focus.SetToolTipString(msg) |
---|
104 | self.rb_panel_on_focus.Disable() |
---|
105 | self.Bind(wx.EVT_RADIOBUTTON, self.on_choose_panel, |
---|
106 | id=self.rb_panel_on_focus.GetId()) |
---|
107 | if self.panel_on_focus is not None: |
---|
108 | self.rb_panel_on_focus.Enable() |
---|
109 | self.rb_panel_on_focus.SetLabel(str(panel.window_name)) |
---|
110 | self.sizer_selection.AddMany([(self.rb_single_data_panel, |
---|
111 | 1, wx.ALL, 10), |
---|
112 | (self.rb_panel_on_focus,1, wx.ALL, 10)]) |
---|
113 | |
---|
114 | def layout_multiple(self, list_of_data=[]): |
---|
115 | """ |
---|
116 | """ |
---|
117 | self.sizer_selection.Clear(True) |
---|
118 | if self.panel_on_focus is None and list_of_data <= 1: |
---|
119 | return |
---|
120 | name = "Plot each data separately" |
---|
121 | self.rb_single_data_panel = wx.RadioButton(self, -1,name, |
---|
122 | style=wx.RB_GROUP) |
---|
123 | msg = "Each data will be plotted separately on a new panel" |
---|
124 | self.rb_single_data_panel.SetToolTipString(msg) |
---|
125 | self.rb_single_data_panel.SetValue(True) |
---|
126 | self.Bind(wx.EVT_RADIOBUTTON, self.on_choose_panel, |
---|
127 | id=self.rb_single_data_panel.GetId()) |
---|
128 | name = "Append all to new panel" |
---|
129 | self.rb_new_panel = wx.RadioButton(self, -1,name) |
---|
130 | msg = "All Data will be appended to a new panel" |
---|
131 | self.rb_new_panel.SetToolTipString(msg) |
---|
132 | self.Bind(wx.EVT_RADIOBUTTON, self.on_choose_panel, |
---|
133 | id=self.rb_new_panel.GetId()) |
---|
134 | self.rb_panel_on_focus = wx.RadioButton(self, -1,"No Panel on Focus") |
---|
135 | msg = "All Data will be appended to panel on focus" |
---|
136 | self.rb_panel_on_focus.SetToolTipString(msg) |
---|
137 | self.rb_panel_on_focus.Disable() |
---|
138 | self.Bind(wx.EVT_RADIOBUTTON, self.on_choose_panel, |
---|
139 | id=self.rb_panel_on_focus.GetId()) |
---|
140 | if self.panel_on_focus is not None: |
---|
141 | self.rb_panel_on_focus.Enable() |
---|
142 | self.rb_panel_on_focus.SetLabel(str(panel.window_name)) |
---|
143 | |
---|
144 | self.sizer_selection.AddMany([(self.rb_single_data_panel, |
---|
145 | 1, wx.LEFT|wx.RIGHT|wx.BOTTOM, 10), |
---|
146 | (self.rb_new_panel, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM, 10), |
---|
147 | (self.rb_panel_on_focus, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM, 10),]) |
---|
148 | def on_ok(self, event): |
---|
149 | """ |
---|
150 | """ |
---|
151 | def on_choose_panel(self, event): |
---|
152 | """ |
---|
153 | """ |
---|
154 | class Plugin: |
---|
155 | """ |
---|
156 | Plug-in class to be instantiated by the GUI manager |
---|
157 | """ |
---|
158 | |
---|
159 | def __init__(self): |
---|
160 | """ |
---|
161 | Initialize the plug-in |
---|
162 | """ |
---|
163 | ## Plug-in name |
---|
164 | self.sub_menu = "Plotting" |
---|
165 | |
---|
166 | ## Reference to the parent window |
---|
167 | self.parent = None |
---|
168 | |
---|
169 | ## List of panels for the simulation perspective (names) |
---|
170 | self.perspective = [] |
---|
171 | |
---|
172 | ## Plot panels |
---|
173 | self.plot_panels = [] |
---|
174 | |
---|
175 | def populate_menu(self, id, parent): |
---|
176 | """ |
---|
177 | Create a 'Plot' menu to list the panels |
---|
178 | available for displaying |
---|
179 | |
---|
180 | :param id: next available unique ID for wx events |
---|
181 | :param parent: parent window |
---|
182 | |
---|
183 | """ |
---|
184 | self.menu = wx.Menu() |
---|
185 | |
---|
186 | self.menu.Append(wx.NewId(), "No plot available", |
---|
187 | "No plot available") |
---|
188 | self.menu.FindItemByPosition(0).Enable(False) |
---|
189 | return [(id, self.menu, "Plot")] |
---|
190 | |
---|
191 | def get_panels(self, parent): |
---|
192 | """ |
---|
193 | Create and return a list of panel objects |
---|
194 | """ |
---|
195 | ## Save a reference to the parent |
---|
196 | self.parent = parent |
---|
197 | # Connect to plotting events |
---|
198 | self.parent.Bind(EVT_NEW_LOADED_DATA, self._on_plot) |
---|
199 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
200 | # We have no initial panels for this plug-in |
---|
201 | return [] |
---|
202 | |
---|
203 | def get_perspective(self): |
---|
204 | """ |
---|
205 | Get the list of panel names for this perspective |
---|
206 | """ |
---|
207 | return self.perspective |
---|
208 | |
---|
209 | def on_perspective(self, event): |
---|
210 | """ |
---|
211 | Call back function for the perspective menu item. |
---|
212 | We notify the parent window that the perspective |
---|
213 | has changed. |
---|
214 | @param event: menu event |
---|
215 | """ |
---|
216 | self.parent.set_perspective(self.perspective) |
---|
217 | |
---|
218 | def post_init(self): |
---|
219 | """ |
---|
220 | Post initialization call back to close the loose ends |
---|
221 | [Somehow openGL needs this call] |
---|
222 | """ |
---|
223 | pass |
---|
224 | |
---|
225 | def _on_show_panel(self, event): |
---|
226 | """show plug-in panel""" |
---|
227 | pass |
---|
228 | |
---|
229 | def _on_plot(self, event): |
---|
230 | """ |
---|
231 | check the contains of event. |
---|
232 | if it is a list of data to plot plot each one at the time |
---|
233 | """ |
---|
234 | list_of_data1d = [] |
---|
235 | if hasattr(event, "plots"): |
---|
236 | for plot, path in event.plots: |
---|
237 | print "plotting _on_plot" |
---|
238 | if plot.__class__.__name__ == "Data2D": |
---|
239 | wx.PostEvent(self.parent, |
---|
240 | NewPlotEvent(plot=plot, title=plot.name)) |
---|
241 | else: |
---|
242 | list_of_data1d.append((plot, path)) |
---|
243 | wx.PostEvent(self.parent, |
---|
244 | NewPlotEvent(plot=plot, title=plot.name)) |
---|
245 | |
---|
246 | def _on_plot_event(self, event): |
---|
247 | """ |
---|
248 | A new plottable is being shipped to the plotting plug-in. |
---|
249 | Check whether we have a panel to put in on, or create |
---|
250 | a new one |
---|
251 | |
---|
252 | :param event: EVT_NEW_PLOT event |
---|
253 | |
---|
254 | """ |
---|
255 | # Check whether we already have a graph with the same units |
---|
256 | # as the plottable we just received. |
---|
257 | |
---|
258 | is_available = False |
---|
259 | for panel in self.plot_panels: |
---|
260 | if event.plot._xunit == panel.graph.prop["xunit_base"] \ |
---|
261 | and event.plot._yunit == panel.graph.prop["yunit_base"]: |
---|
262 | if hasattr(event.plot, "group_id"): |
---|
263 | ## if same group_id used the same panel to plot |
---|
264 | if not event.plot.group_id==None \ |
---|
265 | and event.plot.group_id==panel.group_id: |
---|
266 | is_available = True |
---|
267 | |
---|
268 | panel._onEVT_1DREPLOT(event) |
---|
269 | self.parent.show_panel(panel.uid) |
---|
270 | else: |
---|
271 | # Check that the plot panel has no group ID |
---|
272 | ## Use a panel with group_id ==None to plot |
---|
273 | |
---|
274 | if panel.group_id==None: |
---|
275 | is_available = True |
---|
276 | panel._onEVT_1DREPLOT(event) |
---|
277 | self.parent.show_panel(panel.uid) |
---|
278 | |
---|
279 | # Create a new plot panel if none was available |
---|
280 | if not is_available: |
---|
281 | #print"event.plot",hasattr(event.plot,'data') |
---|
282 | if not hasattr(event.plot,'data'): |
---|
283 | from Plotter1D import ModelPanel1D |
---|
284 | ## get the data representation label of the data to plot |
---|
285 | ## when even the user select "change scale" |
---|
286 | if hasattr(event.plot,"xtransform"): |
---|
287 | xtransform = event.plot.xtransform |
---|
288 | else: |
---|
289 | xtransform =None |
---|
290 | |
---|
291 | if hasattr(event.plot,"ytransform"): |
---|
292 | ytransform= event.plot.ytransform |
---|
293 | else: |
---|
294 | ytransform=None |
---|
295 | ## create a plotpanel for 1D Data |
---|
296 | new_panel = ModelPanel1D(self.parent, -1,xtransform=xtransform, |
---|
297 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
298 | |
---|
299 | else: |
---|
300 | ##Create a new plotpanel for 2D data |
---|
301 | from Plotter2D import ModelPanel2D |
---|
302 | new_panel = ModelPanel2D(self.parent, id = -1, data2d=event.plot,style=wx.RAISED_BORDER) |
---|
303 | |
---|
304 | ## Set group ID if available |
---|
305 | ## Assign data properties to the new create panel |
---|
306 | group_id_str = '' |
---|
307 | if hasattr(event.plot, "group_id"): |
---|
308 | if not event.plot.group_id==None: |
---|
309 | new_panel.group_id = event.plot.group_id |
---|
310 | group_id_str = ' [%s]' % event.plot.group_id |
---|
311 | |
---|
312 | if hasattr(event, "title"): |
---|
313 | new_panel.window_caption = event.title |
---|
314 | new_panel.window_name = event.title |
---|
315 | |
---|
316 | event_id = self.parent.popup_panel(new_panel) |
---|
317 | #remove the default item in the menu |
---|
318 | if len(self.plot_panels) == 0: |
---|
319 | self.menu.RemoveItem(self.menu.FindItemByPosition(0)) |
---|
320 | |
---|
321 | self.menu.Append(event_id, new_panel.window_caption, |
---|
322 | "Show %s plot panel" % new_panel.window_caption) |
---|
323 | # Set UID to allow us to reference the panel later |
---|
324 | new_panel.uid = event_id |
---|
325 | # Ship the plottable to its panel |
---|
326 | new_panel._onEVT_1DREPLOT(event) |
---|
327 | self.plot_panels.append(new_panel) |
---|
328 | |
---|
329 | return |
---|
330 | |
---|
331 | class Data(object): |
---|
332 | def __init__(self, name): |
---|
333 | self.name = str(name) |
---|
334 | class MyApp(wx.App): |
---|
335 | def OnInit(self): |
---|
336 | wx.InitAllImageHandlers() |
---|
337 | list =[Data(name="Data1D")]#, |
---|
338 | # Data(name="Data2D"),Data(name="Data3D")] |
---|
339 | dialog = PlottingDialog(list_of_data=list) |
---|
340 | if dialog.ShowModal() == wx.ID_OK: |
---|
341 | pass |
---|
342 | dialog.Destroy() |
---|
343 | |
---|
344 | return 1 |
---|
345 | |
---|
346 | # end of class MyApp |
---|
347 | |
---|
348 | if __name__ == "__main__": |
---|
349 | app = MyApp(0) |
---|
350 | app.MainLoop() |
---|