/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 annotate/gannotate.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-06 14:53:44 UTC
  • Revision ID: jelmer@samba.org-20110406145344-m6s0i7q7ssjwhmwq
Support use without gtk.Spinner, which is only available in pygtk >= 2.22.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import time
18
18
 
19
 
from gi.repository import GObject
20
 
from gi.repository import Gdk
21
 
from gi.repository import Gtk
22
 
from gi.repository import Pango
 
19
import pygtk
 
20
pygtk.require("2.0")
 
21
import gobject
 
22
import gtk
 
23
import pango
23
24
import re
24
25
 
25
26
from bzrlib import patiencediff
49
50
        self.plain = plain
50
51
        self._branch = branch
51
52
 
52
 
        super(GAnnotateWindow, self).__init__(parent=parent)
 
53
        Window.__init__(self, parent)
53
54
 
54
 
        self.set_icon(
55
 
            self.render_icon_pixbuf(Gtk.STOCK_FIND, Gtk.IconSize.BUTTON))
 
55
        self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
56
56
        self.annotate_colormap = AnnotateColorSaturation()
57
57
 
58
58
        self._create()
70
70
                                   lambda: CURRENT_REVISION)()
71
71
 
72
72
        # [revision id, line number, author, revno, highlight color, line]
73
 
        self.annomodel = Gtk.ListStore(GObject.TYPE_STRING,
74
 
                                       GObject.TYPE_INT,
75
 
                                       GObject.TYPE_STRING,
76
 
                                       GObject.TYPE_STRING,
77
 
                                       GObject.TYPE_STRING,
78
 
                                       GObject.TYPE_STRING)
 
73
        self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
 
74
                                       gobject.TYPE_INT,
 
75
                                       gobject.TYPE_STRING,
 
76
                                       gobject.TYPE_STRING,
 
77
                                       gobject.TYPE_STRING,
 
78
                                       gobject.TYPE_STRING)
79
79
 
80
80
        last_seen = None
81
81
        try:
129
129
        else:
130
130
            row = lineno - 1
131
131
 
132
 
        tree_path = Gtk.TreePath(path=row)
133
 
        self.annoview.set_cursor(tree_path, None, False)
134
 
        self.annoview.scroll_to_cell(tree_path, use_align=True)
 
132
        self.annoview.set_cursor(row)
 
133
        self.annoview.scroll_to_cell(row, use_align=True)
 
134
 
135
135
 
136
136
    def _annotate(self, tree, file_id):
137
137
        current_revision = FakeRevision(CURRENT_REVISION)
167
167
    def _highlight_annotation(self, model, path, iter, now):
168
168
        revision_id, = model.get(iter, REVISION_ID_COL)
169
169
        revision = self.revisions[revision_id]
170
 
        # XXX sinzui 2011-08-12: What does get_color return?
171
 
        color = self.annotate_colormap.get_color(revision, now)
172
 
        model.set_value(iter, HIGHLIGHT_COLOR_COL, color)
 
170
        model.set(iter, HIGHLIGHT_COLOR_COL,
 
171
                  self.annotate_colormap.get_color(revision, now))
173
172
 
174
173
    def _selected_revision(self):
175
174
        (path, col) = self.annoview.get_cursor()
194
193
        self.revisionview = self._create_log_view()
195
194
        self.annoview = self._create_annotate_view()
196
195
 
197
 
        vbox = Gtk.VBox(homogeneous=False, spacing=0)
 
196
        vbox = gtk.VBox(False)
198
197
        vbox.show()
199
198
 
200
 
        sw = Gtk.ScrolledWindow()
201
 
        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
202
 
        sw.set_shadow_type(Gtk.ShadowType.IN)
 
199
        sw = gtk.ScrolledWindow()
 
200
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
201
        sw.set_shadow_type(gtk.SHADOW_IN)
203
202
        sw.add(self.annoview)
204
203
        self.annoview.gwindow = self
205
204
        sw.show()
206
205
 
207
 
        swbox = Gtk.VBox()
208
 
        swbox.pack_start(sw, True, True, 0)
 
206
        swbox = gtk.VBox()
 
207
        swbox.pack_start(sw)
209
208
        swbox.show()
210
209
 
211
 
        hbox = Gtk.HBox(homogeneous=False, spacing=6)
 
210
        hbox = gtk.HBox(False, 6)
212
211
        self.back_button = self._create_back_button()
213
 
        hbox.pack_start(self.back_button, False, True, 0)
 
212
        hbox.pack_start(self.back_button, expand=False, fill=True)
214
213
        self.forward_button = self._create_forward_button()
