source: sasview/src/sas/guiframe/gui_manager.py @ 225aca8

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 225aca8 was 225aca8, checked in by mathieu, 9 years ago

Merge pull request #3 from SasView?/standalone_cleanup

Standalone cleanup

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