source: sasview/sansguiframe/src/sans/guiframe/gui_manager.py @ a602942e

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 a602942e was a602942e, checked in by Jae Cho <jhjcho@…>, 11 years ago

disabling the save app menu correctly

  • Property mode set to 100644
File size: 141.2 KB
Line 
1"""
2    Gui manager: manages the widgets making up an application
3"""
4################################################################################
5#This software was developed by the University of Tennessee as part of the
6#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
7#project funded by the US National Science Foundation.
8#
9#See the license text in license.txt
10#
11#copyright 2008, University of Tennessee
12################################################################################
13
14
15import wx
16import wx.aui
17import os
18import sys
19import time
20import imp
21import warnings
22import re
23warnings.simplefilter("ignore")
24import logging
25#import urllib2
26import httplib
27
28from sans.guiframe.events import EVT_CATEGORY
29from sans.guiframe.events import EVT_STATUS
30from sans.guiframe.events import EVT_APPEND_BOOKMARK
31from sans.guiframe.events import EVT_PANEL_ON_FOCUS
32from sans.guiframe.events import EVT_NEW_LOAD_DATA
33from sans.guiframe.events import EVT_NEW_COLOR
34from sans.guiframe.events import StatusEvent
35from sans.guiframe.events import NewPlotEvent
36from sans.guiframe.gui_style import GUIFRAME
37from sans.guiframe.gui_style import GUIFRAME_ID
38from sans.guiframe.data_panel import DataPanel
39from sans.guiframe.panel_base import PanelBase
40from sans.guiframe.gui_toolbar import GUIToolBar
41from sans.guiframe.data_processor import GridFrame
42from sans.guiframe.events import EVT_NEW_BATCH
43from sans.guiframe.CategoryManager import CategoryManager
44from sans.dataloader.loader import Loader
45
46def get_app_dir():
47    """
48        The application directory is the one where the default custom_config.py
49        file resides.
50    """
51    # First, try the directory of the executable we are running
52    app_path = sys.path[0]
53    if os.path.isfile(app_path):
54        app_path = os.path.dirname(app_path)
55    if os.path.isfile(os.path.join(app_path, "custom_config.py")):
56        app_path = os.path.abspath(app_path)
57        logging.info("Using application path: %s", app_path)
58        return app_path
59   
60    # Next, try the current working directory
61    if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")):
62        logging.info("Using application path: %s", os.getcwd())
63        return os.path.abspath(os.getcwd())
64   
65    # Finally, try the directory of the sasview module
66    #TODO: gui_manager will have to know about sasview until we
67    # clean all these module variables and put them into a config class
68    # that can be passed by sasview.py.
69    logging.info(sys.executable)
70    logging.info(str(sys.argv))
71    from sans import sansview as sasview
72    app_path = os.path.dirname(sasview.__file__)
73    logging.info("Using application path: %s", app_path)
74    return app_path
75
76def get_user_directory():
77    """
78        Returns the user's home directory
79    """
80    userdir = os.path.join(os.path.expanduser("~"),".sasview")
81    if not os.path.isdir(userdir):
82        os.makedirs(userdir)
83    return userdir
84   
85def _find_local_config(file, path):
86    """
87        Find configuration file for the current application
88    """   
89    config_module = None
90    fObj = None
91    try:
92        fObj, path_config, descr = imp.find_module(file, [path])
93        config_module = imp.load_module(file, fObj, path_config, descr) 
94    except:
95        pass
96    finally:
97        if fObj is not None:
98            fObj.close()
99    return config_module
100
101# Get APP folder
102PATH_APP = get_app_dir() 
103DATAPATH = PATH_APP
104
105# GUI always starts from the App folder
106#os.chdir(PATH_APP)
107# Read in the local config, which can either be with the main
108# application or in the installation directory
109config = _find_local_config('local_config', PATH_APP)
110if config is None:
111    config = _find_local_config('local_config', os.getcwd())
112    if config is None:
113        # Didn't find local config, load the default
114        import sans.guiframe.config as config
115        logging.info("using default local_config")       
116    else:
117        logging.info("found local_config in %s" % os.getcwd()) 
118else:
119    logging.info("found local_config in %s" % PATH_APP)     
120           
121from sans.guiframe.customdir  import SetupCustom
122c_conf_dir = SetupCustom().setup_dir(PATH_APP)
123custom_config = _find_local_config('custom_config', c_conf_dir)
124if custom_config is None:
125    custom_config = _find_local_config('custom_config', os.getcwd())
126    if custom_config is None:
127        msgConfig = "Custom_config file was not imported"
128        logging.info(msgConfig)
129    else:
130        logging.info("using custom_config in %s" % os.getcwd())
131else:
132    logging.info("using custom_config from %s" % c_conf_dir)
133
134#read some constants from config
135APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION
136APPLICATION_NAME = config.__appname__
137SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH
138WELCOME_PANEL_ON = config.WELCOME_PANEL_ON
139SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH
140SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT
141SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME
142if not WELCOME_PANEL_ON:
143    WELCOME_PANEL_SHOW = False
144else:
145    WELCOME_PANEL_SHOW = True
146try:
147    DATALOADER_SHOW = custom_config.DATALOADER_SHOW
148    TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW
149    FIXED_PANEL = custom_config.FIXED_PANEL
150    if WELCOME_PANEL_ON:
151        WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW
152    PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH
153    DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH
154    GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH
155    GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT
156    DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE
157    CLEANUP_PLOT = custom_config.CLEANUP_PLOT
158    # custom open_path
159    open_folder = custom_config.DEFAULT_OPEN_FOLDER
160    if open_folder != None and os.path.isdir(open_folder):
161        DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder)
162    else:
163        DEFAULT_OPEN_FOLDER = PATH_APP
164except:
165    DATALOADER_SHOW = True
166    TOOLBAR_SHOW = True
167    FIXED_PANEL = True
168    WELCOME_PANEL_SHOW = False
169    PLOPANEL_WIDTH = config.PLOPANEL_WIDTH
170    DATAPANEL_WIDTH = config.DATAPANEL_WIDTH
171    GUIFRAME_WIDTH = config.GUIFRAME_WIDTH
172    GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT
173    DEFAULT_PERSPECTIVE = None
174    CLEANUP_PLOT = False
175    DEFAULT_OPEN_FOLDER = PATH_APP
176
177DEFAULT_STYLE = config.DEFAULT_STYLE
178
179PLUGIN_STATE_EXTENSIONS =  config.PLUGIN_STATE_EXTENSIONS
180OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU
181VIEW_MENU = config.VIEW_MENU
182EDIT_MENU = config.EDIT_MENU
183extension_list = []
184if APPLICATION_STATE_EXTENSION is not None:
185    extension_list.append(APPLICATION_STATE_EXTENSION)
186EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list
187try:
188    PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST)
189except:
190    PLUGINS_WLIST = ''
191APPLICATION_WLIST = config.APPLICATION_WLIST
192IS_WIN = True
193CLOSE_SHOW = True
194TIME_FACTOR = 1
195NOT_SO_GRAPH_LIST = ["BoxSum"]
196if sys.platform.count("win32")==0:
197    IS_WIN = False
198    TIME_FACTOR = 2
199    if int(str(wx.__version__).split('.')[0]) == 2:
200        if int(str(wx.__version__).split('.')[1]) < 9:
201            CLOSE_SHOW = False
202   
203class ViewerFrame(wx.Frame):
204    """
205    Main application frame
206    """
207   
208    def __init__(self, parent, title, 
209                 size=(GUIFRAME_WIDTH, GUIFRAME_HEIGHT),
210                 gui_style=DEFAULT_STYLE, 
211                 style=wx.DEFAULT_FRAME_STYLE,
212                 pos=wx.DefaultPosition):
213        """
214        Initialize the Frame object
215        """
216
217        wx.Frame.__init__(self, parent=parent, title=title, pos=pos, size=size)
218        # title
219        self.title = title
220        # Preferred window size
221        self._window_width, self._window_height = size
222        self.__gui_style = gui_style       
223        path = os.path.dirname(__file__)
224        temp_path = os.path.join(path,'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        else:
229            temp_path = os.path.join(os.getcwd(),'images')
230            ico_file = os.path.join(temp_path,'ball.ico')
231            if os.path.isfile(ico_file):
232                self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO))
233            else:
234                ico_file = os.path.join(os.path.dirname(os.path.sys.path[0]),
235                             'images', 'ball.ico')
236                if os.path.isfile(ico_file):
237                    self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO))
238        self.path = PATH_APP
239        self.application_name = APPLICATION_NAME
240        ## Application manager
241        self._input_file = None
242        self.app_manager = None
243        self._mgr = None
244        #add current perpsective
245        self._current_perspective = None
246        self._plotting_plugin = None
247        self._data_plugin = None
248        #Menu bar and item
249        self._menubar = None
250        self._file_menu = None
251        self._data_menu = None
252        self._view_menu = None
253        self._window_menu = None
254        self._data_panel_menu = None
255        self._help_menu = None
256        self._tool_menu = None
257        self._applications_menu_pos = -1
258        self._applications_menu_name = None
259        self._applications_menu = None
260        self._edit_menu = None
261        self._toolbar_menu = None
262        self._save_appl_menu = None
263        #tool bar
264        self._toolbar = None
265        # Status bar
266        self.sb = None
267        # number of plugins
268        self._num_perspectives = 0
269        # plot duck cleanup option
270        self.cleanup_plots = CLEANUP_PLOT
271        # (un)-focus color
272        #self.color = '#b3b3b3'
273        ## Find plug-ins
274        # Modify this so that we can specify the directory to look into
275        self.plugins = []
276        #add local plugin
277        self.plugins += self._get_local_plugins()
278        self.plugins += self._find_plugins()
279        ## List of panels
280        self.panels = {}
281        # List of plot panels
282        self.plot_panels = {}
283        # default Graph number fot the plotpanel caption
284        self.graph_num = 0
285
286        # Default locations
287        self._default_save_location = DEFAULT_OPEN_FOLDER       
288        # Welcome panel
289        self.defaultPanel = None
290        #panel on focus
291        self.panel_on_focus = None
292        #control_panel on focus
293        self.cpanel_on_focus = None
294        self.loader = Loader()   
295        #data manager
296        self.batch_on = False
297        from sans.guiframe.data_manager import DataManager
298        self._data_manager = DataManager()
299        self._data_panel = DataPanel(parent=self)
300        if self.panel_on_focus is not None:
301            self._data_panel.set_panel_on_focus(
302                                self.panel_on_focus.window_caption)
303        # list of plot panels in schedule to full redraw
304        self.schedule = False
305        #self.callback = True
306        self._idle_count = 0
307        self.schedule_full_draw_list = []
308        self.idletimer = wx.CallLater(TIME_FACTOR, self._onDrawIdle)
309       
310        self.batch_frame = GridFrame(parent=self)
311        self.batch_frame.Hide()
312        self.on_batch_selection(event=None)
313        self.add_icon()
314        # Register the close event so it calls our own method
315        wx.EVT_CLOSE(self, self.WindowClose)
316        # Register to status events
317        self.Bind(EVT_STATUS, self._on_status_event)
318        #Register add extra data on the same panel event on load
319        self.Bind(EVT_PANEL_ON_FOCUS, self.set_panel_on_focus)
320        self.Bind(EVT_APPEND_BOOKMARK, self.append_bookmark)
321        self.Bind(EVT_NEW_LOAD_DATA, self.on_load_data)
322        self.Bind(EVT_NEW_BATCH, self.on_batch_selection)
323        self.Bind(EVT_NEW_COLOR, self.on_color_selection)
324        self.Bind(EVT_CATEGORY, self.on_change_categories)
325        self.setup_custom_conf()
326       
327    def add_icon(self):
328        """
329        get list of child and attempt to add the default icon
330        """
331       
332        list_children = self.GetChildren() 
333        for frame in list_children:
334            self.put_icon(frame)
335       
336    def put_icon(self, frame): 
337        """
338        Put icon on the tap of a panel
339        """
340        if hasattr(frame, "IsIconized"):
341            if not frame.IsIconized():
342                try:
343                    icon = self.GetIcon()
344                    frame.SetIcon(icon)
345                except:
346                    pass 
347       
348    def on_change_categories(self, evt):
349        # ILL
350        fitpanel = None
351        for item in self.plugins:
352            if hasattr(item, "get_panels"):
353                if hasattr(item, "fit_panel"):
354                    fitpanel = item.fit_panel
355
356        if fitpanel != None:
357            for i in range(0,fitpanel.GetPageCount()):
358                fitpanel.GetPage(i)._populate_listbox()
359
360
361
362    def on_set_batch_result(self, data_outputs, data_inputs=None,
363                             plugin_name=""):
364        """
365        Display data into a grid in batch mode and show the grid
366        """
367        t = time.localtime(time.time())
368        time_str = time.strftime("%b %d %H;%M of %Y", t)
369        details = "File Generated by %s : %s" % (APPLICATION_NAME,
370                                                     str(plugin_name))
371        details += "on %s.\n" % time_str
372        ext = ".csv"
373        file_name = "Batch_" + str(plugin_name)+ "_" + time_str + ext
374        file_name = self._default_save_location + str(file_name)
375       
376        self.open_with_localapp(file_name=file_name,
377                                details=details,
378                                data_inputs=data_inputs,
379                                    data_outputs=data_outputs)
380     
381   
382    def open_with_localapp(self, data_inputs=None, details="", file_name=None,
383                           data_outputs=None):
384        """
385        Display value of data into the application grid
386        :param data: dictionary of string and list of items
387        """
388        self.batch_frame.set_data(data_inputs=data_inputs, 
389                                  data_outputs=data_outputs,
390                                  details=details,
391                                  file_name=file_name)
392        self.show_batch_frame(None)
393       
394    def on_read_batch_tofile(self, base):
395        """
396        Open a file dialog , extract the file to read and display values
397        into a grid
398        """
399        path = None
400        if self._default_save_location == None:
401            self._default_save_location = os.getcwd()
402        wildcard = "(*.csv; *.txt)|*.csv; *.txt"
403        dlg = wx.FileDialog(base, 
404                            "Choose a file", 
405                            self._default_save_location, "",
406                             wildcard)
407        if dlg.ShowModal() == wx.ID_OK:
408            path = dlg.GetPath()
409            if path is not None:
410                self._default_save_location = os.path.dirname(path)
411        dlg.Destroy()
412        try:
413            self.read_batch_tofile(file_name=path)
414        except:
415            msg = "Error occurred when reading the file; %s\n"% path
416            msg += "%s\n"% sys.exc_value
417            wx.PostEvent(self, StatusEvent(status=msg,
418                                             info="error"))
419           
420    def read_batch_tofile(self, file_name):
421        """
422        Extract value from file name and Display them into a grid
423        """
424        if file_name is None or file_name.strip() == "":
425            return
426        data = {}
427        fd = open(file_name, 'r')
428        _, ext = os.path.splitext(file_name)
429        separator = None
430        if ext.lower() == ".csv":
431            separator = ","
432        buffer = fd.read()
433        lines = buffer.split('\n')
434        fd.close()
435        column_names_line  = ""
436        index = None
437        details = ""
438        for index in range(len(lines)):
439            line = lines[index]
440            line.strip()
441            count = 0
442            if separator == None:
443                line.replace('\t', ' ')
444                #found the first line containing the label
445                col_name_toks = line.split()
446                for item in col_name_toks:
447                    if item.strip() != "":
448                        count += 1
449                    else:
450                        line = " "
451            elif line.find(separator) != -1:
452                if line.count(separator) >= 2:
453                    #found the first line containing the label
454                    col_name_toks = line.split(separator)
455                    for item in col_name_toks:
456                        if item.strip() != "":
457                            count += 1
458            else:
459                details += line
460            if count >= 2:
461                column_names_line = line
462                break 
463           
464        if column_names_line.strip() == "" or index is None:
465            return 
466
467        col_name_toks = column_names_line.split(separator)
468        c_index = 0
469        for col_index in range(len(col_name_toks)):
470            c_name = col_name_toks[col_index]
471            if c_name.strip() != "":
472                # distinguish between column name and value
473                try:
474                    float(c_name)
475                    col_name = "Column %s"% str(col_index + 1)
476                    index_min = index
477                except:
478                    col_name = c_name
479                    index_min = index + 1
480                data[col_name] = [ lines[row].split(separator)[c_index]
481                                for row in range(index_min, len(lines)-1)]
482                c_index += 1
483               
484        self.open_with_localapp(data_outputs=data, data_inputs=None,
485                                file_name=file_name, details=details)
486       
487    def write_batch_tofile(self, data, file_name, details=""):
488        """
489        Helper to write result from batch into cvs file
490        """
491        self._default_save_location = os.path.dirname(file_name)
492        name = os.path.basename(file_name)
493        if data is None or file_name is None or file_name.strip() == "":
494            return
495        _, ext = os.path.splitext(name)
496        try:
497            fd = open(file_name, 'w')
498        except:
499            # On Permission denied: IOError
500            temp_dir = get_user_directory()
501            temp_file_name = os.path.join(temp_dir, name)
502            fd = open(temp_file_name, 'w')
503        separator = "\t"
504        if ext.lower() == ".csv":
505            separator = ","
506        fd.write(str(details))
507        for col_name  in data.keys():
508            fd.write(str(col_name))
509            fd.write(separator)
510        fd.write('\n')
511        max_list = [len(value) for value in data.values()]
512        if len(max_list) == 0:
513            return
514        max_index = max(max_list)
515        index = 0
516        while(index < max_index):
517            for value_list in data.values():
518                if index < len(value_list):
519                    fd.write(str(value_list[index]))
520                    fd.write(separator)
521                else:
522                    fd.write('')
523                    fd.write(separator)
524            fd.write('\n')
525            index += 1
526        fd.close()
527           
528    def open_with_externalapp(self, data, file_name, details=""):
529        """
530        Display data in the another application , by default Excel
531        """
532        if not os.path.exists(file_name):
533            self.write_batch_tofile(data=data, file_name=file_name,
534                                               details=details)
535        try:
536            from win32com.client import Dispatch
537            excel_app = Dispatch('Excel.Application')     
538            wb = excel_app.Workbooks.Open(file_name) 
539            excel_app.Visible = 1
540        except:
541            msg = "Error occured when calling Excel.\n"
542            msg += "Check that Excel installed in this machine or \n"
543            msg += "check that %s really exists.\n" % str(file_name)
544            wx.PostEvent(self, StatusEvent(status=msg,
545                                             info="error"))
546           
547         
548    def on_batch_selection(self, event=None):
549        """
550        :param event: contains parameter enable . when enable is set to True
551        the application is in Batch mode
552        else the application is default mode(single mode)
553        """
554        if event is not None:
555            self.batch_on = event.enable
556        for plug in self.plugins:
557            plug.set_batch_selection(self.batch_on)
558           
559    def on_color_selection(self, event):
560        """
561        :param event: contains parameters for id and color
562        """ 
563        color, id = event.color, event.id
564        for plug in self.plugins:
565            plug.add_color(color, id)
566       
567       
568    def setup_custom_conf(self):
569        """
570        Set up custom configuration if exists
571        """
572        if custom_config == None:
573            return
574       
575        if not FIXED_PANEL:
576            self.__gui_style &= (~GUIFRAME.FIXED_PANEL)
577            self.__gui_style |= GUIFRAME.FLOATING_PANEL
578
579        if not DATALOADER_SHOW:
580            self.__gui_style &= (~GUIFRAME.MANAGER_ON)
581
582        if not TOOLBAR_SHOW:
583            self.__gui_style &= (~GUIFRAME.TOOLBAR_ON)
584
585        if WELCOME_PANEL_SHOW:
586            self.__gui_style |= GUIFRAME.WELCOME_PANEL_ON   
587             
588    def set_custom_default_perspective(self):
589        """
590        Set default starting perspective
591        """
592        if custom_config == None:
593            return
594        for plugin in self.plugins:
595            try:
596                if plugin.sub_menu == DEFAULT_PERSPECTIVE:
597                   
598                    plugin.on_perspective(event=None)
599                    #self._check_applications_menu()
600                    break
601            except:
602                pass 
603        return         
604               
605    def on_load_data(self, event):
606        """
607        received an event to trigger load from data plugin
608        """
609        if self._data_plugin is not None:
610            self._data_plugin.load_data(event)
611           
612    def get_current_perspective(self):
613        """
614        return the current perspective
615        """
616        return self._current_perspective
617
618    def get_save_location(self):
619        """
620        return the _default_save_location
621        """
622        return self._default_save_location
623   
624    def set_input_file(self, input_file):
625        """
626        :param input_file: file to read
627        """
628        self._input_file = input_file
629       
630    def get_data_manager(self):
631        """
632        return the data manager.
633        """
634        return self._data_manager
635   
636    def get_toolbar(self):
637        """
638        return the toolbar.
639        """
640        return self._toolbar
641   
642    def set_panel_on_focus(self, event):
643        """
644        Store reference to the last panel on focus
645        update the toolbar if available
646        update edit menu if available
647        """
648        if event != None:
649            self.panel_on_focus = event.panel
650        if self.panel_on_focus is not None:
651            #Disable save application if the current panel is in batch mode
652            flag = self.panel_on_focus.get_save_flag()
653            self._save_appl_menu.Enable(flag)
654
655            if self.panel_on_focus not in self.plot_panels.values():
656                for ID in self.panels.keys():
657                    if self.panel_on_focus != self.panels[ID]:
658                        self.panels[ID].on_kill_focus(None)
659
660            if self._data_panel is not None and \
661                            self.panel_on_focus is not None:
662                self.set_panel_on_focus_helper()
663                #update toolbar
664                self._update_toolbar_helper()
665                #update edit menu
666                self.enable_edit_menu()
667   
668    def disable_app_menu(self, p_panel=None): 
669        """
670        Disables all menus in the menubar
671        """
672        if p_panel == None:
673            return
674        p_name = p_panel.window_name
675        enable = not self._mgr.GetPane(p_name).IsMaximized()
676        if self._data_panel is not None:
677            self._data_panel.disable_app_combo(enable)
678        if self._menubar is not None:
679            for menu, _ in self._menubar.GetMenus():
680                for items in menu.GetMenuItems():
681                    items.Enable(enable)
682        c_panel = self.cpanel_on_focus
683        # disable depending on the save_app flag
684        self._save_appl_menu.Enable(c_panel.get_save_flag())
685        if not enable:
686            if self._toolbar is not None:
687                self._toolbar.update_toolbar(None)
688            p_panel.on_set_focus(None)
689        else:
690            self._toolbar.update_toolbar(c_panel)
691       
692    def set_panel_on_focus_helper(self):
693        """
694        Helper for panel on focus with data_panel
695        """
696        ID = self.panel_on_focus.uid
697        self._data_panel.set_panel_on_focus(ID)
698        #update combo
699        if self.panel_on_focus in self.plot_panels.values():
700            combo = self._data_panel.cb_plotpanel
701            combo_title = str(self.panel_on_focus.window_caption)
702            combo.SetStringSelection(combo_title)
703            combo.SetToolTip( wx.ToolTip(combo_title )) 
704        elif self.panel_on_focus != self._data_panel:
705            cpanel = self.panel_on_focus
706            if self.cpanel_on_focus != cpanel:
707                cpanel.on_tap_focus()
708                self.cpanel_on_focus = self.panel_on_focus
709               
710    def reset_bookmark_menu(self, panel):
711        """
712        Reset Bookmark menu list
713       
714        : param panel: a control panel or tap where the bookmark is
715        """
716        cpanel = panel
717        if self._toolbar != None and cpanel._bookmark_flag:
718            for item in  self._toolbar.get_bookmark_items():
719                self._toolbar.remove_bookmark_item(item)
720            self._toolbar.add_bookmark_default()
721            pos = 0
722            for bitem in cpanel.popUpMenu.GetMenuItems():
723                pos += 1
724                if pos < 3:
725                    continue
726                id =  bitem.GetId()
727                label = bitem.GetLabel()
728                self._toolbar.append_bookmark_item(id, label)
729                wx.EVT_MENU(self, id, cpanel._back_to_bookmark)
730            self._toolbar.Realize()
731             
732
733    def build_gui(self):
734        """
735        Build the GUI by setting up the toolbar, menu and layout.
736        """
737        # set tool bar
738        self._setup_tool_bar()
739        # Set up the layout
740        self._setup_layout()
741       
742        # Set up the menu
743        self._setup_menus()
744       
745        try:
746            self.load_from_cmd(self._input_file)
747        except:
748            msg = "%s Cannot load file %s\n" %(str(APPLICATION_NAME), 
749                                             str(self._input_file))
750            msg += str(sys.exc_value) + '\n'
751            print msg
752        if self._data_panel is not None and len(self.plugins) > 0:
753            self._data_panel.fill_cbox_analysis(self.plugins)
754        self.post_init()
755        # Set Custom default
756        self.set_custom_default_perspective()
757        # Set up extra custom tool menu
758        self._setup_extra_custom()
759        self._check_update(None)
760   
761    def _setup_extra_custom(self): 
762        """
763        Set up toolbar and welcome view if needed
764        """
765        style = self.__gui_style & GUIFRAME.TOOLBAR_ON
766        if (style == GUIFRAME.TOOLBAR_ON) & (not self._toolbar.IsShown()):
767            self._on_toggle_toolbar() 
768       
769        # Set Custom deafult start page
770        welcome_style = self.__gui_style & GUIFRAME.WELCOME_PANEL_ON
771        if welcome_style == GUIFRAME.WELCOME_PANEL_ON:
772            self.show_welcome_panel(None)
773     
774    def _setup_layout(self):
775        """
776        Set up the layout
777        """
778        # Status bar
779        from sans.guiframe.gui_statusbar import StatusBar
780        self.sb = StatusBar(self, wx.ID_ANY)
781        self.SetStatusBar(self.sb)
782        # Add panel
783        default_flag = wx.aui.AUI_MGR_DEFAULT#| wx.aui.AUI_MGR_ALLOW_ACTIVE_PANE
784        self._mgr = wx.aui.AuiManager(self, flags=default_flag)
785        self._mgr.SetDockSizeConstraint(0.5, 0.5)
786        # Load panels
787        self._load_panels()
788        self.set_default_perspective()
789        self._mgr.Update()
790       
791    def SetStatusText(self, *args, **kwds):
792        """
793        """
794        number = self.sb.get_msg_position()
795        wx.Frame.SetStatusText(self, number=number, *args, **kwds)
796       
797    def PopStatusText(self, *args, **kwds):
798        """
799        """
800        field = self.sb.get_msg_position()
801        wx.Frame.PopStatusText(self, field=field)
802       
803    def PushStatusText(self, *args, **kwds):
804        """
805            FIXME: No message is passed. What is this supposed to do?
806        """
807        field = self.sb.get_msg_position()
808        wx.Frame.PushStatusText(self, field=field, 
809                        string="FIXME: PushStatusText called without text")
810
811    def add_perspective(self, plugin):
812        """
813        Add a perspective if it doesn't already
814        exist.
815        """
816        self._num_perspectives += 1
817        is_loaded = False
818        for item in self.plugins:
819            item.set_batch_selection(self.batch_on)
820            if plugin.__class__ == item.__class__:
821                msg = "Plugin %s already loaded" % plugin.sub_menu
822                logging.info(msg)
823                is_loaded = True 
824        if not is_loaded:
825            self.plugins.append(plugin) 
826     
827    def _get_local_plugins(self):
828        """
829        get plugins local to guiframe and others
830        """
831        plugins = []
832        #import guiframe local plugins
833        #check if the style contain guiframe.dataloader
834        style1 = self.__gui_style & GUIFRAME.DATALOADER_ON
835        style2 = self.__gui_style & GUIFRAME.PLOTTING_ON
836        if style1 == GUIFRAME.DATALOADER_ON:
837            try:
838                from sans.guiframe.local_perspectives.data_loader import data_loader
839                self._data_plugin = data_loader.Plugin()
840                plugins.append(self._data_plugin)
841            except:
842                msg = "ViewerFrame._get_local_plugins:"
843                msg += "cannot import dataloader plugin.\n %s" % sys.exc_value
844                logging.error(msg)
845        if style2 == GUIFRAME.PLOTTING_ON:
846            try:
847                from sans.guiframe.local_perspectives.plotting import plotting
848                self._plotting_plugin = plotting.Plugin()
849                plugins.append(self._plotting_plugin)
850            except:
851                msg = "ViewerFrame._get_local_plugins:"
852                msg += "cannot import plotting plugin.\n %s" % sys.exc_value
853                logging.error(msg)
854     
855        return plugins
856   
857    def _find_plugins(self, dir="perspectives"):
858        """
859        Find available perspective plug-ins
860       
861        :param dir: directory in which to look for plug-ins
862       
863        :return: list of plug-ins
864       
865        """
866        plugins = []
867        # Go through files in panels directory
868        try:
869            list = os.listdir(dir)
870            ## the default panel is the panel is the last plugin added
871            for item in list:
872                toks = os.path.splitext(os.path.basename(item))
873                name = ''
874                if not toks[0] == '__init__':
875                    if toks[1] == '.py' or toks[1] == '':
876                        name = toks[0]
877                    #check the validity of the module name parsed
878                    #before trying to import it
879                    if name is None or name.strip() == '':
880                        continue
881                    path = [os.path.abspath(dir)]
882                    file = None
883                    try:
884                        if toks[1] == '':
885                            mod_path = '.'.join([dir, name])
886                            module = __import__(mod_path, globals(),
887                                                locals(), [name])
888                        else:
889                            (file, path, info) = imp.find_module(name, path)
890                            module = imp.load_module( name, file, item, info)
891                        if hasattr(module, "PLUGIN_ID"):
892                            try: 
893                                plug = module.Plugin()
894                                if plug.set_default_perspective():
895                                    self._current_perspective = plug
896                                plugins.append(plug)
897                               
898                                msg = "Found plug-in: %s" % module.PLUGIN_ID
899                                logging.info(msg)
900                            except:
901                                msg = "Error accessing PluginPanel"
902                                msg += " in %s\n  %s" % (name, sys.exc_value)
903                                config.printEVT(msg)
904                    except:
905                        msg = "ViewerFrame._find_plugins: %s" % sys.exc_value
906                        #print msg
907                        logging.error(msg)
908                    finally:
909                        if not file == None:
910                            file.close()
911        except:
912            # Should raise and catch at a higher level and
913            # display error on status bar
914            pass 
915
916        return plugins
917   
918    def set_welcome_panel(self, panel_class):
919        """
920        Sets the default panel as the given welcome panel
921       
922        :param panel_class: class of the welcome panel to be instantiated
923       
924        """
925        self.defaultPanel = panel_class(self, -1, style=wx.RAISED_BORDER)
926       
927    def _get_panels_size(self, p):
928        """
929        find the proper size of the current panel
930        get the proper panel width and height
931        """
932        ## Check and set the size
933        if DATAPANEL_WIDTH < 0:
934            panel_width = int(self._window_width * 0.22)
935        else:
936            panel_width = DATAPANEL_WIDTH
937        panel_height = int(self._window_height * 0.9)
938        style = self.__gui_style & (GUIFRAME.MANAGER_ON)
939        if self._data_panel is not None  and (p == self._data_panel):
940            return panel_width, panel_height
941        if hasattr(p, "CENTER_PANE") and p.CENTER_PANE:
942            style = self.__gui_style & (GUIFRAME.PLOTTING_ON|GUIFRAME.MANAGER_ON)
943            if style == (GUIFRAME.PLOTTING_ON|GUIFRAME.MANAGER_ON):
944                panel_width = self._window_width - panel_width
945            return panel_width, panel_height
946        return panel_width, panel_height
947   
948    def _load_panels(self):
949        """
950        Load all panels in the panels directory
951        """
952       
953        # Look for plug-in panels
954        panels = []   
955        for item in self.plugins:
956            if hasattr(item, "get_panels"):
957                ps = item.get_panels(self)
958                panels.extend(ps)
959       
960        # Show a default panel with some help information
961        # It also sets the size of the application windows
962        #TODO: Use this for splash screen
963        if self.defaultPanel is None:
964            self.defaultPanel = DefaultPanel(self, -1, style=wx.RAISED_BORDER)
965        # add a blank default panel always present
966        self.panels["default"] = self.defaultPanel
967        self._mgr.AddPane(self.defaultPanel, wx.aui.AuiPaneInfo().
968                              Name("default").
969                              CenterPane().
970                              # This is where we set the size of
971                              # the application window
972                              BestSize(wx.Size(self._window_width, 
973                                               self._window_height)).
974                              Show())
975
976        #add data panel
977        self.panels["data_panel"] = self._data_panel
978        w, h = self._get_panels_size(self._data_panel)
979        self._mgr.AddPane(self._data_panel, wx.aui.AuiPaneInfo().
980                              Name(self._data_panel.window_name).
981                              Caption(self._data_panel.window_caption).
982                              Left().
983                              CloseButton(CLOSE_SHOW).
984                              TopDockable(False).
985                              BottomDockable(False).
986                              LeftDockable(True).
987                              RightDockable(False).
988                              BestSize(wx.Size(w, h)).
989                              Hide())
990
991        style = self.__gui_style & GUIFRAME.MANAGER_ON
992        data_pane = self._mgr.GetPane(self.panels["data_panel"].window_name)
993        if style != GUIFRAME.MANAGER_ON:
994            data_pane.Hide()
995        else:
996            data_pane.Show()
997           
998        # Add the panels to the AUI manager
999        for panel_class in panels:
1000            p = panel_class
1001            id = wx.NewId()
1002            # Check whether we need to put this panel
1003            # in the center pane
1004            if hasattr(p, "CENTER_PANE") and p.CENTER_PANE:
1005                w, h = self._get_panels_size(p)
1006                if p.CENTER_PANE:
1007                    self.panels[str(id)] = p
1008                    self._mgr.AddPane(p, wx.aui.AuiPaneInfo().
1009                                          Name(p.window_name).
1010                                          CenterPane().
1011                                          Center().
1012                                          CloseButton(False).
1013                                          Hide())
1014            else:
1015                self.panels[str(id)] = p
1016                self._mgr.AddPane(p, wx.aui.AuiPaneInfo().
1017                                  Name(p.window_name).Caption(p.window_caption).
1018                                  Right().
1019                                  Dock().
1020                                  TopDockable().
1021                                  BottomDockable().
1022                                  LeftDockable().
1023                                  RightDockable().
1024                                  MinimizeButton().
1025                                  Hide())       
1026     
1027    def update_data(self, prev_data, new_data):
1028        """
1029        Update the data.
1030        """
1031        prev_id, data_state = self._data_manager.update_data(
1032                              prev_data=prev_data, new_data=new_data)
1033       
1034        self._data_panel.remove_by_id(prev_id)
1035        self._data_panel.load_data_list(data_state)
1036       
1037    def update_theory(self, data_id, theory, state=None):
1038        """
1039        Update the theory
1040        """ 
1041        data_state = self._data_manager.update_theory(data_id=data_id, 
1042                                         theory=theory,
1043                                         state=state) 
1044        wx.CallAfter(self._data_panel.load_data_list, data_state)
1045       
1046    def onfreeze(self, theory_id):
1047        """
1048        """
1049        data_state_list = self._data_manager.freeze(theory_id)
1050        self._data_panel.load_data_list(list=data_state_list)
1051        for data_state in data_state_list.values():
1052            new_plot = data_state.get_data()
1053           
1054            wx.PostEvent(self, NewPlotEvent(plot=new_plot,
1055                                             title=new_plot.title))
1056       
1057    def freeze(self, data_id, theory_id):
1058        """
1059        """
1060        data_state_list = self._data_manager.freeze_theory(data_id=data_id, 
1061                                                theory_id=theory_id)
1062        self._data_panel.load_data_list(list=data_state_list)
1063        for data_state in data_state_list.values():
1064            new_plot = data_state.get_data()
1065            wx.PostEvent(self, NewPlotEvent(plot=new_plot,
1066                                             title=new_plot.title))
1067       
1068    def delete_data(self, data):
1069        """
1070        Delete the data.
1071        """
1072        self._current_perspective.delete_data(data)
1073       
1074   
1075    def get_context_menu(self, plotpanel=None):
1076        """
1077        Get the context menu items made available
1078        by the different plug-ins.
1079        This function is used by the plotting module
1080        """
1081        if plotpanel is None:
1082            return
1083        menu_list = []
1084        for item in self.plugins:
1085            menu_list.extend(item.get_context_menu(plotpanel=plotpanel))
1086        return menu_list
1087       
1088    def get_current_context_menu(self, plotpanel=None):
1089        """
1090        Get the context menu items made available
1091        by the current plug-in.
1092        This function is used by the plotting module
1093        """
1094        if plotpanel is None:
1095            return
1096        menu_list = []
1097        item = self._current_perspective
1098        if item != None:
1099            menu_list.extend(item.get_context_menu(plotpanel=plotpanel))
1100        return menu_list
1101           
1102    def on_panel_close(self, event):
1103        """
1104        Gets called when the close event for a panel runs.
1105        This will check which panel has been closed and
1106        delete it.
1107        """
1108        panel = event.GetPane()
1109        if panel.IsMaximized():
1110            self._mgr.RestoreMaximizedPane()
1111        for ID in self.plot_panels.keys():
1112            if self.plot_panels[ID].window_name == panel.name:
1113                self.disable_app_menu(self.plot_panels[ID])
1114                self.delete_panel(ID)
1115                break
1116        self.cpanel_on_focus.SetFocus()
1117   
1118   
1119    def popup_panel(self, p):
1120        """
1121        Add a panel object to the AUI manager
1122       
1123        :param p: panel object to add to the AUI manager
1124       
1125        :return: ID of the event associated with the new panel [int]
1126       
1127        """
1128        ID = wx.NewId()
1129        self.panels[str(ID)] = p
1130        self.graph_num += 1
1131        if p.window_caption.split()[0] in NOT_SO_GRAPH_LIST:
1132            windowcaption = p.window_caption
1133        else:
1134            windowcaption = 'Graph'#p.window_caption
1135        windowname = p.window_name
1136
1137        # Append nummber
1138        captions = self._get_plotpanel_captions()
1139        while (1):
1140            caption = windowcaption + '%s'% str(self.graph_num)
1141            if caption not in captions:
1142                break
1143            self.graph_num += 1
1144            # protection from forever-loop: max num = 1000
1145            if self.graph_num > 1000:
1146                break
1147        if p.window_caption.split()[0] not in NOT_SO_GRAPH_LIST:
1148            p.window_caption = caption
1149        p.window_name = windowname + str(self.graph_num)
1150       
1151        style1 = self.__gui_style & GUIFRAME.FIXED_PANEL
1152        style2 = self.__gui_style & GUIFRAME.FLOATING_PANEL
1153       
1154        ## Check and set the size
1155        if PLOPANEL_WIDTH < 0:
1156            p_panel_width = int(self._window_width * 0.36)
1157        else:
1158            p_panel_width = PLOPANEL_WIDTH
1159        p_panel_height = int(p_panel_width * 0.75)
1160       
1161        if style1 == GUIFRAME.FIXED_PANEL:
1162            self._mgr.AddPane(p, wx.aui.AuiPaneInfo().
1163                              Name(p.window_name).
1164                              Caption(p.window_caption).
1165                              Position(10).
1166                              Floatable().
1167                              Right().
1168                              Dock().
1169                              MinimizeButton(True).MaximizeButton(True).
1170                              Resizable(True).
1171                              # Use a large best size to make sure the AUI
1172                              # manager takes all the available space
1173                              BestSize(wx.Size(p_panel_width, 
1174                                               p_panel_height)))
1175            self._popup_fixed_panel(p)
1176   
1177        elif style2 == GUIFRAME.FLOATING_PANEL:
1178            self._mgr.AddPane(p, wx.aui.AuiPaneInfo().
1179                              Name(p.window_name).Caption(p.window_caption).
1180                              MinimizeButton(True).MaximizeButton(True).
1181                              Resizable(True).
1182                              # Use a large best size to make sure the AUI
1183                              #  manager takes all the available space
1184                              BestSize(wx.Size(p_panel_width, 
1185                                               p_panel_height)))
1186           
1187            self._popup_floating_panel(p)
1188        # Register for closing of panels
1189        self.Bind(wx.aui.EVT_AUI_PANE_CLOSE, self.on_panel_close)
1190        # Register for showing/hiding the panel
1191        wx.EVT_MENU(self, ID, self.on_view)
1192        if p not in self.plot_panels.values() and p.group_id != None:
1193            self.plot_panels[ID] = p
1194            if len(self.plot_panels) == 1:
1195                self.panel_on_focus = p
1196                self.set_panel_on_focus(None)
1197            if self._data_panel is not None and \
1198                self._plotting_plugin is not None:
1199                ind = self._data_panel.cb_plotpanel.FindString('None')
1200                if ind != wx.NOT_FOUND:
1201                    self._data_panel.cb_plotpanel.Delete(ind)
1202                if caption not in self._data_panel.cb_plotpanel.GetItems():
1203                    self._data_panel.cb_plotpanel.Append(str(caption), p)
1204        return ID
1205   
1206    def _get_plotpanel_captions(self):
1207        """
1208        Get all the plotpanel cations
1209       
1210        : return: list of captions
1211        """
1212        captions = []
1213        for Id in self.plot_panels.keys():
1214            captions.append(self.plot_panels[Id].window_caption)
1215       
1216        return captions
1217         
1218    def _setup_menus(self):
1219        """
1220        Set up the application menus
1221        """
1222        # Menu
1223        self._menubar = wx.MenuBar()
1224        self._add_menu_file()
1225        self._add_menu_edit()
1226        self._add_menu_view()
1227        #self._add_menu_data()
1228        self._add_menu_application()
1229        self._add_menu_tool()
1230        self._add_current_plugin_menu()
1231        self._add_menu_window()
1232        self._add_help_menu()
1233        self.SetMenuBar(self._menubar)
1234       
1235    def _setup_tool_bar(self):
1236        """
1237        add toolbar to the frame
1238        """
1239        #set toolbar
1240        self._toolbar = GUIToolBar(self, -1)
1241        self.SetToolBar(self._toolbar)
1242        self._update_toolbar_helper()
1243        self._on_toggle_toolbar(event=None)
1244   
1245    def _update_toolbar_helper(self):
1246        """
1247        Helping to update the toolbar
1248        """
1249        application_name = 'No Selected Analysis'
1250        panel_name = 'No Panel on Focus'
1251        c_panel = self.cpanel_on_focus
1252        if self._toolbar is  None:
1253            return
1254        if c_panel is not None:
1255            self.reset_bookmark_menu(self.cpanel_on_focus)
1256        if self._current_perspective is not None:
1257            application_name = self._current_perspective.sub_menu
1258        c_panel_state = c_panel
1259        if c_panel is not None:
1260            panel_name = c_panel.window_caption
1261            if not c_panel.IsShownOnScreen():
1262                c_panel_state = None
1263        self._toolbar.update_toolbar(c_panel_state)
1264        self._toolbar.update_button(application_name=application_name, 
1265                                        panel_name=panel_name)
1266        self._toolbar.Realize()
1267       
1268    def _add_menu_tool(self):
1269        """
1270        Tools menu
1271        Go through plug-ins and find tools to populate the tools menu
1272        """
1273        style = self.__gui_style & GUIFRAME.CALCULATOR_ON
1274        if style == GUIFRAME.CALCULATOR_ON:
1275            self._tool_menu = None
1276            for item in self.plugins:
1277                if hasattr(item, "get_tools"):
1278                    for tool in item.get_tools():
1279                        # Only create a menu if we have at least one tool
1280                        if self._tool_menu is None:
1281                            self._tool_menu = wx.Menu()
1282                        if tool[0].lower().count('python') > 0:
1283                            self._tool_menu.AppendSeparator()
1284                        id = wx.NewId()
1285                        self._tool_menu.Append(id, tool[0], tool[1])
1286                        wx.EVT_MENU(self, id, tool[2])
1287            if self._tool_menu is not None:
1288                self._menubar.Append(self._tool_menu, '&Tool')
1289               
1290    def _add_current_plugin_menu(self):
1291        """
1292        add current plugin menu
1293        Look for plug-in menus
1294        Add available plug-in sub-menus.
1295        """
1296        if (self._menubar is None) or (self._current_perspective is None):
1297            return
1298        #replace or add a new menu for the current plugin
1299       
1300        pos = self._menubar.FindMenu(str(self._applications_menu_name))
1301        if pos != -1:
1302            menu_list = self._current_perspective.populate_menu(self)
1303            if menu_list:
1304                for (menu, name) in menu_list:
1305                    hidden_menu = self._menubar.Replace(pos, menu, name) 
1306                    self._applications_menu_name = name
1307                #self._applications_menu_pos = pos
1308            else:
1309                hidden_menu = self._menubar.Remove(pos)
1310                self._applications_menu_name = None
1311            #get the position of the menu when it first added
1312            self._applications_menu_pos = pos
1313           
1314        else:
1315            menu_list = self._current_perspective.populate_menu(self)
1316            if menu_list:
1317                for (menu, name) in menu_list:
1318                    if self._applications_menu_pos == -1:
1319                        self._menubar.Append(menu, name)
1320                    else:
1321                        self._menubar.Insert(self._applications_menu_pos, 
1322                                             menu, name)
1323                    self._applications_menu_name = name
1324                 
1325    def _add_help_menu(self):
1326        """
1327        add help menu
1328        """
1329        # Help menu
1330        self._help_menu = wx.Menu()
1331        style = self.__gui_style & GUIFRAME.WELCOME_PANEL_ON
1332        if style == GUIFRAME.WELCOME_PANEL_ON or custom_config != None:
1333            # add the welcome panel menu item
1334            if config.WELCOME_PANEL_ON and self.defaultPanel is not None:
1335                id = wx.NewId()
1336                self._help_menu.Append(id, '&Welcome', '')
1337                self._help_menu.AppendSeparator()
1338                wx.EVT_MENU(self, id, self.show_welcome_panel)
1339        # Look for help item in plug-ins
1340        for item in self.plugins:
1341            if hasattr(item, "help"):
1342                id = wx.NewId()
1343                self._help_menu.Append(id,'&%s Help' % item.sub_menu, '')
1344                wx.EVT_MENU(self, id, item.help)
1345        if config._do_tutorial and (IS_WIN or sys.platform =='darwin'):
1346            self._help_menu.AppendSeparator()
1347            id = wx.NewId()
1348            self._help_menu.Append(id, '&Tutorial', 'Software tutorial')
1349            wx.EVT_MENU(self, id, self._onTutorial)
1350           
1351        if config._do_aboutbox:
1352            self._help_menu.AppendSeparator()
1353            id = wx.NewId()
1354            self._help_menu.Append(id, '&About', 'Software information')
1355            wx.EVT_MENU(self, id, self._onAbout)
1356       
1357        # Checking for updates
1358        id = wx.NewId()
1359        self._help_menu.Append(id,'&Check for update', 
1360         'Check for the latest version of %s' % config.__appname__)
1361        wx.EVT_MENU(self, id, self._check_update)
1362        self._menubar.Append(self._help_menu, '&Help')
1363           
1364    def _add_menu_view(self):
1365        """
1366        add menu items under view menu
1367        """
1368        if not VIEW_MENU:
1369            return
1370        self._view_menu = wx.Menu()
1371       
1372        id = wx.NewId()
1373        hint = "Display the Grid Window for batch results etc."
1374        self._view_menu.Append(id, '&Show Grid Window', hint) 
1375        wx.EVT_MENU(self, id, self.show_batch_frame)
1376       
1377        self._view_menu.AppendSeparator()
1378        style = self.__gui_style & GUIFRAME.MANAGER_ON
1379        id = wx.NewId()
1380        self._data_panel_menu = self._view_menu.Append(id,
1381                                                '&Show Data Explorer', '')
1382        wx.EVT_MENU(self, id, self.show_data_panel)
1383        if style == GUIFRAME.MANAGER_ON:
1384            self._data_panel_menu.SetText('Hide Data Explorer')
1385        else:
1386            self._data_panel_menu.SetText('Show Data Explorer')
1387 
1388        self._view_menu.AppendSeparator()
1389        id = wx.NewId()
1390        style1 = self.__gui_style & GUIFRAME.TOOLBAR_ON
1391        if style1 == GUIFRAME.TOOLBAR_ON:
1392            self._toolbar_menu = self._view_menu.Append(id, '&Hide Toolbar', '')
1393        else:
1394            self._toolbar_menu = self._view_menu.Append(id, '&Show Toolbar', '')
1395        wx.EVT_MENU(self, id, self._on_toggle_toolbar)
1396
1397        if custom_config != None:
1398            self._view_menu.AppendSeparator()
1399            id = wx.NewId()
1400            hint_ss = "Select the current/default configuration "
1401            hint_ss += "as a startup setting"
1402            preference_menu = self._view_menu.Append(id, 'Startup Setting', 
1403                                                     hint_ss)
1404            wx.EVT_MENU(self, id, self._on_preference_menu)
1405           
1406        id = wx.NewId()
1407        self._view_menu.AppendSeparator()
1408        self._view_menu.Append(id, 'Category Manager', 'Edit model categories')
1409        wx.EVT_MENU(self, id, self._on_category_manager)
1410
1411        self._menubar.Append(self._view_menu, '&View')   
1412         
1413    def show_batch_frame(self, event=None):
1414        """
1415        show the grid of result
1416        """
1417        # Show(False) before Show(True) in order to bring it to the front
1418        self.batch_frame.Show(False)
1419        self.batch_frame.Show(True)
1420   
1421    def  on_category_panel(self, event): 
1422        """
1423        On cat panel
1424        """
1425        self._on_category_manager(event)
1426         
1427    def _on_category_manager(self, event):
1428        """
1429        Category manager frame
1430        """
1431        frame = CategoryManager(self, -1, 'Model Category Manager')
1432        icon = self.GetIcon()
1433        frame.SetIcon(icon)
1434
1435    def _on_preference_menu(self, event):     
1436        """
1437        Build a panel to allow to edit Mask
1438        """
1439       
1440        from sans.guiframe.startup_configuration \
1441        import StartupConfiguration as ConfDialog
1442       
1443        self.panel = ConfDialog(parent=self, gui=self.__gui_style)
1444        self.panel.ShowModal()
1445        #wx.PostEvent(self.parent, event)
1446       
1447
1448    def _add_menu_window(self):
1449        """
1450        add a menu window to the menu bar
1451        Window menu
1452        Attach a menu item for each panel in our
1453        panel list that also appears in a plug-in.
1454       
1455        Only add the panel menu if there is only one perspective and
1456        it has more than two panels.
1457        Note: the first plug-in is always the plotting plug-in.
1458        The first application
1459        #plug-in is always the second one in the list.
1460        """
1461        self._window_menu = wx.Menu()
1462        if self._plotting_plugin is not None:
1463            for (menu, name) in self._plotting_plugin.populate_menu(self):
1464                self._window_menu.AppendSubMenu(menu, name)
1465        self._menubar.Append(self._window_menu, '&Graph')
1466
1467        style = self.__gui_style & GUIFRAME.PLOTTING_ON
1468        if style == GUIFRAME.PLOTTING_ON:
1469           
1470            self._window_menu.AppendSeparator()
1471            id = wx.NewId()
1472            hint = "Hide all the graph panels"
1473            self._window_menu.Append(id, '&Hide  All', hint)
1474            wx.EVT_MENU(self, id, self.hide_all_plotpanels)
1475            id = wx.NewId()
1476            hint = "Show all the graph panels"
1477            self._window_menu.Append(id, '&Show All', hint)
1478            wx.EVT_MENU(self, id, self.show_all_plotpanels)
1479           
1480            self._window_menu.AppendSeparator()
1481            id = wx.NewId()
1482            preferences_menu = wx.Menu()
1483            hint = "All plot panels will floating"
1484            preferences_menu.AppendRadioItem(id, '&Float All', hint)
1485            wx.EVT_MENU(self, id, self.set_plotpanel_floating)
1486            style = self.__gui_style & GUIFRAME.FLOATING_PANEL
1487            f_menu = preferences_menu.FindItemById(id)
1488            if style == GUIFRAME.FLOATING_PANEL: 
1489                f_checked = True
1490            else:
1491                f_checked = False
1492            f_menu.Check(f_checked)
1493
1494            id = wx.NewId()
1495            hint = "All plot panels will displayed within the frame"
1496            preferences_menu.AppendRadioItem(id, '&Dock All', hint)
1497            wx.EVT_MENU(self, id, self.set_plotpanel_fixed) 
1498            if not f_checked:
1499                d_menu = preferences_menu.FindItemById(id)
1500                d_menu.Check(True)
1501            preferences_menu.AppendSeparator()
1502            id = wx.NewId()
1503            hint = "Clean up the dock area for plots on new-plot"
1504            preferences_menu.AppendCheckItem(id, '&CleanUp Dock on NewPlot', 
1505                                             hint)
1506            wx.EVT_MENU(self, id, self.on_cleanup_dock)
1507            flag = self.cleanup_plots
1508            if self.cleanup_plots:
1509                c_menu = preferences_menu.FindItemById(id)
1510                c_menu.Check(True) 
1511            self._window_menu.AppendSubMenu(preferences_menu,'&Preferences')
1512        if self._window_menu.GetMenuItemCount() == 0:
1513            pos = self._menubar.FindMenu('Graph')
1514            self._menubar.Remove(pos)
1515        #wx.EVT_MENU(self, id, self.show_preferences_panel)   
1516        #if len(self.plugins) == 2:
1517        #    plug = self.plugins[1]
1518        #    pers = plug.get_perspective()
1519        #
1520        #    if len(pers) > 1:
1521        #        self._window_menu = wx.Menu()
1522        #        for item in self.panels:
1523        #            if item == 'default':
1524        #                continue
1525        #            panel = self.panels[item]
1526        #            if panel.window_name in pers:
1527        #                self._window_menu.Append(int(item),
1528        #                                          panel.window_caption,
1529        #                                "Show %s window" % panel.window_caption)
1530        #                wx.EVT_MENU(self, int(item), self.on_view)
1531        #        self._menubar.Append(self._window_menu, '&Window')
1532
1533               
1534    def _add_menu_application(self):
1535        """
1536        # Attach a menu item for each defined perspective or application.
1537        # Only add the perspective menu if there are more than one perspectives
1538        add menu application
1539        """
1540        #style = self.__gui_style & GUIFRAME.MULTIPLE_APPLICATIONS
1541        #if style == GUIFRAME.MULTIPLE_APPLICATIONS:
1542        if self._num_perspectives  > 1:
1543            plug_data_count = False
1544            plug_no_data_count = False
1545            self._applications_menu = wx.Menu()
1546            pos = 0
1547            separator = self._applications_menu.AppendSeparator()
1548            for plug in self.plugins:
1549                if len(plug.get_perspective()) > 0:
1550                    id = wx.NewId()
1551                    if plug.use_data():
1552                       
1553                        self._applications_menu.InsertCheckItem(pos, id, plug.sub_menu,
1554                                      "Switch to analysis: %s" % plug.sub_menu)
1555                        plug_data_count = True
1556                        pos += 1
1557                    else:
1558                        plug_no_data_count = True
1559                        self._applications_menu.AppendCheckItem(id, plug.sub_menu,
1560                                      "Switch to analysis: %s" % plug.sub_menu)
1561                    wx.EVT_MENU(self, id, plug.on_perspective)
1562            #self._applications_menu.
1563            if (not plug_data_count or not plug_no_data_count):
1564                self._applications_menu.RemoveItem(separator)
1565            self._menubar.Append(self._applications_menu, '&Analysis')
1566            self._check_applications_menu()
1567           
1568    def _populate_file_menu(self):
1569        """
1570        Insert menu item under file menu
1571        """
1572        for plugin in self.plugins:
1573            if len(plugin.populate_file_menu()) > 0:
1574                for item in plugin.populate_file_menu():
1575                    m_name, m_hint, m_handler = item
1576                    id = wx.NewId()
1577                    self._file_menu.Append(id, m_name, m_hint)
1578                    wx.EVT_MENU(self, id, m_handler)
1579                self._file_menu.AppendSeparator()
1580               
1581    def _add_menu_file(self):
1582        """
1583        add menu file
1584        """
1585       
1586        # File menu
1587        self._file_menu = wx.Menu()
1588        #append item from plugin under menu file if necessary
1589        self._populate_file_menu()
1590        style1 = self.__gui_style & GUIFRAME.MULTIPLE_APPLICATIONS
1591        if OPEN_SAVE_MENU:
1592            id = wx.NewId()
1593            hint_load_file = "read all analysis states saved previously"
1594            self._save_appl_menu = self._file_menu.Append(id, 
1595                                    '&Open Project', hint_load_file)
1596            wx.EVT_MENU(self, id, self._on_open_state_project)
1597           
1598        if style1 == GUIFRAME.MULTIPLE_APPLICATIONS:
1599            # some menu of plugin to be seen under file menu
1600            hint_load_file = "Read a status files and load"
1601            hint_load_file += " them into the analysis"
1602            id = wx.NewId()
1603            self._save_appl_menu = self._file_menu.Append(id, 
1604                                    '&Open Analysis', hint_load_file)
1605            wx.EVT_MENU(self, id, self._on_open_state_application)
1606        if OPEN_SAVE_MENU:       
1607            self._file_menu.AppendSeparator()
1608            id = wx.NewId()
1609            self._file_menu.Append(id, '&Save Project',
1610                                 'Save the state of the whole analysis')
1611            wx.EVT_MENU(self, id, self._on_save_project)
1612        if style1 == GUIFRAME.MULTIPLE_APPLICATIONS:
1613            #self._file_menu.AppendSeparator()
1614            id = wx.NewId()
1615            self._save_appl_menu = self._file_menu.Append(id, 
1616                                                      '&Save Analysis',
1617                        'Save state of the current active analysis panel')
1618            wx.EVT_MENU(self, id, self._on_save_application)
1619            self._file_menu.AppendSeparator()
1620       
1621        id = wx.NewId()
1622        self._file_menu.Append(id, '&Quit', 'Exit') 
1623        wx.EVT_MENU(self, id, self.Close)
1624        # Add sub menus
1625        self._menubar.Append(self._file_menu, '&File')
1626       
1627    def _add_menu_edit(self):
1628        """
1629        add menu edit
1630        """
1631        if not EDIT_MENU:
1632            return
1633        # Edit Menu
1634        self._edit_menu = wx.Menu()
1635        self._edit_menu.Append(GUIFRAME_ID.UNDO_ID, '&Undo', 
1636                               'Undo the previous action')
1637        wx.EVT_MENU(self, GUIFRAME_ID.UNDO_ID, self.on_undo_panel)
1638        self._edit_menu.Append(GUIFRAME_ID.REDO_ID, '&Redo', 
1639                               'Redo the previous action')
1640        wx.EVT_MENU(self, GUIFRAME_ID.REDO_ID, self.on_redo_panel)
1641        self._edit_menu.AppendSeparator()
1642        self._edit_menu.Append(GUIFRAME_ID.COPY_ID, '&Copy Params', 
1643                               'Copy parameter values')
1644        wx.EVT_MENU(self, GUIFRAME_ID.COPY_ID, self.on_copy_panel)
1645        self._edit_menu.Append(GUIFRAME_ID.PASTE_ID, '&Paste Params', 
1646                               'Paste parameter values')
1647        wx.EVT_MENU(self, GUIFRAME_ID.PASTE_ID, self.on_paste_panel)
1648        self._edit_menu.AppendSeparator()
1649       
1650        self._edit_menu.Append(GUIFRAME_ID.PREVIEW_ID, '&Report Results',
1651                               'Preview current panel')
1652        wx.EVT_MENU(self, GUIFRAME_ID.PREVIEW_ID, self.on_preview_panel)
1653        #self._edit_menu.Append(GUIFRAME_ID.PRINT_ID, '&Print',
1654        #                       'Print current panel')
1655        #wx.EVT_MENU(self, GUIFRAME_ID.PRINT_ID, self.on_print_panel)
1656        self._edit_menu.Append(GUIFRAME_ID.RESET_ID, '&Reset Page', 
1657                               'Reset current panel')
1658        wx.EVT_MENU(self, GUIFRAME_ID.RESET_ID, self.on_reset_panel)
1659   
1660        self._menubar.Append(self._edit_menu,  '&Edit')
1661        self.enable_edit_menu()
1662       
1663    def get_style(self):
1664        """
1665        Return the gui style
1666        """
1667        return  self.__gui_style
1668   
1669    def _add_menu_data(self):
1670        """
1671        Add menu item item data to menu bar
1672        """
1673        if self._data_plugin is not None:
1674            menu_list = self._data_plugin.populate_menu(self)
1675            if menu_list:
1676                for (menu, name) in menu_list:
1677                    self._menubar.Append(menu, name)
1678       
1679                       
1680    def _on_toggle_toolbar(self, event=None):
1681        """
1682        hide or show toolbar
1683        """
1684        if self._toolbar is None:
1685            return
1686        if self._toolbar.IsShown():
1687            if self._toolbar_menu is not None:
1688                self._toolbar_menu.SetItemLabel('Show Toolbar')
1689            self._toolbar.Hide()
1690        else:
1691            if self._toolbar_menu is not None:
1692                self._toolbar_menu.SetItemLabel('Hide Toolbar')
1693            self._toolbar.Show()
1694        self._toolbar.Realize()
1695       
1696    def _on_status_event(self, evt):
1697        """
1698        Display status message
1699        """
1700        # This CallAfter fixes many crashes on MAC.
1701        wx.CallAfter(self.sb.set_status, evt)
1702       
1703    def on_view(self, evt):
1704        """
1705        A panel was selected to be shown. If it's not already
1706        shown, display it.
1707       
1708        :param evt: menu event
1709       
1710        """
1711        panel_id = str(evt.GetId())
1712        self.on_set_plot_focus(self.panels[panel_id])
1713        self.show_panel(evt.GetId(), 'on')     
1714        wx.CallLater(5*TIME_FACTOR, self.set_schedule(True))
1715        self.set_plot_unfocus()
1716       
1717    def on_close_welcome_panel(self):
1718        """
1719        Close the welcome panel
1720        """
1721        if self.defaultPanel is None:
1722            return 
1723        default_panel = self._mgr.GetPane(self.panels["default"].window_name)
1724        if default_panel.IsShown():
1725            default_panel.Hide()
1726            # Recover current perspective
1727            perspective = self._current_perspective
1728            perspective.on_perspective(event=None)
1729            self._mgr.Update()
1730            # Show toolbar
1731            #style = self.__gui_style & GUIFRAME.TOOLBAR_ON
1732            #if (style == GUIFRAME.TOOLBAR_ON) & (not self._toolbar.IsShown()):
1733            #    self._on_toggle_toolbar()
1734           
1735    def show_welcome_panel(self, event):
1736        """   
1737        Display the welcome panel
1738        """
1739        if self.defaultPanel is None:
1740            return 
1741        for id, panel in self.panels.iteritems():
1742            if id  ==  'default':
1743                # Show default panel
1744                if not self._mgr.GetPane(self.panels["default"].window_name).IsShown():
1745                    self._mgr.GetPane(self.panels["default"].window_name).Show(True)
1746            elif id == "data_panel":
1747                flag = self._mgr.GetPane(self.panels["data_panel"].window_name).IsShown()
1748                self._mgr.GetPane(self.panels["data_panel"].window_name).Show(flag)
1749            elif panel not in self.plot_panels.values() :
1750                self._mgr.GetPane(self.panels[id].window_name).IsShown()
1751                self._mgr.GetPane(self.panels[id].window_name).Hide()
1752        #style = self.__gui_style & GUIFRAME.TOOLBAR_ON
1753        #if (style == GUIFRAME.TOOLBAR_ON) & (self._toolbar.IsShown()):
1754        #    #    self._toolbar.Show(True)
1755        #    self._on_toggle_toolbar()
1756
1757        self._mgr.Update()
1758       
1759    def show_panel(self, uid, show=None):
1760        """
1761        Shows the panel with the given id
1762       
1763        :param uid: unique ID number of the panel to show
1764       
1765        """
1766        ID = str(uid)
1767        config.printEVT("show_panel: %s" % ID)
1768        if ID in self.panels.keys():
1769            if not self._mgr.GetPane(self.panels[ID].window_name).IsShown(): 
1770                if show == 'on':
1771                    self._mgr.GetPane(self.panels[ID].window_name).Show()   
1772                elif self.panels[ID].window_caption.split(" ")[0] == \
1773                                                            "Residuals":
1774                    self._mgr.GetPane(self.panels[ID].window_name).Hide()
1775                else:
1776                    self._mgr.GetPane(self.panels[ID].window_name).Show()
1777                # Hide default panel
1778                self._mgr.GetPane(self.panels["default"].window_name).Hide()
1779        self._mgr.Update()     
1780        self._redraw_idle()
1781
1782    def show_all_plotpanels(self, event):
1783        """
1784        Show all plotpanels shown
1785       
1786        :param event: menu event
1787        """
1788        #event.Skip()
1789        any_hidden = False
1790        for id in self.plot_panels.keys():
1791            if self._mgr.GetPane(self.plot_panels[id].window_name).IsShown():
1792                continue
1793            else:
1794                any_hidden = True
1795                self.show_panel(id)
1796        if not any_hidden:
1797            msg = "No hidden graph panel exists."
1798        else:
1799            # Make sure the Checkmenuitem checked: Need this for-loop \
1800            # because the check menu is not responding on floating panel
1801            if IS_WIN:
1802                try:
1803                    for item in self._plotting_plugin.menu.GetMenuItems():
1804                        item.Check(True)
1805                except:
1806                    # MAC is not using checkmenuitem
1807                    pass
1808            msg = "All graph panels are shown."
1809        wx.PostEvent(self, StatusEvent(status=msg))
1810           
1811    def hide_all_plotpanels(self, event):
1812        """
1813        Hide all plotpanels shown
1814       
1815        :param event: menu event
1816        """
1817        #event.Skip()
1818        any_shown = False
1819        for ID in self.plot_panels.keys():
1820            if self._mgr.GetPane(self.plot_panels[ID].window_name).IsShown():
1821                any_shown = True
1822                try:
1823                    self.hide_panel(ID)
1824                except:
1825                    print "hide_panel: No such plot id %s" % ID
1826            else:
1827                continue
1828        if not any_shown:
1829            msg = "No shown graph panel exists."
1830        else:
1831            # Make sure the Checkmenuitem unchecked: Need this for-loop
1832            # because the check menu is not responding on floating panel
1833            if IS_WIN:
1834                try:
1835                    for item in self._plotting_plugin.menu.GetMenuItems():
1836                        item.Check(False)
1837                except:
1838                    # MAC is not using checkmenuitem
1839                    pass
1840            msg = "All graph panels are hidden."
1841        wx.PostEvent(self, StatusEvent(status=msg))
1842                   
1843    def hide_panel(self, uid):
1844        """
1845        hide panel except default panel
1846        """
1847        ID = str(uid)
1848        caption = self.panels[ID].window_caption
1849        config.printEVT("hide_panel: %s" % ID)
1850        if ID in self.panels.keys():
1851            pane = self._mgr.GetPane(self.panels[ID].window_name)
1852            if pane.IsMaximized():
1853                self._mgr.RestoreMaximizedPane()
1854                self.disable_app_menu(self.panels[ID])
1855            if pane.IsShown():
1856                pane.Hide()
1857                item = self._plotting_plugin.menu.FindItemById(uid)
1858                item.Check(False)
1859                if self._data_panel is not None and \
1860                            ID in self.plot_panels.keys():
1861                    self._data_panel.cb_plotpanel.Append(str(caption), 
1862                                                         self.panels[ID])
1863                # Do not Hide default panel here...
1864                #self._mgr.GetPane(self.panels["default"].window_name).Hide()
1865            wx.CallAfter(self._mgr.Update)
1866            self.cpanel_on_focus.SetFocus()
1867               
1868    def delete_panel(self, uid):
1869        """
1870        delete panel given uid
1871        """
1872        ID = str(uid)
1873        config.printEVT("delete_panel: %s" % ID)
1874        try:
1875            caption = self.panels[ID].window_caption
1876        except:
1877            print "delete_panel: No such plot id as %s" % ID
1878            return
1879        if ID in self.panels.keys():
1880            self.panel_on_focus = None
1881            panel = self.panels[ID]
1882            self._mgr.DetachPane(panel)
1883            self._plotting_plugin.delete_panel(panel.group_id)
1884            panel.Hide()
1885            panel.clear()
1886            panel.Close()
1887            if panel in self.schedule_full_draw_list:
1888                self.schedule_full_draw_list.remove(panel) 
1889           
1890            #delete uid number not str(uid)
1891            if ID in self.plot_panels.keys():
1892                del self.plot_panels[ID]
1893            if ID in self.panels.keys():
1894                del self.panels[ID]
1895            #CallAfter: make sure panel is clear before updating mgr
1896            wx.CallAfter(self._mgr.Update)
1897            return 
1898     
1899    def clear_panel(self):
1900        """
1901        Clear the panel
1902        """
1903        for item in self.panels:
1904            try:
1905                self.panels[item].clear_panel()
1906            except:
1907                pass
1908           
1909    def create_gui_data(self, data, path=None):
1910        """
1911        """
1912        return self._data_manager.create_gui_data(data, path)
1913   
1914    def get_data(self, path):
1915        """
1916        """
1917        message = ""
1918        log_msg = ''
1919        output = []
1920        error_message = ""
1921        basename  = os.path.basename(path)
1922        root, extension = os.path.splitext(basename)
1923        if extension.lower() not in EXTENSIONS:
1924            log_msg = "File Loader cannot "
1925            log_msg += "load: %s\n" % str(basename)
1926            log_msg += "Try Data opening...."
1927            logging.info(log_msg)
1928            print log_msg
1929            #self.load_complete(output=output, error_message=error_message,
1930            #       message=log_msg, path=path)   
1931            return
1932       
1933        #reading a state file
1934        for plug in self.plugins:
1935            reader, ext = plug.get_extensions()
1936            if reader is not None:
1937                #read the state of the single plugin
1938                if extension == ext:
1939                    reader.read(path)
1940                    return
1941                elif extension == APPLICATION_STATE_EXTENSION:
1942                    try:
1943                        reader.read(path)
1944                    except:
1945                        msg = "DataLoader Error: Encounted Non-ASCII character"
1946                        msg += "\n(%s)"% sys.exc_value
1947                        wx.PostEvent(self, StatusEvent(status=msg, 
1948                                                info="error", type="stop"))
1949                        return
1950       
1951        style = self.__gui_style & GUIFRAME.MANAGER_ON
1952        if style == GUIFRAME.MANAGER_ON:
1953            if self._data_panel is not None:
1954                #data_state = self._data_manager.get_selected_data()
1955                #self._data_panel.load_data_list(data_state)
1956                self._mgr.GetPane(self._data_panel.window_name).Show(True)
1957     
1958    def load_from_cmd(self,  path):   
1959        """
1960        load data from cmd or application
1961        """ 
1962        if path is None:
1963            return
1964        else:
1965            path = os.path.abspath(path)
1966            if not os.path.isfile(path) and not os.path.isdir(path):
1967                return
1968           
1969            if os.path.isdir(path):
1970                self.load_folder(path)
1971                return
1972
1973        basename  = os.path.basename(path)
1974        root, extension = os.path.splitext(basename)
1975        if extension.lower() not in EXTENSIONS:
1976            self.load_data(path)
1977        else:
1978            self.load_state(path)
1979
1980        self._default_save_location = os.path.dirname(path)
1981
1982    def load_state(self, path):   
1983        """
1984        load data from command line or application
1985        """
1986        if path and (path is not None) and os.path.isfile(path):
1987            basename  = os.path.basename(path)
1988            if APPLICATION_STATE_EXTENSION is not None \
1989                and basename.endswith(APPLICATION_STATE_EXTENSION):
1990                #Hide current plot_panels i
1991                for ID in self.plot_panels.keys():
1992                    panel = self._mgr.GetPane(self.plot_panels[ID].window_name)
1993                    if panel.IsShown():
1994                        panel.Hide()
1995            self.get_data(path)
1996        if self.defaultPanel is not None and \
1997            self._mgr.GetPane(self.panels["default"].window_name).IsShown():
1998            self.on_close_welcome_panel()
1999           
2000    def load_data(self, path):
2001        """
2002        load data from command line
2003        """
2004        if not os.path.isfile(path):
2005            return
2006        basename  = os.path.basename(path)
2007        root, extension = os.path.splitext(basename)
2008        if extension.lower() in EXTENSIONS:
2009            log_msg = "Data Loader cannot "
2010            log_msg += "load: %s\n" % str(path)
2011            log_msg += "Try File opening ...."
2012            print log_msg
2013            return
2014        log_msg = ''
2015        output = {}
2016        error_message = ""
2017        try:
2018            print "Loading Data...:\n" + str(path) + "\n"
2019            temp =  self.loader.load(path)
2020            if temp.__class__.__name__ == "list":
2021                for item in temp:
2022                    data = self.create_gui_data(item, path)
2023                    output[data.id] = data
2024            else:
2025                data = self.create_gui_data(temp, path)
2026                output[data.id] = data
2027           
2028            self.add_data(data_list=output)
2029        except:
2030            error_message = "Error while loading"
2031            error_message += " Data from cmd:\n %s\n" % str(path)
2032            error_message += str(sys.exc_value) + "\n"
2033            print error_message
2034 
2035    def load_folder(self, path):
2036        """
2037        Load entire folder
2038        """   
2039        if not os.path.isdir(path):
2040            return
2041        if self._data_plugin is None:
2042            return
2043        try:
2044            if path is not None:
2045                self._default_save_location = os.path.dirname(path)
2046                file_list = self._data_plugin.get_file_path(path)
2047                self._data_plugin.get_data(file_list)
2048            else:
2049                return 
2050        except:
2051            error_message = "Error while loading"
2052            error_message += " Data folder from cmd:\n %s\n" % str(path)
2053            error_message += str(sys.exc_value) + "\n"
2054            print error_message
2055           
2056    def _on_open_state_application(self, event):
2057        """
2058        """
2059        path = None
2060        if self._default_save_location == None:
2061            self._default_save_location = os.getcwd()
2062       
2063        plug_wlist = self._on_open_state_app_helper()
2064        dlg = wx.FileDialog(self, 
2065                            "Choose a file", 
2066                            self._default_save_location, "",
2067                            plug_wlist)
2068        if dlg.ShowModal() == wx.ID_OK:
2069            path = dlg.GetPath()
2070            if path is not None:
2071                self._default_save_location = os.path.dirname(path)
2072        dlg.Destroy()
2073        self.load_state(path=path) 
2074   
2075    def _on_open_state_app_helper(self):
2076        """
2077        Helps '_on_open_state_application()' to find the extension of
2078        the current perspective/application
2079        """
2080        # No current perspective or no extension attr
2081        if self._current_perspective is None:
2082            return PLUGINS_WLIST
2083        try:
2084            # Find the extension of the perspective
2085            # and get that as 1st item in list
2086            ind = None
2087            app_ext = self._current_perspective._extensions
2088            plug_wlist = config.PLUGINS_WLIST
2089            for ext in set(plug_wlist):
2090                if ext.count(app_ext) > 0:
2091                    ind = ext
2092                    break
2093            # Found the extension
2094            if ind != None:
2095                plug_wlist.remove(ind)
2096                plug_wlist.insert(0, ind)
2097                try:
2098                    plug_wlist = '|'.join(plug_wlist)
2099                except:
2100                    plug_wlist = ''
2101
2102        except:
2103            plug_wlist = PLUGINS_WLIST
2104           
2105        return plug_wlist
2106           
2107    def _on_open_state_project(self, event):
2108        """
2109        """
2110        path = None
2111        if self._default_save_location == None:
2112            self._default_save_location = os.getcwd()
2113       
2114        dlg = wx.FileDialog(self, 
2115                            "Choose a file", 
2116                            self._default_save_location, "",
2117                             APPLICATION_WLIST)
2118        if dlg.ShowModal() == wx.ID_OK:
2119            path = dlg.GetPath()
2120            if path is not None:
2121                self._default_save_location = os.path.dirname(path)
2122        dlg.Destroy()
2123       
2124        #try:   
2125        #    os.popen(path)
2126        #    #self.Close()
2127        #except:
2128        self.load_state(path=path)
2129       
2130    def _on_save_application(self, event):
2131        """
2132        save the state of the current active application
2133        """
2134        if self.cpanel_on_focus is not None:
2135            try:
2136                self.cpanel_on_focus.on_save(event)
2137            except:
2138                msg = "Error occurred while saving: "
2139                msg += "To save, the application panel should have a data set.."
2140                wx.PostEvent(self, StatusEvent(status=msg)) 
2141           
2142    def _on_save_project(self, event):
2143        """
2144        save the state of the SasView as *.svs
2145        """
2146        if self._current_perspective is  None:
2147            return
2148        reader, ext = self._current_perspective.get_extensions()
2149        path = None
2150        extension = '*' + APPLICATION_STATE_EXTENSION
2151        dlg = wx.FileDialog(self, "Save Project file",
2152                            self._default_save_location, "sasview_proj",
2153                             extension, 
2154                             wx.SAVE)
2155        if dlg.ShowModal() == wx.ID_OK:
2156            path = dlg.GetPath()
2157            self._default_save_location = os.path.dirname(path)
2158        else:
2159            return None
2160        dlg.Destroy()
2161        try:
2162            if path is None:
2163                return
2164            # default cansas xml doc
2165            doc = None
2166            for panel in self.panels.values():
2167                temp = panel.save_project(doc)
2168                if temp is not None:
2169                    doc = temp
2170             
2171            # Write the XML document
2172            extens = APPLICATION_STATE_EXTENSION
2173            fName = os.path.splitext(path)[0] + extens
2174            if doc != None:
2175                fd = open(fName, 'w')
2176                fd.write(doc.toprettyxml())
2177                fd.close()
2178            else:
2179                msg = "%s cannot read %s\n" % (str(APPLICATION_NAME), str(path))
2180                logging.error(msg)
2181        except:
2182            msg = "Error occurred while saving: "
2183            msg += "To save, at leat one application panel "
2184            msg += "should have a data set.."
2185            wx.PostEvent(self, StatusEvent(status=msg))   
2186                   
2187    def on_save_helper(self, doc, reader, panel, path):
2188        """
2189        Save state into a file
2190        """
2191        try:
2192            if reader is not None:
2193                # case of a panel with multi-pages
2194                if hasattr(panel, "opened_pages"):
2195                    for uid, page in panel.opened_pages.iteritems():
2196                        data = page.get_data()
2197                        # state must be cloned
2198                        state = page.get_state().clone()
2199                        if data is not None:
2200                            new_doc = reader.write_toXML(data, state)
2201                            if doc != None and hasattr(doc, "firstChild"):
2202                                child = new_doc.firstChild.firstChild
2203                                doc.firstChild.appendChild(child) 
2204                            else:
2205                                doc = new_doc
2206                # case of only a panel
2207                else:
2208                    data = panel.get_data()
2209                    state = panel.get_state()
2210                    if data is not None:
2211                        new_doc = reader.write_toXML(data, state)
2212                        if doc != None and hasattr(doc, "firstChild"):
2213                            child = new_doc.firstChild.firstChild
2214                            doc.firstChild.appendChild(child) 
2215                        else:
2216                            doc = new_doc
2217        except: 
2218            raise
2219            #pass
2220
2221        return doc
2222
2223    def quit_guiframe(self):
2224        """
2225        Pop up message to make sure the user wants to quit the application
2226        """
2227        message = "\nDo you really want to exit this application?        \n\n"
2228        dial = wx.MessageDialog(self, message, 'Confirm Exit',
2229                           wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
2230        if dial.ShowModal() == wx.ID_YES:
2231            return True
2232        else:
2233            return False   
2234       
2235    def WindowClose(self, event=None):
2236        """
2237        Quit the application from x icon
2238        """
2239        flag = self.quit_guiframe()
2240        if flag:
2241            self.Close()
2242           
2243    def Close(self, event=None):
2244        """
2245        Quit the application
2246        """
2247        wx.Exit()
2248        sys.exit()
2249           
2250    def _check_update(self, event=None): 
2251        """
2252        Check with the deployment server whether a new version
2253        of the application is available.
2254        A thread is started for the connecting with the server. The thread calls
2255        a call-back method when the current version number has been obtained.
2256        """
2257        try:
2258            conn = httplib.HTTPConnection(config.__update_URL__[0], 
2259                              timeout=3.0)
2260            conn.request("GET", config.__update_URL__[1])
2261            res = conn.getresponse()
2262            content = res.read()
2263            conn.close()
2264            #f=urllib2.urlopen(config.__update_URL__,
2265            #                  timeout=1.0)
2266            #content = f.read()
2267        except:
2268            content = "0.0.0"
2269       
2270        version = content.strip()
2271        if len(re.findall('\d+\.\d+\.\d+$', version)) < 0:
2272            content = "0.0.0"
2273        self._process_version(content, standalone=event==None)
2274   
2275    def _process_version(self, version, standalone=True):
2276        """
2277        Call-back method for the process of checking for updates.
2278        This methods is called by a VersionThread object once the current
2279        version number has been obtained. If the check is being done in the
2280        background, the user will not be notified unless there's an update.
2281       
2282        :param version: version string
2283        :param standalone: True of the update is being checked in
2284           the background, False otherwise.
2285           
2286        """
2287        try:
2288            if version == "0.0.0":
2289                msg = "Could not connect to the application server."
2290                msg += " Please try again later."
2291                self.SetStatusText(msg)
2292            elif cmp(version, config.__version__) > 0:
2293                msg = "Version %s is available! " % str(version)
2294                if not standalone:
2295                    import webbrowser
2296                    webbrowser.open(config.__download_page__)
2297                else:
2298                    msg +=  "See the help menu to download it." 
2299                self.SetStatusText(msg)
2300            else:
2301                if not standalone:
2302                    msg = "You have the latest version"
2303                    msg += " of %s" % str(config.__appname__)
2304                    self.SetStatusText(msg)
2305        except:
2306            msg = "guiframe: could not get latest application"
2307            msg += " version number\n  %s" % sys.exc_value
2308            logging.error(msg)
2309            if not standalone:
2310                msg = "Could not connect to the application server."
2311                msg += " Please try again later."
2312                self.SetStatusText(msg)
2313                   
2314    def _onAbout(self, evt):
2315        """
2316        Pop up the about dialog
2317       
2318        :param evt: menu event
2319       
2320        """
2321        if config._do_aboutbox:
2322            import sans.guiframe.aboutbox as AboutBox 
2323            dialog = AboutBox.DialogAbout(None, -1, "")
2324            dialog.ShowModal()   
2325                     
2326    def _onTutorial(self, evt):
2327        """
2328        Pop up the tutorial dialog
2329       
2330        :param evt: menu event
2331       
2332        """
2333        if config._do_tutorial:   
2334            path = config.TUTORIAL_PATH
2335            if IS_WIN:
2336                try:
2337                    from sans.guiframe.pdfview import PDFFrame
2338                    dialog = PDFFrame(None, -1, "Tutorial", path)
2339                    # put icon
2340                    self.put_icon(dialog) 
2341                    dialog.Show(True) 
2342                except:
2343                    print "Error in _onTutorial: %s" % sys.exc_value
2344                    try:
2345                        #in case when the pdf default set other than acrobat
2346                        import ho.pisa as pisa
2347                        pisa.startViewer(path)
2348                    except:
2349                        msg = "This feature requires 'PDF Viewer'\n"
2350                        msg += "Please install it first (Free)..."
2351                        wx.MessageBox(msg, 'Error')
2352            else:
2353                try:
2354                    command = "open '%s'" % path
2355                    os.system(command)
2356                except:
2357                    try:
2358                        #in case when the pdf default set other than preview
2359                        import ho.pisa as pisa
2360                        pisa.startViewer(path)
2361                    except:
2362                        msg = "This feature requires 'Preview' Application\n"
2363                        msg += "Please install it first..."
2364                        wx.MessageBox(msg, 'Error')
2365
2366                     
2367    def set_manager(self, manager):
2368        """
2369        Sets the application manager for this frame
2370       
2371        :param manager: frame manager
2372        """
2373        self.app_manager = manager
2374       
2375    def post_init(self):
2376        """
2377        This initialization method is called after the GUI
2378        has been created and all plug-ins loaded. It calls
2379        the post_init() method of each plug-in (if it exists)
2380        so that final initialization can be done.
2381        """
2382        for item in self.plugins:
2383            if hasattr(item, "post_init"):
2384                item.post_init()
2385       
2386    def set_default_perspective(self):
2387        """
2388        Choose among the plugin the first plug-in that has
2389        "set_default_perspective" method and its return value is True will be
2390        as a default perspective when the welcome page is closed
2391        """
2392        for item in self.plugins:
2393            if hasattr(item, "set_default_perspective"):
2394                if item.set_default_perspective():
2395                    item.on_perspective(event=None)
2396                    return 
2397       
2398    def set_perspective(self, panels):
2399        """
2400        Sets the perspective of the GUI.
2401        Opens all the panels in the list, and closes
2402        all the others.
2403       
2404        :param panels: list of panels
2405        """
2406        #style = self.__gui_style & GUIFRAME.TOOLBAR_ON
2407        #if (style == GUIFRAME.TOOLBAR_ON) & (not self._toolbar.IsShown()):
2408        #    self._on_toggle_toolbar()
2409        for item in self.panels:
2410            # Check whether this is a sticky panel
2411            if hasattr(self.panels[item], "ALWAYS_ON"):
2412                if self.panels[item].ALWAYS_ON:
2413                    continue 
2414           
2415            if self.panels[item].window_name in panels:
2416                if not self._mgr.GetPane(self.panels[item].window_name).IsShown():
2417                    self._mgr.GetPane(self.panels[item].window_name).Show()
2418            else:
2419                # always show the data panel if enable
2420                style = self.__gui_style & GUIFRAME.MANAGER_ON
2421                if (style == GUIFRAME.MANAGER_ON) and self.panels[item] == self._data_panel:
2422                    if 'data_panel' in self.panels.keys():
2423                        flag = self._mgr.GetPane(self.panels['data_panel'].window_name).IsShown()
2424                        self._mgr.GetPane(self.panels['data_panel'].window_name).Show(flag)
2425                else:
2426                    if self._mgr.GetPane(self.panels[item].window_name).IsShown():
2427                        self._mgr.GetPane(self.panels[item].window_name).Hide()
2428               
2429        self._mgr.Update()
2430       
2431    def show_data_panel(self, event=None, action=True):
2432        """
2433        show the data panel
2434        """
2435        if self._data_panel_menu == None:
2436            return
2437        label = self._data_panel_menu.GetText()
2438        if label == 'Show Data Explorer':
2439            pane = self._mgr.GetPane(self.panels["data_panel"].window_name)
2440            #if not pane.IsShown():
2441            if action: 
2442                pane.Show(True)
2443                self._mgr.Update()
2444            self.__gui_style = self.__gui_style | GUIFRAME.MANAGER_ON
2445           
2446            self._data_panel_menu.SetText('Hide Data Explorer')
2447        else:
2448            pane = self._mgr.GetPane(self.panels["data_panel"].window_name)
2449            #if not pane.IsShown():
2450            if action:
2451                pane.Show(False)
2452                self._mgr.Update()
2453            self.__gui_style = self.__gui_style & (~GUIFRAME.MANAGER_ON)
2454            self._data_panel_menu.SetText('Show Data Explorer')
2455   
2456    def add_data_helper(self, data_list):
2457        """
2458        """
2459        if self._data_manager is not None:
2460            self._data_manager.add_data(data_list)
2461       
2462    def add_data(self, data_list):
2463        """
2464        receive a dictionary of data from loader
2465        store them its data manager if possible
2466        send to data the current active perspective if the data panel
2467        is not active.
2468        :param data_list: dictionary of data's ID and value Data
2469        """
2470        #Store data into manager
2471        self.add_data_helper(data_list)
2472        # set data in the data panel
2473        if self._data_panel is not None:
2474            data_state = self._data_manager.get_data_state(data_list.keys())
2475            self._data_panel.load_data_list(data_state)
2476        #if the data panel is shown wait for the user to press a button
2477        #to send data to the current perspective. if the panel is not
2478        #show  automatically send the data to the current perspective
2479        style = self.__gui_style & GUIFRAME.MANAGER_ON
2480        if style == GUIFRAME.MANAGER_ON:
2481            #wait for button press from the data panel to set_data
2482            if self._data_panel is not None:
2483                self._mgr.GetPane(self._data_panel.window_name).Show(True)
2484                self._mgr.Update() 
2485        else:
2486            #automatically send that to the current perspective
2487            self.set_data(data_id=data_list.keys())
2488            self.on_close_welcome_panel()
2489       
2490    def set_data(self, data_id, theory_id=None): 
2491        """
2492        set data to current perspective
2493        """
2494        list_data, _ = self._data_manager.get_by_id(data_id)
2495        if self._current_perspective is not None:
2496            if self.cleanup_plots:
2497                for uid, panel in self.plot_panels.iteritems():
2498                    #panel = self.plot_panels[uid]
2499                    window = self._mgr.GetPane(panel.window_name)
2500                    # To hide all docked plot panels when set the data
2501                    if not window.IsFloating():
2502                        self.hide_panel(uid)
2503            self._current_perspective.set_data(list_data.values())
2504            self.on_close_welcome_panel()
2505        else:
2506            msg = "Guiframe does not have a current perspective"
2507            logging.info(msg)
2508           
2509    def set_theory(self, state_id, theory_id=None):
2510        """
2511        """
2512        _, list_theory = self._data_manager.get_by_id(theory_id)
2513        if self._current_perspective is not None:
2514            try:
2515                self._current_perspective.set_theory(list_theory.values())
2516            except:
2517                msg = "Guiframe set_theory: \n" + str(sys.exc_value)
2518                logging.info(msg)
2519                wx.PostEvent(self, StatusEvent(status=msg, info="error"))
2520        else:
2521            msg = "Guiframe does not have a current perspective"
2522            logging.info(msg)
2523           
2524    def plot_data(self,  state_id, data_id=None,
2525                  theory_id=None, append=False):
2526        """
2527        send a list of data to plot
2528        """
2529        total_plot_list = []
2530        data_list, _ = self._data_manager.get_by_id(data_id)
2531        _, temp_list_theory = self._data_manager.get_by_id(theory_id)
2532        total_plot_list = data_list.values()
2533        for item in temp_list_theory.values():
2534            theory_data, theory_state = item
2535            total_plot_list.append(theory_data)
2536        GROUP_ID = wx.NewId()
2537        for new_plot in total_plot_list:
2538            if append:
2539                if self.panel_on_focus is None:
2540                    message = "cannot append plot. No plot panel on focus!"
2541                    message += "please click on any available plot to set focus"
2542                    wx.PostEvent(self, StatusEvent(status=message, 
2543                                                   info='warning'))
2544                    return 
2545                else:
2546                    if self.enable_add_data(new_plot):
2547                        new_plot.group_id = self.panel_on_focus.group_id
2548                    else:
2549                        message = "Only 1D Data can be append to"
2550                        message += " plot panel containing 1D data.\n"
2551                        message += "%s not be appended.\n" %str(new_plot.name)
2552                        message += "try new plot option.\n"
2553                        wx.PostEvent(self, StatusEvent(status=message, 
2554                                                   info='warning'))
2555            else:
2556                if self.cleanup_plots:
2557                    for id, panel in self.plot_panels.iteritems():
2558                        window = self._mgr.GetPane(panel.window_name)
2559                        # To hide all docked plot panels when set the data
2560                        if not window.IsFloating():
2561                            self.hide_panel(id)
2562                #if not append then new plot
2563                from sans.guiframe.dataFitting import Data2D
2564                if issubclass(Data2D, new_plot.__class__):
2565                    #for 2 D always plot in a separated new plot
2566                    new_plot.group_id = wx.NewId()
2567                else:
2568                    # plot all 1D in a new plot
2569                    new_plot.group_id = GROUP_ID
2570            title = "PLOT " + str(new_plot.title)
2571            wx.PostEvent(self, NewPlotEvent(plot=new_plot,
2572                                                  title=title,
2573                                                  group_id = new_plot.group_id))
2574           
2575    def remove_data(self, data_id, theory_id=None):
2576        """
2577        Delete data state if data_id is provide
2578        delete theory created with data of id data_id if theory_id is provide
2579        if delete all true: delete the all state
2580        else delete theory
2581        """
2582        temp = data_id + theory_id
2583        """
2584        value = [plug.is_in_use(temp) for plug in self.plugins]
2585        if len(value) > 0:
2586            print "value"
2587            return
2588            from data_panel import DataDialog
2589            dlg = DataDialog(data_list=data_list, nb_data=MAX_NBR_DATA)
2590            if dlg.ShowModal() == wx.ID_OK:
2591                selected_data_list = dlg.get_data()
2592            dlg.Destroy()
2593        """
2594        for plug in self.plugins:
2595            plug.delete_data(temp)
2596        total_plot_list = []
2597        data_list, _ = self._data_manager.get_by_id(data_id)
2598        _, temp_list_theory = self._data_manager.get_by_id(theory_id)
2599        total_plot_list = data_list.values()
2600        for item in temp_list_theory.values():
2601            theory_data, theory_state = item
2602            total_plot_list.append(theory_data)
2603        for new_plot in total_plot_list:
2604            id = new_plot.id
2605            for group_id in new_plot.list_group_id:
2606                wx.PostEvent(self, NewPlotEvent(id=id,
2607                                                   group_id=group_id,
2608                                                   action='remove'))
2609                #remove res plot: Todo: improve
2610                wx.CallAfter(self._remove_res_plot, id)
2611        self._data_manager.delete_data(data_id=data_id, 
2612                                       theory_id=theory_id)
2613       
2614    def _remove_res_plot(self, id):
2615        """
2616        Try to remove corresponding res plot
2617       
2618        : param id: id of the data
2619        """
2620        try:
2621            wx.PostEvent(self, NewPlotEvent(id=("res"+str(id)),
2622                                           group_id=("res"+str(id)),
2623                                           action='remove'))
2624        except:
2625            pass
2626   
2627    def save_data1d(self, data, fname):
2628        """
2629        Save data dialog
2630        """
2631        default_name = fname
2632        wildcard = "Text files (*.txt)|*.txt|"\
2633                    "CanSAS 1D files(*.xml)|*.xml" 
2634        path = None
2635        dlg = wx.FileDialog(self, "Choose a file",
2636                            self._default_save_location,
2637                            default_name, wildcard , wx.SAVE)
2638       
2639        if dlg.ShowModal() == wx.ID_OK:
2640            path = dlg.GetPath()
2641            # ext_num = 0 for .txt, ext_num = 1 for .xml
2642            # This is MAC Fix
2643            ext_num = dlg.GetFilterIndex()
2644            if ext_num == 0:
2645                format = '.txt'
2646            else:
2647                format = '.xml'
2648            path = os.path.splitext(path)[0] + format
2649            mypath = os.path.basename(path)
2650           
2651            #TODO: This is bad design. The DataLoader is designed
2652            #to recognize extensions.
2653            # It should be a simple matter of calling the .
2654            #save(file, data, '.xml') method
2655            # of the sans.dataloader.loader.Loader class.
2656            from sans.dataloader.loader import  Loader
2657            #Instantiate a loader
2658            loader = Loader() 
2659            format = ".txt"
2660            if os.path.splitext(mypath)[1].lower() == format:
2661                # Make sure the ext included in the file name
2662                # especially on MAC
2663                fName = os.path.splitext(path)[0] + format
2664                self._onsaveTXT(data, fName)
2665            format = ".xml"
2666            if os.path.splitext(mypath)[1].lower() == format:
2667                # Make sure the ext included in the file name
2668                # especially on MAC
2669                fName = os.path.splitext(path)[0] + format
2670                loader.save(fName, data, format)
2671            try:
2672                self._default_save_location = os.path.dirname(path)
2673            except:
2674                pass   
2675        dlg.Destroy()
2676       
2677       
2678    def _onsaveTXT(self, data, path):
2679        """
2680        Save file as txt 
2681        :TODO: Refactor and remove this method. See TODO in _onSave.
2682        """
2683        if not path == None:
2684            out = open(path, 'w')
2685            has_errors = True
2686            if data.dy == None or data.dy == []:
2687                has_errors = False
2688            # Sanity check
2689            if has_errors:
2690                try:
2691                    if len(data.y) != len(data.dy):
2692                        has_errors = False
2693                except:
2694                    has_errors = False
2695            if has_errors:
2696                if data.dx != None and data.dx != []:
2697                    out.write("<X>   <Y>   <dY>   <dX>\n")
2698                else:
2699                    out.write("<X>   <Y>   <dY>\n")
2700            else:
2701                out.write("<X>   <Y>\n")
2702               
2703            for i in range(len(data.x)):
2704                if has_errors:
2705                    if data.dx != None and data.dx != []:
2706                        if  data.dx[i] != None:
2707                            out.write("%g  %g  %g  %g\n" % (data.x[i], 
2708                                                        data.y[i],
2709                                                        data.dy[i],
2710                                                        data.dx[i]))
2711                        else:
2712                            out.write("%g  %g  %g\n" % (data.x[i], 
2713                                                        data.y[i],
2714                                                        data.dy[i]))
2715                    else:
2716                        out.write("%g  %g  %g\n" % (data.x[i], 
2717                                                    data.y[i],
2718                                                    data.dy[i]))
2719                else:
2720                    out.write("%g  %g\n" % (data.x[i], 
2721                                            data.y[i]))
2722            out.close() 
2723                             
2724    def show_data1d(self, data, name):
2725        """
2726        Show data dialog
2727        """   
2728        try:
2729            xmin = min(data.x)
2730            ymin = min(data.y)
2731        except:
2732            msg = "Unable to find min/max of \n data named %s"% \
2733                        data.filename 
2734            wx.PostEvent(self, StatusEvent(status=msg,
2735                                       info="error"))
2736            raise ValueError, msg
2737        text = data.__str__() 
2738        text += 'Data Min Max:\n'
2739        text += 'X_min = %s:  X_max = %s\n'% (xmin, max(data.x))
2740        text += 'Y_min = %s:  Y_max = %s\n'% (ymin, max(data.y))
2741        if data.dy != None:
2742            text += 'dY_min = %s:  dY_max = %s\n'% (min(data.dy), max(data.dy))
2743        text += '\nData Points:\n'
2744        x_st = "X"
2745        for index in range(len(data.x)):
2746            if data.dy != None:
2747                dy_val = data.dy[index]
2748            else:
2749                dy_val = 0.0
2750            if data.dx != None:
2751                dx_val = data.dx[index]
2752            else:
2753                dx_val = 0.0
2754            if data.dxl != None:
2755                if index == 0: 
2756                    x_st = "Xl"
2757                dx_val = data.dxl[index]
2758            elif data.dxw != None:
2759                if index == 0: 
2760                    x_st = "Xw"
2761                dx_val = data.dxw[index]
2762           
2763            if index == 0:
2764                text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n"% x_st
2765            text += "%s \t%s \t%s \t%s \t%s\n" % (index,
2766                                            data.x[index], 
2767                                            data.y[index],
2768                                            dy_val,
2769                                            dx_val)
2770        from pdfview import TextFrame
2771        frame = TextFrame(None, -1, "Data Info: %s"% data.name, text) 
2772        # put icon
2773        self.put_icon(frame) 
2774        frame.Show(True) 
2775           
2776    def save_data2d(self, data, fname):   
2777        """
2778        Save data2d dialog
2779        """
2780        default_name = fname
2781        wildcard = "IGOR/DAT 2D file in Q_map (*.dat)|*.DAT"
2782        dlg = wx.FileDialog(self, "Choose a file",
2783                            self._default_save_location,
2784                            default_name, wildcard , wx.SAVE)
2785       
2786        if dlg.ShowModal() == wx.ID_OK:
2787            path = dlg.GetPath()
2788            # ext_num = 0 for .txt, ext_num = 1 for .xml
2789            # This is MAC Fix
2790            ext_num = dlg.GetFilterIndex()
2791            if ext_num == 0:
2792                format = '.dat'
2793            else:
2794                format = ''
2795            path = os.path.splitext(path)[0] + format
2796            mypath = os.path.basename(path)
2797           
2798            #TODO: This is bad design. The DataLoader is designed
2799            #to recognize extensions.
2800            # It should be a simple matter of calling the .
2801            #save(file, data, '.xml') method
2802            # of the DataLoader.loader.Loader class.
2803            from sans.dataloader.loader import  Loader
2804            #Instantiate a loader
2805            loader = Loader() 
2806
2807            format = ".dat"
2808            if os.path.splitext(mypath)[1].lower() == format:
2809                # Make sure the ext included in the file name
2810                # especially on MAC
2811                fileName = os.path.splitext(path)[0] + format
2812                loader.save(fileName, data, format)
2813            try:
2814                self._default_save_location = os.path.dirname(path)
2815            except:
2816                pass   
2817        dlg.Destroy() 
2818                             
2819    def show_data2d(self, data, name):
2820        """
2821        Show data dialog
2822        """   
2823
2824        wx.PostEvent(self, StatusEvent(status = "Gathering Data2D Info.", 
2825                                       type = 'start' ))
2826        text = data.__str__() 
2827        text += 'Data Min Max:\n'
2828        text += 'I_min = %s\n'% min(data.data)
2829        text += 'I_max = %s\n\n'% max(data.data)
2830        text += 'Data (First 2501) Points:\n'
2831        text += 'Data columns include err(I).\n'
2832        text += 'ASCII data starts here.\n'
2833        text += "<index> \t<Qx> \t<Qy> \t<I> \t<dI> \t<dQparal> \t<dQperp>\n"
2834        di_val = 0.0
2835        dx_val = 0.0
2836        dy_val = 0.0
2837        #mask_val = True
2838        len_data = len(data.qx_data)
2839        for index in xrange(0, len_data):
2840            x_val = data.qx_data[index]
2841            y_val = data.qy_data[index]
2842            i_val = data.data[index]
2843            if data.err_data != None: 
2844                di_val = data.err_data[index]
2845            if data.dqx_data != None: 
2846                dx_val = data.dqx_data[index]
2847            if data.dqy_data != None: 
2848                dy_val = data.dqy_data[index]
2849            #if data.mask != None: mask_val = data.mask[index]
2850 
2851            text += "%s \t%s \t%s \t%s \t%s \t%s \t%s\n" % (index,
2852                                            x_val, 
2853                                            y_val,
2854                                            i_val,
2855                                            di_val,
2856                                            dx_val,
2857                                            dy_val)
2858            # Takes too long time for typical data2d: Break here
2859            if index >= 2500:
2860                text += ".............\n"
2861                break
2862
2863        from pdfview import TextFrame
2864        frame = TextFrame(None, -1, "Data Info: %s"% data.name, text) 
2865        # put icon
2866        self.put_icon(frame)
2867        frame.Show(True) 
2868        wx.PostEvent(self, StatusEvent(status = "Data2D Info Displayed", 
2869                                       type = 'stop' ))
2870                                 
2871    def set_current_perspective(self, perspective):
2872        """
2873        set the current active perspective
2874        """
2875        self._current_perspective = perspective
2876        name = "No current analysis selected"
2877        if self._current_perspective is not None:
2878            self._add_current_plugin_menu()
2879            for panel in self.panels.values():
2880                if hasattr(panel, 'CENTER_PANE') and panel.CENTER_PANE:
2881                    for name in self._current_perspective.get_perspective():
2882                        if name == panel.window_name:
2883                            panel.on_set_focus(event=None)
2884                            break               
2885            name = self._current_perspective.sub_menu
2886            if self._data_panel is not None:
2887                self._data_panel.set_active_perspective(name)
2888                self._check_applications_menu()
2889            #Set the SasView title
2890            self._set_title_name(name)
2891         
2892           
2893    def _set_title_name(self, name):
2894        """
2895        Set the SasView title w/ the current application name
2896       
2897        : param name: application name [string]
2898        """
2899        # Set SanView Window title w/ application anme
2900        title = self.title + "  - " + name + " -"
2901        self.SetTitle(title)
2902           
2903    def _check_applications_menu(self):
2904        """
2905        check the menu of the current application
2906        """
2907        if self._applications_menu is not None:
2908            for menu in self._applications_menu.GetMenuItems():
2909                if self._current_perspective is not None:
2910                    name = self._current_perspective.sub_menu
2911                    if menu.IsCheckable():
2912                        if menu.GetLabel() == name:
2913                            menu.Check(True)
2914                        else:
2915                            menu.Check(False) 
2916           
2917    def set_plotpanel_floating(self, event=None):
2918        """
2919        make the plot panel floatable
2920        """
2921       
2922        self.__gui_style &= (~GUIFRAME.FIXED_PANEL)
2923        self.__gui_style |= GUIFRAME.FLOATING_PANEL
2924        plot_panel = []
2925        id = event.GetId()
2926        menu = self._window_menu.FindItemById(id)
2927        if self._plotting_plugin is not None:
2928            plot_panel = self.plot_panels.values()
2929            for p in plot_panel:
2930                self._popup_floating_panel(p)
2931            menu.Check(True)
2932           
2933    def set_plotpanel_fixed(self, event=None):
2934        """
2935        make the plot panel fixed
2936        """
2937        self.__gui_style &= (~GUIFRAME.FLOATING_PANEL)
2938        self.__gui_style |= GUIFRAME.FIXED_PANEL
2939        plot_panel = []
2940        id = event.GetId()
2941        menu = self._window_menu.FindItemById(id)
2942        if self._plotting_plugin is not None:
2943            plot_panel = self.plot_panels.values()
2944            for p in plot_panel:
2945                self._popup_fixed_panel(p)
2946            menu.Check(True)
2947           
2948    def on_cleanup_dock(self, event=None):     
2949        """
2950        Set Cleanup Dock option
2951        """
2952        if event == None:
2953            return
2954        id = event.GetId()
2955        menu = self._window_menu.FindItemById(id)
2956        Flag = self.cleanup_plots
2957        if not Flag:
2958            menu.Check(True)
2959            self.cleanup_plots = True
2960            msg = "Cleanup-Dock option set to 'ON'."
2961        else:
2962            menu.Check(False)
2963            self.cleanup_plots = False
2964            msg = "Cleanup-Dock option set to 'OFF'."
2965
2966        wx.PostEvent(self, StatusEvent(status= msg))
2967         
2968    def _popup_fixed_panel(self, p):
2969        """
2970        """
2971        style = self.__gui_style & GUIFRAME.FIXED_PANEL
2972        if style == GUIFRAME.FIXED_PANEL:
2973            pane = self._mgr.GetPane(p.window_name)
2974            pane.Dock()
2975            pane.Floatable()
2976            pane.Right()
2977            pane.TopDockable(False)
2978            pane.BottomDockable(False)
2979            pane.LeftDockable(False)
2980            pane.RightDockable(True)
2981            self._mgr.Update()
2982           
2983    def _popup_floating_panel(self, p):
2984        """
2985        """
2986        style = self.__gui_style &  GUIFRAME.FLOATING_PANEL
2987        if style == GUIFRAME.FLOATING_PANEL: 
2988            pane = self._mgr.GetPane(p.window_name)
2989            pane.Floatable(True)
2990            pane.Float()
2991            pane.Dockable(False)
2992            self._mgr.Update()
2993           
2994    def enable_add_data(self, new_plot):
2995        """
2996        Enable append data on a plot panel
2997        """
2998
2999        if self.panel_on_focus not in self._plotting_plugin.plot_panels.values():
3000            return
3001        is_theory = len(self.panel_on_focus.plots) <= 1 and \
3002            self.panel_on_focus.plots.values()[0].__class__.__name__ == "Theory1D"
3003           
3004        is_data2d = hasattr(new_plot, 'data')
3005       
3006        is_data1d = self.panel_on_focus.__class__.__name__ == "ModelPanel1D"\
3007            and self.panel_on_focus.group_id is not None
3008        has_meta_data = hasattr(new_plot, 'meta_data')
3009       
3010        #disable_add_data if the data is being recovered from  a saved state file.
3011        is_state_data = False
3012        if has_meta_data:
3013            if 'invstate' in new_plot.meta_data: 
3014                is_state_data = True
3015            if  'prstate' in new_plot.meta_data: 
3016                is_state_data = True
3017            if  'fitstate' in new_plot.meta_data: 
3018                is_state_data = True
3019   
3020        return is_data1d and not is_data2d and not is_theory and not is_state_data
3021   
3022    def check_multimode(self, perspective=None):
3023        """
3024        Check the perspective have batch mode capablitity
3025        """
3026        if perspective == None or self._data_panel == None:
3027            return
3028        flag = perspective.get_batch_capable()
3029        flag_on = perspective.batch_on
3030        if flag:
3031            self._data_panel.rb_single_mode.SetValue(not flag_on)
3032            self._data_panel.rb_batch_mode.SetValue(flag_on)
3033        else:
3034            self._data_panel.rb_single_mode.SetValue(True)
3035            self._data_panel.rb_batch_mode.SetValue(False)
3036        self._data_panel.rb_single_mode.Enable(flag)
3037        self._data_panel.rb_batch_mode.Enable(flag)
3038               
3039
3040   
3041    def enable_edit_menu(self):
3042        """
3043        enable menu item under edit menu depending on the panel on focus
3044        """
3045        if self.cpanel_on_focus is not None and self._edit_menu is not None:
3046            flag = self.cpanel_on_focus.get_undo_flag()
3047            self._edit_menu.Enable(GUIFRAME_ID.UNDO_ID, flag)
3048            flag = self.cpanel_on_focus.get_redo_flag()
3049            self._edit_menu.Enable(GUIFRAME_ID.REDO_ID, flag)
3050            flag = self.cpanel_on_focus.get_copy_flag()
3051            self._edit_menu.Enable(GUIFRAME_ID.COPY_ID, flag)
3052            flag = self.cpanel_on_focus.get_paste_flag()
3053            self._edit_menu.Enable(GUIFRAME_ID.PASTE_ID, flag)
3054            #flag = self.cpanel_on_focus.get_print_flag()
3055            #self._edit_menu.Enable(GUIFRAME_ID.PRINT_ID, flag)
3056            flag = self.cpanel_on_focus.get_preview_flag()
3057            self._edit_menu.Enable(GUIFRAME_ID.PREVIEW_ID, flag)
3058            flag = self.cpanel_on_focus.get_reset_flag()
3059            self._edit_menu.Enable(GUIFRAME_ID.RESET_ID, flag)
3060        else:
3061            flag = False
3062            self._edit_menu.Enable(GUIFRAME_ID.UNDO_ID, flag)
3063            self._edit_menu.Enable(GUIFRAME_ID.REDO_ID, flag)
3064            self._edit_menu.Enable(GUIFRAME_ID.COPY_ID, flag)
3065            self._edit_menu.Enable(GUIFRAME_ID.PASTE_ID, flag)
3066            #self._edit_menu.Enable(GUIFRAME_ID.PRINT_ID, flag)
3067            self._edit_menu.Enable(GUIFRAME_ID.PREVIEW_ID, flag)
3068            self._edit_menu.Enable(GUIFRAME_ID.RESET_ID, flag)
3069           
3070    def on_undo_panel(self, event=None):
3071        """
3072        undo previous action of the last panel on focus if possible
3073        """
3074        if self.cpanel_on_focus is not None:
3075            self.cpanel_on_focus.on_undo(event)
3076           
3077    def on_redo_panel(self, event=None):
3078        """
3079        redo the last cancel action done on the last panel on focus
3080        """
3081        if self.cpanel_on_focus is not None:
3082            self.cpanel_on_focus.on_redo(event)
3083           
3084    def on_copy_panel(self, event=None):
3085        """
3086        copy the last panel on focus if possible
3087        """
3088        if self.cpanel_on_focus is not None:
3089            self.cpanel_on_focus.on_copy(event)
3090           
3091    def on_paste_panel(self, event=None):
3092        """
3093        paste clipboard to the last panel on focus
3094        """
3095        if self.cpanel_on_focus is not None:
3096            self.cpanel_on_focus.on_paste(event)
3097                   
3098    def on_bookmark_panel(self, event=None):
3099        """
3100        bookmark panel
3101        """
3102        if self.cpanel_on_focus is not None:
3103            self.cpanel_on_focus.on_bookmark(event)
3104           
3105    def append_bookmark(self, event=None):
3106        """
3107        Bookmark available information of the panel on focus
3108        """
3109        self._toolbar.append_bookmark(event)
3110           
3111    def on_save_panel(self, event=None):
3112        """
3113        save possible information on the current panel
3114        """
3115        if self.cpanel_on_focus is not None:
3116            self.cpanel_on_focus.on_save(event)
3117           
3118    def on_preview_panel(self, event=None):
3119        """
3120        preview information on the panel on focus
3121        """
3122        if self.cpanel_on_focus is not None:
3123            self.cpanel_on_focus.on_preview(event)
3124           
3125    def on_print_panel(self, event=None):
3126        """
3127        print available information on the last panel on focus
3128        """
3129        if self.cpanel_on_focus is not None:
3130            self.cpanel_on_focus.on_print(event)
3131           
3132    def on_zoom_panel(self, event=None):
3133        """
3134        zoom on the current panel if possible
3135        """
3136        if self.cpanel_on_focus is not None:
3137            self.cpanel_on_focus.on_zoom(event)
3138           
3139    def on_zoom_in_panel(self, event=None):
3140        """
3141        zoom in of the panel on focus
3142        """
3143        if self.cpanel_on_focus is not None:
3144            self.cpanel_on_focus.on_zoom_in(event)
3145           
3146    def on_zoom_out_panel(self, event=None):
3147        """
3148        zoom out on the panel on focus
3149        """
3150        if self.cpanel_on_focus is not None:
3151            self.cpanel_on_focus.on_zoom_out(event)
3152           
3153    def on_drag_panel(self, event=None):
3154        """
3155        drag apply to the panel on focus
3156        """
3157        if self.cpanel_on_focus is not None:
3158            self.cpanel_on_focus.on_drag(event)
3159           
3160    def on_reset_panel(self, event=None):
3161        """
3162        reset the current panel
3163        """
3164        if self.cpanel_on_focus is not None:
3165            self.cpanel_on_focus.on_reset(event)
3166           
3167    def on_change_caption(self, name, old_caption, new_caption):     
3168        """
3169        Change the panel caption
3170       
3171        :param name: window_name of the pane
3172        :param old_caption: current caption [string]
3173        :param new_caption: new caption [string]
3174        """
3175        # wx.aui.AuiPaneInfo
3176        pane_info = self.get_paneinfo(name) 
3177        # update the data_panel.cb_plotpanel
3178        if 'data_panel' in self.panels.keys():
3179            # remove from data_panel combobox
3180            data_panel = self.panels["data_panel"]
3181            if data_panel.cb_plotpanel is not None:
3182                # Check if any panel has the same caption
3183                has_newstring = data_panel.cb_plotpanel.FindString\
3184                                                            (str(new_caption)) 
3185                caption = new_caption
3186                if has_newstring != wx.NOT_FOUND:
3187                    captions = self._get_plotpanel_captions()
3188                    # Append nummber
3189                    inc = 1
3190                    while (1):
3191                        caption = new_caption + '_%s'% str(inc)
3192                        if caption not in captions:
3193                            break
3194                        inc += 1
3195                    # notify to users
3196                    msg = "Found Same Title: Added '_%s'"% str(inc)
3197                    wx.PostEvent(self, StatusEvent(status=msg))
3198                # update data_panel cb
3199                pos = data_panel.cb_plotpanel.FindString(str(old_caption)) 
3200                if pos != wx.NOT_FOUND:
3201                    data_panel.cb_plotpanel.SetString(pos, caption)
3202                    data_panel.cb_plotpanel.SetStringSelection(caption)
3203        # update window Show menu
3204        if self._window_menu != None:
3205            for item in self._window_menu.GetMenuItems():
3206                pos = self._window_menu.FindItem(old_caption)
3207                if self._window_menu.GetLabel(pos) == str(old_caption):
3208                    self._window_menu.SetLabel(pos, caption)
3209                break
3210        # New Caption
3211        pane_info.Caption(caption)
3212        # update aui manager
3213        self._mgr.Update()
3214        return caption
3215       
3216    def get_paneinfo(self, name):
3217        """
3218        Get pane Caption from window_name
3219       
3220        :param name: window_name in AuiPaneInfo
3221        : return: AuiPaneInfo of the name
3222        """
3223        return self._mgr.GetPane(name) 
3224   
3225    def enable_undo(self):
3226        """
3227        enable undo related control
3228        """
3229        if self.cpanel_on_focus is not None:
3230            self._toolbar.enable_undo(self.cpanel_on_focus)
3231           
3232    def enable_redo(self):
3233        """
3234        enable redo
3235        """
3236        if self.cpanel_on_focus is not None:
3237            self._toolbar.enable_redo(self.cpanel_on_focus)
3238           
3239    def enable_copy(self):
3240        """
3241        enable copy related control
3242        """
3243        if self.cpanel_on_focus is not None:
3244            self._toolbar.enable_copy(self.cpanel_on_focus)
3245           
3246    def enable_paste(self):
3247        """
3248        enable paste
3249        """
3250        if self.cpanel_on_focus is not None:
3251            self._toolbar.enable_paste(self.cpanel_on_focus)
3252                       
3253    def enable_bookmark(self):
3254        """
3255        Bookmark
3256        """
3257        if self.cpanel_on_focus is not None:
3258            self._toolbar.enable_bookmark(self.cpanel_on_focus)
3259           
3260    def enable_save(self):
3261        """
3262        save
3263        """
3264        if self.cpanel_on_focus is not None:
3265            self._toolbar.enable_save(self.cpanel_on_focus)
3266           
3267    def enable_preview(self):
3268        """
3269        preview
3270        """
3271        if self.cpanel_on_focus is not None:
3272            self._toolbar.enable_preview(self.cpanel_on_focus)
3273           
3274    def enable_print(self):
3275        """
3276        print
3277        """
3278        if self.cpanel_on_focus is not None:
3279            self._toolbar.enable_print(self.cpanel_on_focus)
3280           
3281    def enable_zoom(self):
3282        """
3283        zoom
3284        """
3285        if self.cpanel_on_focus is not None:
3286            self._toolbar.enable_zoom(self.panel_on_focus)
3287           
3288    def enable_zoom_in(self):
3289        """
3290        zoom in
3291        """
3292        if self.cpanel_on_focus is not None:
3293            self._toolbar.enable_zoom_in(self.panel_on_focus)
3294           
3295    def enable_zoom_out(self):
3296        """
3297        zoom out
3298        """
3299        if self.cpanel_on_focus is not None:
3300            self._toolbar.enable_zoom_out(self.panel_on_focus)
3301           
3302    def enable_drag(self, event=None):
3303        """
3304        drag
3305        """
3306        #if self.cpanel_on_focus is not None:
3307        #    self._toolbar.enable_drag(self.panel_on_focus)
3308           
3309    def enable_reset(self):
3310        """
3311        reset the current panel
3312        """
3313        if self.cpanel_on_focus is not None:
3314            self._toolbar.enable_reset(self.panel_on_focus)
3315
3316    def set_schedule_full_draw(self, panel=None, func='del'):
3317        """
3318        Add/subtract the schedule full draw list with the panel given
3319       
3320        :param panel: plot panel
3321        :param func: append or del [string]
3322        """
3323
3324        # append this panel in the schedule list if not in yet
3325        if func == 'append':
3326            if not panel in self.schedule_full_draw_list:
3327                self.schedule_full_draw_list.append(panel) 
3328        # remove this panel from schedule list
3329        elif func == 'del':
3330            if len(self.schedule_full_draw_list) > 0:
3331                if panel in self.schedule_full_draw_list:
3332                    self.schedule_full_draw_list.remove(panel)
3333
3334        # reset the schdule
3335        if len(self.schedule_full_draw_list) == 0:
3336            self.schedule = False
3337        else:
3338            self.schedule = True   
3339       
3340    def full_draw(self):
3341        """
3342        Draw the panels with axes in the schedule to full dwar list
3343        """
3344        count = len(self.schedule_full_draw_list)
3345        #if not self.schedule:
3346        if count < 1:
3347            self.set_schedule(False)
3348            return
3349        else:
3350            ind = 0
3351            # if any of the panel is shown do full_draw
3352            for panel in self.schedule_full_draw_list:
3353                ind += 1
3354                if self._mgr.GetPane(panel.window_name).IsShown():
3355                    break
3356                # otherwise, return
3357                if ind == count:
3358                    return
3359
3360        #Simple redraw only for a panel shown
3361        def f_draw(panel):
3362            """
3363            Draw A panel in the full dwar list
3364            """
3365            try:
3366                # This checking of GetCapture is to stop redrawing
3367                # while any panel is capture.
3368                if self.GetCapture() == None:
3369                    # draw if possible
3370                    panel.set_resizing(False)
3371                    panel.Show(False)
3372                    panel.draw_plot()
3373                   
3374                    # Check if the panel is not shown
3375                    if not self._mgr.GetPane(panel.window_name).IsShown():
3376                        self._mgr.GetPane(panel.window_name).Hide()
3377                    else:
3378                        panel.Show(True)
3379            except:
3380                pass
3381       
3382        # Draw all panels       
3383        map(f_draw, self.schedule_full_draw_list)
3384       
3385        # Reset the attr 
3386        if len(self.schedule_full_draw_list) == 0:
3387            self.set_schedule(False)
3388        else:
3389            self.set_schedule(True)
3390        # do not update mgr
3391        #self._mgr.Update()
3392       
3393    def set_schedule(self, schedule=False): 
3394        """
3395        Set schedule
3396        """
3397        self.schedule = schedule
3398               
3399    def get_schedule(self): 
3400        """
3401        Get schedule
3402        """
3403        return self.schedule
3404   
3405    def on_set_plot_focus(self, panel):
3406        """
3407        Set focus on a plot panel
3408        """
3409        if panel == None:
3410            return
3411        self.set_plot_unfocus()
3412        panel.on_set_focus(None) 
3413        # set focusing panel
3414        self.panel_on_focus = panel 
3415        self.set_panel_on_focus(None)
3416
3417    def set_plot_unfocus(self): 
3418        """
3419        Un focus all plot panels
3420        """
3421        for plot in self.plot_panels.values():
3422            plot.on_kill_focus(None)
3423   
3424    def get_window_size(self):
3425        """
3426        Get window size
3427       
3428        :return size: tuple
3429        """
3430        width, height = self.GetSizeTuple()
3431        if not IS_WIN:
3432            # Subtract toolbar height to get real window side
3433            if self._toolbar.IsShown():
3434                height -= 45
3435        return (width, height)
3436           
3437    def _onDrawIdle(self, *args, **kwargs):
3438        """
3439        ReDraw with axes
3440        """
3441        try:
3442            # check if it is time to redraw
3443            if self.GetCapture() == None:
3444                # Draw plot, changes resizing too
3445                self.full_draw()
3446        except:
3447            pass
3448           
3449        # restart idle       
3450        self._redraw_idle(*args, **kwargs)
3451
3452           
3453    def _redraw_idle(self, *args, **kwargs):
3454        """
3455        Restart Idle
3456        """
3457        # restart idle   
3458        self.idletimer.Restart(100*TIME_FACTOR, *args, **kwargs)
3459
3460       
3461class DefaultPanel(wx.Panel, PanelBase):
3462    """
3463    Defines the API for a panels to work with
3464    the GUI manager
3465    """
3466    ## Internal nickname for the window, used by the AUI manager
3467    window_name = "default"
3468    ## Name to appear on the window title bar
3469    window_caption = "Welcome panel"
3470    ## Flag to tell the AUI manager to put this panel in the center pane
3471    CENTER_PANE = True
3472    def __init__(self, parent, *args, **kwds):
3473        wx.Panel.__init__(self, parent, *args, **kwds)
3474        PanelBase.__init__(self, parent)
3475   
3476
3477
3478class ViewApp(wx.App):
3479    """
3480    Toy application to test this Frame
3481    """
3482    def OnInit(self):
3483        """
3484        When initialised
3485        """
3486        pos, size, self.is_max = self.window_placement((GUIFRAME_WIDTH, 
3487                                           GUIFRAME_HEIGHT))     
3488        self.frame = ViewerFrame(parent=None, 
3489                             title=APPLICATION_NAME, 
3490                             pos=pos, 
3491                             gui_style = DEFAULT_STYLE,
3492                             size=size)
3493        self.frame.Hide()
3494        self.s_screen = None
3495
3496        try:
3497            self.open_file()
3498        except:
3499            msg = "%s Could not load " % str(APPLICATION_NAME)
3500            msg += "input file from command line.\n"
3501            logging.error(msg)
3502        # Display a splash screen on top of the frame.
3503        try:
3504            if os.path.isfile(SPLASH_SCREEN_PATH):
3505                self.s_screen = self.display_splash_screen(parent=self.frame, 
3506                                        path=SPLASH_SCREEN_PATH)
3507            else:
3508                self.frame.Show()   
3509        except:
3510            if self.s_screen is not None:
3511                self.s_screen.Close()
3512            msg = "Cannot display splash screen\n"
3513            msg += str (sys.exc_value)
3514            logging.error(msg)
3515            self.frame.Show()
3516 
3517        #if hasattr(self.frame, 'special'):
3518        #    self.frame.special.SetCurrent()
3519        self.SetTopWindow(self.frame)
3520 
3521        return True
3522   
3523    def maximize_win(self):
3524        """
3525        Maximize the window after the frame shown
3526        """
3527        if self.is_max:
3528            if self.frame.IsShown():
3529                # Max window size
3530                self.frame.Maximize(self.is_max)
3531
3532    def open_file(self):
3533        """
3534        open a state file at the start of the application
3535        """
3536        input_file = None
3537        if len(sys.argv) >= 2:
3538            cmd = sys.argv[0].lower()
3539            basename  = os.path.basename(cmd)
3540            app_base = str(APPLICATION_NAME).lower()
3541            if os.path.isfile(cmd) or basename.lower() == app_base:
3542                app_py = app_base + '.py'
3543                app_exe = app_base + '.exe'
3544                app_app = app_base + '.app'
3545                if basename.lower() in [app_py, app_exe, app_app, app_base]:
3546                    data_base = sys.argv[1]
3547                    input_file = os.path.normpath(os.path.join(DATAPATH, 
3548                                                               data_base))
3549        if input_file is None:
3550            return
3551        if self.frame is not None:
3552            self.frame.set_input_file(input_file=input_file)
3553         
3554    def clean_plugin_models(self, path): 
3555        """
3556        Delete plugin models  in app folder
3557       
3558        :param path: path of the plugin_models folder in app
3559        """
3560        # do it only the first time app loaded
3561        # delete unused model folder   
3562        model_folder = os.path.join(PATH_APP, path)
3563        if os.path.exists(model_folder) and os.path.isdir(model_folder):
3564            if len(os.listdir(model_folder)) > 0:
3565                try:
3566                    for file in os.listdir(model_folder):
3567                        file_path = os.path.join(model_folder, file)
3568                        if os.path.isfile(file_path):
3569                            os.remove(file_path)
3570                except:
3571                    logging.error("gui_manager.clean_plugin_models:\n  %s" \
3572                                  % sys.exc_value)
3573             
3574    def set_manager(self, manager):
3575        """
3576        Sets a reference to the application manager
3577        of the GUI manager (Frame)
3578        """
3579        self.frame.set_manager(manager)
3580       
3581    def build_gui(self):
3582        """
3583        Build the GUI
3584        """
3585        #try to load file at the start
3586        try:
3587            self.open_file()
3588        except:
3589            raise
3590        self.frame.build_gui()
3591        #if self.s_screen is not None and self.s_screen.IsShown():
3592        #    self.s_screen.Close()
3593       
3594    def set_welcome_panel(self, panel_class):
3595        """
3596        Set the welcome panel
3597       
3598        :param panel_class: class of the welcome panel to be instantiated
3599       
3600        """
3601        self.frame.set_welcome_panel(panel_class)
3602       
3603    def add_perspective(self, perspective):
3604        """
3605        Manually add a perspective to the application GUI
3606        """
3607        self.frame.add_perspective(perspective)
3608   
3609    def window_placement(self, size):
3610        """
3611        Determines the position and size of the application frame such that it
3612        fits on the user's screen without obstructing (or being obstructed by)
3613        the Windows task bar.  The maximum initial size in pixels is bounded by
3614        WIDTH x HEIGHT.  For most monitors, the application
3615        will be centered on the screen; for very large monitors it will be
3616        placed on the left side of the screen.
3617        """
3618        is_maximized = False
3619        # Get size of screen without
3620        for screenCount in range(wx.Display().GetCount()):
3621            screen = wx.Display(screenCount)
3622            if screen.IsPrimary():
3623                displayRect = screen.GetClientArea()
3624                break
3625       
3626        posX, posY, displayWidth, displayHeight = displayRect       
3627        customWidth, customHeight = size
3628       
3629        # If the custom size is default, set 90% of the screen size
3630        if customWidth <= 0 and customHeight <= 0:
3631            if customWidth == 0 and customHeight == 0:
3632                is_maximized = True
3633            customWidth = displayWidth * 0.9
3634            customHeight = displayHeight * 0.9
3635        else:
3636            # If the custom screen is bigger than the
3637            # window screen than make maximum size
3638            if customWidth > displayWidth:
3639                customWidth = displayWidth
3640            if customHeight > displayHeight:
3641                customHeight = displayHeight
3642           
3643        # Note that when running Linux and using an Xming (X11) server on a PC
3644        # with a dual  monitor configuration, the reported display size may be
3645        # that of both monitors combined with an incorrect display count of 1.
3646        # To avoid displaying this app across both monitors, we check for
3647        # screen 'too big'.  If so, we assume a smaller width which means the
3648        # application will be placed towards the left hand side of the screen.
3649       
3650        # If dual screen registered as 1 screen. Make width half.
3651        # MAC just follows the default behavior of pos
3652        if IS_WIN:
3653            if displayWidth > (displayHeight*2):
3654                if (customWidth == displayWidth):
3655                    customWidth = displayWidth/2
3656                # and set the position to be the corner of the screen.
3657                posX = 0
3658                posY = 0
3659               
3660            # Make the position the middle of the screen. (Not 0,0)
3661            else:
3662                posX = (displayWidth - customWidth)/2
3663                posY = (displayHeight - customHeight)/2
3664       
3665        # Return the suggested position and size for the application frame.   
3666        return (posX, posY), (customWidth, customHeight), is_maximized
3667
3668   
3669    def display_splash_screen(self, parent, 
3670                              path=SPLASH_SCREEN_PATH):
3671        """Displays the splash screen.  It will exactly cover the main frame."""
3672       
3673        # Prepare the picture.  On a 2GHz intel cpu, this takes about a second.
3674        image = wx.Image(path, wx.BITMAP_TYPE_PNG)
3675        image.Rescale(SPLASH_SCREEN_WIDTH, 
3676                      SPLASH_SCREEN_HEIGHT, wx.IMAGE_QUALITY_HIGH)
3677        bm = image.ConvertToBitmap()
3678
3679        # Create and show the splash screen.  It will disappear only when the
3680        # program has entered the event loop AND either the timeout has expired
3681        # or the user has left clicked on the screen.  Thus any processing
3682        # performed in this routine (including sleeping) or processing in the
3683        # calling routine (including doing imports) will prevent the splash
3684        # screen from disappearing.
3685        #
3686        # Note that on Linux, the timeout appears to occur immediately in which
3687        # case the splash screen disappears upon entering the event loop.
3688        s_screen = wx.SplashScreen(bitmap=bm,
3689                         splashStyle=(wx.SPLASH_TIMEOUT|
3690                                              wx.SPLASH_CENTRE_ON_SCREEN),
3691                                 style=(wx.SIMPLE_BORDER|
3692                                        wx.FRAME_NO_TASKBAR|
3693                                        wx.STAY_ON_TOP),
3694                                       
3695                        milliseconds=SS_MAX_DISPLAY_TIME,
3696                        parent=parent,
3697                        id=wx.ID_ANY)
3698        from sans.guiframe.gui_statusbar import SPageStatusbar
3699        statusBar = SPageStatusbar(s_screen)
3700        s_screen.SetStatusBar(statusBar)
3701        s_screen.Bind(wx.EVT_CLOSE, self.on_close_splash_screen)
3702        s_screen.Show()
3703        return s_screen
3704       
3705       
3706    def on_close_splash_screen(self, event):
3707        """
3708        When the splash screen is closed.
3709        """
3710        self.frame.Show(True)
3711        event.Skip()
3712        self.maximize_win()
3713
3714if __name__ == "__main__": 
3715    app = ViewApp(0)
3716    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.