/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to viz/diffwin.py

  • Committer: Jelmer Vernooij
  • Date: 2007-02-01 15:50:40 UTC
  • Revision ID: jelmer@samba.org-20070201155040-3hq4mfbxs99kzazy
add framework for tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
__author__    = "Scott James Remnant <scott@ubuntu.com>"
11
11
 
12
12
 
13
 
import os
14
 
 
15
13
from cStringIO import StringIO
16
14
 
17
15
import gtk
18
 
import gobject
19
16
import pango
 
17
import sys
20
18
 
21
19
try:
22
20
    import gtksourceview
24
22
except ImportError:
25
23
    have_gtksourceview = False
26
24
 
27
 
from bzrlib.delta import compare_trees
 
25
import bzrlib
 
26
 
28
27
from bzrlib.diff import show_diff_trees
 
28
from bzrlib.errors import NoSuchFile
29
29
 
30
30
 
31
31
class DiffWindow(gtk.Window):
35
35
    differences between two revisions on a branch.
36
36
    """
37
37
 
38
 
    def __init__(self, app=None):
 
38
    def __init__(self):
39
39
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
40
40
        self.set_border_width(0)
41
41
        self.set_title("bzrk diff")
42
42
 
43
 
        self.app = app
44
 
 
45
43
        # Use two thirds of the screen by default
46
44
        screen = self.get_screen()
47
45
        monitor = screen.get_monitor_geometry(0)
53
51
 
54
52
    def construct(self):
55
53
        """Construct the window contents."""
56
 
        hbox = gtk.HBox(spacing=6)
57
 
        hbox.set_border_width(12)
58
 
        self.add(hbox)
59
 
        hbox.show()
 
54
        # The   window  consists  of   a  pane   containing:  the
 
55
        # hierarchical list  of files on  the left, and  the diff
 
56
        # for the currently selected file on the right.
 
57
        pane = gtk.HPaned()
 
58
        self.add(pane)
 
59
        pane.show()
60
60
 
 
61
        # The file hierarchy: a scrollable treeview
61
62
        scrollwin = gtk.ScrolledWindow()
62
63
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
63
64
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
64
 
        hbox.pack_start(scrollwin, expand=False, fill=True)
 
65
        pane.pack1(scrollwin)
65
66
        scrollwin.show()
66
67
 
67
68
        self.model = gtk.TreeStore(str, str)
79
80
        column.add_attribute(cell, "text", 0)
80
81
        self.treeview.append_column(column)
81
82
 
82
 
 
 
83
        # The diffs of the  selected file: a scrollable source or
 
84
        # text view
83
85
        scrollwin = gtk.ScrolledWindow()
84
86
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
85
87
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
86
 
        hbox.pack_start(scrollwin, expand=True, fill=True)
 
88
        pane.pack2(scrollwin)
87
89
        scrollwin.show()
88
90
 
89
91
        if have_gtksourceview:
99
101
            sourceview = gtk.TextView(self.buffer)
100
102
 
101
103
        sourceview.set_editable(False)
102
 
        sourceview.set_wrap_mode(gtk.WRAP_CHAR)
103
104
        sourceview.modify_font(pango.FontDescription("Monospace"))
104
105
        scrollwin.add(sourceview)
105
106
        sourceview.show()
106
107
 
107
 
    def set_diff(self, branch, revid, parentid):
 
108
    def set_diff(self, description, rev_tree, parent_tree):
108
109
        """Set the differences showed by this window.
109
110
 
110
111
        Compares the two trees and populates the window with the
111
112
        differences.
112
113
        """
113
 
        self.rev_tree = branch.revision_tree(revid)
114
 
        self.parent_tree = branch.revision_tree(parentid)
 
114
        self.rev_tree = rev_tree
 
115
        self.parent_tree = parent_tree
115
116
 
116
117
        self.model.clear()
117
 
        delta = compare_trees(self.parent_tree, self.rev_tree)
 
118
        delta = self.rev_tree.changes_from(self.parent_tree)
 
119
 
 
120
        self.model.append(None, [ "Complete Diff", "" ])
118
121
 
119
122
        if len(delta.added):
120
123
            titer = self.model.append(None, [ "Added", None ])
130
133
            titer = self.model.append(None, [ "Renamed", None ])
131
134
            for oldpath, newpath, id, kind, text_modified, meta_modified \
132
135
                    in delta.renamed:
133
 
                self.model.append(titer, [ oldpath, oldpath ])
 
136
                self.model.append(titer, [ oldpath, newpath ])
134
137
 
135
138
        if len(delta.modified):
136
139
            titer = self.model.append(None, [ "Modified", None ])
138
141
                self.model.append(titer, [ path, path ])
139
142
 
140
143
        self.treeview.expand_all()
141
 
        self.set_title(os.path.basename(branch.base) + " - bzrk diff")
 
144
        self.set_title(description + " - bzrk diff")
 
145
 
 
146
    def set_file(self, file_path):
 
147
        tv_path = None
 
148
        for data in self.model:
 
149
            for child in data.iterchildren():
 
150
                if child[0] == file_path or child[1] == file_path:
 
151
                    tv_path = child.path
 
152
                    break
 
153
        if tv_path is None:
 
154
            raise NoSuchFile(file_path)
 
155
        self.treeview.set_cursor(tv_path)
 
156
        self.treeview.scroll_to_cell(tv_path)
142
157
 
143
158
    def _treeview_cursor_cb(self, *args):
144
159
        """Callback for when the treeview cursor changes."""
145
160
        (path, col) = self.treeview.get_cursor()
146
 
        path = self.model[path][1]
147
 
        if path is None:
 
161
        specific_files = [ self.model[path][1] ]
 
162
        if specific_files == [ None ]:
148
163
            return
 
164
        elif specific_files == [ "" ]:
 
165
            specific_files = []
149
166
 
150
167
        s = StringIO()
151
 
        show_diff_trees(self.parent_tree, self.rev_tree, s, [ path ])
152
 
        self.buffer.set_text(s.getvalue())
 
168
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
 
169
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))