/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: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-16 23:51:20 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-20060716235120-0ea07ea7fc6e9330
2006-07-17  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * setup.py: some tweaks
    * olive-gtk: added main executable
    * olive/frontend/gtk/handler.py: signal handler class added (OliveHandler)
    * olive/frontend/gtk/__init__.py: main GTK class added (OliveGtk)
    * olive.glade: GTK UI description added (generated by Glade)
    * oliveicon2.png: icon added

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Dan Loda <danloda@gmail.com>
2
 
 
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
 
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
 
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
import time
18
 
 
19
 
import pygtk
20
 
pygtk.require("2.0")
21
 
import gobject
22
 
import gtk
23
 
import pango
24
 
import re
25
 
 
26
 
from bzrlib import patiencediff, tsort
27
 
from bzrlib.errors import NoSuchRevision
28
 
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
29
 
 
30
 
from colormap import AnnotateColorMap, AnnotateColorSaturation
31
 
from logview import LogView
32
 
from spanselector import SpanSelector
33
 
 
34
 
 
35
 
(
36
 
    REVISION_ID_COL,
37
 
    LINE_NUM_COL,
38
 
    COMMITTER_COL,
39
 
    REVNO_COL,
40
 
    HIGHLIGHT_COLOR_COL,
41
 
    TEXT_LINE_COL
42
 
) = range(6)
43
 
 
44
 
 
45
 
class GAnnotateWindow(gtk.Window):
46
 
    """Annotate window."""
47
 
 
48
 
    def __init__(self, all=False, plain=False):
49
 
        self.all = all
50
 
        self.plain = plain
51
 
        
52
 
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
53
 
        
54
 
        self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
55
 
        self.annotate_colormap = AnnotateColorSaturation()
56
 
 
57
 
        self._create()
58
 
        self.revisions = {}
59
 
 
60
 
    def annotate(self, tree, branch, file_id):
61
 
        self.annotations = []
62
 
        self.branch = branch
63
 
        self.tree = tree
64
 
        self.file_id = file_id
65
 
        self.revision_id = getattr(tree, 'get_revision_id', 
66
 
                                   lambda: CURRENT_REVISION)()
67
 
        
68
 
        # [revision id, line number, committer, revno, highlight color, line]
69
 
        self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
70
 
                                       gobject.TYPE_STRING,
71
 
                                       gobject.TYPE_STRING,
72
 
                                       gobject.TYPE_STRING,
73
 
                                       gobject.TYPE_STRING,
74
 
                                       gobject.TYPE_STRING)
75
 
        
76
 
        last_seen = None
77
 
        try:
78
 
            branch.lock_read()
79
 
            branch.repository.lock_read()
80
 
            for line_no, (revision, revno, line)\
81
 
                    in enumerate(self._annotate(tree, file_id)):
82
 
                if revision.revision_id == last_seen and not self.all:
83
 
                    revno = committer = ""
84
 
                else:
85
 
                    last_seen = revision.revision_id
86
 
                    committer = revision.committer
87
 
 
88
 
                if revision.revision_id not in self.revisions:
89
 
                    self.revisions[revision.revision_id] = revision
90
 
 
91
 
                self.annomodel.append([revision.revision_id,
92
 
                                       line_no + 1,
93
 
                                       committer,
94
 
                                       revno,
95
 
                                       None,
96
 
                                       line.rstrip("\r\n")
97
 
                                      ])
98
 
                self.annotations.append(revision)
99
 
 
100
 
            if not self.plain:
101
 
                now = time.time()
102
 
                self.annomodel.foreach(self._highlight_annotation, now)
103
 
        finally:
104
 
            branch.repository.unlock()
105
 
            branch.unlock()
106
 
 
107
 
        self.annoview.set_model(self.annomodel)
108
 
        self.annoview.grab_focus()
109
 
 
110
 
    def jump_to_line(self, lineno):
111
 
        if lineno > len(self.annomodel) or lineno < 1:
112
 
            row = 0
113
 
            # FIXME:should really deal with this in the gui. Perhaps a status
114
 
            # bar?
115
 
            print("gannotate: Line number %d does't exist. Defaulting to "
116
 
                  "line 1." % lineno)
117
 
            return
118
 
        else:
119
 
            row = lineno - 1
