Ejemplo n.º 1
0
    def __init__(self):
        self.model = gtk.ListStore(gobject.TYPE_PYOBJECT, # Commit
                                   gobject.TYPE_STRING,   # Author
                                   gobject.TYPE_STRING)   # Date

        gtk.TreeView.__init__(self, self.model)

        self.selected_commit = None
        self.references = {}

        column = gtk.TreeViewColumn("Commit", CommitCellRenderer(),
                                    commit=self.COLUMN_COMMIT)
        column.set_resizable(True)
        column.set_expand(True)
        self.append_column(column)

        column = gtk.TreeViewColumn("Author", gtk.CellRendererText(),
                                    markup=self.COLUMN_AUTHOR)
        column.set_resizable(True)
        self.append_column(column)

        column = gtk.TreeViewColumn("Date", gtk.CellRendererText(),
                                    markup=self.COLUMN_DATE)
        column.set_resizable(True)
        #column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
        self.append_column(column)

        self.connect("button-press-event", self.on_button_press)
        self.get_selection().connect("changed", self.on_selection_changed)

        self.set_search_equal_func(self.__search_equal_func)
        self.set_search_column(self.COLUMN_COMMIT)

        self.graph = CommitGraph()
Ejemplo n.º 2
0
class CommitsTree(gtk.TreeView):
    __gsignals__ = {
        'commit_changed': (gobject.SIGNAL_RUN_FIRST,
                           gobject.TYPE_NONE,
                           (gobject.TYPE_PYOBJECT,)),
        'references_changed': (gobject.SIGNAL_RUN_FIRST,
                               gobject.TYPE_NONE,
                               (gobject.TYPE_PYOBJECT,)),
    }

    COLUMN_COMMIT = 0
    COLUMN_AUTHOR = 1
    COLUMN_DATE = 2

    def __init__(self):
        self.model = gtk.ListStore(gobject.TYPE_PYOBJECT, # Commit
                                   gobject.TYPE_STRING,   # Author
                                   gobject.TYPE_STRING)   # Date

        gtk.TreeView.__init__(self, self.model)

        self.selected_commit = None
        self.references = {}

        column = gtk.TreeViewColumn("Commit", CommitCellRenderer(),
                                    commit=self.COLUMN_COMMIT)
        column.set_resizable(True)
        column.set_expand(True)
        self.append_column(column)

        column = gtk.TreeViewColumn("Author", gtk.CellRendererText(),
                                    markup=self.COLUMN_AUTHOR)
        column.set_resizable(True)
        self.append_column(column)

        column = gtk.TreeViewColumn("Date", gtk.CellRendererText(),
                                    markup=self.COLUMN_DATE)
        column.set_resizable(True)
        #column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
        self.append_column(column)

        self.connect("button-press-event", self.on_button_press)
        self.get_selection().connect("changed", self.on_selection_changed)

        self.set_search_equal_func(self.__search_equal_func)
        self.set_search_column(self.COLUMN_COMMIT)

        self.graph = CommitGraph()

    def update_commits(self):
        self.model.clear()
        self.references = {}

        for commit in self.graph.get_commits():
            i = commit.author.find("<")
            author_name = commit.author[0:i - 1]
            iter = self.model.append((
                commit,
                "<small>%s</small>" % escape(author_name),
                "<small>%s</small>" % escape(commit.date)
            ))

            for ref in commit.references:
                self.references[ref] = iter;

        sorted_refs = sorted(self.references.keys(), key=attrgetter('name'))
        self.emit('references_changed', sorted_refs)

        self.queue_resize()

    def select_reference(self, ref):
        if ref in self.references:
            iter = self.references[ref]
            self.get_selection().select_iter(iter)
            self.scroll_to_cell(self.model.get_path(iter), use_align=True,
                                row_align=0.5)

    def on_button_press(self, widget, event):
        if event.button == 3:
            path, col, cell_x, cell_y = self.get_path_at_pos(int(event.x),
                                                             int(event.y))
            self.set_cursor(path, col, 0)

            commit = self.model.get(self.model.get_iter(path),
                                    self.COLUMN_COMMIT)[0]

            if commit:
                # We got the commit. See if we clicked a ref.
                for ref in commit.ref_boxes:
                    print "Comparing %d, %d to %d - %d, %d - %d" % \
                        (cell_x, cell_y, ref['x'], ref['x'] + ref['width'],
                         ref['y'], ref['y'] + ref['height'])

                    if (ref['x'] <= cell_x <= ref['x'] + ref['width'] and
                        ref['y'] <= cell_y <= ref['y'] + ref['height']):
                        print ref
                        break

            return True

        return False

    def on_selection_changed(self, selection):
        i = selection.get_selected()[1]

        if i:
            commit = self.model.get(i, self.COLUMN_COMMIT)[0]

            if commit != self.selected_commit:
                self.selected_commit = commit
                self.emit('commit_changed', commit)

    def __search_equal_func(self, model, column, key, iter):
        commit = model.get(iter, column)[0]
        assert isinstance(commit, Commit)

        # Oddly, we return False to indicate a match.
        return not commit.message.lower().startswith(key.lower())