215
 
        hbox.pack_start(self.forward_button, False, True, 0)
 
214
        hbox.pack_start(self.forward_button, expand=False, fill=True)
216
215
        self.find_button = self._create_find_button()
217
 
        hbox.pack_start(self.find_button, False, True, 0)
 
216
        hbox.pack_start(self.find_button, expand=False, fill=True)
218
217
        self.goto_button = self._create_goto_button()
219
 
        hbox.pack_start(self.goto_button, False, True, 0)
 
218
        hbox.pack_start(self.goto_button, expand=False, fill=True)
220
219
        hbox.show()
221
 
        vbox.pack_start(hbox, False, True, 0)
 
220
        vbox.pack_start(hbox, expand=False, fill=True)
222
221
 
223
 
        self.pane = pane = Gtk.Paned.new(Gtk.Orientation.VERTICAL)
 
222
        self.pane = pane = gtk.VPaned()
224
223
        pane.add1(swbox)
225
224
        pane.add2(self.revisionview)
226
225
        pane.show()
227
 
        vbox.pack_start(pane, True, True, 0)
 
226
        vbox.pack_start(pane, expand=True, fill=True)
228
227
 
229
228
        self._search = SearchBox()
230
 
        swbox.pack_start(self._search, False, True, 0)
231
 
        accels = Gtk.AccelGroup()
232
 
        accels.connect(Gdk.KEY_f, Gdk.ModifierType.CONTROL_MASK,
233
 
                             Gtk.AccelFlags.LOCKED,
 
229
        swbox.pack_start(self._search, expand=False, fill=True)
 
230
        accels = gtk.AccelGroup()
 
231
        accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
 
232
                             gtk.ACCEL_LOCKED,
234
233
                             self._search_by_text)
235
 
        accels.connect(Gdk.KEY_g, Gdk.ModifierType.CONTROL_MASK,
236
 
                             Gtk.AccelFlags.LOCKED,
 
234
        accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
 
235
                             gtk.ACCEL_LOCKED,
237
236
                             self._search_by_line)
238
237
        self.add_accel_group(accels)
239
238
 
248
247
        self._search.set_target(self.annoview, LINE_NUM_COL)
249
248
 
250
249
    def line_diff(self, tv, path, tvc):
251
 
        row = path.get_indices()[0]
 
250
        row = path[0]
252
251
        revision = self.annotations[row]
253
252
        repository = self.branch.repository
254
253
        if revision.revision_id == CURRENT_REVISION:
268
267
 
269
268
 
270
269
    def _create_annotate_view(self):
271
 
        tv = Gtk.TreeView()
 
270
        tv = gtk.TreeView()
272
271
        tv.set_rules_hint(False)
273
272
        tv.connect("cursor-changed", self._activate_selected_revision)
274
273
        tv.show()
275
274
        tv.connect("row-activated", self.line_diff)
276
275
 
277
 
        cell = Gtk.CellRendererText()
 
276
        cell = gtk.CellRendererText()
278
277
        cell.set_property("xalign", 1.0)
279
278
        cell.set_property("ypad", 0)
280
279
        cell.set_property("family", "Monospace")
281
280
        cell.set_property("cell-background-gdk",
282
 
                          tv.get_style().bg[Gtk.StateType.NORMAL])
283
 
        col = Gtk.TreeViewColumn()
 
281
                          tv.get_style().bg[gtk.STATE_NORMAL])
 
282
        col = gtk.TreeViewColumn()
284
283
        col.set_resizable(False)
285
 
        col.pack_start(cell, True)
 
284
        col.pack_start(cell, expand=True)
286
285
        col.add_attribute(cell, "text", LINE_NUM_COL)
287
286
        tv.append_column(col)
288
287
 
289
 
        cell = Gtk.CellRendererText()
 
288
        cell = gtk.CellRendererText()
290
289
        cell.set_property("ypad", 0)
291
 
        cell.set_property("ellipsize", Pango.EllipsizeMode.END)
 
290
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
292
291
        cell.set_property("cell-background-gdk",
293
 
                          self.get_style().bg[Gtk.StateType.NORMAL])
294
 
        col = Gtk.TreeViewColumn("Committer")
 
292
                          self.get_style().bg[gtk.STATE_NORMAL])
 
293
        col = gtk.TreeViewColumn("Committer")
295
294
        col.set_resizable(True)
296
 
        col.pack_start(cell, True)
 
295
        col.pack_start(cell, expand=True)
297
296
        col.add_attribute(cell, "text", COMMITTER_COL)
298
297
        tv.append_column(col)
299
298
 
