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