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