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