120
 
 
121
 
        self.annoview.set_cursor(row)
122
 
        self.annoview.scroll_to_cell(row, use_align=True)
123
 
 
124
 
    def _dotted_revnos(self, repository, revision_id):
125
 
        """Return a dict of revision_id -> dotted revno
126
 
        
127
 
        :param repository: The repository to get the graph from
128
 
        :param revision_id: The last revision for which this info is needed
129
 
        """
130
 
        graph = repository.get_revision_graph(revision_id)
131
 
        dotted = {}
132
 
        for n, revision_id, d, revno, e in tsort.merge_sort(graph, 
133
 
            revision_id, generate_revno=True):
134
 
            dotted[revision_id] = '.'.join(str(num) for num in revno)
135
 
        return dotted
136
 
 
137
 
    def _annotate(self, tree, file_id):
138
 
        current_revision = FakeRevision(CURRENT_REVISION)
139
 
        current_revision.committer = self.branch.get_config().username()
140
 
        current_revision.timestamp = time.time()
141
 
        current_revision.message = '[Not yet committed]'
142
 
        current_revision.parent_ids = tree.get_parent_ids()
143
 
        current_revno = '%d?' % (self.branch.revno() + 1)
144
 
        repository = self.branch.repository
145
 
        if self.revision_id == CURRENT_REVISION:
146
 
            revision_id = self.branch.last_revision()
147
 
        else:
148
 
            revision_id = self.revision_id
149
 
        dotted = self._dotted_revnos(repository, revision_id)
150
 
        revision_cache = RevisionCache(repository, self.revisions)
151
 
        for origin, text in tree.annotate_iter(file_id):
152
 
            rev_id = origin
153
 
            if rev_id == CURRENT_REVISION:
154
 
                revision = current_revision
155
 
                revno = current_revno
156
 
            else:
157
 
                try:
158
 
                    revision = revision_cache.get_revision(rev_id)
159
 
                    revno = dotted.get(rev_id, 'merge')
160
 
                    if len(revno) > 15:
161
 
                        revno = 'merge'
162
 
                except NoSuchRevision:
163
 
                    revision = FakeRevision(rev_id)
164
 
                    revno = "?"
165
 
 
166
 
            yield revision, revno, text
167
 
 
168
 
    def _highlight_annotation(self, model, path, iter, now):
169
 
        revision_id, = model.get(iter, REVISION_ID_COL)
170
 
        revision = self.revisions[revision_id]
171
 
        model.set(iter, HIGHLIGHT_COLOR_COL,
172
 
                  self.annotate_colormap.get_color(revision, now))
173
 
 
174
 
    def _selected_revision(self):
175
 
        (path, col) = self.annoview.get_cursor()
176
 
        if path is None:
177
 
            return None
178
 
        return self.annomodel[path][REVISION_ID_COL]
179
 
 
180
 
    def _show_log(self, w):
181
 
        rev_id = self._selected_revision()
182
 
        if rev_id is None:
183
 
            return
184
 
        self.logview.set_revision(self.revisions[rev_id])
185
 
 
186
 
    def _create(self):
187
 
        self.logview = self._create_log_view()
188
 
        self.annoview = self._create_annotate_view()
189
 
 
190
 
        vbox = gtk.VBox(False, 12)
191
 
        vbox.set_border_width(12)
192
 
        vbox.show()
193
 
 
194
 
        sw = gtk.ScrolledWindow()
195
 
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
196
 
        sw.set_shadow_type(gtk.SHADOW_IN)
197
 
        sw.add(self.annoview)
198
 
        self.annoview.gwindow = self
199
 
        sw.show()
200
 
        
201
 
        self.pane = pane = gtk.VPaned()
202
 
        pane.add1(sw)
203
 
        pane.add2(self.logview)
204
 
        pane.show()
205
 
        vbox.pack_start(pane, expand=True, fill=True)
206
 
 
207
 
        self._search = SearchBox()
208
 
        vbox.pack_start(self._search, expand=False, fill=True)
209
 
        accels = gtk.AccelGroup()
210
 
        accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
211
 
                             gtk.ACCEL_LOCKED,
212
 
                             self._search_by_text)
213
 
        accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
214
 
                             gtk.ACCEL_LOCKED,
215
 
                             self._search_by_line)
216
 
        self.add_accel_group(accels)
