/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
0.1.1 by Dan Loda
First working version of xannotate.
31
from logview import LogView
0.2.4 by Dan Loda
Extract class SpanSelector out of GAnnotateWindow.
32
from spanselector import SpanSelector
0.1.1 by Dan Loda
First working version of xannotate.
33
34
35
(
36
    REVISION_ID_COL,
37
    LINE_NUM_COL,
38
    COMMITTER_COL,
39
    REVNO_COL,
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
40
    HIGHLIGHT_COLOR_COL,
0.1.1 by Dan Loda
First working version of xannotate.
41
    TEXT_LINE_COL
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
42
) = range(6)
0.1.1 by Dan Loda
First working version of xannotate.
43
44
0.1.2 by Dan Loda
Rename plugin: xannotate => gannotate
45
class GAnnotateWindow(gtk.Window):
0.1.1 by Dan Loda
First working version of xannotate.
46
    """Annotate window."""
47
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
48
    def __init__(self, all=False, plain=False):
49
        self.all = all
50
        self.plain = plain
51
        
0.1.1 by Dan Loda
First working version of xannotate.
52
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
53
        
0.1.1 by Dan Loda
First working version of xannotate.
54
        self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
0.1.18 by Aaron Bentley
Switched to using pink backgrounds
55
        self.annotate_colormap = AnnotateColorSaturation()
0.1.1 by Dan Loda
First working version of xannotate.
56
57
        self._create()
58
        self.revisions = {}
0.1.17 by Dan Loda
A little refactoring.
59
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
60
    def annotate(self, tree, branch, file_id):
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
61
        self.annotations = []
62
        self.branch = branch
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
63
        self.tree = tree
59.2.3 by Aaron Bentley
Gannotate-launched diffs now jump to correct file
64
        self.file_id = file_id
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
65
        self.revision_id = getattr(tree, 'get_revision_id', 
66
                                   lambda: CURRENT_REVISION)()
0.1.1 by Dan Loda
First working version of xannotate.
67
        
0.2.1 by Dan Loda
first go at emacs vc-annotate like highlighting
68
        # [revision id, line number, committer, revno, highlight color, line]
0.1.17 by Dan Loda
A little refactoring.
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)
0.1.1 by Dan Loda
First working version of xannotate.
75
        
76
        last_seen = None
0.4.1 by Aaron Bentley
Updated performance, API use
77
        try:
78
            branch.lock_read()
79
            branch.repository.lock_read()
80
            for line_no, (revision, revno, line)\
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
81
                    in enumerate(self._annotate(tree, file_id)):
0.4.1 by Aaron Bentley
Updated performance, API use
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
                                      ])
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
98
                self.annotations.append(revision)
0.4.1 by Aaron Bentley
Updated performance, API use
99
100
            if not self.plain:
66.6.1 by Aaron Bentley
Remove usused span selector
101
                now = time.time()
102
                self.annomodel.foreach(self._highlight_annotation, now)
0.4.1 by Aaron Bentley
Updated performance, API use
103
        finally:
104
            branch.repository.unlock()
105
            branch.unlock()
0.2.6 by Dan Loda
--plain option to disable highlighting. And update README
106
0.1.17 by Dan Loda
A little refactoring.
107
        self.annoview.set_model(self.annomodel)
108
        self.annoview.grab_focus()
0.1.1 by Dan Loda
First working version of xannotate.
109
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
110
    def jump_to_line(self, lineno):
0.1.17 by Dan Loda
A little refactoring.
111
        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.
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)
79 by Jelmer Vernooij
Handle empty files more gracefully. Fixes #58951.
117
	    return
0.1.12 by Dan Loda
New --line option. Can now jump to a specific line number.
118
        else:
119
            row = lineno - 1
120
0.1.17 by Dan Loda
A little refactoring.
121
        self.annoview.set_cursor(row)
59.2.1 by Aaron Bentley
Gannotate takes a line number
122
        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.
123
66.2.4 by Aaron Bentley
Use dotted revnos instead of 'merge' where possible
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
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
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]'
66.6.6 by Aaron Bentley
Support scrolling based on an offset
142
        current_revision.parent_ids = tree.get_parent_ids()
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)
59.2.2 by Aaron Bentley
Annotate launches a diff for the particular revision
247
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
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
316
        return lv
0.1.1 by Dan Loda
First working version of xannotate.
317
318
    def _create_button_box(self):
319
        box = gtk.HButtonBox()
320
        box.set_layout(gtk.BUTTONBOX_END)
321
        box.show()
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
322
0.1.1 by Dan Loda
First working version of xannotate.
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()
66.5.2 by v.ladeuil+lp at free
Better fix for bug #73965.
328
0.1.1 by Dan Loda
First working version of xannotate.
329
        box.pack_start(button, expand=False, fill=False)
0.2.4 by Dan Loda
Extract class SpanSelector out of GAnnotateWindow.
330
0.1.1 by Dan Loda
First working version of xannotate.
331
        return box
332
66.6.4 by Aaron Bentley
Add back button to see older versions
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:
66.6.6 by Aaron Bentley
Support scrolling based on an offset
351
            offset = self.get_scroll_offset(tree)
352
            (row,), col = self.annoview.get_cursor()
66.6.4 by Aaron Bentley
Add back button to see older versions
353
            self.annotate(tree, self.branch, self.file_id)
66.6.6 by Aaron Bentley
Support scrolling based on an offset
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
66.6.4 by Aaron Bentley
Add back button to see older versions
366
0.1.1 by Dan Loda
First working version of xannotate.
367
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
368
class FakeRevision:
0.1.1 by Dan Loda
First working version of xannotate.
369
    """ A fake revision.
370
371
    For when a revision is referenced but not present.
372
    """
373
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
374
    def __init__(self, revision_id, committer='?'):
0.1.1 by Dan Loda
First working version of xannotate.
375
        self.revision_id = revision_id
376
        self.parent_ids = []
66.2.14 by Aaron Bentley
Annotate showing uncommitted changes
377
        self.committer = committer
0.1.1 by Dan Loda
First working version of xannotate.
378
        self.message = "?"
379
        self.timestamp = 0.0
380
        self.timezone = 0
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