/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
524 by Jelmer Vernooij
Add search dialog.
1
# Copyright (C) 2008 by Jelmer Vernooij <jelmer@samba.org>
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
try:
18
    import pygtk
19
    pygtk.require("2.0")
20
except:
21
    pass
22
525 by Jelmer Vernooij
Show results in search window.
23
import gobject, gtk
524 by Jelmer Vernooij
Add search dialog.
24
from bzrlib.plugins.search import index as _mod_index
25
525 by Jelmer Vernooij
Show results in search window.
26
524 by Jelmer Vernooij
Add search dialog.
27
class SearchDialog(gtk.Dialog):
28
    """Search dialog."""
533.11.1 by Jelmer Vernooij
Ask user whether to index if there is no index present yet.
29
    def __init__(self, index, parent=None):
524 by Jelmer Vernooij
Add search dialog.
30
        gtk.Dialog.__init__(self, title="Search Revisions",
31
                                  parent=parent,
32
                                  flags=gtk.DIALOG_MODAL,
526 by Jelmer Vernooij
Switch to found revision when clicking ok in search window.
33
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
34
                                           gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
524 by Jelmer Vernooij
Add search dialog.
35
    
36
        # Get arguments
533.11.1 by Jelmer Vernooij
Ask user whether to index if there is no index present yet.
37
        self.index = index
525 by Jelmer Vernooij
Show results in search window.
38
39
        self.searchbar = gtk.HBox()
40
        self.searchentry = gtk.Entry()
41
        self.searchentry.connect('activate', self._searchentry_activate)
527 by Jelmer Vernooij
Remove partial completion support, since I can't find the required functionality in pygtk.
42
        # TODO: Completion using the bzr-search suggests functionality
525 by Jelmer Vernooij
Show results in search window.
43
        self.searchbar.add(self.searchentry)
44
        self.vbox.pack_start(self.searchbar, expand=False, fill=False)
45
526 by Jelmer Vernooij
Switch to found revision when clicking ok in search window.
46
        self.results_model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
525 by Jelmer Vernooij
Show results in search window.
47
        self.results_treeview = gtk.TreeView(self.results_model)
48
49
        documentname_column = gtk.TreeViewColumn("Document", gtk.CellRendererText(), text=0)
50
        self.results_treeview.append_column(documentname_column)
51
52
        summary_column = gtk.TreeViewColumn("Summary", gtk.CellRendererText(), text=1)
53
        self.results_treeview.append_column(summary_column)
54
55
        results_scrolledwindow = gtk.ScrolledWindow()
56
        results_scrolledwindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
57
        results_scrolledwindow.add(self.results_treeview)
58
59
        self.vbox.pack_start(results_scrolledwindow, expand=True, fill=True)
60
61
        self.set_default_size(600, 400)
524 by Jelmer Vernooij
Add search dialog.
62
        # Show the dialog
63
        self.show_all()
525 by Jelmer Vernooij
Show results in search window.
64
526 by Jelmer Vernooij
Switch to found revision when clicking ok in search window.
65
    def get_revision(self):
66
        (path, focus) = self.results_treeview.get_cursor()
67
        if path is None:
68
            return None
69
        iter = self.results_model.get_iter(path)
70
        return self.results_model.get_value(iter, 2)
71
525 by Jelmer Vernooij
Show results in search window.
72
    def _searchentry_activate(self, entry):
73
        self.results_model.clear()
74
        self.index._branch.lock_read()
75
        try:
76
            query = [(query_item,) for query_item in self.searchentry.get_text().split(" ")]
77
            for result in self.index.search(query):
526 by Jelmer Vernooij
Switch to found revision when clicking ok in search window.
78
                if isinstance(result, _mod_index.FileTextHit):
79
                    revid = result.text_key[-1]
80
                elif isinstance(result, _mod_index.RevisionHit):
81
                    revid = result.revision_key[0]
82
                else:
83
                    raise AssertionError()
84
                self.results_model.append([result.document_name(), result.summary(), revid])
525 by Jelmer Vernooij
Show results in search window.
85
        finally:
86
            self.index._branch.unlock()