/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 olive/frontend/gtk/viz/diffwin.py

Merge from Richard Ferguson's development branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Modified for use with Olive:
 
2
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
3
# Original copyright holder:
 
4
# Copyright (C) 2005 by Canonical Ltd. (Scott James Remnant <scott@ubuntu.com>)
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
"""Difference window.
 
21
 
 
22
This module contains the code to manage the diff window which shows
 
23
the changes made between two revisions on a branch.
 
24
"""
 
25
 
 
26
__copyright__ = "Copyright © 2005 Canonical Ltd."
 
27
__author__    = "Scott James Remnant <scott@ubuntu.com>"
 
28
 
 
29
 
 
30
from cStringIO import StringIO
 
31
 
 
32
import gtk
 
33
import pango
 
34
import sys
 
35
 
 
36
try:
 
37
    import gtksourceview
 
38
    have_gtksourceview = True
 
39
except ImportError:
 
40
    have_gtksourceview = False
 
41
 
 
42
import bzrlib
 
43
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
44
    # function deprecated after 0.9
 
45
    from bzrlib.delta import compare_trees
 
46
 
 
47
from bzrlib.diff import show_diff_trees
 
48
from bzrlib.errors import NoSuchFile
 
49
 
 
50
 
 
51
class DiffWindow(gtk.Window):
 
52
    """Diff window.
 
53
 
 
54
    This object represents and manages a single window containing the
 
55
    differences between two revisions on a branch.
 
56
    """
 
57
 
 
58
    def __init__(self):
 
59
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
 
60
        self.set_border_width(0)
 
61
        self.set_title("bzrk diff")
 
62
 
 
63
        # Use two thirds of the screen by default
 
64
        screen = self.get_screen()
 
65
        monitor = screen.get_monitor_geometry(0)
 
66
        width = int(monitor.width * 0.66)
 
67
        height = int(monitor.height * 0.66)
 
68
        self.set_default_size(width, height)
 
69
 
 
70
        self.construct()
 
71
 
 
72
    def construct(self):
 
73
        """Construct the window contents."""
 
74
        # The   window  consists  of   a  pane   containing:  the
 
75
        # hierarchical list  of files on  the left, and  the diff
 
76
        # for the currently selected file on the right.
 
77
        pane = gtk.HPaned()
 
78
        self.add(pane)
 
79
        pane.show()
 
80
 
 
81
        # The file hierarchy: a scrollable treeview
 
82
        scrollwin = gtk.ScrolledWindow()
 
83
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
84
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
85
        pane.pack1(scrollwin)
 
86
        scrollwin.show()
 
87
 
 
88
        self.model = gtk.TreeStore(str, str)
 
89
        self.treeview = gtk.TreeView(self.model)
 
90
        self.treeview.set_headers_visible(False)
 
91
        self.treeview.set_search_column(1)
 
92
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
 
93
        scrollwin.add(self.treeview)
 
94
        self.treeview.show()
 
95
 
 
96
        cell = gtk.CellRendererText()
 
97
        cell.set_property("width-chars", 20)
 
98
        column = gtk.TreeViewColumn()
 
99
        column.pack_start(cell, expand=True)
 
100
        column.add_attribute(cell, "text", 0)
 
101
        self.treeview.append_column(column)
 
102
 
 
103
        # The diffs of the  selected file: a scrollable source or
 
104
        # text view
 
105
        scrollwin = gtk.ScrolledWindow()
 
106
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
107
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
108
        pane.pack2(scrollwin)
 
109
        scrollwin.show()
 
110
 
 
111
        if have_gtksourceview:
 
112
            self.buffer = gtksourceview.SourceBuffer()
 
113
            slm = gtksourceview.SourceLanguagesManager()
 
114
            gsl = slm.get_language_from_mime_type("text/x-patch")
 
115
            self.buffer.set_language(gsl)
 
116
            self.buffer.set_highlight(True)
 
117
 
 
118
            sourceview = gtksourceview.SourceView(self.buffer)
 
119
        else:
 
120
            self.buffer = gtk.TextBuffer()
 
121
            sourceview = gtk.TextView(self.buffer)
 
122
 
 
123
        sourceview.set_editable(False)
 
124
        sourceview.modify_font(pango.FontDescription("Monospace"))
 
125
        scrollwin.add(sourceview)
 
126
        sourceview.show()
 
127
 
 
128
    def set_diff(self, description, rev_tree, parent_tree):
 
129
        """Set the differences showed by this window.
 
130
 
 
131
        Compares the two trees and populates the window with the
 
132
        differences.
 
133
        """
 
134
        self.rev_tree = rev_tree
 
135
        self.parent_tree = parent_tree
 
136
 
 
137
        self.model.clear()
 
138
 
 
139
        if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
140
            delta = compare_trees(self.parent_tree, self.rev_tree)
 
141
        else:
 
142
            delta = self.rev_tree.changes_from(self.parent_tree)
 
143
 
 
144
        self.model.append(None, [ _('Complete Diff'), "" ])
 
145
 
 
146
        if len(delta.added):
 
147
            titer = self.model.append(None, [ _('Added'), None ])
 
148
            for path, id, kind in delta.added:
 
149
                self.model.append(titer, [ path, path ])
 
150
 
 
151
        if len(delta.removed):
 
152
            titer = self.model.append(None, [ _('Removed'), None ])
 
153
            for path, id, kind in delta.removed:
 
154
                self.model.append(titer, [ path, path ])
 
155
 
 
156
        if len(delta.renamed):
 
157
            titer = self.model.append(None, [ _('Renamed'), None ])
 
158
            for oldpath, newpath, id, kind, text_modified, meta_modified \
 
159
                    in delta.renamed:
 
160
                self.model.append(titer, [ oldpath, newpath ])
 
161
 
 
162
        if len(delta.modified):
 
163
            titer = self.model.append(None, [ _('Modified'), None ])
 
164
            for path, id, kind, text_modified, meta_modified in delta.modified:
 
165
                self.model.append(titer, [ path, path ])
 
166
 
 
167
        self.treeview.expand_all()
 
168
        self.set_title(description + " - bzrk diff")
 
169
 
 
170
    def set_file(self, file_path):
 
171
        tv_path = None
 
172
        for data in self.model:
 
173
            for child in data.iterchildren():
 
174
                if child[0] == file_path or child[1] == file_path:
 
175
                    tv_path = child.path
 
176
                    break
 
177
        if tv_path is None:
 
178
            raise NoSuchFile(file_path)
 
179
        self.treeview.set_cursor(tv_path)
 
180
        self.treeview.scroll_to_cell(tv_path)
 
181
 
 
182
    def _treeview_cursor_cb(self, *args):
 
183
        """Callback for when the treeview cursor changes."""
 
184
        (path, col) = self.treeview.get_cursor()
 
185
        specific_files = [ self.model[path][1] ]
 
186
        if specific_files == [ None ]:
 
187
            return
 
188
        elif specific_files == [ "" ]:
 
189
            specific_files = []
 
190
 
 
191
        s = StringIO()
 
192
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
 
193
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))