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

  • Committer: John Arbash Meinel
  • Date: 2007-09-27 22:16:26 UTC
  • mto: (322.1.1 trunk) (330.3.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 368.
  • Revision ID: john@arbash-meinel.com-20070927221626-tlpljk85fnavtmiu
Just playing around. 
Moved the diff contents into a separate class, so that we
could reuse it without having another top level window.
Playing around with how to lay that out in the gcommit view.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
from bzrlib.trace import warning
38
38
 
39
39
 
40
 
class DiffWindow(gtk.Window):
41
 
    """Diff window.
42
 
 
43
 
    This object represents and manages a single window containing the
44
 
    differences between two revisions on a branch.
45
 
    """
 
40
class DiffDisplay(gtk.ScrolledWindow):
 
41
    """This is the soft and chewy filling for a DiffWindow."""
46
42
 
47
43
    def __init__(self):
48
 
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
49
 
        self.set_border_width(0)
50
 
        self.set_title("bzrk diff")
51
 
 
52
 
        # Use two thirds of the screen by default
53
 
        screen = self.get_screen()
54
 
        monitor = screen.get_monitor_geometry(0)
55
 
        width = int(monitor.width * 0.66)
56
 
        height = int(monitor.height * 0.66)
57
 
        self.set_default_size(width, height)
 
44
        gtk.ScrolledWindow.__init__(self)
58
45
 
59
46
        self.construct()
 
47
        self.rev_tree = None
 
48
        self.parent_tree = None
60
49
 
61
50
    def construct(self):
62
 
        """Construct the window contents."""
63
 
        # The   window  consists  of   a  pane   containing:  the
64
 
        # hierarchical list  of files on  the left, and  the diff
65
 
        # for the currently selected file on the right.
66
 
        pane = gtk.HPaned()
67
 
        self.add(pane)
68
 
        pane.show()
69
 
 
70
 
        # The file hierarchy: a scrollable treeview
71
 
        scrollwin = gtk.ScrolledWindow()
72
 
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
73
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
74
 
        pane.pack1(scrollwin)
75
 
        scrollwin.show()
76
 
 
77
 
        self.model = gtk.TreeStore(str, str)
78
 
        self.treeview = gtk.TreeView(self.model)
79
 
        self.treeview.set_headers_visible(False)
80
 
        self.treeview.set_search_column(1)
81
 
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
82
 
        scrollwin.add(self.treeview)
83
 
        self.treeview.show()
84
 
 
85
 
        cell = gtk.CellRendererText()
86
 
        cell.set_property("width-chars", 20)
87
 
        column = gtk.TreeViewColumn()
88
 
        column.pack_start(cell, expand=True)
89
 
        column.add_attribute(cell, "text", 0)
90
 
        self.treeview.append_column(column)
91
 
 
92
 
        # The diffs of the  selected file: a scrollable source or
93
 
        # text view
94
 
        scrollwin = gtk.ScrolledWindow()
95
 
        scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
96
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
97
 
        pane.pack2(scrollwin)
98
 
        scrollwin.show()
 
51
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
52
        self.set_shadow_type(gtk.SHADOW_IN)
99
53
 
100
54
        if have_gtksourceview:
101
55
            self.buffer = gtksourceview.SourceBuffer()
114
68
 
115
69
        sourceview.set_editable(False)
116
70
        sourceview.modify_font(pango.FontDescription("Monospace"))
117
 
        scrollwin.add(sourceview)
 
71
        self.add(sourceview)
118
72
        sourceview.show()
119
73
 
120
 
    def set_diff(self, description, rev_tree, parent_tree):
121
 
        """Set the differences showed by this window.
122
 
 
123
 
        Compares the two trees and populates the window with the
124
 
        differences.
125
 
        """
126
 
        self.rev_tree = rev_tree
127
 
        self.parent_tree = parent_tree
128
 
 
129
 
        self.model.clear()
130
 
        delta = self.rev_tree.changes_from(self.parent_tree)
131
 
 
132
 
        self.model.append(None, [ "Complete Diff", "" ])
133
 
 
134
 
        if len(delta.added):
135
 
            titer = self.model.append(None, [ "Added", None ])
136
 
            for path, id, kind in delta.added:
137
 
                self.model.append(titer, [ path, path ])
138
 
 
139
 
        if len(delta.removed):
140
 
            titer = self.model.append(None, [ "Removed", None ])
141
 
            for path, id, kind in delta.removed:
142
 
                self.model.append(titer, [ path, path ])
143
 
 
144
 
        if len(delta.renamed):
145
 
            titer = self.model.append(None, [ "Renamed", None ])
146
 
            for oldpath, newpath, id, kind, text_modified, meta_modified \
147
 
                    in delta.renamed:
148
 
                self.model.append(titer, [ oldpath, newpath ])
149
 
 
150
 
        if len(delta.modified):
151
 
            titer = self.model.append(None, [ "Modified", None ])
152
 
            for path, id, kind, text_modified, meta_modified in delta.modified:
153
 
                self.model.append(titer, [ path, path ])
154
 
 
155
 
        self.treeview.expand_all()
156
 
        self.set_title(description + " - bzrk diff")
157
 
 
158
 
    def set_file(self, file_path):
159
 
        tv_path = None
160
 
        for data in self.model:
161
 
            for child in data.iterchildren():
162
 
                if child[0] == file_path or child[1] == file_path:
163
 
                    tv_path = child.path
164
 
                    break
165
 
        if tv_path is None:
166
 
            raise NoSuchFile(file_path)
167
 
        self.treeview.set_cursor(tv_path)
168
 
        self.treeview.scroll_to_cell(tv_path)
169
 
 
170
 
    def _treeview_cursor_cb(self, *args):
171
 
        """Callback for when the treeview cursor changes."""
172
 
        (path, col) = self.treeview.get_cursor()
173
 
        specific_files = [ self.model[path][1] ]
174
 
        if specific_files == [ None ]:
175
 
            return
176
 
        elif specific_files == [ "" ]:
177
 
            specific_files = None
178
 
 
179
 
        s = StringIO()
180
 
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
181
 
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
182
 
 
183
74
    @staticmethod
184
75
    def apply_gedit_colors(lang):
185
76
        """Set style for lang to that specified in gedit configuration.
186
77
 
187
78
        This method needs the gconf module.
188
 
        
 
79
 
189
80
        :param lang: a gtksourceview.SourceLanguage object.
190
81
        """
191
82
        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
261
152
                except IOError, e:
262
153
                    warning('could not open file %s: %s' % (f, str(e)))
263
154
                else:
264
 
                    colors.update(DiffWindow.parse_colordiffrc(f))
 
155
                    colors.update(DiffDisplay.parse_colordiffrc(f))
265
156
                    f.close()
266
157
 
267
158
        if not colors:
303
194
    @staticmethod
304
195
    def parse_colordiffrc(fileobj):
305
196
        """Parse fileobj as a colordiff configuration file.
306
 
        
 
197
 
307
198
        :return: A dict with the key -> value pairs.
308
199
        """
309
200
        colors = {}
316
207
            colors[key.strip()] = val.strip()
317
208
        return colors
318
209
 
 
210
    def set_trees(self, rev_tree, parent_tree):
 
211
        self.rev_tree = rev_tree
 
212
        self.parent_tree = parent_tree
 
213
 
 
214
    def show_diff(self, specific_files):
 
215
        s = StringIO()
 
216
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
 
217
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
 
218
 
 
219
 
 
220
class DiffWindow(gtk.Window):
 
221
    """Diff window.
 
222
 
 
223
    This object represents and manages a single window containing the
 
224
    differences between two revisions on a branch.
 
225
    """
 
226
 
 
227
    def __init__(self):
 
228
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
 
229
        self.set_border_width(0)
 
230
        self.set_title("bzrk diff")
 
231
 
 
232
        # Use two thirds of the screen by default
 
233
        screen = self.get_screen()
 
234
        monitor = screen.get_monitor_geometry(0)
 
235
        width = int(monitor.width * 0.66)
 
236
        height = int(monitor.height * 0.66)
 
237
        self.set_default_size(width, height)
 
238
 
 
239
        self.construct()
 
240
 
 
241
    def construct(self):
 
242
        """Construct the window contents."""
 
243
        # The   window  consists  of   a  pane   containing:  the
 
244
        # hierarchical list  of files on  the left, and  the diff
 
245
        # for the currently selected file on the right.
 
246
        pane = gtk.HPaned()
 
247
        self.add(pane)
 
248
        pane.show()
 
249
 
 
250
        # The file hierarchy: a scrollable treeview
 
251
        scrollwin = gtk.ScrolledWindow()
 
252
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
253
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
254
        pane.pack1(scrollwin)
 
255
        scrollwin.show()
 
256
 
 
257
        self.model = gtk.TreeStore(str, str)
 
258
        self.treeview = gtk.TreeView(self.model)
 
259
        self.treeview.set_headers_visible(False)
 
260
        self.treeview.set_search_column(1)
 
261
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
 
262
        scrollwin.add(self.treeview)
 
263
        self.treeview.show()
 
264
 
 
265
        cell = gtk.CellRendererText()
 
266
        cell.set_property("width-chars", 20)
 
267
        column = gtk.TreeViewColumn()
 
268
        column.pack_start(cell, expand=True)
 
269
        column.add_attribute(cell, "text", 0)
 
270
        self.treeview.append_column(column)
 
271
 
 
272
        # The diffs of the  selected file: a scrollable source or
 
273
        # text view
 
274
        self.diff_win = DiffDisplay()
 
275
        pane.pack2(self.diff_win)
 
276
        self.diff_win.show()
 
277
 
 
278
    def set_diff(self, description, rev_tree, parent_tree):
 
279
        """Set the differences showed by this window.
 
280
 
 
281
        Compares the two trees and populates the window with the
 
282
        differences.
 
283
        """
 
284
        self.diff_win.set_trees(rev_tree, parent_tree)
 
285
        self.rev_tree = rev_tree
 
286
        self.parent_tree = parent_tree
 
287
 
 
288
        self.model.clear()
 
289
        delta = self.rev_tree.changes_from(self.parent_tree)
 
290
 
 
291
        self.model.append(None, [ "Complete Diff", "" ])
 
292
 
 
293
        if len(delta.added):
 
294
            titer = self.model.append(None, [ "Added", None ])
 
295
            for path, id, kind in delta.added:
 
296
                self.model.append(titer, [ path, path ])
 
297
 
 
298
        if len(delta.removed):
 
299
            titer = self.model.append(None, [ "Removed", None ])
 
300
            for path, id, kind in delta.removed:
 
301
                self.model.append(titer, [ path, path ])
 
302
 
 
303
        if len(delta.renamed):
 
304
            titer = self.model.append(None, [ "Renamed", None ])
 
305
            for oldpath, newpath, id, kind, text_modified, meta_modified \
 
306
                    in delta.renamed:
 
307
                self.model.append(titer, [ oldpath, newpath ])
 
308
 
 
309
        if len(delta.modified):
 
310
            titer = self.model.append(None, [ "Modified", None ])
 
311
            for path, id, kind, text_modified, meta_modified in delta.modified:
 
312
                self.model.append(titer, [ path, path ])
 
313
 
 
314
        self.treeview.expand_all()
 
315
        self.set_title(description + " - bzrk diff")
 
316
 
 
317
    def set_file(self, file_path):
 
318
        tv_path = None
 
319
        for data in self.model:
 
320
            for child in data.iterchildren():
 
321
                if child[0] == file_path or child[1] == file_path:
 
322
                    tv_path = child.path
 
323
                    break
 
324
        if tv_path is None:
 
325
            raise NoSuchFile(file_path)
 
326
        self.treeview.set_cursor(tv_path)
 
327
        self.treeview.scroll_to_cell(tv_path)
 
328
 
 
329
    def _treeview_cursor_cb(self, *args):
 
330
        """Callback for when the treeview cursor changes."""
 
331
        (path, col) = self.treeview.get_cursor()
 
332
        specific_files = [ self.model[path][1] ]
 
333
        if specific_files == [ None ]:
 
334
            return
 
335
        elif specific_files == [ "" ]:
 
336
            specific_files = None
 
337
 
 
338
        self.diff_win.show_diff(specific_files)