Esempio n. 1
0
    def __init__(self):
#        print >>sys.stderr, "creating main window"
        Window.__init__(self, statusbar=True, size=(800, 600))
        self.title = 'Grafit'

        # for example
        self.main = MainPanel(self)

        self.shell = ScriptWindow(self.main.bottom_panel,
                                  page_label='console', page_pixmap='console.png')
        self.shell.locals['mainwin'] = self

        try:
            self.shell._widget.history = settings.get('script', 'history').split('\n')
        except:
            self.shell._widget.history = []
            pass
        self.explorer = ProjectExplorer(self.main.left_panel,
                                        page_label='project', page_pixmap='stock_navigator.png')
        self.actionlist = ActionList(action_list, self.main.left_panel,
                                        page_label='actions', page_pixmap='stock_undo.png')
        self.explorer.connect('item-activated', self.on_item_activated)

        self.project = Project()
        self.open_project(self.project)

        self.book = Notebook(self.main)
#        self.book.connect('page-changed', self.on_page_changed)

        self.main.left_panel.open(self.explorer)

        global_connect('status-message', self.on_status_message)
        action_list.connect('added', self.on_action)
        action_list.connect('removed', self.on_action)
        action_list.connect('modified', self.on_action)

        self.actions = actions = {
            'file-new': Command('New', 'Create a new project', self.on_project_new, 'new.png', 'Ctrl+N'),
            'file-open': Command('Open...', 'Open a project', self.on_project_open, 'open.png', 'Ctrl+O'),
            'file-save': Command('Save', 'Save the project', 
                                self.on_project_save, 'save.png', 'Ctrl+S'),
            'file-saveas': Command('Save As...', 'Save the project with a new name', 
                                  self.on_project_saveas, 'saveas.png'),
            'file-quit': Command('Quit', 'Quit grafit', self.on_quit, 'stock_exit.png', 'Ctrl+Q'),

            'edit-undo': Command('Undo', 'Undo the last action', undo, 'stock_undo.png', 'Ctrl+Z'),
            'edit-redo': Command('Redo', 'Redo the last action', redo, 'stock_redo.png', 'Shift+Ctrl+Z'),
            'edit-copy': Command('Copy', 'Undo the last action', object, 'stock_copy.png', 'Ctrl+C'),
            'edit-cut': Command('Cut', 'Undo the last action', object, 'stock_cut.png', 'Ctrl+X'),
            'edit-paste': Command('Paste', 'Undo the last action', 'stock_paste.png', None, 'Ctrl+V'),
            'edit-delete': Command('Delete', 'Undo the last action', 'stock_delete.png', None),

            'import-ascii': Command('Import ASCII...', 'Import and ASCII file', 
                                   self.on_import_ascii, 'import_ascii.png', 'Ctrl+I'),
            'object-new-worksheet': Command('New Worksheet', 'Create a new worksheet', 
                                           self.on_new_worksheet, 'new-worksheet.png'),
            'object-new-graph': Command('New Graph', 'Create a new worksheet', 
                                       self.on_new_graph, 'new-graph.png'),
            'object-new-folder': Command('New Folder', 'Create a new worksheet', 
                                        self.on_new_folder, 'new-folder.png'),
            'functions': Command('Functions...', '', object),
            'filters': Command('Filters...', '', object),
            'scripts': Command('Scripts...', '', object),
            'run-script': Command('Run script...', '', self.on_run_script),
            'integrate': Command('Integrate', '', self.on_integrate),
            'close-active-page': Command('Close', 'Close this worksheet',
                                         lambda: self.book.active_page.on_close(), 'close.png', 'Ctrl+W'),
            None: None
        }

        self.menubar = Menubar(self)
        for title, items in [
            ('&File', ['file-new', 'file-open', None, 
                       'file-save', 'file-saveas', None, 
                       'file-quit']),
            ('&Edit', ['edit-undo', 'edit-redo', None, 
                       'edit-cut', 'edit-copy', 'edit-paste', None, 'edit-delete']),
            ('&Tools', ['functions', 'filters', 'scripts', None, 'run-script']),
            ('&Analysis', ['integrate']),
            ('&Help', []),
        ]:
            menu = Menu(self.menubar, title)
            for item in items:
                menu.append(actions[item])

        self.toolbar = Toolbar(self)
        for item in [
            'file-new', 'file-open', 'file-save', 'file-saveas', None,
            'object-new-folder', 'object-new-worksheet', 'object-new-graph', None,
            'edit-undo', 'edit-redo', None,
            'import-ascii', None,
            'close-active-page',
        ]:
            self.toolbar.append(actions[item])
        self.toolbar._widget.Realize()
        
        self.connect('close', self.on_quit)

        self.main.bottom_panel._widget.toolbar.Realize()
        self.main.left_panel._widget.toolbar.Realize()
        self.main.right_panel._widget.toolbar.Realize()

        # preload
        wx.xrc.XmlResource(os.path.join(DATADIR, 'data', 'resources.xrc'))

        self.on_action()
        self.on_project_modified(False)

        if len(self.args) > 0:
            name = self.args[0]
            if ':' in name:
                prj, obj = name.split(':')
                self.open_project(Project(prj))
                self.on_item_activated(self.project.top[obj])
            else:
                self.open_project(Project(name))
