/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.1.1 by Dan Loda
First working version of xannotate.
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
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
17
import time
18
0.1.1 by Dan Loda
First working version of xannotate.
19
import pygtk
20
pygtk.require("2.0")
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
21
import gobject
0.1.1 by Dan Loda
First working version of xannotate.
22
import gtk
23
import pango
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
24
import re
0.1.1 by Dan Loda
First working version of xannotate.
25
66.6.6 by Aaron Bentley
Support scrolling based on an offset
26
from bzrlib import patiencediff, tsort
0.1.1 by Dan Loda
First working version of xannotate.
27
from bzrlib.errors import NoSuchRevision
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
28
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
0.1.1 by Dan Loda
First working version of xannotate.
29
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
30
from colormap import AnnotateColorMap, AnnotateColorSaturation
146 by Jelmer Vernooij
Move more code to top-level directory.
31
from bzrlib.plugins.gtk.logview import LogView
0.1.1 by Dan Loda
First working version of xannotate.
32
33
34
(
35
    REVISION_ID_COL,
36
    LINE_NUM_COL,
37
    COMMITTER_COL,
38
    REVNO_COL,
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
39
    HIGHLIGHT_COLOR_COL,
0.1.1 by Dan Loda
First working version of xannotate.
40
    TEXT_LINE_COL
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
41
) = range(6)
0.1.1 by Dan Loda
First working version of xannotate.
42
43
0.1.2 by Dan Loda
Rename plugin: xannotate => gannotate
44
class GAnnotateWindow(gtk.Window):
0.1.1 by Dan Loda
First working version of xannotate.
45
    """Annotate window."""
46
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
47
    def __init__(self, all=False, plain=False):
48
        self.all = all
49
        self.plain = plain
50
        
0.1.1 by Dan Loda
First working version of xannotate.
51
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
52
        
0.1.1 by Dan Loda
First working version of xannotate.
53
        self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
54
        self.annotate_colormap = AnnotateColorSaturation()
0.1.1 by Dan Loda
First working version of xannotate.
55
56
        self._create()
57
        self.revisions = {}
0.1.17 by Dan Loda
A little refactoring.
58
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
59
    def annotate(self, tree, branch, file_id):
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
60
        self.annotations = []
61
        self.branch = branch
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
62
        self.tree = tree
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
63
        self.file_id = file_id
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
64
        self.revision_id = getattr(tree, 'get_revision_id', 
65
                                   lambda: CURRENT_REVISION)()
0.1.1 by Dan Loda
First working version of xannotate.
66
        
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
67
        # [revision id, line number, committer, revno, highlight color, line]
0.1.17 by Dan Loda
A little refactoring.
68
        self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
69
                                       gobject.TYPE_STRING,
70
                                       gobject.TYPE_STRING,
71
                                       gobject.TYPE_STRING,
72
                                       gobject.TYPE_STRING,
73
                                       gobject.TYPE_STRING)
0.1.1 by Dan Loda
First working version of xannotate.
74
        
75
        last_seen = None
0.4.1 by Aaron Bentley
Updated performance, API use
76
        try:
77
            branch.lock_read()
78
            branch.repository.lock_read()
79
            for line_no, (revision, revno, line)\
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
80
                    in enumerate(self._annotate(tree, file_id)):
0.4.1 by Aaron Bentley
Updated performance, API use
81
                if revision.revision_id == last_seen and not self.all:
82
                    revno = committer = ""
83
                else:
84
                    last_seen = revision.revision_id
85
                    committer = revision.committer
86
87
                if revision.revision_id not in self.revisions:
88
                    self.revisions[revision.revision_id] = revision
89
90
                self.annomodel.append([revision.revision_id,
91
                                       line_no + 1,
92
                                       committer,
93
                                       revno,
94
                                       None,
95
                                       line.rstrip("\r\n")
96
                                      ])
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
97
                self.annotations.append(revision)
0.4.1 by Aaron Bentley
Updated performance, API use
98
99
            if not self.plain:
66.6.1 by Aaron Bentley
Remove usused span selector
100
                now = time.time()
101
                self.annomodel.foreach(self._highlight_annotation, now)
