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 | How-to build an application using guiframe: |
---|
11 | |
---|
12 | 1- Write a main application script along the lines of dummyapp.py |
---|
13 | 2- Write a config script along the lines of config.py, and name it local_config.py |
---|
14 | 3- Write your plug-ins and place them in a directory called "perspectives". |
---|
15 | - Look at local_perspectives/plotting for an example of a plug-in. |
---|
16 | - A plug-in should define a class called Plugin. See abstract class below. |
---|
17 | |
---|
18 | """ |
---|
19 | import wx |
---|
20 | import wx.aui |
---|
21 | import os, sys |
---|
22 | try: |
---|
23 | # Try to find a local config |
---|
24 | import imp |
---|
25 | path = os.getcwd() |
---|
26 | if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \ |
---|
27 | (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))): |
---|
28 | fObj, path, descr = imp.find_module('local_config', [path]) |
---|
29 | config = imp.load_module('local_config', fObj, path, descr) |
---|
30 | else: |
---|
31 | # Try simply importing local_config |
---|
32 | import local_config as config |
---|
33 | except: |
---|
34 | # Didn't find local config, load the default |
---|
35 | import config |
---|
36 | |
---|
37 | from sans.guicomm.events import EVT_STATUS,Model2DPanelEvent |
---|
38 | from sans.guicomm.events import EVT_NEW_PLOT,EVT_SLICER_PARS_UPDATE |
---|
39 | import history |
---|
40 | import warnings |
---|
41 | warnings.simplefilter("ignore") |
---|
42 | |
---|
43 | import logging |
---|
44 | |
---|
45 | class Plugin: |
---|
46 | """ |
---|
47 | This class defines the interface for a Plugin class |
---|
48 | that can be used by the gui_manager. |
---|
49 | |
---|
50 | Plug-ins should be placed in a sub-directory called "perspectives". |
---|
51 | For example, a plug-in called Foo should be place in "perspectives/Foo". |
---|
52 | That directory contains at least two files: |
---|
53 | perspectives/Foo/__init.py contains two lines: |
---|
54 | |
---|
55 | PLUGIN_ID = "Foo plug-in 1.0" |
---|
56 | from Foo import * |
---|
57 | |
---|
58 | perspectives/Foo/Foo.py contains the definition of the Plugin |
---|
59 | class for the Foo plug-in. The interface of that Plugin class |
---|
60 | should follow the interface of the class you are looking at. |
---|
61 | """ |
---|
62 | |
---|
63 | def __init__(self): |
---|
64 | """ |
---|
65 | Abstract class for gui_manager Plugins. |
---|
66 | """ |
---|
67 | ## Plug-in name. It will appear on the application menu. |
---|
68 | self.sub_menu = "Plugin" |
---|
69 | |
---|
70 | ## Reference to the parent window. Filled by get_panels() below. |
---|
71 | self.parent = None |
---|
72 | |
---|
73 | ## List of panels that you would like to open in AUI windows |
---|
74 | # for your plug-in. This defines your plug-in "perspective" |
---|
75 | self.perspective = [] |
---|
76 | |
---|
77 | raise RuntimeError, "gui_manager.Plugin is an abstract class" |
---|
78 | |
---|
79 | def populate_menu(self, id, parent): |
---|
80 | """ |
---|
81 | Create and return the list of application menu |
---|
82 | items for the plug-in. |
---|
83 | |
---|
84 | @param id: deprecated. Un-used. |
---|
85 | @param parent: parent window |
---|
86 | @return: plug-in menu |
---|
87 | """ |
---|
88 | import wx |
---|
89 | # Create a menu |
---|
90 | plug_menu = wx.Menu() |
---|
91 | |
---|
92 | # Always get event IDs from wx |
---|
93 | id = wx.NewId() |
---|
94 | |
---|
95 | # Fill your menu |
---|
96 | plug_menu.Append(id, '&Do something') |
---|
97 | wx.EVT_MENU(owner, id, self._on_do_something) |
---|
98 | |
---|
99 | # Returns the menu and a name for it. |
---|
100 | return [(id, plug_menu, "name of the application menu")] |
---|
101 | |
---|
102 | |
---|
103 | def get_panels(self, parent): |
---|
104 | """ |
---|
105 | Create and return the list of wx.Panels for your plug-in. |
---|
106 | Define the plug-in perspective. |
---|
107 | |
---|
108 | Panels should inherit from DefaultPanel defined below, |
---|
109 | or should present the same interface. They must define |
---|
110 | "window_caption" and "window_name". |
---|
111 | |
---|
112 | @param parent: parent window |
---|
113 | @return: list of panels |
---|
114 | """ |
---|
115 | ## Save a reference to the parent |
---|
116 | self.parent = parent |
---|
117 | |
---|
118 | # Define a panel |
---|
119 | mypanel = DefaultPanel(self.parent, -1) |
---|
120 | |
---|
121 | # If needed, add its name to the perspective list |
---|
122 | self.perspective.append(self.control_panel.window_name) |
---|
123 | |
---|
124 | # Return the list of panels |
---|
125 | return [mypanel] |
---|
126 | |
---|
127 | def get_context_menu(self, graph=None): |
---|
128 | """ |
---|
129 | This method is optional. |
---|
130 | |
---|
131 | When the context menu of a plot is rendered, the |
---|
132 | get_context_menu method will be called to give you a |
---|
133 | chance to add a menu item to the context menu. |
---|
134 | |
---|
135 | A ref to a Graph object is passed so that you can |
---|
136 | investigate the plot content and decide whether you |
---|
137 | need to add items to the context menu. |
---|
138 | |
---|
139 | This method returns a list of menu items. |
---|
140 | Each item is itself a list defining the text to |
---|
141 | appear in the menu, a tool-tip help text, and a |
---|
142 | call-back method. |
---|
143 | |
---|
144 | @param graph: the Graph object to which we attach the context menu |
---|
145 | @return: a list of menu items with call-back function |
---|
146 | """ |
---|
147 | return [["Menu text", |
---|
148 | "Tool-tip help text", |
---|
149 | self._on_context_do_something]] |
---|
150 | |
---|
151 | def get_perspective(self): |
---|
152 | """ |
---|
153 | Get the list of panel names for this perspective |
---|
154 | """ |
---|
155 | return self.perspective |
---|
156 | |
---|
157 | def on_perspective(self, event): |
---|
158 | """ |
---|
159 | Call back function for the perspective menu item. |
---|
160 | We notify the parent window that the perspective |
---|
161 | has changed. |
---|
162 | @param event: menu event |
---|
163 | """ |
---|
164 | self.parent.set_perspective(self.perspective) |
---|
165 | |
---|
166 | def post_init(self): |
---|
167 | """ |
---|
168 | Post initialization call back to close the loose ends |
---|
169 | """ |
---|
170 | pass |
---|
171 | |
---|
172 | |
---|
173 | class ViewerFrame(wx.Frame): |
---|
174 | """ |
---|
175 | Main application frame |
---|
176 | """ |
---|
177 | def __init__(self, parent, id, title, window_height=700, window_width=900): |
---|
178 | #def __init__(self, parent, id, title, window_height=800, window_width=800): |
---|
179 | """ |
---|
180 | Initialize the Frame object |
---|
181 | """ |
---|
182 | from local_perspectives.plotting import plotting |
---|
183 | #wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(800, 700)) |
---|
184 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(900, 600)) |
---|
185 | |
---|
186 | # Preferred window size |
---|
187 | self._window_height = window_height |
---|
188 | self._window_width = window_width |
---|
189 | |
---|
190 | # Logging info |
---|
191 | logging.basicConfig(level=logging.DEBUG, |
---|
192 | format='%(asctime)s %(levelname)s %(message)s', |
---|
193 | filename='sans_app.log', |
---|
194 | filemode='w') |
---|
195 | |
---|
196 | path = os.path.dirname(__file__) |
---|
197 | ico_file = os.path.join(path,'images/ball.ico') |
---|
198 | if os.path.isfile(ico_file): |
---|
199 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
200 | else: |
---|
201 | ico_file = os.path.join(os.getcwd(),'images/ball.ico') |
---|
202 | if os.path.isfile(ico_file): |
---|
203 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
204 | |
---|
205 | ## Application manager |
---|
206 | self.app_manager = None |
---|
207 | |
---|
208 | ## Find plug-ins |
---|
209 | # Modify this so that we can specify the directory to look into |
---|
210 | self.plugins = self._find_plugins() |
---|
211 | self.plugins.append(plotting.Plugin()) |
---|
212 | |
---|
213 | ## List of panels |
---|
214 | self.panels = {} |
---|
215 | |
---|
216 | ## Next available ID for wx gui events |
---|
217 | #TODO: No longer used - remove all calls to this |
---|
218 | self.next_id = 20000 |
---|
219 | |
---|
220 | # Default locations |
---|
221 | self._default_save_location = os.getcwd() |
---|
222 | |
---|
223 | ## Default welcome panel |
---|
224 | self.defaultPanel = DefaultPanel(self, -1, style=wx.RAISED_BORDER) |
---|
225 | |
---|
226 | # History panel |
---|
227 | self.histPanel = history.HistoryPanel(self, style=wx.RAISED_BORDER) |
---|
228 | # self.build_gui() |
---|
229 | |
---|
230 | # Register the close event so it calls our own method |
---|
231 | wx.EVT_CLOSE(self, self._onClose) |
---|
232 | # Register to status events |
---|
233 | self.Bind(EVT_STATUS, self._on_status_event) |
---|
234 | self.Bind(EVT_SLICER_PARS_UPDATE, self._onEVT_SLICER_PANEL) |
---|
235 | |
---|
236 | |
---|
237 | def _onEVT_SLICER_PANEL(self, event): |
---|
238 | """ |
---|
239 | receive and event telling to update a panel with a name starting with |
---|
240 | event.panel_name. this method update slicer panel for a given interactor. |
---|
241 | @param event: contains type of slicer , paramaters for updating the panel |
---|
242 | and panel_name to find the slicer 's panel concerned. |
---|
243 | """ |
---|
244 | for item in self.panels: |
---|
245 | if self.panels[item].window_caption.startswith(event.panel_name): |
---|
246 | self.panels[item].set_slicer(event.type, event.params) |
---|
247 | self._mgr.Update() |
---|
248 | break |
---|
249 | |
---|
250 | def build_gui(self): |
---|
251 | # Set up the layout |
---|
252 | self._setup_layout() |
---|
253 | |
---|
254 | # Set up the menu |
---|
255 | self._setup_menus() |
---|
256 | |
---|
257 | self.Fit() |
---|
258 | |
---|
259 | #self._check_update(None) |
---|
260 | |
---|
261 | def _setup_layout(self): |
---|
262 | """ |
---|
263 | Set up the layout |
---|
264 | """ |
---|
265 | # Status bar |
---|
266 | from statusbar import MyStatusBar |
---|
267 | self.sb = MyStatusBar(self,wx.ID_ANY) |
---|
268 | self.SetStatusBar(self.sb) |
---|
269 | |
---|
270 | # Add panel |
---|
271 | self._mgr = wx.aui.AuiManager(self) |
---|
272 | |
---|
273 | # Load panels |
---|
274 | self._load_panels() |
---|
275 | |
---|
276 | self._mgr.Update() |
---|
277 | |
---|
278 | def add_perspective(self, plugin): |
---|
279 | """ |
---|
280 | Add a perspective if it doesn't already |
---|
281 | exist. |
---|
282 | """ |
---|
283 | is_loaded = False |
---|
284 | for item in self.plugins: |
---|
285 | if plugin.__class__==item.__class__: |
---|
286 | print "Plugin %s already loaded" % plugin.__class__.__name__ |
---|
287 | is_loaded = True |
---|
288 | |
---|
289 | if not is_loaded: |
---|
290 | self.plugins.append(plugin) |
---|
291 | |
---|
292 | def _find_plugins(self, dir="perspectives"): |
---|
293 | """ |
---|
294 | Find available perspective plug-ins |
---|
295 | @param dir: directory in which to look for plug-ins |
---|
296 | @return: list of plug-ins |
---|
297 | """ |
---|
298 | import imp |
---|
299 | print "Looking for plug-ins in %s" % dir |
---|
300 | # List of plug-in objects |
---|
301 | |
---|
302 | #path_exe = os.getcwd() |
---|
303 | #path_plugs = os.path.join(path_exe, dir) |
---|
304 | f = open("load.log",'w') |
---|
305 | f.write(os.getcwd()+'\n\n') |
---|
306 | #f.write(str(os.listdir(dir))+'\n') |
---|
307 | |
---|
308 | |
---|
309 | plugins = [] |
---|
310 | # Go through files in panels directory |
---|
311 | try: |
---|
312 | list = os.listdir(dir) |
---|
313 | for item in list: |
---|
314 | toks = os.path.splitext(os.path.basename(item)) |
---|
315 | name = None |
---|
316 | if not toks[0] == '__init__': |
---|
317 | |
---|
318 | if toks[1]=='.py' or toks[1]=='': |
---|
319 | name = toks[0] |
---|
320 | |
---|
321 | path = [os.path.abspath(dir)] |
---|
322 | file = None |
---|
323 | try: |
---|
324 | if toks[1]=='': |
---|
325 | f.write("trying to import \n") |
---|
326 | mod_path = '.'.join([dir, name]) |
---|
327 | f.write("mod_path= %s\n" % mod_path) |
---|
328 | module = __import__(mod_path, globals(), locals(), [name]) |
---|
329 | f.write(str(module)+'\n') |
---|
330 | else: |
---|
331 | (file, path, info) = imp.find_module(name, path) |
---|
332 | module = imp.load_module( name, file, item, info ) |
---|
333 | if hasattr(module, "PLUGIN_ID"): |
---|
334 | try: |
---|
335 | plugins.append(module.Plugin()) |
---|
336 | print "Found plug-in: %s" % module.PLUGIN_ID |
---|
337 | except: |
---|
338 | config.printEVT("Error accessing PluginPanel in %s\n %s" % (name, sys.exc_value)) |
---|
339 | |
---|
340 | except: |
---|
341 | print sys.exc_value |
---|
342 | f.write(str(sys.exc_value)+'\n') |
---|
343 | finally: |
---|
344 | if not file==None: |
---|
345 | file.close() |
---|
346 | except: |
---|
347 | # Should raise and catch at a higher level and display error on status bar |
---|
348 | pass |
---|
349 | f.write(str(plugins)+'\n') |
---|
350 | f.close() |
---|
351 | return plugins |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | def _load_panels(self): |
---|
356 | """ |
---|
357 | Load all panels in the panels directory |
---|
358 | """ |
---|
359 | |
---|
360 | # Look for plug-in panels |
---|
361 | panels = [] |
---|
362 | for item in self.plugins: |
---|
363 | if hasattr(item, "get_panels"): |
---|
364 | ps = item.get_panels(self) |
---|
365 | panels.extend(ps) |
---|
366 | |
---|
367 | # Show a default panel with some help information |
---|
368 | # It also sets the size of the application windows |
---|
369 | self.panels["default"] = self.defaultPanel |
---|
370 | # History panel |
---|
371 | self.panels["historyPanel"] = self.histPanel |
---|
372 | |
---|
373 | self._mgr.AddPane(self.defaultPanel, wx.aui.AuiPaneInfo(). |
---|
374 | Name("default"). |
---|
375 | CenterPane(). |
---|
376 | # This is where we set the size of the application window |
---|
377 | BestSize(wx.Size(self._window_width, self._window_height)). |
---|
378 | MinSize(wx.Size(self._window_width, self._window_height)). |
---|
379 | Show()) |
---|
380 | self._mgr.AddPane(self.histPanel, wx.aui.AuiPaneInfo(). |
---|
381 | Name("historyPanel").Caption("History"). |
---|
382 | #Float(). |
---|
383 | Bottom(). |
---|
384 | Dock(). |
---|
385 | TopDockable(). |
---|
386 | BottomDockable(). |
---|
387 | LeftDockable(). |
---|
388 | RightDockable(). |
---|
389 | MinimizeButton(). |
---|
390 | Hide(). |
---|
391 | #Show(). |
---|
392 | BestSize(wx.Size(500,600)). |
---|
393 | MinSize(wx.Size(200,150))) |
---|
394 | |
---|
395 | # Add the panels to the AUI manager |
---|
396 | for panel_class in panels: |
---|
397 | p = panel_class |
---|
398 | id = wx.NewId() |
---|
399 | |
---|
400 | # Check whether we need to put this panel |
---|
401 | # in the center pane |
---|
402 | if hasattr(p, "CENTER_PANE"): |
---|
403 | if p.CENTER_PANE: |
---|
404 | self.panels[str(id)] = p |
---|
405 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
406 | Name(p.window_name).Caption(p.window_caption). |
---|
407 | CenterPane(). |
---|
408 | BestSize(wx.Size(600,600)). |
---|
409 | MinSize(wx.Size(400,400)). |
---|
410 | #BestSize(wx.Size(500,500)). |
---|
411 | #MinSize(wx.Size(200,200)). |
---|
412 | Hide()) |
---|
413 | |
---|
414 | else: |
---|
415 | self.panels[str(id)] = p |
---|
416 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
417 | Name(p.window_name).Caption(p.window_caption). |
---|
418 | #Floatable(). |
---|
419 | #Float(). |
---|
420 | Right(). |
---|
421 | Dock(). |
---|
422 | TopDockable(). |
---|
423 | BottomDockable(). |
---|
424 | LeftDockable(). |
---|
425 | RightDockable(). |
---|
426 | MinimizeButton(). |
---|
427 | Hide(). |
---|
428 | #Show(). |
---|
429 | BestSize(wx.Size(600,600)). |
---|
430 | MinSize(wx.Size(500,500))) |
---|
431 | #BestSize(wx.Size(400,400)). |
---|
432 | #MinSize(wx.Size(300,300))) |
---|
433 | |
---|
434 | |
---|
435 | |
---|
436 | def get_context_menu(self, graph=None): |
---|
437 | """ |
---|
438 | Get the context menu items made available |
---|
439 | by the different plug-ins. |
---|
440 | This function is used by the plotting module |
---|
441 | """ |
---|
442 | menu_list = [] |
---|
443 | for item in self.plugins: |
---|
444 | if hasattr(item, "get_context_menu"): |
---|
445 | menu_list.extend(item.get_context_menu(graph)) |
---|
446 | |
---|
447 | return menu_list |
---|
448 | |
---|
449 | def popup_panel(self, p): |
---|
450 | """ |
---|
451 | Add a panel object to the AUI manager |
---|
452 | @param p: panel object to add to the AUI manager |
---|
453 | @return: ID of the event associated with the new panel [int] |
---|
454 | """ |
---|
455 | |
---|
456 | ID = wx.NewId() |
---|
457 | self.panels[str(ID)] = p |
---|
458 | |
---|
459 | count = 0 |
---|
460 | for item in self.panels: |
---|
461 | if self.panels[item].window_name.startswith(p.window_name): |
---|
462 | count += 1 |
---|
463 | |
---|
464 | windowname = p.window_name |
---|
465 | caption = p.window_caption |
---|
466 | |
---|
467 | if count>0: |
---|
468 | windowname += str(count+1) |
---|
469 | caption += (' '+str(count)) |
---|
470 | |
---|
471 | p.window_name = windowname |
---|
472 | p.window_caption = caption |
---|
473 | |
---|
474 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
475 | Name(windowname).Caption(caption). |
---|
476 | Floatable(). |
---|
477 | #Float(). |
---|
478 | Right(). |
---|
479 | Dock(). |
---|
480 | TopDockable(). |
---|
481 | BottomDockable(). |
---|
482 | LeftDockable(). |
---|
483 | RightDockable(). |
---|
484 | MinimizeButton(). |
---|
485 | #Hide(). |
---|
486 | #Show(). |
---|
487 | BestSize(wx.Size(600,600)). |
---|
488 | MinSize(wx.Size(500,500))) |
---|
489 | #BestSize(wx.Size(400,400)). |
---|
490 | #MinSize(wx.Size(350,350))) |
---|
491 | pane = self._mgr.GetPane(windowname) |
---|
492 | self._mgr.MaximizePane(pane) |
---|
493 | self._mgr.RestoreMaximizedPane() |
---|
494 | |
---|
495 | |
---|
496 | # Register for showing/hiding the panel |
---|
497 | |
---|
498 | wx.EVT_MENU(self, ID, self._on_view) |
---|
499 | |
---|
500 | self._mgr.Update() |
---|
501 | return ID |
---|
502 | |
---|
503 | def _setup_menus(self): |
---|
504 | """ |
---|
505 | Set up the application menus |
---|
506 | """ |
---|
507 | # Menu |
---|
508 | menubar = wx.MenuBar() |
---|
509 | |
---|
510 | # File menu |
---|
511 | filemenu = wx.Menu() |
---|
512 | |
---|
513 | id = wx.NewId() |
---|
514 | filemenu.Append(id, '&Open', 'Open a file') |
---|
515 | wx.EVT_MENU(self, id, self._on_open) |
---|
516 | |
---|
517 | #id = wx.NewId() |
---|
518 | #filemenu.Append(id, '&History', 'Register previous States') |
---|
519 | #wx.EVT_MENU(self, id, self._onHistoryPanel) |
---|
520 | |
---|
521 | id = wx.NewId() |
---|
522 | filemenu.Append(id,'&Quit', 'Exit') |
---|
523 | wx.EVT_MENU(self, id, self.Close) |
---|
524 | |
---|
525 | # Add sub menus |
---|
526 | menubar.Append(filemenu, '&File') |
---|
527 | |
---|
528 | # Plot menu |
---|
529 | # Attach a menu item for each panel in our |
---|
530 | # panel list that also appears in a plug-in. |
---|
531 | # TODO: clean this up. We should just identify |
---|
532 | # plug-in panels and add them all. |
---|
533 | |
---|
534 | # Only add the panel menu if there is more than one panel |
---|
535 | n_panels = 0 |
---|
536 | for plug in self.plugins: |
---|
537 | pers = plug.get_perspective() |
---|
538 | if len(pers)>0: |
---|
539 | n_panels += 1 |
---|
540 | |
---|
541 | if n_panels>1: |
---|
542 | viewmenu = wx.Menu() |
---|
543 | for plug in self.plugins: |
---|
544 | plugmenu = wx.Menu() |
---|
545 | pers = plug.get_perspective() |
---|
546 | if len(pers)>0: |
---|
547 | for item in self.panels: |
---|
548 | if item == 'default': |
---|
549 | continue |
---|
550 | panel = self.panels[item] |
---|
551 | if panel.window_name in pers: |
---|
552 | plugmenu.Append(int(item), panel.window_caption, "Show %s window" % panel.window_caption) |
---|
553 | |
---|
554 | |
---|
555 | |
---|
556 | wx.EVT_MENU(self, int(item), self._on_view) |
---|
557 | |
---|
558 | viewmenu.AppendMenu(wx.NewId(), plug.sub_menu, plugmenu, plug.sub_menu) |
---|
559 | |
---|
560 | menubar.Append(viewmenu, '&Panel') |
---|
561 | |
---|
562 | # Perspective |
---|
563 | # Attach a menu item for each defined perspective. |
---|
564 | # Only add the perspective menu if there are more than one perspectves |
---|
565 | n_perspectives = 0 |
---|
566 | for plug in self.plugins: |
---|
567 | if len(plug.get_perspective()) > 0: |
---|
568 | n_perspectives += 1 |
---|
569 | |
---|
570 | if n_perspectives>1: |
---|
571 | p_menu = wx.Menu() |
---|
572 | for plug in self.plugins: |
---|
573 | if len(plug.get_perspective()) > 0: |
---|
574 | id = wx.NewId() |
---|
575 | p_menu.Append(id, plug.sub_menu, "Switch to %s perspective" % plug.sub_menu) |
---|
576 | wx.EVT_MENU(self, id, plug.on_perspective) |
---|
577 | menubar.Append(p_menu, '&Perspective') |
---|
578 | |
---|
579 | # Help menu |
---|
580 | helpmenu = wx.Menu() |
---|
581 | |
---|
582 | # Look for help item in plug-ins |
---|
583 | for item in self.plugins: |
---|
584 | if hasattr(item, "help"): |
---|
585 | id = wx.NewId() |
---|
586 | helpmenu.Append(id,'&%s help' % item.sub_menu, '') |
---|
587 | wx.EVT_MENU(self, id, item.help) |
---|
588 | |
---|
589 | if config._do_aboutbox: |
---|
590 | id = wx.NewId() |
---|
591 | helpmenu.Append(id,'&About', 'Software information') |
---|
592 | wx.EVT_MENU(self, id, self._onAbout) |
---|
593 | id = wx.NewId() |
---|
594 | helpmenu.Append(id,'&Check for update', 'Check for the latest version of %s' % config.__appname__) |
---|
595 | wx.EVT_MENU(self, id, self._check_update) |
---|
596 | |
---|
597 | |
---|
598 | |
---|
599 | |
---|
600 | # Look for plug-in menus |
---|
601 | # Add available plug-in sub-menus. |
---|
602 | for item in self.plugins: |
---|
603 | if hasattr(item, "populate_menu"): |
---|
604 | for (self.next_id, menu, name) in item.populate_menu(self.next_id, self): |
---|
605 | menubar.Append(menu, name) |
---|
606 | |
---|
607 | |
---|
608 | menubar.Append(helpmenu, '&Help') |
---|
609 | |
---|
610 | self.SetMenuBar(menubar) |
---|
611 | |
---|
612 | |
---|
613 | |
---|
614 | def _on_status_event(self, evt): |
---|
615 | """ |
---|
616 | Display status message |
---|
617 | """ |
---|
618 | self.sb.clear_gauge( msg="") |
---|
619 | mythread=None |
---|
620 | mytype= None |
---|
621 | if hasattr(evt, "curr_thread"): |
---|
622 | mythread= evt.curr_thread |
---|
623 | if hasattr(evt, "type"): |
---|
624 | mytype= evt.type |
---|
625 | self.sb.set_status( type=mytype,msg=str(evt.status),thread=mythread) |
---|
626 | |
---|
627 | |
---|
628 | |
---|
629 | def _on_view(self, evt): |
---|
630 | """ |
---|
631 | A panel was selected to be shown. If it's not already |
---|
632 | shown, display it. |
---|
633 | @param evt: menu event |
---|
634 | """ |
---|
635 | self.show_panel(evt.GetId()) |
---|
636 | |
---|
637 | def show_panel(self, uid): |
---|
638 | """ |
---|
639 | Shows the panel with the given id |
---|
640 | @param uid: unique ID number of the panel to show |
---|
641 | """ |
---|
642 | ID = str(uid) |
---|
643 | config.printEVT("show_panel: %s" % ID) |
---|
644 | if ID in self.panels.keys(): |
---|
645 | if not self._mgr.GetPane(self.panels[ID].window_name).IsShown(): |
---|
646 | self._mgr.GetPane(self.panels[ID].window_name).Show() |
---|
647 | # Hide default panel |
---|
648 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
649 | else: |
---|
650 | self.sb.set_status( msg=" Panel %s is displayed"%self.panels[ID].window_name) |
---|
651 | |
---|
652 | self._mgr.Update() |
---|
653 | def _onHistoryPanel(self, event): |
---|
654 | print "on history" |
---|
655 | if not self._mgr.GetPane("historyPanel").IsShown(): |
---|
656 | self._mgr.GetPane("historyPanel").Show() |
---|
657 | self._mgr.Update() |
---|
658 | return |
---|
659 | |
---|
660 | def _on_open(self, event): |
---|
661 | |
---|
662 | from data_loader import plot_data |
---|
663 | path = self.choose_file() |
---|
664 | |
---|
665 | if path and os.path.isfile(path): |
---|
666 | plot_data(self, path) |
---|
667 | |
---|
668 | |
---|
669 | |
---|
670 | def _onClose(self, event): |
---|
671 | import sys |
---|
672 | wx.Exit() |
---|
673 | sys.exit() |
---|
674 | |
---|
675 | def Close(self, event=None): |
---|
676 | """ |
---|
677 | Quit the application |
---|
678 | """ |
---|
679 | import sys |
---|
680 | wx.Frame.Close(self) |
---|
681 | wx.Exit() |
---|
682 | sys.exit() |
---|
683 | |
---|
684 | |
---|
685 | def _check_update(self, event=None): |
---|
686 | """ |
---|
687 | Check with the deployment server whether a new version |
---|
688 | of the application is available |
---|
689 | """ |
---|
690 | import urllib |
---|
691 | try: |
---|
692 | h = urllib.urlopen(config.__update_URL__) |
---|
693 | lines = h.readlines() |
---|
694 | line = '' |
---|
695 | if len(lines)>0: |
---|
696 | line = lines[0] |
---|
697 | |
---|
698 | toks = line.lstrip().rstrip().split('.') |
---|
699 | toks_current = config.__version__.split('.') |
---|
700 | update_available = False |
---|
701 | for i in range(len(toks)): |
---|
702 | if len(toks[i].strip())>0: |
---|
703 | if int(toks[i].strip())>int(toks_current[i]): |
---|
704 | update_available = True |
---|
705 | if update_available: |
---|
706 | #print "Version %s is available" % line.rstrip().lstrip() |
---|
707 | self.SetStatusText("Version %s is available! See the Help menu to download it." % line.rstrip().lstrip()) |
---|
708 | if event != None: |
---|
709 | import webbrowser |
---|
710 | webbrowser.open(config.__download_page__) |
---|
711 | else: |
---|
712 | if event != None: |
---|
713 | self.SetStatusText("You have the latest version of %s" % config.__appname__) |
---|
714 | except: |
---|
715 | if event != None: |
---|
716 | self.SetStatusText("You have the latest version of %s" % config.__appname__) |
---|
717 | |
---|
718 | |
---|
719 | def _onAbout(self, evt): |
---|
720 | """ |
---|
721 | Pop up the about dialog |
---|
722 | @param evt: menu event |
---|
723 | """ |
---|
724 | if config._do_aboutbox: |
---|
725 | import aboutbox |
---|
726 | dialog = aboutbox.DialogAbout(None, -1, "") |
---|
727 | dialog.ShowModal() |
---|
728 | |
---|
729 | def set_manager(self, manager): |
---|
730 | """ |
---|
731 | Sets the application manager for this frame |
---|
732 | @param manager: frame manager |
---|
733 | """ |
---|
734 | self.app_manager = manager |
---|
735 | |
---|
736 | def post_init(self): |
---|
737 | """ |
---|
738 | This initialization method is called after the GUI |
---|
739 | has been created and all plug-ins loaded. It calls |
---|
740 | the post_init() method of each plug-in (if it exists) |
---|
741 | so that final initialization can be done. |
---|
742 | """ |
---|
743 | for item in self.plugins: |
---|
744 | if hasattr(item, "post_init"): |
---|
745 | item.post_init() |
---|
746 | |
---|
747 | def set_perspective(self, panels): |
---|
748 | """ |
---|
749 | Sets the perspective of the GUI. |
---|
750 | Opens all the panels in the list, and closes |
---|
751 | all the others. |
---|
752 | |
---|
753 | @param panels: list of panels |
---|
754 | """ |
---|
755 | for item in self.panels: |
---|
756 | # Check whether this is a sticky panel |
---|
757 | if hasattr(self.panels[item], "ALWAYS_ON"): |
---|
758 | if self.panels[item].ALWAYS_ON: |
---|
759 | continue |
---|
760 | |
---|
761 | if self.panels[item].window_name in panels: |
---|
762 | if not self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
763 | self._mgr.GetPane(self.panels[item].window_name).Show() |
---|
764 | else: |
---|
765 | if self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
766 | self._mgr.GetPane(self.panels[item].window_name).Hide() |
---|
767 | |
---|
768 | self._mgr.Update() |
---|
769 | |
---|
770 | def choose_file(self): |
---|
771 | """ |
---|
772 | Functionality that belongs elsewhere |
---|
773 | Should add a hook to specify the preferred file type/extension. |
---|
774 | """ |
---|
775 | #TODO: clean this up |
---|
776 | from data_loader import choose_data_file |
---|
777 | path = choose_data_file(self, self._default_save_location) |
---|
778 | if not path==None: |
---|
779 | try: |
---|
780 | self._default_save_location = os.path.dirname(path) |
---|
781 | except: |
---|
782 | pass |
---|
783 | return path |
---|
784 | |
---|
785 | def load_ascii_1D(self, path): |
---|
786 | from data_loader import load_ascii_1D |
---|
787 | return load_ascii_1D(path) |
---|
788 | |
---|
789 | class DefaultPanel(wx.Panel): |
---|
790 | """ |
---|
791 | Defines the API for a panels to work with |
---|
792 | the GUI manager |
---|
793 | """ |
---|
794 | ## Internal nickname for the window, used by the AUI manager |
---|
795 | window_name = "default" |
---|
796 | ## Name to appear on the window title bar |
---|
797 | window_caption = "Welcome panel" |
---|
798 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
799 | CENTER_PANE = True |
---|
800 | |
---|
801 | |
---|
802 | # Toy application to test this Frame |
---|
803 | class ViewApp(wx.App): |
---|
804 | def OnInit(self): |
---|
805 | #from gui_manager import ViewerFrame |
---|
806 | self.frame = ViewerFrame(None, -1, config.__appname__) |
---|
807 | self.frame.Show(True) |
---|
808 | |
---|
809 | if hasattr(self.frame, 'special'): |
---|
810 | print "Special?", self.frame.special.__class__.__name__ |
---|
811 | self.frame.special.SetCurrent() |
---|
812 | self.SetTopWindow(self.frame) |
---|
813 | return True |
---|
814 | |
---|
815 | def set_manager(self, manager): |
---|
816 | """ |
---|
817 | Sets a reference to the application manager |
---|
818 | of the GUI manager (Frame) |
---|
819 | """ |
---|
820 | self.frame.set_manager(manager) |
---|
821 | |
---|
822 | def build_gui(self): |
---|
823 | """ |
---|
824 | Build the GUI |
---|
825 | """ |
---|
826 | self.frame.build_gui() |
---|
827 | self.frame.post_init() |
---|
828 | |
---|
829 | def add_perspective(self, perspective): |
---|
830 | """ |
---|
831 | Manually add a perspective to the application GUI |
---|
832 | """ |
---|
833 | self.frame.add_perspective(perspective) |
---|
834 | |
---|
835 | |
---|
836 | if __name__ == "__main__": |
---|
837 | app = ViewApp(0) |
---|
838 | app.MainLoop() |
---|