300
 
        cell = Gtk.CellRendererText()
 
299
        cell = gtk.CellRendererText()
301
300
        cell.set_property("xalign", 1.0)
302
301
        cell.set_property("ypad", 0)
303
302
        cell.set_property("cell-background-gdk",
304
 
                          self.get_style().bg[Gtk.StateType.NORMAL])
305
 
        col = Gtk.TreeViewColumn("Revno")
 
303
                          self.get_style().bg[gtk.STATE_NORMAL])
 
304
        col = gtk.TreeViewColumn("Revno")
306
305
        col.set_resizable(False)
307
 
        col.pack_start(cell, True)
 
306
        col.pack_start(cell, expand=True)
308
307
        col.add_attribute(cell, "markup", REVNO_COL)
309
308
        tv.append_column(col)
310
309
 
311
 
        cell = Gtk.CellRendererText()
 
310
        cell = gtk.CellRendererText()
312
311
        cell.set_property("ypad", 0)
313
312
        cell.set_property("family", "Monospace")
314
 
        col = Gtk.TreeViewColumn()
 
313
        col = gtk.TreeViewColumn()
315
314
        col.set_resizable(False)
316
 
        col.pack_start(cell, True)
 
315
        col.pack_start(cell, expand=True)
317
316
#        col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
318
317
        col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
319
318
        col.add_attribute(cell, "text", TEXT_LINE_COL)
324
323
            return model.get_value(iter, TEXT_LINE_COL).lower().find(key.lower()) == -1
325
324
 
326
325
        tv.set_enable_search(True)
327
 
        tv.set_search_equal_func(search_equal_func, None)
 
326
        tv.set_search_equal_func(search_equal_func)
328
327
 
329
328
        return tv
330
329
 
334
333
        return lv
335
334
 
336
335
    def _create_back_button(self):
337
 
        button = Gtk.Button()
 
336
        button = gtk.Button()
338
337
        button.set_use_stock(True)
339
338
        button.set_label("gtk-go-back")
340
339
        button.connect("clicked", lambda w: self.go_back())
341
 
        button.set_relief(Gtk.ReliefStyle.NONE)
 
340
        button.set_relief(gtk.RELIEF_NONE)
342
341
        button.show()
343
342
        return button
344
343
 
345
344
    def _create_forward_button(self):
346
 
        button = Gtk.Button()
 
345
        button = gtk.Button()
347
346
        button.set_use_stock(True)
348
347
        button.set_label("gtk-go-forward")
349
348
        button.connect("clicked", lambda w: self.go_forward())
350
 
        button.set_relief(Gtk.ReliefStyle.NONE)
 
349
        button.set_relief(gtk.RELIEF_NONE)
351
350
        button.show()
352
351
        button.set_sensitive(False)
353
352
        return button
354
353
 
355
354
    def _create_find_button(self):
356
 
        button = Gtk.Button()
 
355
        button = gtk.Button()
357
356
        button.set_use_stock(True)
358
357
        button.set_label("gtk-find")
359
358
        button.set_tooltip_text("Search for text (Ctrl+F)")
360
359
        button.connect("clicked", self._search_by_text)
361
 
        button.set_relief(Gtk.ReliefStyle.NONE)
 
360
        button.set_relief(gtk.RELIEF_NONE)
362
361
        button.show()
363
362
        button.set_sensitive(True)
364
363
        return button
365
364
 
366
365
    def _create_goto_button(self):
367
 
        button = Gtk.Button()
 
366
        button = gtk.Button()
368
367
        button.set_label("Goto Line")
369
368
        button.set_tooltip_text("Scroll to a line by entering its number (Ctrl+G)")
370
369
        button.connect("clicked", self._search_by_line)
371
 
        button.set_relief(Gtk.ReliefStyle.NONE)
 
370
        button.set_relief(gtk.RELIEF_NONE)
372
371
        button.show()
373
372
        button.set_sensitive(True)
374
373
        return button
397
396
        rev_id = self._selected_revision()
398
397
        if self.file_id in target_tree:
399
398
            offset = self.get_scroll_offset(target_tree)
400
 
            path, col = self.annoview.get_cursor()
401
 
            (row,) = path.get_indices()
 
399
            (row,), col = self.annoview.get_cursor()
402
400
            self.annotate(target_tree, self.branch, self.file_id)
403
401
            new_row = row+offset
404
402
            if new_row < 0:
405
403
                new_row = 0
406
 
            new_path = Gtk.TreePath(path=new_row)
407
 
            self.annoview.set_cursor(new_path, None, False)
 
404
            self.annoview.set_cursor(new_row)
