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