/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/diff.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-08-03 10:44:56 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060803104456-b5f901b6775ef158
Push dialog now displays stored location

2006-08-03  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * olive/frontend/gtk/push.py: display known push location if available
    * olive/backend/info.py: implemented get_push_location()

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
46
if bzrlib.version_info[1] < 9:
 
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
from dialog import OliveDialog
 
55
 
 
56
class OliveDiff:
 
57
    """ Display Diff window and perform the needed actions. """
 
58
    def __init__(self, gladefile, comm):
 
59
        """ Initialize the Diff window. """
 
60
        self.gladefile = gladefile
 
61
        self.glade = gtk.glade.XML(self.gladefile, 'window_diff')
 
62
        
 
63
        self.comm = comm
 
64
        
 
65
        self.dialog = OliveDialog(self.gladefile)
 
66
        
 
67
        # Check if current location is a branch
 
68
        try:
 
69
            (self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
 
70
            branch = self.wt.branch
 
71
        except errors.NotBranchError:
 
72
            self.notbranch = True
 
73
            return
 
74
        except:
 
75
            raise
 
76
 
 
77
        file_id = self.wt.path2id(path)
 
78
 
 
79
        self.notbranch = False
 
80
        if file_id is None:
 
81
            self.notbranch = True
 
82
            return
 
83
        
 
84
        # Set the old working tree
 
85
        self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
86
        
 
87
        # Get the Diff window widget
 
88
        self.window = self.glade.get_widget('window_diff')
 
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:
 
106
            self.dialog.error_dialog('Directory is not a branch.')
 
107
        else:
 
108
            self.window.show_all()
 
109
    
 
110
    def _create_file_view(self):
 
111
        """ Create the list of files. """
 
112
        self.model = gtk.TreeStore(str, str)
 
113
        self.treeview = self.glade.get_widget('treeview_diff_files')
 
114
        self.treeview.set_model(self.model)
 
115
        
 
116
        cell = gtk.CellRendererText()
 
117
        cell.set_property("width-chars", 20)
 
118
        column = gtk.TreeViewColumn()
 
119
        column.pack_start(cell, expand=True)
 
120
        column.add_attribute(cell, "text", 0)
 
121
        self.treeview.append_column(column)
 
122
        
 
123
        if have_gtksourceview:
 
124
            self.buffer = gtksourceview.SourceBuffer()
 
125
            slm = gtksourceview.SourceLanguagesManager()
 
126
            gsl = slm.get_language_from_mime_type("text/x-patch")
 
127
            self.buffer.set_language(gsl)
 
128
            self.buffer.set_highlight(True)
 
129
 
 
130
            sourceview = gtksourceview.SourceView(self.buffer)
 
131
        else:
 
132
            self.buffer = gtk.TextBuffer()
 
133
            sourceview = gtk.TextView(self.buffer)
 
134
 
 
135
        sourceview.set_editable(False)
 
136
        sourceview.modify_font(pango.FontDescription("Monospace"))
 
137
        scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
 
138
        scrollwin_diff.add(sourceview)
 
139
    
 
140
    def _init_diff(self):
 
141
        """ Generate initial diff. """
 
142
        self.model.clear()
 
143
        if bzrlib.version_info[1] < 9:
 
144
            delta = compare_trees(self.old_tree, self.wt)
 
145
        else:
 
146
            delta = self.wt.changes_from(self.old_tree)
 
147
 
 
148
        self.model.append(None, [ "Complete Diff", "" ])
 
149
 
 
150
        if len(delta.added):
 
151
            titer = self.model.append(None, [ "Added", None ])
 
152
            for path, id, kind in delta.added:
 
153
                self.model.append(titer, [ path, path ])
 
154
 
 
155
        if len(delta.removed):
 
156
            titer = self.model.append(None, [ "Removed", None ])
 
157
            for path, id, kind in delta.removed:
 
158
                self.model.append(titer, [ path, path ])
 
159
 
 
160
        if len(delta.renamed):
 
161
            titer = self.model.append(None, [ "Renamed", None ])
 
162
            for oldpath, newpath, id, kind, text_modified, meta_modified \
 
163
                    in delta.renamed:
 
164
                self.model.append(titer, [ oldpath, newpath ])
 
165
 
 
166
        if len(delta.modified):
 
167
            titer = self.model.append(None, [ "Modified", None ])
 
168
            for path, id, kind, text_modified, meta_modified in delta.modified:
 
169
                self.model.append(titer, [ path, path ])
 
170
 
 
171
        self.treeview.expand_all()
 
172
    
 
173
    def cursor_changed(self, *args):
 
174
        """ Callback when the TreeView cursor changes. """
 
175
        (path, col) = self.treeview.get_cursor()
 
176
        specific_files = [ self.model[path][1] ]
 
177
        if specific_files == [ None ]:
 
178
            return
 
179
        elif specific_files == [ "" ]:
 
180
            specific_files = []
 
181
 
 
182
        s = StringIO()
 
183
        show_diff_trees(self.old_tree, self.wt, s, specific_files)
 
184
        self.buffer.set_text(s.getvalue())
 
185
    
 
186
    def close(self, widget=None):
 
187
        self.window.destroy()