408
405
            return True
409
406
        else:
410
407
            return False
412
409
    def get_scroll_offset(self, tree):
413
410
        old = self.tree.get_file(self.file_id)
414
411
        new = tree.get_file(self.file_id)
415
 
        path, col = self.annoview.get_cursor()
416
 
        (row,) = path.get_indices()
 
412
        (row,), col = self.annoview.get_cursor()
417
413
        matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
418
414
                                                       new.readlines())
419
415
        for i, j, n in matcher.get_matching_blocks():
456
452
            self.__cache[revision_id] = revision
457
453
        return self.__cache[revision_id]
458
454
 
459
 
 
460
 
class SearchBox(Gtk.HBox):
 
455
class SearchBox(gtk.HBox):
461
456
    """A button box for searching in text or lines of annotations"""
462
457
    def __init__(self):
463
 
        super(SearchBox, self).__init__(homogeneous=False, spacing=6)
 
458
        gtk.HBox.__init__(self, False, 6)
464
459
 
465
460
        # Close button
466
 
        button = Gtk.Button()
467
 
        image = Gtk.Image()
468
 
        image.set_from_stock('gtk-stop', Gtk.IconSize.BUTTON)
 
461
        button = gtk.Button()
 
462
        image = gtk.Image()
 
463
        image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
469
464
        button.set_image(image)
470
 
        button.set_relief(Gtk.ReliefStyle.NONE)
471
 
        button.connect("clicked", lambda w: self.hide())
472
 
        self.pack_start(button, False, False, 0)
 
465
        button.set_relief(gtk.RELIEF_NONE)
 
466
        button.connect("clicked", lambda w: self.hide_all())
 
467
        self.pack_start(button, expand=False, fill=False)
473
468
 
474
469
        # Search entry
475
 
        label = Gtk.Label()
 
470
        label = gtk.Label()
476
471
        self._label = label
477
 
        self.pack_start(label, False, False, 0)
 
472
        self.pack_start(label, expand=False, fill=False)
478
473
 
479
 
        entry = Gtk.Entry()
 
474
        entry = gtk.Entry()
480
475
        self._entry = entry
481
476
        entry.connect("activate", lambda w, d: self._do_search(d),
482
477
                      'forward')
483
 
        self.pack_start(entry, False, False, 0)
 
478
        self.pack_start(entry, expand=False, fill=False)
484
479
 
485
480
        # Next/previous buttons
486
 
        button = Gtk.Button('_Next')
487
 
        image = Gtk.Image()
488
 
        image.set_from_stock('gtk-go-forward', Gtk.IconSize.BUTTON)
 
481
        button = gtk.Button('_Next')
 
482
        image = gtk.Image()
 
483
        image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
489
484
        button.set_image(image)
490
485
        button.connect("clicked", lambda w, d: self._do_search(d),
491
486
                       'forward')
492
 
        self.pack_start(button, False, False, 0)
 
487
        self.pack_start(button, expand=False, fill=False)
493
488
 
494
 
        button = Gtk.Button('_Previous')
495
 
        image = Gtk.Image()
496
 
        image.set_from_stock('gtk-go-back', Gtk.IconSize.BUTTON)
 
489
        button = gtk.Button('_Previous')
 
490
        image = gtk.Image()
 
491
        image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
497
492
        button.set_image(image)
498
493
        button.connect("clicked", lambda w, d: self._do_search(d),
499
494
                       'backward')
500
 
        self.pack_start(button, False, False, 0)
 
495
        self.pack_start(button, expand=False, fill=False)
501
496
 
502
497
        # Search options
503
 
        check = Gtk.CheckButton('Match case')
 
498
        check = gtk.CheckButton('Match case')
504
499
        self._match_case = check
505
 
        self.pack_start(check, False, False, 0)
 
500
        self.pack_start(check, expand=False, fill=False)
506
501
 
507
 
        check = Gtk.CheckButton('Regexp')
 
502
        check = gtk.CheckButton('Regexp')
508
503
        check.connect("toggled", lambda w: self._set_label())
509
504
        self._regexp = check
510
 
        self.pack_start(check, False, False, 0)
 
505
        self.pack_start(check, expand=False, fill=False)
511
506
 
512
507
        self._view = None
513
508
        self._column = None
593
588
        for row in iterate(model, start):
594
589
            if self._match(model, row, self._column):
595
590
                path = model.get_path(row)
596
 
                self._view.set_cursor(path, None, False)
 
591
                self._view.set_cursor(path)
597
592
                self._view.scroll_to_cell(path, use_align=True)
598
593
                break