/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
#
3
# Some parts of the code:
4
# Copyright (C) 2005 by Canonical Ltd.
5
# Author: Scott James Remnant <scott@ubuntu.com>
6
#
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21
import sys
22
23
from cStringIO import StringIO
24
25
try:
26
    import pygtk
27
    pygtk.require("2.0")
28
except:
29
    pass
30
try:
31
    import gtk
32
    import gtk.glade
33
    import gobject
34
    import pango
35
except:
36
    sys.exit(1)
37
38
try:
39
    import gtksourceview
40
    have_gtksourceview = True
41
except ImportError:
42
    have_gtksourceview = False
43
44
import bzrlib
45
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
46
if bzrlib.version_info < (0, 9):
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
47
    # function deprecated after 0.9
48
    from bzrlib.delta import compare_trees
49
50
from bzrlib.diff import show_diff_trees
51
import bzrlib.errors as errors
52
from bzrlib.workingtree import WorkingTree
53
54
class OliveDiff:
55
    """ Display Diff window and perform the needed actions. """
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
56
    def __init__(self, gladefile, comm, dialog):
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
57
        """ Initialize the Diff window. """
58
        self.gladefile = gladefile
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
59
        self.glade = gtk.glade.XML(self.gladefile, 'window_diff', 'olive-gtk')
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
60
        
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
61
        # Communication object
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
62
        self.comm = comm
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
63
        # Dialog object
64
        self.dialog = dialog
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
65
        
0.8.30 by Szilveszter Farkas (Phanatic)
Some minor tweaks to the Diff window.
66
        # Get some important widgets
67
        self.window = self.glade.get_widget('window_diff')
68
        self.treeview = self.glade.get_widget('treeview_diff_files')
69
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
70
        # Check if current location is a branch
71
        try:
72
            (self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
73
            branch = self.wt.branch
74
        except errors.NotBranchError:
75
            self.notbranch = True
76
            return
77
        except:
78
            raise
79
80
        file_id = self.wt.path2id(path)
81
82
        self.notbranch = False
83
        if file_id is None:
84
            self.notbranch = True
85
            return
86
        
87
        # Set the old working tree
88
        self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
89
        
90
        # Dictionary for signal_autoconnect
91
        dic = { "on_button_diff_close_clicked": self.close,
92
                "on_treeview_diff_files_cursor_changed": self.cursor_changed }
93
        
94
        # Connect the signals to the handlers
95
        self.glade.signal_autoconnect(dic)
96
        
97
        # Create the file list
98
        self._create_file_view()
99
        
100
        # Generate initial diff
101
        self._init_diff()
102
    
103
    def display(self):
104
        """ Display the Diff window. """
105
        if self.notbranch:
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
106
            self.dialog.error_dialog(_('Directory is not a branch'),
107
                                     _('You can perform this action only in a branch.'))
0.8.30 by Szilveszter Farkas (Phanatic)
Some minor tweaks to the Diff window.
108
            self.close()
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
109
        else:
110
            self.window.show_all()
111
    
112
    def _create_file_view(self):
113
        """ Create the list of files. """
114
        self.model = gtk.TreeStore(str, str)
115
        self.treeview.set_model(self.model)
116
        
117
        cell = gtk.CellRendererText()
118
        cell.set_property("width-chars", 20)
119
        column = gtk.TreeViewColumn()
120
        column.pack_start(cell, expand=True)
121
        column.add_attribute(cell, "text", 0)
122
        self.treeview.append_column(column)
123
        
124
        if have_gtksourceview:
125
            self.buffer = gtksourceview.SourceBuffer()
126
            slm = gtksourceview.SourceLanguagesManager()
127
            gsl = slm.get_language_from_mime_type("text/x-patch")
128
            self.buffer.set_language(gsl)
129
            self.buffer.set_highlight(True)
130
131
            sourceview = gtksourceview.SourceView(self.buffer)
132
        else:
133
            self.buffer = gtk.TextBuffer()
134
            sourceview = gtk.TextView(self.buffer)
135
136
        sourceview.set_editable(False)
137
        sourceview.modify_font(pango.FontDescription("Monospace"))
138
        scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
139
        scrollwin_diff.add(sourceview)
140
    
141
    def _init_diff(self):
142
        """ Generate initial diff. """
143
        self.model.clear()
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
144
        if bzrlib.version_info < (0, 9):
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
145
            delta = compare_trees(self.old_tree, self.wt)
146
        else:
147
            delta = self.wt.changes_from(self.old_tree)
148
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
149
        self.model.append(None, [ _('Complete Diff'), "" ])
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
150
151
        if len(delta.added):
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
152
            titer = self.model.append(None, [ _('Added'), None ])
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
153
            for path, id, kind in delta.added:
154
                self.model.append(titer, [ path, path ])
155
156
        if len(delta.removed):
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
157
            titer = self.model.append(None, [ _('Removed'), None ])
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
158
            for path, id, kind in delta.removed:
159
                self.model.append(titer, [ path, path ])
160
161
        if len(delta.renamed):
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
162
            titer = self.model.append(None, [ _('Renamed'), None ])
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
163
            for oldpath, newpath, id, kind, text_modified, meta_modified \
164
                    in delta.renamed:
165
                self.model.append(titer, [ oldpath, newpath ])
166
167
        if len(delta.modified):
0.8.55 by Szilveszter Farkas (Phanatic)
Gettext support added.
168
            titer = self.model.append(None, [ _('Modified'), None ])
0.8.26 by Szilveszter Farkas (Phanatic)
Implemented Diff window; added menu.py (was missing from last commit)
169
            for path, id, kind, text_modified, meta_modified in delta.modified:
170
                self.model.append(titer, [ path, path ])
171
172
        self.treeview.expand_all()
173
    
174
    def cursor_changed(self, *args):
175
        """ Callback when the TreeView cursor changes. """
176
        (path, col) = self.treeview.get_cursor()
177
        specific_files = [ self.model[path][1] ]
178
        if specific_files == [ None ]:
179
            return
180
        elif specific_files == [ "" ]:
181
            specific_files = []
182
183
        s = StringIO()
184
        show_diff_trees(self.old_tree, self.wt, s, specific_files)
185
        self.buffer.set_text(s.getvalue())
186
    
187
    def close(self, widget=None):
188
        self.window.destroy()