217
 
 
218
 
        hbox = gtk.HBox(True, 6)
219
 
        hbox.pack_start(self._create_prev_button(), expand=False, fill=True)
220
 
        hbox.pack_end(self._create_button_box(), expand=False, fill=True)
221
 
        hbox.show()
222
 
        vbox.pack_start(hbox, expand=False, fill=True)
223
 
 
224
 
        self.add(vbox)
225
 
 
226
 
    def _search_by_text(self, accel_group, window, key, modifiers):
227
 
        self._search.show_for('text')
228
 
        self._search.set_target(self.annoview, TEXT_LINE_COL)
229
 
 
230
 
    def _search_by_line(self, accel_group, window, key, modifiers):
231
 
        self._search.show_for('line')
232
 
        self._search.set_target(self.annoview, LINE_NUM_COL)
233
 
 
234
 
    def row_diff(self, tv, path, tvc):
235
 
        row = path[0]
236
 
        revision = self.annotations[row]
237
 
        repository = self.branch.repository
238
 
        if revision.revision_id == CURRENT_REVISION:
239
 
            tree1 = self.tree
240
 
            tree2 = self.tree.basis_tree()
241
 
        else:
242
 
            tree1 = repository.revision_tree(revision.revision_id)
243
 
            if len(revision.parent_ids) > 0:
244
 
                tree2 = repository.revision_tree(revision.parent_ids[0])
245
 
            else:
246
 
                tree2 = repository.revision_tree(NULL_REVISION)
247
 
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
248
 
        window = DiffWindow()
249
 
        window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
250
 
        window.set_file(tree1.id2path(self.file_id))
251
 
        window.show()
252
 
 
253
 
 
254
 
    def _create_annotate_view(self):
255
 
        tv = gtk.TreeView()
256
 
        tv.set_rules_hint(False)
257
 
        tv.connect("cursor-changed", self._show_log)
258
 
        tv.show()
259
 
        tv.connect("row-activated", self.row_diff)
260
 
 
261
 
        cell = gtk.CellRendererText()
262
 
        cell.set_property("xalign", 1.0)
263
 
        cell.set_property("ypad", 0)
264
 
        cell.set_property("family", "Monospace")
265
 
        cell.set_property("cell-background-gdk",
266
 
                          tv.get_style().bg[gtk.STATE_NORMAL])
267
 
        col = gtk.TreeViewColumn()
268
 
        col.set_resizable(False)
269
 
        col.pack_start(cell, expand=True)
270
 
        col.add_attribute(cell, "text", LINE_NUM_COL)
271
 
        tv.append_column(col)
272
 
 
273
 
        cell = gtk.CellRendererText()
274
 
        cell.set_property("ypad", 0)
275
 
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
276
 
        cell.set_property("cell-background-gdk",
277
 
                          self.get_style().bg[gtk.STATE_NORMAL])
278
 
        col = gtk.TreeViewColumn("Committer")
279
 
        col.set_resizable(True)
280
 
        col.pack_start(cell, expand=True)
281
 
        col.add_attribute(cell, "text", COMMITTER_COL)
282
 
        tv.append_column(col)
283
 
 
284
 
        cell = gtk.CellRendererText()
285
 
        cell.set_property("xalign", 1.0)
286
 
        cell.set_property("ypad", 0)
287
 
        cell.set_property("cell-background-gdk",
288
 
                          self.get_style().bg[gtk.STATE_NORMAL])
289
 
        col = gtk.TreeViewColumn("Revno")
290
 
        col.set_resizable(False)
291
 
        col.pack_start(cell, expand=True)
292
 
        col.add_attribute(cell, "markup", REVNO_COL)
293
 
        tv.append_column(col)
294
 
 
295
 
        cell = gtk.CellRendererText()
296
 
        cell.set_property("ypad", 0)
297
 
        cell.set_property("family", "Monospace")
298
 
        col = gtk.TreeViewColumn()
299
 
        col.set_resizable(False)
300
 
        col.pack_start(cell, expand=True)
301
 
#        col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
302
 
        col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
303
 
        col.add_attribute(cell, "text", TEXT_LINE_COL)
304
 
        tv.append_column(col)
305
 
 
306
 
        # FIXME: Now that C-f is now used for search by text we
307
 
        # may as well disable the auto search.