0.4.1 by Aaron Bentley
Updated performance, API use
102
        finally:
103
            branch.repository.unlock()
104
            branch.unlock()
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
105
0.1.17 by Dan Loda
A little refactoring.
106
        self.annoview.set_model(self.annomodel)
107
        self.annoview.grab_focus()
0.1.1 by Dan Loda
First working version of xannotate.
108
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
109
    def jump_to_line(self, lineno):
0.1.17 by Dan Loda
A little refactoring.
110
        if lineno > len(self.annomodel) or lineno < 1:
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
111
            row = 0
112
            # FIXME:should really deal with this in the gui. Perhaps a status
113
            # bar?
114
            print("gannotate: Line number %d does't exist. Defaulting to "
115
                  "line 1." % lineno)
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
116
	    return
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
117
        else:
118
            row = lineno - 1
119
0.1.17 by Dan Loda
A little refactoring.
120
        self.annoview.set_cursor(row)
59.2.1 by Aaron Bentley
Gannotate takes a line number
121
        self.annoview.scroll_to_cell(row, use_align=True)
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
122
66.2.4 by Aaron Bentley
Use dotted revnos instead of 'merge' where possible
123
    def _dotted_revnos(self, repository, revision_id):
124
        """Return a dict of revision_id -> dotted revno
125
        
126
        :param repository: The repository to get the graph from
127
        :param revision_id: The last revision for which this info is needed
128
        """
129
        graph = repository.get_revision_graph(revision_id)
130
        dotted = {}
131
        for n, revision_id, d, revno, e in tsort.merge_sort(graph, 
132
            revision_id, generate_revno=True):
133
            dotted[revision_id] = '.'.join(str(num) for num in revno)
134
        return dotted
135
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
136
    def _annotate(self, tree, file_id):
137
        current_revision = FakeRevision(CURRENT_REVISION)
138
        current_revision.committer = self.branch.get_config().username()
139
        current_revision.timestamp = time.time()
140
        current_revision.message = '[Not yet committed]'
66.6.6 by Aaron Bentley
Support scrolling based on an offset
141
        current_revision.parent_ids = tree.get_parent_ids()
157.1.7 by Aaron Bentley
Fix branch-nick handling
142
        current_revision.properties['branch-nick'] = self.branch.nick
66.2.15 by Aaron Bentley
fix future revno
143
        current_revno = '%d?' % (self.branch.revno() + 1)
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
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
66.2.4 by Aaron Bentley
Use dotted revnos instead of 'merge' where possible
149
        dotted = self._dotted_revnos(repository, revision_id)
66.6.5 by Aaron Bentley
Speed up the 'back' operation
150
        revision_cache = RevisionCache(repository, self.revisions)
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
151
        for origin, text in tree.annotate_iter(file_id):
0.4.1 by Aaron Bentley
Updated performance, API use
152
            rev_id = origin
66.6.7 by Aaron Bentley
Handle current revision better
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:
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
163
                    revision = FakeRevision(rev_id)
164
                    revno = "?"
0.1.1 by Dan Loda
First working version of xannotate.
165
166
            yield revision, revno, text
167
0.2.2 by Dan Loda
Add file's age as default span
168
    def _highlight_annotation(self, model, path, iter, now):
169
        revision_id, = model.get(iter, REVISION_ID_COL)
170
        revision = self.revisions[revision_id]
0.2.5 by Dan Loda
Use granny-like colors as default and rename ColorMap => AnnotateColorMap.
171
        model.set(iter, HIGHLIGHT_COLOR_COL,
0.1.19 by Aaron Bentley
Different colours for different committers
172
                  self.annotate_colormap.get_color(revision, now))
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
173
66.6.4 by Aaron Bentley
Add back button to see older versions
174
    def _selected_revision(self):
0.1.17 by Dan Loda
A little refactoring.
175
        (path, col) = self.annoview.get_cursor()
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
176
        if path is None:
66.6.4 by Aaron Bentley
Add back button to see older versions
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:
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
183
            return
0.1.1 by Dan Loda
First working version of xannotate.
184
        self.logview.set_revision(self.revisions[rev_id])
