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

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-16 16:27:59 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-20060716162759-f3e8596921edc374
2006-07-16  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * Done some directory reorganization.
    * setup.py: added some basic install script
    * Began to implement the GTK UI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
import os
14
 
 
15
 
from cStringIO import StringIO
16
 
 
17
 
import gtk
18
 
import gobject
19
 
import pango
20
 
 
21
 
try:
22
 
    import gtksourceview
23
 
    have_gtksourceview = True
24
 
except ImportError:
25
 
    have_gtksourceview = False
26
 
 
27
 
from bzrlib.delta import compare_trees
28
 
from bzrlib.diff import show_diff_trees
29
 
 
30
 
 
31
 
class DiffWindow(gtk.Window):
32
 
    """Diff window.
33
 
 
34
 
    This object represents and manages a single window containing the
35
 
    differences between two revisions on a branch.
36
 
    """
37
 
 
38
 
    def __init__(self, app=None):
39
 
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
40
 
        self.set_border_width(0)
41
 
        self.set_title("bzrk diff")
42
 
 
43
 
        self.app = app
44
 
 
45
 
        # Use two thirds of the screen by default
46
 
        screen = self.get_screen()
47
 
        monitor = screen.get_monitor_geometry(0)
48
 
        width = int(monitor.width * 0.66)
49
 
        height = int(monitor.height * 0.66)
50
 
        self.set_default_size(width, height)
51
 
 
52
 
        self.construct()
53
 
 
54
 
    def construct(self):
55
 
        """Construct the window contents."""
56
 
        hbox = gtk.HBox(spacing=6)
57
 
        hbox.set_border_width(12)
58
 
        self.add(hbox)
59
 
        hbox.show()
60
 
 
61
 
        scrollwin = gtk.ScrolledWindow()
62
 
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
63
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
64
 
        hbox.pack_start(scrollwin, expand=False, fill=True)
65
 
        scrollwin.show()
66
 
 
67
 
        self.model = gtk.TreeStore(str, str)
68
 
        self.treeview = gtk.TreeView(self.model)
69
 
        self.treeview.set_headers_visible(False)
70
 
        self.treeview.set_search_column(1)
71
 
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
72
 
        scrollwin.add(self.treeview)
73
 
        self.treeview.show()
74
 
 
75
 
        cell = gtk.CellRendererText()
76
 
        cell.set_property("width-chars", 20)
77
 
        column = gtk.TreeViewColumn()
78
 
        column.pack_start(cell, expand=True)
79
 
        column.add_attribute(cell, "text", 0)
80
 
        self.treeview.append_column(column)
81
 
 
82
 
 
83
 
        scrollwin = gtk.ScrolledWindow()
84
 
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
85
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
86
 
        hbox.pack_start(scrollwin, expand=True, fill=True)
87
 
        scrollwin.show()
88
 
 
89
 
        if have_gtksourceview:
90
 
            self.buffer = gtksourceview.SourceBuffer()
91
 
            slm = gtksourceview.SourceLanguagesManager()
92
 
            gsl = slm.get_language_from_mime_type("text/x-patch")
93
 
            self.buffer.set_language(gsl)
94
 
            self.buffer.set_highlight(True)
95
 
 
96
 
            sourceview = gtksourceview.SourceView(self.buffer)
97
 
        else:
98
 
            self.buffer = gtk.TextBuffer()
99
 
            sourceview = gtk.TextView(self.buffer)
100
 
 
101
 
        sourceview.set_editable(False)
102
 
        sourceview.modify_font(pango.FontDescription("Monospace"))
103
 
        scrollwin.add(sourceview)
104
 
        sourceview.show()
105
 
 
106
 
    def set_diff(self, branch, revid, parentid):
107
 
        """Set the differences showed by this window.
108
 
 
109
 
        Compares the two trees and populates the window with the
110
 
        differences.
111
 
        """
112
 
        self.rev_tree = branch.revision_tree(revid)
113
 
        self.parent_tree = branch.revision_tree(parentid)
114
 
 
115
 
        self.model.clear()
116
 
        delta = compare_trees(self.parent_tree, self.rev_tree)
117
 
 
118
 
        self.model.append(None, [ "Complete Diff", "" ])
119
 
 
120
 
        if len(delta.added):
121
 
            titer = self.model.append(None, [ "Added", None ])
122
 
            for path, id, kind in delta.added:
123
 
                self.model.append(titer, [ path, path ])
124
 
 
125
 
        if len(delta.removed):
126
 
            titer = self.model.append(None, [ "Removed", None ])
127
 
            for path, id, kind in delta.removed:
128
 
                self.model.append(titer, [ path, path ])
129
 
 
130
 
        if len(delta.renamed):
131
 
            titer = self.model.append(None, [ "Renamed", None ])
132
 
            for oldpath, newpath, id, kind, text_modified, meta_modified \
133
 
                    in delta.renamed:
134
 
                self.model.append(titer, [ oldpath, oldpath ])
135
 
 
136
 
        if len(delta.modified):
137
 
            titer = self.model.append(None, [ "Modified", None ])
138
 
            for path, id, kind, text_modified, meta_modified in delta.modified:
139
 
                self.model.append(titer, [ path, path ])
140
 
 
141
 
        self.treeview.expand_all()
142
 
        self.set_title(os.path.basename(branch.base) + " - bzrk diff")
143
 
 
144
 
    def _treeview_cursor_cb(self, *args):
145
 
        """Callback for when the treeview cursor changes."""
146
 
        (path, col) = self.treeview.get_cursor()
147
 
        specific_files = [ self.model[path][1] ]
148
 
        if specific_files == [ None ]:
149
 
            return
150
 
        elif specific_files == [ "" ]:
151
 
            specific_files = []
152
 
 
153
 
        s = StringIO()
154
 
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
155
 
        self.buffer.set_text(s.getvalue())