Esempio n. 2
0
class MainWindow(Window):
    def __init__(self):
#        print >>sys.stderr, "creating main window"
        Window.__init__(self, statusbar=True, size=(800, 600))
        self.title = 'Grafit'

        # for example
        self.main = MainPanel(self)

        self.shell = ScriptWindow(self.main.bottom_panel,
                                  page_label='console', page_pixmap='console.png')
        self.shell.locals['mainwin'] = self

        try:
            self.shell._widget.history = settings.get('script', 'history').split('\n')
        except:
            self.shell._widget.history = []
            pass
        self.explorer = ProjectExplorer(self.main.left_panel,
                                        page_label='project', page_pixmap='stock_navigator.png')
        self.actionlist = ActionList(action_list, self.main.left_panel,
                                        page_label='actions', page_pixmap='stock_undo.png')
        self.explorer.connect('item-activated', self.on_item_activated)

        self.project = Project()
        self.open_project(self.project)

        self.book = Notebook(self.main)
#        self.book.connect('page-changed', self.on_page_changed)

        self.main.left_panel.open(self.explorer)

        global_connect('status-message', self.on_status_message)
        action_list.connect('added', self.on_action)
        action_list.connect('removed', self.on_action)
        action_list.connect('modified', self.on_action)

        self.actions = actions = {
            'file-new': Command('New', 'Create a new project', self.on_project_new, 'new.png', 'Ctrl+N'),
            'file-open': Command('Open...', 'Open a project', self.on_project_open, 'open.png', 'Ctrl+O'),
            'file-save': Command('Save', 'Save the project', 
                                self.on_project_save, 'save.png', 'Ctrl+S'),
            'file-saveas': Command('Save As...', 'Save the project with a new name', 
                                  self.on_project_saveas, 'saveas.png'),
            'file-quit': Command('Quit', 'Quit grafit', self.on_quit, 'stock_exit.png', 'Ctrl+Q'),

            'edit-undo': Command('Undo', 'Undo the last action', undo, 'stock_undo.png', 'Ctrl+Z'),
            'edit-redo': Command('Redo', 'Redo the last action', redo, 'stock_redo.png', 'Shift+Ctrl+Z'),
            'edit-copy': Command('Copy', 'Undo the last action', object, 'stock_copy.png', 'Ctrl+C'),
            'edit-cut': Command('Cut', 'Undo the last action', object, 'stock_cut.png', 'Ctrl+X'),
            'edit-paste': Command('Paste', 'Undo the last action', 'stock_paste.png', None, 'Ctrl+V'),
            'edit-delete': Command('Delete', 'Undo the last action', 'stock_delete.png', None),

            'import-ascii': Command('Import ASCII...', 'Import and ASCII file', 
                                   self.on_import_ascii, 'import_ascii.png', 'Ctrl+I'),
            'object-new-worksheet': Command('New Worksheet', 'Create a new worksheet', 
                                           self.on_new_worksheet, 'new-worksheet.png'),
            'object-new-graph': Command('New Graph', 'Create a new worksheet', 
                                       self.on_new_graph, 'new-graph.png'),
            'object-new-folder': Command('New Folder', 'Create a new worksheet', 
                                        self.on_new_folder, 'new-folder.png'),
            'functions': Command('Functions...', '', object),
            'filters': Command('Filters...', '', object),
            'scripts': Command('Scripts...', '', object),
            'run-script': Command('Run script...', '', self.on_run_script),
            'integrate': Command('Integrate', '', self.on_integrate),
            'close-active-page': Command('Close', 'Close this worksheet',
                                         lambda: self.book.active_page.on_close(), 'close.png', 'Ctrl+W'),
            None: None
        }

        self.menubar = Menubar(self)
        for title, items in [
            ('&File', ['file-new', 'file-open', None, 
                       'file-save', 'file-saveas', None, 
                       'file-quit']),
            ('&Edit', ['edit-undo', 'edit-redo', None, 
                       'edit-cut', 'edit-copy', 'edit-paste', None, 'edit-delete']),
            ('&Tools', ['functions', 'filters', 'scripts', None, 'run-script']),
            ('&Analysis', ['integrate']),
            ('&Help', []),
        ]:
            menu = Menu(self.menubar, title)
            for item in items:
                menu.append(actions[item])

        self.toolbar = Toolbar(self)
        for item in [
            'file-new', 'file-open', 'file-save', 'file-saveas', None,
            'object-new-folder', 'object-new-worksheet', 'object-new-graph', None,
            'edit-undo', 'edit-redo', None,
            'import-ascii', None,
            'close-active-page',
        ]:
            self.toolbar.append(actions[item])
        self.toolbar._widget.Realize()
        
        self.connect('close', self.on_quit)

        self.main.bottom_panel._widget.toolbar.Realize()
        self.main.left_panel._widget.toolbar.Realize()
        self.main.right_panel._widget.toolbar.Realize()

        # preload
        wx.xrc.XmlResource(os.path.join(DATADIR, 'data', 'resources.xrc'))

        self.on_action()
        self.on_project_modified(False)

        if len(self.args) > 0:
            name = self.args[0]
            if ':' in name:
                prj, obj = name.split(':')
                self.open_project(Project(prj))
                self.on_item_activated(self.project.top[obj])
            else:
                self.open_project(Project(name))

    def on_integrate(self):
        page = self.book.active_page 
        if not isinstance(page, GraphView):
            return

        sys.path.append(os.path.join(DATADIR, 'data', 'scripts'))
        from integrate import integrate

        for dataset in page.graph.selected_datasets:
            w = dataset.worksheet
            ind = dataset.active_data()
            x = dataset.x[ind]
            y = dataset.y[ind]
            res = integrate(x, y)
            xname = 'int_%s_%s_x' % (dataset.x.name, dataset.y.name)
            yname = 'int_%s_%s_y' % (dataset.x.name, dataset.y.name)
            w[xname], w[yname] = x, res
            page.graph.add(w[xname], w[yname])

            

    def on_action(self, *args, **kwds):
        self.actions['edit-undo'].enabled = action_list.can_undo()
        self.actions['edit-redo'].enabled = action_list.can_redo()

    def on_status_message(self, obj, msg, time=0):
        self.status = msg

    def on_import_ascii(self):
        dlg = wx.FileDialog(self._widget, message="Choose a file", defaultDir=os.getcwd(), 
                            defaultFile="", wildcard="All Files|*.*|Projects|*.gt", style=wx.OPEN | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            ws = self.project.new(Worksheet, None, self.project.here)
            path = dlg.GetPaths()[0]
            ws.array, ws._header = import_ascii(path)
        dlg.Destroy()

    def on_project_modified(self, isit):
        self.actions['file-save'].enabled = isit

    def open_project(self, project):
        self.project = project
        for panel in (self.shell, self.explorer):
            panel.connect_project(self.project)
        self.project.connect('remove-item', self.on_project_remove_item)
        self.project.connect('modified', lambda: self.on_project_modified(True), True)
        self.project.connect('not-modified', lambda: self.on_project_modified(False), True)
#        action_list.clear()

    def on_item_activated(self, item):
#        w = Window()
#        { Graph: GraphView, Worksheet: WorksheetView }[type(item)](w, item)
#        w.show()
#        return
        if isinstance(item, Graph):
            for view in [v for v in self.book.pages if hasattr(v, 'graph')]:
                if item == view.graph:
                    self.book.select(view)
                    view.graph.recalc = True
                    view.graph.emit('redraw')
                    return
            w = GraphView(self.book, item, page_label=item.name, page_pixmap='graph.png')
            self.book.select(w)
        elif isinstance(item, Worksheet):
            for view in [v for v in self.book.pages if hasattr(v, 'worksheet')]:
                if item == view.worksheet:
                    self.book.select(view)
                    return
            w = WorksheetView(self.book, item, page_label=item.name, page_pixmap='worksheet.png')
            self.book.select(w)

#    def on_page_changed(self, item):
#        if isinstance(item, GraphView):
#            item.graph.emit('redraw')
#        print >>sys.stderr, item


    def on_quit(self):
        settings.set('script', 'history', '\n'.join(self.shell._widget.history))
        self._widget.Destroy()

    def close_project(self):
        for panel in (self.shell, self.explorer):
            panel.disconnect_project()
        for page in list(self.book.pages):
            self.book.delete(page)
        self.project.disconnect('remove-item', self.on_project_remove_item)

    def on_project_remove_item(self, item):
        for page in self.book.pages:
            if page.object == item:
                page.on_close()

    def act(self, x, y):
        print 'patataki'

    def on_new_worksheet(self):
        ws = self.project.new(Worksheet, None, self.project.here)
        ws.a = [nan]*100
        ws.b = [nan]*100

    def on_run_script(self):
        try:
            dlg = wx.FileDialog(self._widget, message="Choose a file", defaultDir=os.getcwd(), 
                                defaultFile="", wildcard="Python source files|*.py|All files|*.*", 
                                style=wx.OPEN | wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPaths()[0]
                self.shell.run('execfile("%s")' % path)
            dlg.Destroy()
        except Cancel:
            return


    def on_project_open(self):
        try:
            if self.project.modified and self.ask_savechanges():
                self.on_project_save()

            dlg = wx.FileDialog(self._widget, message="Choose a file", defaultDir=os.getcwd(), 
                                defaultFile="", wildcard="All Files|*.*|Projects|*.gt", style=wx.OPEN | wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPaths()[0]
                self.close_project()
                self.open_project(Project(str(path)))
                action_list.clear()
            dlg.Destroy()
        except Cancel:
            return

    def ask_savechanges(self):
        dlg = wx.MessageDialog(self._widget, 'Save changes to this project?', 'Save?',
                               wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_YES:
            return True
        elif result == wx.ID_NO:
            return False
        elif result == wx.ID_CANCEL:
            raise Cancel
        dlg.Destroy()

    def on_project_saveas(self):
        try:
            dlg = wx.FileDialog(self._widget, message="Choose a file", defaultDir=os.getcwd(), 
                                defaultFile="", wildcard="All Files|*.*|Projects|*.mk", style=wx.SAVE | wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPaths()[0]
                self.project.saveto(path)
                self.open_project(Project(str(path)))
                action_list.clear()
            dlg.Destroy()
        except Cancel:
            return

    def on_project_save(self):
        try:
            if self.project.filename is not None:
                self.project.commit()
            else:
                self.on_project_saveas()
        except Cancel:
            return

    def on_project_new(self):
        try:
            if self.project.modified and self.ask_savechanges():
                self.on_project_save()
            self.close_project()
            self.open_project(Project())
            action_list.clear()
        except Cancel:
            return
            
    def on_new_graph(self):
        g = self.project.new(Graph, None, self.project.here)

    def on_new_folder(self):
        self.project.new(Folder, None, self.project.here)