File: loom.py

package info (click to toggle)
bzr-gtk 0.103.0+bzr792-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,524 kB
  • sloc: python: 9,616; makefile: 16; sh: 10
file content (118 lines) | stat: -rw-r--r-- 4,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from gi.repository import Gtk
from gi.repository import GObject

from bzrlib.plugins.gtk.diff import DiffWidget
from bzrlib.plugins.gtk.dialog import question_dialog
from bzrlib.plugins.loom import (
    branch as loom_branch,
    tree as loom_tree,
    )
from bzrlib.plugins.gtk.i18n import _i18n


class LoomDialog(Gtk.Dialog):
    """Simple Loom browse dialog."""

    def __init__(self, branch, tree=None, parent=None):
        super(LoomDialog, self).__init__(
            title="Threads", parent=parent, flags=0,
            buttons=(Gtk.STOCK_CLOSE,Gtk.ResponseType.OK))
        self.branch = branch
        if tree is not None:
            self.tree = loom_tree.LoomTreeDecorator(tree)
        else:
            self.tree = None

        self._construct()

    def run(self):
        try:
            loom_branch.require_loom_branch(self.branch)
        except loom_branch.NotALoom:
            response = question_dialog(
                _i18n("Upgrade to Loom branch?"),
                _i18n("Branch is not a loom branch. Upgrade to Loom format?"),
                parent=self)
                # Doesn't set a parent for the dialog..
            if response == Gtk.ResponseType.NO:
                return
            assert self.branch.nick is not None
            loom_branch.loomify(self.branch)
        self._load_threads()
        return super(LoomDialog, self).run()

    def _construct(self):
        hbox = Gtk.HBox()

        self._threads_scroller = Gtk.ScrolledWindow()
        self._threads_scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self._threads_view = Gtk.TreeView()
        self._threads_scroller.add(self._threads_view)
        self._threads_scroller.set_shadow_type(Gtk.ShadowType.IN)
        hbox.pack_start(self._threads_scroller, True, True, 0)

        self._threads_store = Gtk.ListStore(
                GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_PYOBJECT, GObject.TYPE_STRING)
        self._threads_view.set_model(self._threads_store)
        self._threads_view.append_column(Gtk.TreeViewColumn("Name", Gtk.CellRendererText(), text=0))
        self._threads_view.connect('cursor-changed', self._on_view_thread)
        if self.tree is not None:
            self._threads_view.connect('row-activated', self._on_switch_thread)

        self._diff = DiffWidget()
        self._diff.show()
        hbox.pack_end(self._diff, False, False, 0)

        hbox.show_all()
        self.get_content_area().pack_start(hbox, True, True, 0)

        # FIXME: Buttons: combine-thread, revert-loom, record
        self.set_default_size(500, 350)

    def _on_view_thread(self, treeview):
        treeselection = treeview.get_selection()
        (model, selection) = treeselection.get_selected()
        if selection is None:
            return
        revid, parent_revid = model.get(selection, 1, 3)
        if parent_revid is None:
            return
        self.branch.lock_read()
        try:
            (rev_tree, parent_tree) = tuple(self.branch.repository.revision_trees([revid, parent_revid]))
            self._diff.set_diff(rev_tree, parent_tree)
        finally:
            self.branch.unlock()

    def _on_switch_thread(self, treeview, path, view_column):
        new_thread = self._threads_store.get_value(self._threads_store.get_iter(path), 0)
        self.tree.down_thread(new_thread)

    def _load_threads(self):
        self._threads_store.clear()
        
        self.branch.lock_read()
        try:
            threads = self.branch.get_loom_state().get_threads()
            last_revid = None
            for name, revid, parent_ids in reversed(threads):
                self._threads_store.append([name, revid, parent_ids, last_revid])
                last_revid = revid
        finally:
            self.branch.unlock()