308
 
        tv.set_search_column(LINE_NUM_COL)
309
 
 
310
 
        return tv
311
 
 
312
 
    def _create_log_view(self):
313
 
        lv = LogView()
314
 
        lv.show()
315
 
 
316
 
        return lv
317
 
 
318
 
    def _create_button_box(self):
319
 
        box = gtk.HButtonBox()
320
 
        box.set_layout(gtk.BUTTONBOX_END)
321
 
        box.show()
322
 
 
323
 
        button = gtk.Button()
324
 
        button.set_use_stock(True)
325
 
        button.set_label("gtk-close")
326
 
        button.connect("clicked", lambda w: self.destroy())
327
 
        button.show()
328
 
 
329
 
        box.pack_start(button, expand=False, fill=False)
330
 
 
331
 
        return box
332
 
 
333
 
    def _create_prev_button(self):
334
 
        box = gtk.HButtonBox()
335
 
        box.set_layout(gtk.BUTTONBOX_START)
336
 
        box.show()
337
 
        
338
 
        button = gtk.Button()
339
 
        button.set_use_stock(True)
340
 
        button.set_label("gtk-go-back")
341
 
        button.connect("clicked", lambda w: self.go_back())
342
 
        button.show()
343
 
        box.pack_start(button, expand=False, fill=False)
344
 
        return box
345
 
 
346
 
    def go_back(self):
347
 
        rev_id = self._selected_revision()
348
 
        parent_id = self.revisions[rev_id].parent_ids[0]
349
 
        tree = self.branch.repository.revision_tree(parent_id)
350
 
        if self.file_id in tree:
351
 
            offset = self.get_scroll_offset(tree)
352
 
            (row,), col = self.annoview.get_cursor()
353
 
            self.annotate(tree, self.branch, self.file_id)
354
 
            self.annoview.set_cursor(row+offset)
355
 
 
356
 
    def get_scroll_offset(self, tree):
357
 
        old = self.tree.get_file(self.file_id)
358
 
        new = tree.get_file(self.file_id)
359
 
        (row,), col = self.annoview.get_cursor()
360
 
        matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
361
 
                                                       new.readlines())
362
 
        for i, j, n in matcher.get_matching_blocks():
363
 
            if i + n >= row:
364
 
                return j - i
365
 
 
366
 
 
367
 
 
368
 
class FakeRevision:
369
 
    """ A fake revision.
370
 
 
371
 
    For when a revision is referenced but not present.
372
 
    """
373
 
 
374
 
    def __init__(self, revision_id, committer='?'):
375
 
        self.revision_id = revision_id
376
 
        self.parent_ids = []
377
 
        self.committer = committer
378
 
        self.message = "?"
379
 
        self.timestamp = 0.0
380
 
        self.timezone = 0
381
 
 
382
 
 
383
 
class RevisionCache(object):
384
 
    """A caching revision source"""
385
 
    def __init__(self, real_source, seed_cache=None):
386
 
        self.__real_source = real_source
387
 
        if seed_cache is None:
388
 
            self.__cache = {}
389
 
        else:
390
 
            self.__cache = dict(seed_cache)
391
 
 
392
 
    def get_revision(self, revision_id):
393
 
        if revision_id not in self.__cache:
394
 
            revision = self.__real_source.get_revision(revision_id)
395
 
            self.__cache[revision_id] = revision
396
 
        return self.__cache[revision_id]
397
 
 
398
 
class SearchBox(gtk.HBox):
399
 
    """A button box for searching in text or lines of annotations"""
400
 
    def __init__(self):
401
 
        gtk.HBox.__init__(self, False, 6)
402
 
 
403
 
        # Close button
404
 
        button = gtk.Button()
405
 
        image = gtk.Image()
406
 
        image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
407
 
        button.set_image(image)
408
 
        button.set_relief(gtk.RELIEF_NONE)
409
 
        button.connect("clicked", lambda w: self.hide_all())
410
 
        self.pack_start(button, expand=False, fill=False)
411
 
 
412
 
        # Search entry
413
 
        label = gtk.Label()
414
 
        self._label = label
415
 
        self.pack_start(label, expand=False, fill=False)
416
 
 
417
 
        entry = gtk.Entry()
418
 
        self._entry = entry
