/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
1
#!/usr/bin/python
2
# -*- coding: UTF-8 -*-
3
"""Difference window.
4
5
This module contains the code to manage the diff window which shows
6
the changes made between two revisions on a branch.
7
"""
8
9
__copyright__ = "Copyright © 2005 Canonical Ltd."
10
__author__    = "Scott James Remnant <scott@ubuntu.com>"
11
12
13
from cStringIO import StringIO
14
15
import gtk
16
import pango
17
18
try:
19
    import gtksourceview
20
    have_gtksourceview = True
21
except ImportError:
22
    have_gtksourceview = False
23
71 by Szilveszter Farkas (Phanatic)
(phanatic) compare_trees deprecated in 0.9
24
import bzrlib
25
if bzrlib.version_info < (0, 9):
26
    # function deprecated in 0.9
27
    from bzrlib.delta import compare_trees
28
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
29
from bzrlib.diff import show_diff_trees
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
30
from bzrlib.errors import NoSuchFile
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
31
32
33
class DiffWindow(gtk.Window):
34
    """Diff window.
35
36
    This object represents and manages a single window containing the
37
    differences between two revisions on a branch.
38
    """
39
51 by Jelmer Vernooij
Rework some of the parameters to DiffWindow.set_diff() to be
40
    def __init__(self):
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
41
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
42
        self.set_border_width(0)
43
        self.set_title("bzrk diff")
44
45
        # Use two thirds of the screen by default
46
        screen = self.get_screen()
47
        monitor = screen.get_monitor_geometry(0)
48
        width = int(monitor.width * 0.66)
49
        height = int(monitor.height * 0.66)
50
        self.set_default_size(width, height)
51
52
        self.construct()
53
54
    def construct(self):
55
        """Construct the window contents."""
56
        hbox = gtk.HBox(spacing=6)
45.1.2 by Michael Ellerman
Remove the border on the diff window, so that when maximized the scroll
57
        hbox.set_border_width(0)
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
58
        self.add(hbox)
59
        hbox.show()
60
61
        scrollwin = gtk.ScrolledWindow()
62
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
63
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
64
        hbox.pack_start(scrollwin, expand=False, fill=True)
65
        scrollwin.show()
66
67
        self.model = gtk.TreeStore(str, str)
68
        self.treeview = gtk.TreeView(self.model)
69
        self.treeview.set_headers_visible(False)
70
        self.treeview.set_search_column(1)
71
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
72
        scrollwin.add(self.treeview)
73
        self.treeview.show()
74
75
        cell = gtk.CellRendererText()
76
        cell.set_property("width-chars", 20)
77
        column = gtk.TreeViewColumn()
78
        column.pack_start(cell, expand=True)
79
        column.add_attribute(cell, "text", 0)
80
        self.treeview.append_column(column)
81
82
83
        scrollwin = gtk.ScrolledWindow()
84
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
85
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
86
        hbox.pack_start(scrollwin, expand=True, fill=True)
87
        scrollwin.show()
88
89
        if have_gtksourceview:
90
            self.buffer = gtksourceview.SourceBuffer()
91
            slm = gtksourceview.SourceLanguagesManager()
92
            gsl = slm.get_language_from_mime_type("text/x-patch")
93
            self.buffer.set_language(gsl)
94
            self.buffer.set_highlight(True)
95
96
            sourceview = gtksourceview.SourceView(self.buffer)
97
        else:
98
            self.buffer = gtk.TextBuffer()
99
            sourceview = gtk.TextView(self.buffer)
100
101
        sourceview.set_editable(False)
102
        sourceview.modify_font(pango.FontDescription("Monospace"))
103
        scrollwin.add(sourceview)
104
        sourceview.show()
105
51 by Jelmer Vernooij
Rework some of the parameters to DiffWindow.set_diff() to be
106
    def set_diff(self, description, rev_tree, parent_tree):
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
107
        """Set the differences showed by this window.
108
109
        Compares the two trees and populates the window with the
110
        differences.
111
        """
51 by Jelmer Vernooij
Rework some of the parameters to DiffWindow.set_diff() to be
112
        self.rev_tree = rev_tree
113
        self.parent_tree = parent_tree
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
114
115
        self.model.clear()
71 by Szilveszter Farkas (Phanatic)
(phanatic) compare_trees deprecated in 0.9
116
        if bzrlib.version_info < (0, 9):
117
            delta = compare_trees(self.parent_tree, self.rev_tree)
118
        else:
119
            delta = self.rev_tree.changes_from(self.parent_tree)
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
120
11 by Scott James Remnant
Add a default "Complete Diff" option to the top of the diff window
121
        self.model.append(None, [ "Complete Diff", "" ])
122
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
123
        if len(delta.added):
124
            titer = self.model.append(None, [ "Added", None ])
125
            for path, id, kind in delta.added:
126
                self.model.append(titer, [ path, path ])
127
128
        if len(delta.removed):
129
            titer = self.model.append(None, [ "Removed", None ])
130
            for path, id, kind in delta.removed:
131
                self.model.append(titer, [ path, path ])
132
133
        if len(delta.renamed):
134
            titer = self.model.append(None, [ "Renamed", None ])
135
            for oldpath, newpath, id, kind, text_modified, meta_modified \
136
                    in delta.renamed:
63 by Aaron Bentley
Accept either side of a rename for DiffWindow.set_file
137
                self.model.append(titer, [ oldpath, newpath ])
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
138
139
        if len(delta.modified):
140
            titer = self.model.append(None, [ "Modified", None ])
141
            for path, id, kind, text_modified, meta_modified in delta.modified:
142
                self.model.append(titer, [ path, path ])
143
144
        self.treeview.expand_all()
51 by Jelmer Vernooij
Rework some of the parameters to DiffWindow.set_diff() to be
145
        self.set_title(description + " - bzrk diff")
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
146
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
147
    def set_file(self, file_path):
148
        tv_path = None
149
        for data in self.model:
150
            for child in data.iterchildren():
63 by Aaron Bentley
Accept either side of a rename for DiffWindow.set_file
151
                if child[0] == file_path or child[1] == file_path:
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
152
                    tv_path = child.path
153
                    break
59.2.4 by Aaron Bentley
Teach gdiff to accept a single file argument
154
        if tv_path is None:
155
            raise NoSuchFile(file_path)
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
156
        self.treeview.set_cursor(tv_path)
64 by Aaron Bentley
Scroll to the appropriate cell when file selected
157
        self.treeview.scroll_to_cell(tv_path)
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
158
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
159
    def _treeview_cursor_cb(self, *args):
160
        """Callback for when the treeview cursor changes."""
161
        (path, col) = self.treeview.get_cursor()
11 by Scott James Remnant
Add a default "Complete Diff" option to the top of the diff window
162
        specific_files = [ self.model[path][1] ]
163
        if specific_files == [ None ]:
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
164
            return
11 by Scott James Remnant
Add a default "Complete Diff" option to the top of the diff window
165
        elif specific_files == [ "" ]:
166
            specific_files = []
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
167
168
        s = StringIO()
11 by Scott James Remnant
Add a default "Complete Diff" option to the top of the diff window
169
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
10 by Scott James Remnant
Add an extra window type, clicking the little icons next to a parent
170
        self.buffer.set_text(s.getvalue())