예제 #1
0
    def on_open(self, event=None):
        '''Raise a dialog for opening a gui project from file.'''

        if self.gui_project is not None:

            if hasattr(sys, 'frozen'):
                program_to_run = [sys.executable]
                we_are_main_program = 'GarlicSim' in sys.executable
            else:
                main_script = os.path.abspath(sys.argv[0])
                program_to_run = [sys.executable, main_script]
                we_are_main_program = ('run_gui' in main_script) or \
                                    ('garlicsim_wx' in main_script) or \
                                    ('GarlicSim' in main_script)

            if not we_are_main_program:
                dialog = \
                    garlicsim_wx.widgets.misc.NotMainProgramWarningDialog(self)
                try:
                    if dialog.ShowModal() != wx.ID_YES:
                        return
                finally:
                    dialog.Destroy()

        # Todo: something more sensible here. Ideally should be last place you
        # saved in, but for starters can be desktop.
        folder = os.getcwd()

        gui_project_vars = None

        open_dialog = wx.FileDialog(self,
                                    message='Choose a file',
                                    defaultDir=folder,
                                    defaultFile='',
                                    wildcard=wildcard_text,
                                    style=wx.OPEN)
        try:
            if open_dialog.ShowModal() == wx.ID_OK:
                path = open_dialog.GetPath()

                if self.gui_project is None:
                    self._open_gui_project_from_path(path)
                else:
                    if hasattr(sys, 'frozen'):
                        program = [sys.executable]
                    else:
                        program = \
                            [sys.executable, os.path.abspath(sys.argv[0])]

                    program.append(path)

                    for simpack_place in garlicsim_wx.simpack_places:
                        program.append('__garlicsim_wx_simpack_place=%s' % \
                                       ','.join(simpack_place))

                    with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
                        subprocess.Popen(program)
        finally:
            open_dialog.Destroy()
예제 #2
0
 def _new_gui_project_from_simpack(self, simpack):
     '''
     Start a new gui project, given the simpack to start it with.
     
     Internal use.
     '''
     assert self.gui_project is None
     with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
         gui_project = GuiProject(simpack, self)
         self.__setup_gui_project(gui_project)
예제 #3
0
    def on_new(self, event=None):
        '''Create a new gui project.'''

        if self.gui_project is not None:

            if hasattr(sys, 'frozen'):
                program_to_run = [sys.executable]
                we_are_main_program = 'GarlicSim' in sys.executable
            else:
                main_script = os.path.abspath(sys.argv[0])
                program_to_run = [sys.executable, main_script]
                we_are_main_program = ('run_gui' in main_script) or \
                                    ('garlicsim_wx' in main_script) or \
                                    ('GarlicSim' in main_script)

            if not we_are_main_program:
                warning_dialog = \
                    garlicsim_wx.widgets.misc.NotMainProgramWarningDialog(self)
                try:
                    if warning_dialog.ShowModal() != wx.ID_YES:
                        return
                finally:
                    warning_dialog.Destroy()

        dialog = garlicsim_wx.widgets.misc.SimpackSelectionDialog(self)

        try:
            if dialog.ShowModal() == wx.ID_OK:
                simpack = dialog.get_simpack_selection()
            else:
                dialog.Destroy()
                return
        finally:
            dialog.Destroy()

        if self.gui_project is None:
            self._new_gui_project_from_simpack(simpack)
        else:

            program_to_run.append('__garlicsim_wx_new=%s' % simpack.__name__)
            for simpack_place in garlicsim_wx.simpack_places:
                program_to_run.append('__garlicsim_wx_simpack_place=%s' % \
                                      ','.join(simpack_place))

            with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
                subprocess.Popen(program_to_run)

            return
예제 #4
0
 def open_editing_dialog(self):
     '''Open a dialog to edit the hue.'''
     old_hue = self.getter()
     
     with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
         hue_selection_dialog = HueSelectionDialog(
             self.GetTopLevelParent(), self.getter, self.setter,
             self.emitter,
             lightness=self.lightness, saturation=self.saturation,
             title=self.dialog_title
         )
     
     try:
         hue_selection_dialog.ShowModal()
     finally:
         hue_selection_dialog.Destroy()
예제 #5
0
    def on_save(self, event=None):
        '''Raise a dialog for saving a gui project to file.'''

        assert self.gui_project is not None

        folder = os.getcwd()

        save_dialog = wx.FileDialog(self,
                                    message='Save file as...',
                                    defaultDir=folder,
                                    defaultFile='',
                                    wildcard=wildcard_text,
                                    style=wx.SAVE | wx.OVERWRITE_PROMPT)
        try:
            if save_dialog.ShowModal() == wx.ID_OK:
                path = save_dialog.GetPath()

                # Adding extension if got a plain file because wxPython doesn't
                # give the checkbox that's supposed to do it on Mac:
                path = misc_tools.add_extension_if_plain(path, '.gssp')

                with TempRecursionLimitSetter(10000):
                    try:
                        with open(path, 'wb') as my_file:
                            with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
                                with self.gui_project.project.tree.lock.read:
                                    pickler = misc.pickling.Pickler(
                                        my_file,
                                        protocol=2,
                                    )
                                    pickler.dump(self.gui_project)

                    except Exception, exception:
                        error_dialog = wx.MessageDialog(
                            self,
                            'Error saving to file:\n' + traceback.format_exc(),
                            style=(wx.OK | wx.ICON_ERROR))
                        error_dialog.ShowModal()
                        error_dialog.Destroy()

        finally:
            save_dialog.Destroy()
예제 #6
0
    def _open_gui_project_from_path(self, path):
        '''
        Open a gui project saved to a file specified by `path`.
        
        Internal use.
        '''

        with TempRecursionLimitSetter(10000):
            try:
                with open(path, 'rb') as my_file:
                    with wx_tools.CursorChanger(self, wx.CURSOR_WAIT):
                        unpickler = misc.pickling.Unpickler(my_file)
                        gui_project = unpickler.load()

            except Exception, exception:
                dialog = wx.MessageDialog(self,
                                          'Error opening file:\n' +
                                          traceback.format_exc(),
                                          style=(wx.OK | wx.ICON_ERROR))
                try:
                    dialog.ShowModal()
                finally:
                    dialog.Destroy()
                return