185
186
    def _create(self):
0.1.17 by Dan Loda
A little refactoring.
187
        self.logview = self._create_log_view()
188
        self.annoview = self._create_annotate_view()
189
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
190
        vbox = gtk.VBox(False, 12)
191
        vbox.set_border_width(12)
192
        vbox.show()
0.1.17 by Dan Loda
A little refactoring.
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)
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
198
        self.annoview.gwindow = self
0.1.17 by Dan Loda
A little refactoring.
199
        sw.show()
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
200
        
0.1.8 by Dan Loda
Remember window state. This introduces the gannotate.conf configuration file,
201
        self.pane = pane = gtk.VPaned()
0.1.17 by Dan Loda
A little refactoring.
202
        pane.add1(sw)
203
        pane.add2(self.logview)
0.1.1 by Dan Loda
First working version of xannotate.
204
        pane.show()
205
        vbox.pack_start(pane, expand=True, fill=True)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
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
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
218
        hbox = gtk.HBox(True, 6)
66.6.4 by Aaron Bentley
Add back button to see older versions
219
        hbox.pack_start(self._create_prev_button(), expand=False, fill=True)
220
        hbox.pack_end(self._create_button_box(), expand=False, fill=True)
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
221
        hbox.show()
222
        vbox.pack_start(hbox, expand=False, fill=True)
223
0.1.1 by Dan Loda
First working version of xannotate.
224
        self.add(vbox)
225
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
226
    def _search_by_text(self, accel_group, window, key, modifiers):
227
        self._search.show_for('text')
66.5.3 by v.ladeuil+lp at free
Realbetter fix for bug #73965.
228
        self._search.set_target(self.annoview, TEXT_LINE_COL)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
229
230
    def _search_by_line(self, accel_group, window, key, modifiers):
231
        self._search.show_for('line')
66.5.3 by v.ladeuil+lp at free
Realbetter fix for bug #73965.
232
        self._search.set_target(self.annoview, LINE_NUM_COL)
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
233
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
234
    def row_diff(self, tv, path, tvc):
235
        row = path[0]
236
        revision = self.annotations[row]
65 by Aaron Bentley
Handle first revision properly
237
        repository = self.branch.repository
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
238
        if revision.revision_id == CURRENT_REVISION:
239
            tree1 = self.tree
240
            tree2 = self.tree.basis_tree()
65 by Aaron Bentley
Handle first revision properly
241
        else:
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
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)
150 by Jelmer Vernooij
Fix handling showing diffs of working tree changes.
247
        from bzrlib.plugins.gtk.diff import DiffWindow
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
248
        window = DiffWindow()
66 by Aaron Bentley
Fix annotate diff window title
249
        window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
250
        window.set_file(tree1.id2path(self.file_id))
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
251
        window.show()
252
253
0.1.1 by Dan Loda
First working version of xannotate.
254
    def _create_annotate_view(self):
0.1.17 by Dan Loda
A little refactoring.
255
        tv = gtk.TreeView()
256
        tv.set_rules_hint(False)
257
        tv.connect("cursor-changed", self._show_log)
258
        tv.show()
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
259
        tv.connect("row-activated", self.row_diff)
0.1.1 by Dan Loda
First working version of xannotate.
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",
0.1.17 by Dan Loda
A little refactoring.
266
                          tv.get_style().bg[gtk.STATE_NORMAL])
0.1.1 by Dan Loda
First working version of xannotate.
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)
0.1.17 by Dan Loda
A little refactoring.
271
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
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)
0.1.17 by Dan Loda
A little refactoring.
282
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
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)
0.1.17 by Dan Loda
A little refactoring.
293
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
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)
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
301
#        col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
302
        col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
0.1.1 by Dan Loda
First working version of xannotate.
303
        col.add_attribute(cell, "text", TEXT_LINE_COL)
0.1.17 by Dan Loda
A little refactoring.
304
        tv.append_column(col)
0.1.1 by Dan Loda
First working version of xannotate.
305
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
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)
66.5.1 by v.ladeuil+lp at free
Minimal fix for bug #73965.
309
0.1.17 by Dan Loda
A little refactoring.
310
        return tv
