bzr branch
http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
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 |
||
35 |
try: |
|
36 |
import gtksourceview |
|
37 |
have_gtksourceview = True |
|
38 |
except ImportError: |
|
39 |
have_gtksourceview = False |
|
40 |
||
41 |
import bzrlib |
|
42 |
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9): |
|
43 |
# function deprecated after 0.9
|
|
44 |
from bzrlib.delta import compare_trees |
|
45 |
||
46 |
from bzrlib.diff import show_diff_trees |
|
47 |
from bzrlib.errors import NoSuchFile |
|
48 |
||
49 |
||
50 |
class DiffWindow(gtk.Window): |
|
51 |
"""Diff window. |
|
52 |
||
53 |
This object represents and manages a single window containing the
|
|
54 |
differences between two revisions on a branch.
|
|
55 |
"""
|
|
56 |
||
57 |
def __init__(self): |
|
58 |
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL) |
|
59 |
self.set_border_width(0) |
|
60 |
self.set_title("bzrk diff") |
|
61 |
||
62 |
# Use two thirds of the screen by default
|
|
63 |
screen = self.get_screen() |
|
64 |
monitor = screen.get_monitor_geometry(0) |
|
65 |
width = int(monitor.width * 0.66) |
|
66 |
height = int(monitor.height * 0.66) |
|
67 |
self.set_default_size(width, height) |
|
68 |
||
69 |
self.construct() |
|
70 |
||
71 |
def construct(self): |
|
72 |
"""Construct the window contents.""" |
|
73 |
hbox = gtk.HBox(spacing=6) |
|
74 |
hbox.set_border_width(0) |
|
75 |
self.add(hbox) |
|
76 |
hbox.show() |
|
77 |
||
78 |
scrollwin = gtk.ScrolledWindow() |
|
79 |
scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) |
|
80 |
scrollwin.set_shadow_type(gtk.SHADOW_IN) |
|
81 |
hbox.pack_start(scrollwin, expand=False, fill=True) |
|
82 |
scrollwin.show() |
|
83 |
||
84 |
self.model = gtk.TreeStore(str, str) |
|
85 |
self.treeview = gtk.TreeView(self.model) |
|
86 |
self.treeview.set_headers_visible(False) |
|
87 |
self.treeview.set_search_column(1) |
|
88 |
self.treeview.connect("cursor-changed", self._treeview_cursor_cb) |
|
89 |
scrollwin.add(self.treeview) |
|
90 |
self.treeview.show() |
|
91 |
||
92 |
cell = gtk.CellRendererText() |
|
93 |
cell.set_property("width-chars", 20) |
|
94 |
column = gtk.TreeViewColumn() |
|
95 |
column.pack_start(cell, expand=True) |
|
96 |
column.add_attribute(cell, "text", 0) |
|
97 |
self.treeview.append_column(column) |
|
98 |
||
99 |
||
100 |
scrollwin = gtk.ScrolledWindow() |
|
101 |
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|
102 |
scrollwin.set_shadow_type(gtk.SHADOW_IN) |
|
103 |
hbox.pack_start(scrollwin, expand=True, fill=True) |
|
104 |
scrollwin.show() |
|
105 |
||
106 |
if have_gtksourceview: |
|
107 |
self.buffer = gtksourceview.SourceBuffer() |
|
108 |
slm = gtksourceview.SourceLanguagesManager() |
|
109 |
gsl = slm.get_language_from_mime_type("text/x-patch") |
|
110 |
self.buffer.set_language(gsl) |
|
111 |
self.buffer.set_highlight(True) |
|
112 |
||
113 |
sourceview = gtksourceview.SourceView(self.buffer) |
|
114 |
else: |
|
115 |
self.buffer = gtk.TextBuffer() |
|
116 |
sourceview = gtk.TextView(self.buffer) |
|
117 |
||
118 |
sourceview.set_editable(False) |
|
119 |
sourceview.modify_font(pango.FontDescription("Monospace")) |
|
120 |
scrollwin.add(sourceview) |
|
121 |
sourceview.show() |
|
122 |
||
123 |
def set_diff(self, description, rev_tree, parent_tree): |
|
124 |
"""Set the differences showed by this window. |
|
125 |
||
126 |
Compares the two trees and populates the window with the
|
|
127 |
differences.
|
|
128 |
"""
|
|
129 |
self.rev_tree = rev_tree |
|
130 |
self.parent_tree = parent_tree |
|
131 |
||
132 |
self.model.clear() |
|
133 |
||
134 |
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9): |
|
135 |
delta = compare_trees(self.parent_tree, self.rev_tree) |
|
136 |
else: |
|
137 |
delta = self.rev_tree.changes_from(self.parent_tree) |
|
138 |
||
0.8.55
by Szilveszter Farkas (Phanatic)
Gettext support added. |
139 |
self.model.append(None, [ _('Complete Diff'), "" ]) |
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
140 |
|
141 |
if len(delta.added): |
|
0.8.55
by Szilveszter Farkas (Phanatic)
Gettext support added. |
142 |
titer = self.model.append(None, [ _('Added'), None ]) |
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
143 |
for path, id, kind in delta.added: |
144 |
self.model.append(titer, [ path, path ]) |
|
145 |
||
146 |
if len(delta.removed): |
|
0.8.55
by Szilveszter Farkas (Phanatic)
Gettext support added. |
147 |
titer = self.model.append(None, [ _('Removed'), None ]) |
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
148 |
for path, id, kind in delta.removed: |
149 |
self.model.append(titer, [ path, path ]) |
|
150 |
||
151 |
if len(delta.renamed): |
|
0.8.55
by Szilveszter Farkas (Phanatic)
Gettext support added. |
152 |
titer = self.model.append(None, [ _('Renamed'), None ]) |
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
153 |
for oldpath, newpath, id, kind, text_modified, meta_modified \ |
154 |
in delta.renamed: |
|
155 |
self.model.append(titer, [ oldpath, newpath ]) |
|
156 |
||
157 |
if len(delta.modified): |
|
0.8.55
by Szilveszter Farkas (Phanatic)
Gettext support added. |
158 |
titer = self.model.append(None, [ _('Modified'), None ]) |
0.8.43
by Szilveszter Farkas (Phanatic)
Implemented Log functionality (via bzrk). |
159 |
for path, id, kind, text_modified, meta_modified in delta.modified: |
160 |
self.model.append(titer, [ path, path ]) |
|
161 |
||
162 |
self.treeview.expand_all() |
|
163 |
self.set_title(description + " - bzrk diff") |
|
164 |
||
165 |
def set_file(self, file_path): |
|
166 |
tv_path = None |
|
167 |
for data in self.model: |
|
168 |
for child in data.iterchildren(): |
|
169 |
if child[0] == file_path or child[1] == file_path: |
|
170 |
tv_path = child.path |
|
171 |
break
|
|
172 |
if tv_path is None: |
|
173 |
raise NoSuchFile(file_path) |
|
174 |
self.treeview.set_cursor(tv_path) |
|
175 |
self.treeview.scroll_to_cell(tv_path) |
|
176 |
||
177 |
def _treeview_cursor_cb(self, *args): |
|
178 |
"""Callback for when the treeview cursor changes.""" |
|
179 |
(path, col) = self.treeview.get_cursor() |
|
180 |
specific_files = [ self.model[path][1] ] |
|
181 |
if specific_files == [ None ]: |
|
182 |
return
|
|
183 |
elif specific_files == [ "" ]: |
|
184 |
specific_files = [] |
|
185 |
||
186 |
s = StringIO() |
|
187 |
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files) |
|
188 |
self.buffer.set_text(s.getvalue()) |