419
 
        entry.connect("activate", lambda w, d: self._do_search(d),
420
 
                      'forward')
421
 
        self.pack_start(entry, expand=False, fill=False)
422
 
 
423
 
        # Next/previous buttons
424
 
        button = gtk.Button('_Next')
425
 
        image = gtk.Image()
426
 
        image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
427
 
        button.set_image(image)
428
 
        button.connect("clicked", lambda w, d: self._do_search(d),
429
 
                       'forward')
430
 
        self.pack_start(button, expand=False, fill=False)
431
 
 
432
 
        button = gtk.Button('_Previous')
433
 
        image = gtk.Image()
434
 
        image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
435
 
        button.set_image(image)
436
 
        button.connect("clicked", lambda w, d: self._do_search(d),
437
 
                       'backward')
438
 
        self.pack_start(button, expand=False, fill=False)
439
 
 
440
 
        # Search options
441
 
        check = gtk.CheckButton('Match case')
442
 
        self._match_case = check
443
 
        self.pack_start(check, expand=False, fill=False)
444
 
 
445
 
        check = gtk.CheckButton('Regexp')
446
 
        check.connect("toggled", lambda w: self._set_label())
447
 
        self._regexp = check
448
 
        self.pack_start(check, expand=False, fill=False)
449
 
 
450
 
        self._view = None
451
 
        self._column = None
452
 
        # Note that we stay hidden (we do not call self.show_all())
453
 
 
454
 
 
455
 
    def show_for(self, kind):
456
 
        self._kind = kind
457
 
        self.show_all()
458
 
        self._set_label()
459
 
        # Hide unrelated buttons
460
 
        if kind == 'line':
461
 
            self._match_case.hide()
462
 
            self._regexp.hide()
463
 
        # Be ready
464
 
        self._entry.grab_focus()
465
 
 
466
 
    def _set_label(self):
467
 
        if self._kind == 'line':
468
 
            self._label.set_text('Find Line: ')
469
 
        else:
470
 
            if self._regexp.get_active():
471
 
                self._label.set_text('Find Regexp: ')
472
 
            else:
473
 
                self._label.set_text('Find Text: ')
474
 
 
475
 
    def set_target(self, view,column):
476
 
        self._view = view
477
 
        self._column = column
478
 
 
479
 
    def _match(self, model, iterator, column):
480
 
        matching_case = self._match_case.get_active()
481
 
        string, = model.get(iterator, column)
482
 
        key = self._entry.get_text()
483
 
        if self._regexp.get_active():
484
 
            if matching_case:
485
 
                match = re.compile(key).search(string, 1)
486
 
            else:
487
 
                match = re.compile(key, re.I).search(string, 1)
488
 
        else:
489
 
            if not matching_case:
490
 
                string = string.lower()
491
 
                key = key.lower()
492
 
            match = string.find(key) != -1
493
 
 
494
 
        return match
495
 
 
496
 
    def _iterate_rows_forward(self, model, start):
497
 
        model_size = len(model)
498
 
        current = start + 1
499
 
        while model_size != 0:
500
 
            if current >= model_size: current =  0
501
 
            yield model.get_iter_from_string('%d' % current)
502
 
            if current == start: raise StopIteration
503
 
            current += 1
504
 
 
505
 
    def _iterate_rows_backward(self, model, start):
506
 
        model_size = len(model)
507
 
        current = start - 1
508
 
        while model_size != 0:
509
 
            if current < 0: current = model_size - 1
510
 
            yield model.get_iter_from_string('%d' % current)
511
 
            if current == start: raise StopIteration
512
 
            current -= 1
513
 
 
514
 
    def _do_search(self, direction):
515
 
        if direction == 'forward':
516
 
            iterate = self._iterate_rows_forward
517
 
        else:
518
 
            iterate = self._iterate_rows_backward
519
 
 
520
 
        model, sel = self._view.get_selection().get_selected()
521
 
        if sel is None:
522
 
            start = 0
523
 
        else:
524
 
            path = model.get_string_from_iter(sel)
525
 
            start = int(path)
526
 
 
527
 
        for row in iterate(model, start):
528
 
            if self._match(model, row, self._column):
529
 
                path = model.get_path(row)
530
 
                self._view.set_cursor(path)
531
 
                self._view.scroll_to_cell(path, use_align=True)
532
 
                break