/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 search.py

  • Committer: Daniel Schierbeck
  • Date: 2008-01-13 14:12:49 UTC
  • mto: (423.1.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 429.
  • Revision ID: daniel.schierbeck@gmail.com-20080113141249-gd0i2lknr3yik55r
Moved branch view to its own package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
from gi.repository import GObject
18
 
from gi.repository import Gtk
19
 
 
20
 
from bzrlib.plugins.search import index as _mod_index
21
 
 
22
 
from bzrlib.plugins.gtk.i18n import _i18n
23
 
 
24
 
 
25
 
class SearchDialog(Gtk.Dialog):
26
 
    """Search dialog."""
27
 
 
28
 
    def __init__(self, index, parent=None):
29
 
        super(SearchDialog, self).__init__(
30
 
            title="Search Revisions", parent=parent,
31
 
            flags=Gtk.DialogFlags.MODAL,
32
 
            buttons=(Gtk.STOCK_OK, Gtk.ResponseType.OK,
33
 
                Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL))
34
 
        pixbuf = self.render_icon_pixbuf(Gtk.STOCK_FIND, Gtk.IconSize.MENU)
35
 
        self.set_icon(pixbuf)
36
 
        
37
 
        # Get arguments
38
 
        self.index = index
39
 
 
40
 
        self.searchbar = Gtk.HBox()
41
 
        searchbar_label = Gtk.Label(label=_i18n("Search for:"))
42
 
        self.searchbar.pack_start(searchbar_label, False, False, 0)
43
 
        self.searchentry = Gtk.Entry()
44
 
        self.searchentry.connect('activate', self._searchentry_activate)
45
 
        # TODO: Completion using the bzr-search suggests functionality
46
 
        self.searchbar.add(self.searchentry)
47
 
        self.get_content_area().pack_start(self.searchbar, False, False, 0)
48
 
 
49
 
        self.results_model = Gtk.ListStore(
50
 
            GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_PYOBJECT)
51
 
        self.results_treeview = Gtk.TreeView(self.results_model)
52
 
        self.results_treeview.connect(
53
 
            "row-activated", self._searchresult_row_activated)
54
 
 
55
 
        documentname_column = Gtk.TreeViewColumn(
56
 
            _i18n("Document"), Gtk.CellRendererText(), text=0)
57
 
        self.results_treeview.append_column(documentname_column)
58
 
 
59
 
        summary_column = Gtk.TreeViewColumn(
60
 
            _i18n("Summary"), Gtk.CellRendererText(), text=1)
61
 
        self.results_treeview.append_column(summary_column)
62
 
 
63
 
        results_scrolledwindow = Gtk.ScrolledWindow()
64
 
        results_scrolledwindow.set_policy(
65
 
            Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
66
 
        results_scrolledwindow.add(self.results_treeview)
67
 
 
68
 
        self.get_content_area().pack_start(
69
 
            results_scrolledwindow, True, True, 0)
70
 
 
71
 
        self.set_default_size(600, 400)
72
 
        # Show the dialog
73
 
        self.show_all()
74
 
 
75
 
    def get_revision(self):
76
 
        (path, focus) = self.results_treeview.get_cursor()
77
 
        if path is None:
78
 
            return None
79
 
        iter = self.results_model.get_iter(path)
80
 
        return self.results_model.get_value(iter, 2)
81
 
 
82
 
    def _searchentry_activate(self, entry):
83
 
        self.results_model.clear()
84
 
        self.index._branch.lock_read()
85
 
        try:
86
 
            query = [
87
 
                (query_item,)
88
 
                for query_item in self.searchentry.get_text().split(" ")]
89
 
            for result in self.index.search(query):
90
 
                if isinstance(result, _mod_index.FileTextHit):
91
 
                    revid = result.text_key[-1]
92
 
                elif isinstance(result, _mod_index.RevisionHit):
93
 
                    revid = result.revision_key[0]
94
 
                else:
95
 
                    raise AssertionError()
96
 
                self.results_model.append(
97
 
                    [result.document_name(), result.summary(), revid])
98
 
        finally:
99
 
            self.index._branch.unlock()
100
 
    
101
 
    def _searchresult_row_activated(self, treeview, path, view_column):
102
 
        self.response(Gtk.ResponseType.OK)