1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2008, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | |
---|
13 | import wx |
---|
14 | import wx.aui |
---|
15 | import os |
---|
16 | import sys |
---|
17 | import xml |
---|
18 | |
---|
19 | try: |
---|
20 | # Try to find a local config |
---|
21 | import imp |
---|
22 | path = os.getcwd() |
---|
23 | if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \ |
---|
24 | (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))): |
---|
25 | fObj, path, descr = imp.find_module('local_config', [path]) |
---|
26 | config = imp.load_module('local_config', fObj, path, descr) |
---|
27 | else: |
---|
28 | # Try simply importing local_config |
---|
29 | import local_config as config |
---|
30 | except: |
---|
31 | # Didn't find local config, load the default |
---|
32 | import config |
---|
33 | |
---|
34 | import warnings |
---|
35 | warnings.simplefilter("ignore") |
---|
36 | |
---|
37 | import logging |
---|
38 | |
---|
39 | from sans.guiframe.events import EVT_STATUS |
---|
40 | from sans.guiframe.events import EVT_ADD_MANY_DATA |
---|
41 | from sans.guiframe.events import StatusEvent |
---|
42 | from sans.guiframe.events import NewPlotEvent |
---|
43 | |
---|
44 | |
---|
45 | STATE_FILE_EXT = ['.inv', '.fitv', '.prv'] |
---|
46 | |
---|
47 | DATA_MANAGER = False |
---|
48 | AUTO_PLOT = False |
---|
49 | AUTO_SET_DATA = True |
---|
50 | |
---|
51 | class ViewerFrame(wx.Frame): |
---|
52 | """ |
---|
53 | Main application frame |
---|
54 | """ |
---|
55 | |
---|
56 | def __init__(self, parent, id, title, |
---|
57 | window_height=300, window_width=300): |
---|
58 | """ |
---|
59 | Initialize the Frame object |
---|
60 | """ |
---|
61 | |
---|
62 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
63 | size=(window_width, window_height)) |
---|
64 | # Preferred window size |
---|
65 | self._window_height = window_height |
---|
66 | self._window_width = window_width |
---|
67 | |
---|
68 | # Logging info |
---|
69 | logging.basicConfig(level=logging.DEBUG, |
---|
70 | format='%(asctime)s %(levelname)s %(message)s', |
---|
71 | filename='sans_app.log', |
---|
72 | filemode='w') |
---|
73 | path = os.path.dirname(__file__) |
---|
74 | temp_path = os.path.join(path,'images') |
---|
75 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
76 | if os.path.isfile(ico_file): |
---|
77 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
78 | else: |
---|
79 | temp_path = os.path.join(os.getcwd(),'images') |
---|
80 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
81 | if os.path.isfile(ico_file): |
---|
82 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
83 | |
---|
84 | ## Application manager |
---|
85 | self.app_manager = None |
---|
86 | self._mgr = None |
---|
87 | #data manager |
---|
88 | from .data_manager import DataManager |
---|
89 | self.data_manager = DataManager() |
---|
90 | #add current perpsective |
---|
91 | self._current_perspective = None |
---|
92 | #file menu |
---|
93 | self.file_menu = None |
---|
94 | |
---|
95 | ## Find plug-ins |
---|
96 | # Modify this so that we can specify the directory to look into |
---|
97 | self.plugins = [] |
---|
98 | #add local plugin |
---|
99 | |
---|
100 | from sans.guiframe.local_perspectives.plotting import plotting |
---|
101 | from sans.guiframe.local_perspectives.data_loader import data_loader |
---|
102 | self.plugins.append(plotting.Plugin()) |
---|
103 | self.plugins.append(data_loader.Plugin()) |
---|
104 | |
---|
105 | self.plugins += self._find_plugins() |
---|
106 | |
---|
107 | ## List of panels |
---|
108 | self.panels = {} |
---|
109 | |
---|
110 | ## Next available ID for wx gui events |
---|
111 | #TODO: No longer used - remove all calls to this |
---|
112 | self.next_id = 20000 |
---|
113 | |
---|
114 | # Default locations |
---|
115 | self._default_save_location = os.getcwd() |
---|
116 | |
---|
117 | # Welcome panel |
---|
118 | self.defaultPanel = None |
---|
119 | #panel on focus |
---|
120 | self.panel_on_focus = None |
---|
121 | # Check for update |
---|
122 | #self._check_update(None) |
---|
123 | # Register the close event so it calls our own method |
---|
124 | wx.EVT_CLOSE(self, self._onClose) |
---|
125 | # Register to status events |
---|
126 | self.Bind(EVT_STATUS, self._on_status_event) |
---|
127 | #Register add extra data on the same panel event on load |
---|
128 | self.Bind(EVT_ADD_MANY_DATA, self.set_panel_on_focus) |
---|
129 | |
---|
130 | def set_panel_on_focus(self, event): |
---|
131 | """ |
---|
132 | Store reference to the last panel on focus |
---|
133 | """ |
---|
134 | self.panel_on_focus = event.panel |
---|
135 | |
---|
136 | def build_gui(self): |
---|
137 | """ |
---|
138 | """ |
---|
139 | # Set up the layout |
---|
140 | self._setup_layout() |
---|
141 | |
---|
142 | # Set up the menu |
---|
143 | self._setup_menus() |
---|
144 | #self.Fit() |
---|
145 | #self._check_update(None) |
---|
146 | |
---|
147 | def _setup_layout(self): |
---|
148 | """ |
---|
149 | Set up the layout |
---|
150 | """ |
---|
151 | # Status bar |
---|
152 | from statusbar import StatusBar |
---|
153 | self.sb = StatusBar(self, wx.ID_ANY) |
---|
154 | self.SetStatusBar(self.sb) |
---|
155 | # Add panel |
---|
156 | self._mgr = wx.aui.AuiManager(self) |
---|
157 | self._mgr.SetDockSizeConstraint(0.5, 0.5) |
---|
158 | # Load panels |
---|
159 | self._load_panels() |
---|
160 | self._mgr.Update() |
---|
161 | |
---|
162 | def SetStatusText(self, *args, **kwds): |
---|
163 | """ |
---|
164 | """ |
---|
165 | number = self.sb.get_msg_position() |
---|
166 | wx.Frame.SetStatusText(number=number, *args, **kwds) |
---|
167 | |
---|
168 | def PopStatusText(self, *args, **kwds): |
---|
169 | """ |
---|
170 | """ |
---|
171 | field = self.sb.get_msg_position() |
---|
172 | wx.Frame.PopStatusText(field=field) |
---|
173 | |
---|
174 | def PushStatusText(self, *args, **kwds): |
---|
175 | """ |
---|
176 | """ |
---|
177 | field = self.sb.get_msg_position() |
---|
178 | wx.Frame.PushStatusText(self, field=field, string=string) |
---|
179 | |
---|
180 | def add_perspective(self, plugin): |
---|
181 | """ |
---|
182 | Add a perspective if it doesn't already |
---|
183 | exist. |
---|
184 | """ |
---|
185 | is_loaded = False |
---|
186 | for item in self.plugins: |
---|
187 | if plugin.__class__ == item.__class__: |
---|
188 | msg = "Plugin %s already loaded" % plugin.__class__.__name__ |
---|
189 | logging.info(msg) |
---|
190 | is_loaded = True |
---|
191 | if not is_loaded: |
---|
192 | self.plugins.append(plugin) |
---|
193 | |
---|
194 | def _find_plugins(self, dir="perspectives"): |
---|
195 | """ |
---|
196 | Find available perspective plug-ins |
---|
197 | |
---|
198 | :param dir: directory in which to look for plug-ins |
---|
199 | |
---|
200 | :return: list of plug-ins |
---|
201 | |
---|
202 | """ |
---|
203 | import imp |
---|
204 | plugins = [] |
---|
205 | # Go through files in panels directory |
---|
206 | try: |
---|
207 | list = os.listdir(dir) |
---|
208 | ## the default panel is the panel is the last plugin added |
---|
209 | for item in list: |
---|
210 | toks = os.path.splitext(os.path.basename(item)) |
---|
211 | name = None |
---|
212 | if not toks[0] == '__init__': |
---|
213 | |
---|
214 | if toks[1] == '.py' or toks[1] == '': |
---|
215 | name = toks[0] |
---|
216 | |
---|
217 | path = [os.path.abspath(dir)] |
---|
218 | file = None |
---|
219 | try: |
---|
220 | if toks[1] == '': |
---|
221 | mod_path = '.'.join([dir, name]) |
---|
222 | module = __import__(mod_path, globals(), |
---|
223 | locals(), [name]) |
---|
224 | else: |
---|
225 | (file, path, info) = imp.find_module(name, path) |
---|
226 | module = imp.load_module( name, file, item, info) |
---|
227 | if hasattr(module, "PLUGIN_ID"): |
---|
228 | try: |
---|
229 | plug = module.Plugin() |
---|
230 | if plug.set_default_perspective(): |
---|
231 | self._current_perspective = plug |
---|
232 | plugins.append(plug) |
---|
233 | msg = "Found plug-in: %s" % module.PLUGIN_ID |
---|
234 | logging.info(msg) |
---|
235 | except: |
---|
236 | msg = "Error accessing PluginPanel" |
---|
237 | msg += " in %s\n %s" % (name, sys.exc_value) |
---|
238 | config.printEVT(msg) |
---|
239 | except: |
---|
240 | #print sys.exc_value |
---|
241 | msg = "ViewerFrame._find_plugins: %s" % sys.exc_value |
---|
242 | logging.error(msg) |
---|
243 | finally: |
---|
244 | if not file == None: |
---|
245 | file.close() |
---|
246 | except: |
---|
247 | # Should raise and catch at a higher level and |
---|
248 | # display error on status bar |
---|
249 | pass |
---|
250 | return plugins |
---|
251 | |
---|
252 | def set_welcome_panel(self, panel_class): |
---|
253 | """ |
---|
254 | Sets the default panel as the given welcome panel |
---|
255 | |
---|
256 | :param panel_class: class of the welcome panel to be instantiated |
---|
257 | |
---|
258 | """ |
---|
259 | self.defaultPanel = panel_class(self, -1, style=wx.RAISED_BORDER) |
---|
260 | |
---|
261 | def _load_panels(self): |
---|
262 | """ |
---|
263 | Load all panels in the panels directory |
---|
264 | """ |
---|
265 | |
---|
266 | # Look for plug-in panels |
---|
267 | panels = [] |
---|
268 | for item in self.plugins: |
---|
269 | if hasattr(item, "get_panels"): |
---|
270 | ps = item.get_panels(self) |
---|
271 | panels.extend(ps) |
---|
272 | |
---|
273 | # Show a default panel with some help information |
---|
274 | # It also sets the size of the application windows |
---|
275 | #TODO: Use this for slpash screen |
---|
276 | if self.defaultPanel is None: |
---|
277 | self.defaultPanel = DefaultPanel(self, -1, style=wx.RAISED_BORDER) |
---|
278 | |
---|
279 | self.panels["default"] = self.defaultPanel |
---|
280 | |
---|
281 | self._mgr.AddPane(self.defaultPanel, wx.aui.AuiPaneInfo(). |
---|
282 | Name("default"). |
---|
283 | CenterPane(). |
---|
284 | # This is where we set the size of |
---|
285 | # the application window |
---|
286 | BestSize(wx.Size(self._window_width, |
---|
287 | self._window_height)). |
---|
288 | #MinSize(wx.Size(self._window_width, |
---|
289 | #self._window_height)). |
---|
290 | Show()) |
---|
291 | |
---|
292 | # Add the panels to the AUI manager |
---|
293 | for panel_class in panels: |
---|
294 | p = panel_class |
---|
295 | id = wx.NewId() |
---|
296 | |
---|
297 | # Check whether we need to put this panel |
---|
298 | # in the center pane |
---|
299 | if hasattr(p, "CENTER_PANE") and p.CENTER_PANE: |
---|
300 | if p.CENTER_PANE: |
---|
301 | self.panels[str(id)] = p |
---|
302 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
303 | Name(p.window_name).Caption(p.window_caption). |
---|
304 | CenterPane(). |
---|
305 | #BestSize(wx.Size(550,600)). |
---|
306 | #MinSize(wx.Size(500,500)). |
---|
307 | Hide()) |
---|
308 | else: |
---|
309 | self.panels[str(id)] = p |
---|
310 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
311 | Name(p.window_name).Caption(p.window_caption). |
---|
312 | Right(). |
---|
313 | Dock(). |
---|
314 | TopDockable(). |
---|
315 | BottomDockable(). |
---|
316 | LeftDockable(). |
---|
317 | RightDockable(). |
---|
318 | MinimizeButton(). |
---|
319 | Hide()) |
---|
320 | #BestSize(wx.Size(550,600))) |
---|
321 | #MinSize(wx.Size(500,500))) |
---|
322 | |
---|
323 | def get_context_menu(self, graph=None): |
---|
324 | """ |
---|
325 | Get the context menu items made available |
---|
326 | by the different plug-ins. |
---|
327 | This function is used by the plotting module |
---|
328 | """ |
---|
329 | menu_list = [] |
---|
330 | for item in self.plugins: |
---|
331 | if hasattr(item, "get_context_menu"): |
---|
332 | menu_list.extend(item.get_context_menu(graph)) |
---|
333 | return menu_list |
---|
334 | |
---|
335 | def popup_panel(self, p): |
---|
336 | """ |
---|
337 | Add a panel object to the AUI manager |
---|
338 | |
---|
339 | :param p: panel object to add to the AUI manager |
---|
340 | |
---|
341 | :return: ID of the event associated with the new panel [int] |
---|
342 | |
---|
343 | """ |
---|
344 | ID = wx.NewId() |
---|
345 | self.panels[str(ID)] = p |
---|
346 | |
---|
347 | count = 0 |
---|
348 | for item in self.panels: |
---|
349 | if self.panels[item].window_name.startswith(p.window_name): |
---|
350 | count += 1 |
---|
351 | windowname = p.window_name |
---|
352 | caption = p.window_caption |
---|
353 | if count > 0: |
---|
354 | windowname += str(count+1) |
---|
355 | caption += (' '+str(count)) |
---|
356 | p.window_name = windowname |
---|
357 | p.window_caption = caption |
---|
358 | |
---|
359 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
360 | Name(windowname).Caption(caption). |
---|
361 | Floatable(). |
---|
362 | #Float(). |
---|
363 | Right(). |
---|
364 | Dock(). |
---|
365 | TopDockable(). |
---|
366 | BottomDockable(). |
---|
367 | LeftDockable(). |
---|
368 | RightDockable(). |
---|
369 | MinimizeButton(). |
---|
370 | #Hide(). |
---|
371 | #Show(). |
---|
372 | Resizable(True). |
---|
373 | # Use a large best size to make sure the AUI manager |
---|
374 | # takes all the available space |
---|
375 | BestSize(wx.Size(400,400))) |
---|
376 | pane = self._mgr.GetPane(windowname) |
---|
377 | self._mgr.MaximizePane(pane) |
---|
378 | self._mgr.RestoreMaximizedPane() |
---|
379 | # Register for showing/hiding the panel |
---|
380 | wx.EVT_MENU(self, ID, self._on_view) |
---|
381 | |
---|
382 | self._mgr.Update() |
---|
383 | return ID |
---|
384 | |
---|
385 | def _populate_file_menu(self): |
---|
386 | """ |
---|
387 | Insert menu item under file menu |
---|
388 | """ |
---|
389 | for plugin in self.plugins: |
---|
390 | if len(plugin.populate_file_menu()) > 0: |
---|
391 | for item in plugin.populate_file_menu(): |
---|
392 | id = wx.NewId() |
---|
393 | m_name, m_hint, m_handler = item |
---|
394 | self.filemenu.Append(id, m_name, m_hint) |
---|
395 | wx.EVT_MENU(self, id, m_handler) |
---|
396 | self.filemenu.AppendSeparator() |
---|
397 | |
---|
398 | def _setup_menus(self): |
---|
399 | """ |
---|
400 | Set up the application menus |
---|
401 | """ |
---|
402 | # Menu |
---|
403 | self._menubar = wx.MenuBar() |
---|
404 | # File menu |
---|
405 | self.filemenu = wx.Menu() |
---|
406 | |
---|
407 | # some menu of plugin to be seen under file menu |
---|
408 | self._populate_file_menu() |
---|
409 | id = wx.NewId() |
---|
410 | self.filemenu.Append(id, '&Save state into File', |
---|
411 | 'Save project as a SansView (svs) file') |
---|
412 | wx.EVT_MENU(self, id, self._on_save) |
---|
413 | #self.filemenu.AppendSeparator() |
---|
414 | |
---|
415 | id = wx.NewId() |
---|
416 | self.filemenu.Append(id, '&Quit', 'Exit') |
---|
417 | wx.EVT_MENU(self, id, self.Close) |
---|
418 | |
---|
419 | # Add sub menus |
---|
420 | self._menubar.Append(self.filemenu, '&File') |
---|
421 | |
---|
422 | # Window menu |
---|
423 | # Attach a menu item for each panel in our |
---|
424 | # panel list that also appears in a plug-in. |
---|
425 | |
---|
426 | # Only add the panel menu if there is only one perspective and |
---|
427 | # it has more than two panels. |
---|
428 | # Note: the first plug-in is always the plotting plug-in. |
---|
429 | #The first application |
---|
430 | # plug-in is always the second one in the list. |
---|
431 | if len(self.plugins) == 2: |
---|
432 | plug = self.plugins[1] |
---|
433 | pers = plug.get_perspective() |
---|
434 | |
---|
435 | if len(pers) > 1: |
---|
436 | viewmenu = wx.Menu() |
---|
437 | for item in self.panels: |
---|
438 | if item == 'default': |
---|
439 | continue |
---|
440 | panel = self.panels[item] |
---|
441 | if panel.window_name in pers: |
---|
442 | viewmenu.Append(int(item), panel.window_caption, |
---|
443 | "Show %s window" % panel.window_caption) |
---|
444 | wx.EVT_MENU(self, int(item), self._on_view) |
---|
445 | self._menubar.Append(viewmenu, '&Window') |
---|
446 | |
---|
447 | # Perspective |
---|
448 | # Attach a menu item for each defined perspective. |
---|
449 | # Only add the perspective menu if there are more than one perspectives |
---|
450 | n_perspectives = 0 |
---|
451 | for plug in self.plugins: |
---|
452 | if len(plug.get_perspective()) > 0: |
---|
453 | n_perspectives += 1 |
---|
454 | |
---|
455 | if n_perspectives > 1: |
---|
456 | p_menu = wx.Menu() |
---|
457 | for plug in self.plugins: |
---|
458 | if len(plug.get_perspective()) > 0: |
---|
459 | id = wx.NewId() |
---|
460 | p_menu.Append(id, plug.sub_menu, |
---|
461 | "Switch to %s perspective" % plug.sub_menu) |
---|
462 | wx.EVT_MENU(self, id, plug.on_perspective) |
---|
463 | self._menubar.Append(p_menu, '&Perspective') |
---|
464 | |
---|
465 | # Tools menu |
---|
466 | # Go through plug-ins and find tools to populate the tools menu |
---|
467 | toolsmenu = None |
---|
468 | for item in self.plugins: |
---|
469 | if hasattr(item, "get_tools"): |
---|
470 | for tool in item.get_tools(): |
---|
471 | # Only create a menu if we have at least one tool |
---|
472 | if toolsmenu is None: |
---|
473 | toolsmenu = wx.Menu() |
---|
474 | id = wx.NewId() |
---|
475 | toolsmenu.Append(id, tool[0], tool[1]) |
---|
476 | wx.EVT_MENU(self, id, tool[2]) |
---|
477 | if toolsmenu is not None: |
---|
478 | self._menubar.Append(toolsmenu, '&Tools') |
---|
479 | |
---|
480 | # Help menu |
---|
481 | helpmenu = wx.Menu() |
---|
482 | # add the welcome panel menu item |
---|
483 | if self.defaultPanel is not None: |
---|
484 | id = wx.NewId() |
---|
485 | helpmenu.Append(id, '&Welcome', '') |
---|
486 | helpmenu.AppendSeparator() |
---|
487 | wx.EVT_MENU(self, id, self.show_welcome_panel) |
---|
488 | # Look for help item in plug-ins |
---|
489 | for item in self.plugins: |
---|
490 | if hasattr(item, "help"): |
---|
491 | id = wx.NewId() |
---|
492 | helpmenu.Append(id,'&%s help' % item.sub_menu, '') |
---|
493 | wx.EVT_MENU(self, id, item.help) |
---|
494 | if config._do_aboutbox: |
---|
495 | id = wx.NewId() |
---|
496 | helpmenu.Append(id,'&About', 'Software information') |
---|
497 | wx.EVT_MENU(self, id, self._onAbout) |
---|
498 | |
---|
499 | # Checking for updates needs major refactoring to work with py2exe |
---|
500 | # We need to make sure it doesn't hang the application if the server |
---|
501 | # is not up. We also need to make sure there's a proper executable to |
---|
502 | # run if we spawn a new background process. |
---|
503 | #id = wx.NewId() |
---|
504 | #helpmenu.Append(id,'&Check for update', |
---|
505 | #'Check for the latest version of %s' % config.__appname__) |
---|
506 | #wx.EVT_MENU(self, id, self._check_update) |
---|
507 | |
---|
508 | # Look for plug-in menus |
---|
509 | # Add available plug-in sub-menus. |
---|
510 | for item in self.plugins: |
---|
511 | if hasattr(item, "populate_menu"): |
---|
512 | for (self.next_id, menu, name) in \ |
---|
513 | item.populate_menu(self.next_id, self): |
---|
514 | self._menubar.Append(menu, name) |
---|
515 | |
---|
516 | self._menubar.Append(helpmenu, '&Help') |
---|
517 | self.SetMenuBar(self._menubar) |
---|
518 | |
---|
519 | def _on_status_event(self, evt): |
---|
520 | """ |
---|
521 | Display status message |
---|
522 | """ |
---|
523 | self.sb.set_status(event=evt) |
---|
524 | |
---|
525 | def _on_view(self, evt): |
---|
526 | """ |
---|
527 | A panel was selected to be shown. If it's not already |
---|
528 | shown, display it. |
---|
529 | |
---|
530 | :param evt: menu event |
---|
531 | |
---|
532 | """ |
---|
533 | self.show_panel(evt.GetId()) |
---|
534 | |
---|
535 | def on_close_welcome_panel(self): |
---|
536 | """ |
---|
537 | Close the welcome panel |
---|
538 | """ |
---|
539 | if self.defaultPanel is None: |
---|
540 | return |
---|
541 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
542 | self._mgr.Update() |
---|
543 | # set a default perspective |
---|
544 | self.set_default_perspective() |
---|
545 | |
---|
546 | def show_welcome_panel(self, event): |
---|
547 | """ |
---|
548 | Display the welcome panel |
---|
549 | """ |
---|
550 | if self.defaultPanel is None: |
---|
551 | return |
---|
552 | for id in self.panels.keys(): |
---|
553 | if self._mgr.GetPane(self.panels[id].window_name).IsShown(): |
---|
554 | self._mgr.GetPane(self.panels[id].window_name).Hide() |
---|
555 | # Show default panel |
---|
556 | if not self._mgr.GetPane(self.panels["default"].window_name).IsShown(): |
---|
557 | self._mgr.GetPane(self.panels["default"].window_name).Show() |
---|
558 | |
---|
559 | self._mgr.Update() |
---|
560 | |
---|
561 | def show_panel(self, uid): |
---|
562 | """ |
---|
563 | Shows the panel with the given id |
---|
564 | |
---|
565 | :param uid: unique ID number of the panel to show |
---|
566 | |
---|
567 | """ |
---|
568 | ID = str(uid) |
---|
569 | config.printEVT("show_panel: %s" % ID) |
---|
570 | if ID in self.panels.keys(): |
---|
571 | if not self._mgr.GetPane(self.panels[ID].window_name).IsShown(): |
---|
572 | self._mgr.GetPane(self.panels[ID].window_name).Show() |
---|
573 | # Hide default panel |
---|
574 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
575 | self._mgr.Update() |
---|
576 | |
---|
577 | def _on_open(self, event): |
---|
578 | """ |
---|
579 | """ |
---|
580 | path = self.choose_file() |
---|
581 | if path is None: |
---|
582 | return |
---|
583 | |
---|
584 | from data_loader import plot_data |
---|
585 | from sans.perspectives import invariant |
---|
586 | if path and os.path.isfile(path): |
---|
587 | basename = os.path.basename(path) |
---|
588 | if basename.endswith('.svs'): |
---|
589 | #remove panels for new states |
---|
590 | for item in self.panels: |
---|
591 | try: |
---|
592 | self.panels[item].clear_panel() |
---|
593 | except: |
---|
594 | pass |
---|
595 | #reset states and plot data |
---|
596 | for item in STATE_FILE_EXT: |
---|
597 | exec "plot_data(self, path,'%s')" % str(item) |
---|
598 | else: |
---|
599 | plot_data(self, path) |
---|
600 | if self.defaultPanel is not None and \ |
---|
601 | self._mgr.GetPane(self.panels["default"].window_name).IsShown(): |
---|
602 | self.on_close_welcome_panel() |
---|
603 | |
---|
604 | def _on_save(self, event): |
---|
605 | """ |
---|
606 | Save state into a file |
---|
607 | """ |
---|
608 | # Ask the user the location of the file to write to. |
---|
609 | |
---|
610 | ## Default file location for save |
---|
611 | self._default_save_location = os.getcwd() |
---|
612 | path = None |
---|
613 | dlg = wx.FileDialog(self, "Choose a file", |
---|
614 | self._default_save_location, "", "*.svs", wx.SAVE) |
---|
615 | if dlg.ShowModal() == wx.ID_OK: |
---|
616 | path = dlg.GetPath() |
---|
617 | self._default_save_location = os.path.dirname(path) |
---|
618 | else: |
---|
619 | return None |
---|
620 | dlg.Destroy() |
---|
621 | if path is None: |
---|
622 | return |
---|
623 | # default cansas xml doc |
---|
624 | doc = None |
---|
625 | for item in self.panels: |
---|
626 | try: |
---|
627 | if self.panels[item].window_name == 'Invariant': |
---|
628 | data = self.panels[item]._data |
---|
629 | if data != None: |
---|
630 | state = self.panels[item].state |
---|
631 | manager = self.panels[item]._manager |
---|
632 | new_doc = manager.state_reader.write_toXML(data, state) |
---|
633 | if hasattr(doc, "firstChild"): |
---|
634 | child = new_doc.firstChild.firstChild |
---|
635 | doc.firstChild.appendChild(child) |
---|
636 | else: |
---|
637 | doc = new_doc |
---|
638 | elif self.panels[item].window_name == 'pr_control': |
---|
639 | data = self.panels[item].manager.current_plottable |
---|
640 | if data != None: |
---|
641 | state = self.panels[item].get_state() |
---|
642 | manager = self.panels[item].manager |
---|
643 | new_doc = manager.state_reader.write_toXML(data, state) |
---|
644 | if hasattr(doc, "firstChild"): |
---|
645 | child = new_doc.firstChild.firstChild |
---|
646 | doc.firstChild.appendChild(child) |
---|
647 | else: |
---|
648 | doc = new_doc |
---|
649 | elif self.panels[item].window_name == 'Fit panel': |
---|
650 | for index in range(self.panels[item].GetPageCount()): |
---|
651 | selected_page = self.panels[item].GetPage(index) |
---|
652 | if hasattr(selected_page,"get_data"): |
---|
653 | data = selected_page.get_data() |
---|
654 | state = selected_page.state |
---|
655 | reader = selected_page.manager.state_reader |
---|
656 | new_doc = reader.write_toXML(data, state) |
---|
657 | if doc != None and hasattr(doc, "firstChild"): |
---|
658 | child = new_doc.firstChild.firstChild |
---|
659 | doc.firstChild.appendChild(child) |
---|
660 | else: |
---|
661 | doc = new_doc |
---|
662 | except: |
---|
663 | pass |
---|
664 | # Write the XML document |
---|
665 | if doc != None: |
---|
666 | fd = open(path, 'w') |
---|
667 | fd.write(doc.toprettyxml()) |
---|
668 | fd.close() |
---|
669 | else: |
---|
670 | #print "Nothing to save..." |
---|
671 | raise RuntimeError, "%s is not a SansView (.svs) file..." % path |
---|
672 | |
---|
673 | def _onClose(self, event): |
---|
674 | """ |
---|
675 | Store info to retrieve in xml before closing the application |
---|
676 | """ |
---|
677 | wx.Exit() |
---|
678 | sys.exit() |
---|
679 | |
---|
680 | def quit_guiframe(self): |
---|
681 | """ |
---|
682 | Pop up message to make sure the user wants to quit the application |
---|
683 | """ |
---|
684 | message = "Do you really want to quit \n" |
---|
685 | message += "this application?" |
---|
686 | dial = wx.MessageDialog(self, message, 'Question', |
---|
687 | wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION) |
---|
688 | if dial.ShowModal() == wx.ID_YES: |
---|
689 | return True |
---|
690 | else: |
---|
691 | return False |
---|
692 | |
---|
693 | def Close(self, event=None): |
---|
694 | """ |
---|
695 | Quit the application |
---|
696 | """ |
---|
697 | flag = self.quit_guiframe() |
---|
698 | if flag: |
---|
699 | wx.Frame.Close(self) |
---|
700 | wx.Exit() |
---|
701 | sys.exit() |
---|
702 | |
---|
703 | def _check_update(self, event=None): |
---|
704 | """ |
---|
705 | Check with the deployment server whether a new version |
---|
706 | of the application is available. |
---|
707 | A thread is started for the connecting with the server. The thread calls |
---|
708 | a call-back method when the current version number has been obtained. |
---|
709 | """ |
---|
710 | if hasattr(config, "__update_URL__"): |
---|
711 | import version |
---|
712 | checker = version.VersionThread(config.__update_URL__, |
---|
713 | self._process_version, |
---|
714 | baggage=event==None) |
---|
715 | checker.start() |
---|
716 | |
---|
717 | def _process_version(self, version, standalone=True): |
---|
718 | """ |
---|
719 | Call-back method for the process of checking for updates. |
---|
720 | This methods is called by a VersionThread object once the current |
---|
721 | version number has been obtained. If the check is being done in the |
---|
722 | background, the user will not be notified unless there's an update. |
---|
723 | |
---|
724 | :param version: version string |
---|
725 | :param standalone: True of the update is being checked in |
---|
726 | the background, False otherwise. |
---|
727 | |
---|
728 | """ |
---|
729 | try: |
---|
730 | if cmp(version, config.__version__) > 0: |
---|
731 | msg = "Version %s is available! See the Help " |
---|
732 | msg += "menu to download it." % version |
---|
733 | self.SetStatusText(msg) |
---|
734 | if not standalone: |
---|
735 | import webbrowser |
---|
736 | webbrowser.open(config.__download_page__) |
---|
737 | else: |
---|
738 | if not standalone: |
---|
739 | msg = "You have the latest version" |
---|
740 | msg += " of %s" % config.__appname__ |
---|
741 | self.SetStatusText(msg) |
---|
742 | except: |
---|
743 | msg = "guiframe: could not get latest application" |
---|
744 | msg += " version number\n %s" % sys.exc_value |
---|
745 | logging.error(msg) |
---|
746 | if not standalone: |
---|
747 | msg = "Could not connect to the application server." |
---|
748 | msg += " Please try again later." |
---|
749 | self.SetStatusText(msg) |
---|
750 | |
---|
751 | def _onAbout(self, evt): |
---|
752 | """ |
---|
753 | Pop up the about dialog |
---|
754 | |
---|
755 | :param evt: menu event |
---|
756 | |
---|
757 | """ |
---|
758 | if config._do_aboutbox: |
---|
759 | import aboutbox |
---|
760 | dialog = aboutbox.DialogAbout(None, -1, "") |
---|
761 | dialog.ShowModal() |
---|
762 | |
---|
763 | def set_manager(self, manager): |
---|
764 | """ |
---|
765 | Sets the application manager for this frame |
---|
766 | |
---|
767 | :param manager: frame manager |
---|
768 | """ |
---|
769 | self.app_manager = manager |
---|
770 | |
---|
771 | def post_init(self): |
---|
772 | """ |
---|
773 | This initialization method is called after the GUI |
---|
774 | has been created and all plug-ins loaded. It calls |
---|
775 | the post_init() method of each plug-in (if it exists) |
---|
776 | so that final initialization can be done. |
---|
777 | """ |
---|
778 | for item in self.plugins: |
---|
779 | if hasattr(item, "post_init"): |
---|
780 | item.post_init() |
---|
781 | |
---|
782 | def set_default_perspective(self): |
---|
783 | """ |
---|
784 | Choose among the plugin the first plug-in that has |
---|
785 | "set_default_perspective" method and its return value is True will be |
---|
786 | as a default perspective when the welcome page is closed |
---|
787 | """ |
---|
788 | for item in self.plugins: |
---|
789 | if hasattr(item, "set_default_perspective"): |
---|
790 | if item.set_default_perspective(): |
---|
791 | item.on_perspective(event=None) |
---|
792 | self._current_perspective = item |
---|
793 | return |
---|
794 | |
---|
795 | def set_perspective(self, panels): |
---|
796 | """ |
---|
797 | Sets the perspective of the GUI. |
---|
798 | Opens all the panels in the list, and closes |
---|
799 | all the others. |
---|
800 | |
---|
801 | :param panels: list of panels |
---|
802 | """ |
---|
803 | |
---|
804 | for item in self.panels: |
---|
805 | # Check whether this is a sticky panel |
---|
806 | if hasattr(self.panels[item], "ALWAYS_ON"): |
---|
807 | if self.panels[item].ALWAYS_ON: |
---|
808 | continue |
---|
809 | if self.panels[item].window_name in panels: |
---|
810 | if not self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
811 | self._mgr.GetPane(self.panels[item].window_name).Show() |
---|
812 | else: |
---|
813 | if self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
814 | self._mgr.GetPane(self.panels[item].window_name).Hide() |
---|
815 | self._mgr.Update() |
---|
816 | |
---|
817 | def choose_file(self, path=None): |
---|
818 | """ |
---|
819 | Functionality that belongs elsewhere |
---|
820 | Should add a hook to specify the preferred file type/extension. |
---|
821 | """ |
---|
822 | #TODO: clean this up |
---|
823 | from .data_loader import choose_data_file |
---|
824 | # Choose a file path |
---|
825 | if path == None: |
---|
826 | path = choose_data_file(self, self._default_save_location) |
---|
827 | if not path == None: |
---|
828 | try: |
---|
829 | self._default_save_location = os.path.dirname(path) |
---|
830 | except: |
---|
831 | pass |
---|
832 | return path |
---|
833 | |
---|
834 | def add_data(self, data_list): |
---|
835 | """ |
---|
836 | receive a list of data . store them its data manager if possible |
---|
837 | determine if data was be plot of send to data perspectives |
---|
838 | """ |
---|
839 | self.data_manager.add_data(data_list) |
---|
840 | if AUTO_SET_DATA: |
---|
841 | if self._current_perspective is not None: |
---|
842 | try: |
---|
843 | self._current_perspective.set_data(data_list) |
---|
844 | except: |
---|
845 | raise |
---|
846 | #msg = str(sys.exc_value) |
---|
847 | #wx.PostEvent(self, StatusEvent(status=msg, |
---|
848 | # info="error")) |
---|
849 | else: |
---|
850 | msg = "Guiframe does not have a current perspective" |
---|
851 | logging.info(msg) |
---|
852 | if DATA_MANAGER: |
---|
853 | print "will show data panel" |
---|
854 | if AUTO_PLOT: |
---|
855 | self.plot_data(data_list) |
---|
856 | |
---|
857 | def plot_data(self, data_list): |
---|
858 | """ |
---|
859 | send a list of data to plot |
---|
860 | """ |
---|
861 | for new_plot in data_list: |
---|
862 | wx.PostEvent(self, NewPlotEvent(plot=new_plot, |
---|
863 | title=str(new_plot.title))) |
---|
864 | |
---|
865 | def set_current_perspective(self, perspective): |
---|
866 | """ |
---|
867 | set the current active perspective |
---|
868 | """ |
---|
869 | self._current_perspective = perspective |
---|
870 | |
---|
871 | class DefaultPanel(wx.Panel): |
---|
872 | """ |
---|
873 | Defines the API for a panels to work with |
---|
874 | the GUI manager |
---|
875 | """ |
---|
876 | ## Internal nickname for the window, used by the AUI manager |
---|
877 | window_name = "default" |
---|
878 | ## Name to appear on the window title bar |
---|
879 | window_caption = "Welcome panel" |
---|
880 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
881 | CENTER_PANE = True |
---|
882 | |
---|
883 | |
---|
884 | # Toy application to test this Frame |
---|
885 | class ViewApp(wx.App): |
---|
886 | """ |
---|
887 | """ |
---|
888 | def OnInit(self): |
---|
889 | """ |
---|
890 | """ |
---|
891 | self.frame = ViewerFrame(None, -1, config.__appname__) |
---|
892 | self.frame.Show(True) |
---|
893 | |
---|
894 | if hasattr(self.frame, 'special'): |
---|
895 | self.frame.special.SetCurrent() |
---|
896 | self.SetTopWindow(self.frame) |
---|
897 | return True |
---|
898 | |
---|
899 | def set_manager(self, manager): |
---|
900 | """ |
---|
901 | Sets a reference to the application manager |
---|
902 | of the GUI manager (Frame) |
---|
903 | """ |
---|
904 | self.frame.set_manager(manager) |
---|
905 | |
---|
906 | def build_gui(self): |
---|
907 | """ |
---|
908 | Build the GUI |
---|
909 | """ |
---|
910 | self.frame.build_gui() |
---|
911 | self.frame.post_init() |
---|
912 | |
---|
913 | def set_welcome_panel(self, panel_class): |
---|
914 | """ |
---|
915 | Set the welcome panel |
---|
916 | |
---|
917 | :param panel_class: class of the welcome panel to be instantiated |
---|
918 | |
---|
919 | """ |
---|
920 | self.frame.set_welcome_panel(panel_class) |
---|
921 | |
---|
922 | def add_perspective(self, perspective): |
---|
923 | """ |
---|
924 | Manually add a perspective to the application GUI |
---|
925 | """ |
---|
926 | self.frame.add_perspective(perspective) |
---|
927 | |
---|
928 | |
---|
929 | if __name__ == "__main__": |
---|
930 | app = ViewApp(0) |
---|
931 | app.MainLoop() |
---|
932 | |
---|
933 | |
---|