0.1.1 by Dan Loda
First working version of xannotate.
311
312
    def _create_log_view(self):
0.1.17 by Dan Loda
A little refactoring.
313
        lv = LogView()
314
        lv.show()
315
        return lv
0.1.1 by Dan Loda
First working version of xannotate.
316
317
    def _create_button_box(self):
318
        box = gtk.HButtonBox()
319
        box.set_layout(gtk.BUTTONBOX_END)
320
        box.show()
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
321
0.1.1 by Dan Loda
First working version of xannotate.
322
        button = gtk.Button()
323
        button.set_use_stock(True)
324
        button.set_label("gtk-close")
325
        button.connect("clicked", lambda w: self.destroy())
326
        button.show()
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
327
0.1.1 by Dan Loda
First working version of xannotate.
328
        box.pack_start(button, expand=False, fill=False)
0.2.4 by Dan Loda
Extract class SpanSelector out of GAnnotateWindow.
329
0.1.1 by Dan Loda
First working version of xannotate.
330
        return box
331
66.6.4 by Aaron Bentley
Add back button to see older versions
332
    def _create_prev_button(self):
333
        box = gtk.HButtonBox()
334
        box.set_layout(gtk.BUTTONBOX_START)
335
        box.show()
336
        
337
        button = gtk.Button()
338
        button.set_use_stock(True)
339
        button.set_label("gtk-go-back")
340
        button.connect("clicked", lambda w: self.go_back())
341
        button.show()
342
        box.pack_start(button, expand=False, fill=False)
343
        return box
344
345
    def go_back(self):
346
        rev_id = self._selected_revision()
347
        parent_id = self.revisions[rev_id].parent_ids[0]
348
        tree = self.branch.repository.revision_tree(parent_id)
349
        if self.file_id in tree:
66.6.6 by Aaron Bentley
Support scrolling based on an offset
350
            offset = self.get_scroll_offset(tree)
351
            (row,), col = self.annoview.get_cursor()
66.6.4 by Aaron Bentley
Add back button to see older versions
352
            self.annotate(tree, self.branch, self.file_id)
66.6.6 by Aaron Bentley
Support scrolling based on an offset
353
            self.annoview.set_cursor(row+offset)
354
355
    def get_scroll_offset(self, tree):
356
        old = self.tree.get_file(self.file_id)
357
        new = tree.get_file(self.file_id)
358
        (row,), col = self.annoview.get_cursor()
359
        matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
360
                                                       new.readlines())
361
        for i, j, n in matcher.get_matching_blocks():
362
            if i + n >= row:
363
                return j - i
364
66.6.4 by Aaron Bentley
Add back button to see older versions
365
0.1.1 by Dan Loda
First working version of xannotate.
366
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
367
class FakeRevision:
0.1.1 by Dan Loda
First working version of xannotate.
368
    """ A fake revision.
369
370
    For when a revision is referenced but not present.
371
    """
372
157.1.7 by Aaron Bentley
Fix branch-nick handling
373
    def __init__(self, revision_id, committer='?', nick=None):
0.1.1 by Dan Loda
First working version of xannotate.
374
        self.revision_id = revision_id
375
        self.parent_ids = []
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
376
        self.committer = committer
0.1.1 by Dan Loda
First working version of xannotate.
377
        self.message = "?"
378
        self.timestamp = 0.0
379
        self.timezone = 0
157.1.7 by Aaron Bentley
Fix branch-nick handling
380
        self.properties = {}
0.1.1 by Dan Loda
First working version of xannotate.
381
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
382
383
class RevisionCache(object):
384
    """A caching revision source"""
66.6.5 by Aaron Bentley
Speed up the 'back' operation
385
    def __init__(self, real_source, seed_cache=None):
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
386
        self.__real_source = real_source
66.6.5 by Aaron Bentley
Speed up the 'back' operation
387
        if seed_cache is None:
388
            self.__cache = {}
389
        else:
390
            self.__cache = dict(seed_cache)
0.1.23 by Aaron Bentley
Added revision caching to initial annotation
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]
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
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