Пример #1
0
class Main(Widgets):
    def __init__(self):
        self.widgets = gtk.glade.XML("pixmaps/grafity.glade", 'mainwin')
        signals = { "on_quit" : self.on_quit,
                    "on_file_open" : self.on_file_open,
                    "on_object_activated" : self.on_object_activated }
        class getter(object):
            def __getattr__(s, attr):
                return getattr(self, attr)
        signals = getter()
        self.widgets.signal_autoconnect(signals)

        graphicon = gtk.gdk.pixbuf_new_from_file('/home/daniel/grafity/data/images/16/graph.png')
        foldericon = gtk.gdk.pixbuf_new_from_file('/home/daniel/grafity/data/images/16/folder.png')
        worksheeticon = gtk.gdk.pixbuf_new_from_file('/home/daniel/grafity/data/images/16/worksheet.png')

        def obj_id_str(treeviewcolumn, cell_renderer, model, iter):
            obj = model.get_value(iter, 0)
            cell_renderer.set_property('text', obj.name)

        def obj_id_icon(treeviewcolumn, cell_renderer, model, iter):
            obj = model.get_value(iter, 0)
            icon = {grafity.Graph: graphicon,
                    grafity.Worksheet: worksheeticon,
                    grafity.Folder: foldericon } [type(obj)]
            cell_renderer.set_property('pixbuf', icon)

        # tree columns
        cell = gtk.CellRendererPixbuf()
        column = gtk.TreeViewColumn("icon", cell)
        column.set_cell_data_func(cell, obj_id_icon)
        self.folder_tree.append_column(column)

        cell = gtk.CellRendererText()
        column = gtk.TreeViewColumn("name", cell)
        column.set_cell_data_func(cell, obj_id_str)
        self.folder_tree.append_column(column)

        # list columns
        cell = gtk.CellRendererPixbuf()
        column = gtk.TreeViewColumn("icon", cell)
        column.set_cell_data_func(cell, obj_id_icon)
        self.object_list.append_column(column)

        cell = gtk.CellRendererText()
        column = gtk.TreeViewColumn("name", cell)
        column.set_cell_data_func(cell, obj_id_str)
        self.object_list.append_column(column)

#        self.object_list.connect('row-activated', self.on_object_activated)
        self.folder_tree.get_selection().connect('changed', self.on_tree_select)

        self.console = Console( #                          locals={"project":self.project},
                          use_rlcompleter=True,
                          start_script="from grafity import Folder, Worksheet, Graph\n")
        self.console_box.add(self.console)
        self.console.show()

        make_panel(self.left_panel, self.left_paned, 'left')
        make_panel(self.bottom_panel, self.bottom_paned, 'bottom')

        self.open_project(grafity.Project('test/pdms.gt'))

        self.left_panel.set_current_page(1)

    def open_project(self, project):
        """Connect a project to the gui"""
        self.project = project

        # tree
        self.project._tree_model = project_tree_store(self.project)
        self.folder_tree.set_model(self.project._tree_model)

        # folders
        for folder in self.project.top.all_subfolders():
            folder._list_model = folder_list_store(folder)
        self.project.top._list_model = folder_list_store(self.project.top)

        self.project.connect('change-current-folder', self.on_change_folder)
        self.console.locals['project'] = self.project
 
    def close_project(self):
        """Disconnect the current project form the gui"""
        self.project.disconnect('change-current-folder', self.on_change_folder)
        self.folder_tree.set_model(None)
        self.object_list.set_model(None)
        # and close all pages
        self.project = None

    def ask_save(self):
        """Prepare to close the project. Ask the user whether to
        save changes and save is necessary. Raises Cancel if 
        the user cancels.
        """
        msg = gtk.glade.XML("pixmaps/grafity.glade", 'ask_save').get_widget('ask_save')
        resp = msg.run()
        msg.destroy()
        if resp == gtk.RESPONSE_DELETE_EVENT or resp == gtk.RESPONSE_CANCEL:
            raise Cancel
        if resp == gtk.RESPONSE_YES:
            self.on_file_save(None)
        elif resp == gtk.RESPONSE_NO:
            pass


    def view(self, obj):
        """Create a view for the object `obj` (Worksheet or Graph)
        in the notebook, or switch to it if one exists,
        """
        if isinstance(obj, grafity.Graph):
            view = GraphView(obj)
            self.notebook.append_page(view.page, view.label)
        elif isinstance(obj, grafity.Worksheet):
            view = WorksheetView(obj)
            self.notebook.append_page(view.page, view.label)


    # Signals from the project

    def on_change_folder(self, folder):
        """The project's current folder has changer"""
        self.object_list.set_model(folder._list_model)


    # Signals from the gui

    def on_object_activated(self, treeview, path, view_column):
        """An item in the current folder has been double-clicked"""
        self.view(treeview.get_model()[path][0])

    def on_tree_select(self, selection):
        """An item in the project tree has been selected"""
        model, paths = selection.get_selected_rows()
        self.project.cd(self.folder_tree.get_model()[paths[0]][0])


    # Menu and toolbar actions

    def on_file_open(self, item):
        """File/Open"""
        try:
            self.ask_save()
        except Cancel:
            return

        filesel = gtk.FileSelection(title="Open Project")
        resp = filesel.run()
        if resp == gtk.RESPONSE_DELETE_EVENT or resp == gtk.RESPONSE_CANCEL:
            pass
        if resp == gtk.RESPONSE_OK:
            self.close_project()
            self.open_project(grafity.Project(filesel.get_filename()))
        filesel.destroy()

    def on_file_save(self, item):
        if self.project.filename is not None:
            self.project.commit()
        else:
            self.on_file_saveas(item)

    def on_file_saveas(self, item):
        filesel = gtk.FileSelection(title="Save Project")
        resp = filesel.run()
        if resp == gtk.RESPONSE_DELETE_EVENT or resp == gtk.RESPONSE_CANCEL:
            pass
        if resp == gtk.RESPONSE_OK:
            self.project.saveto(filesel.get_filename())
            self.close_project()
            self.open_project(grafity.Project(filesel.get_filename()))
        filesel.destroy()

    def on_file_new(self, item):
        try:
            self.ask_save()
        except Cancel:
            return
        self.close_project()
        self.open_project(grafity.Project())


    def on_quit(self, item):
        """File/Quit"""
        try:
            self.ask_save()
        except Cancel:
            return
        gtk.main_quit()

    def on_new_worksheet(self, item):
        self.project.new(grafity.Worksheet)

    def on_new_graph(self, item):
        self.project.new(grafity.Graph)

    def on_new_folder(self, item):
        self.project.new(grafity.Folder)