source: sasview/guiframe/gui_manager.py @ 2fc9243

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 2fc9243 was ba535a6, checked in by Gervaise Alina <gervyh@…>, 14 years ago

change open in menu file to load data

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