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