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