~ubuntu-branches/ubuntu/karmic/software-store/karmic

« back to all changes in this revision

Viewing changes to softwarestore/view/widgets/searchentry.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-09-10 16:13:57 UTC
  • Revision ID: james.westby@ubuntu.com-20090910161357-og1z58504dqht7xl
Tags: 0.3.1
* softwarestore/app.py, softwarestore/view/appdetailsview.py:
  - send pkgname instead of apt.Package object in the selected
    signal (LP: #427157)
* softwarestore/view/availablepane.py:
  - Show installed software in the "Get Free Software" pane
    as well (as the spec says) LP: #427014
* softwarestore/view/appview.py:
  - log icon_load errors only in loglevel DEBUG to avoid
    noise on the console
* softwarestore/view/searchentry.py:
  - grab focus when clear icon is clicked (LP: #421552)
* data/ui/SoftwareStore.ui:
  - adjust default size so that all icons fit into the
    window in a english locale (thanks to mac_v)
* updated icon (LP: #427279)
* properly update the current app list (ensure installed emblems
  are good) if the cache is re-opened
* when the cache is refreshed, ensure that the scrolled position
  is kept

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# coding: utf-8
 
2
#
 
3
# SearchEntry - An enhanced search entry with alternating background colouring 
 
4
#               and timeout support
 
5
#
 
6
# Copyright (C) 2007 Sebastian Heinlein
 
7
#               2007-2009 Canonical Ltd.
 
8
#
 
9
# Authors:
 
10
#  Sebastian Heinlein <glatzor@ubuntu.com>
 
11
#
 
12
# This program is free software; you can redistribute it and/or modify it under
 
13
# the terms of the GNU General Public License as published by the Free Software
 
14
# Foundation; either version 3 of the License, or (at your option) any later
 
15
# version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful, but WITHOUT
 
18
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
19
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
20
# details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License along with
 
23
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
24
# Place, Suite 330, Boston, MA 02111-1307 USA
 
25
 
 
26
import sexy
 
27
import gtk
 
28
import gobject
 
29
 
 
30
class SearchEntry(sexy.IconEntry):
 
31
 
 
32
    __gsignals__ = {'terms-changed':(gobject.SIGNAL_RUN_FIRST,
 
33
                                     gobject.TYPE_NONE,
 
34
                                     (gobject.TYPE_STRING,))}
 
35
 
 
36
    SEARCH_TIMEOUT = 200
 
37
 
 
38
    def __init__(self, icon_theme=None):
 
39
        """
 
40
        Creates an enhanced IconEntry that supports a time out when typing
 
41
        and uses a different background colour when the search is active
 
42
        """
 
43
        sexy.IconEntry.__init__(self)
 
44
        if not icon_theme:
 
45
            icon_theme = gtk.icon_theme_get_default()
 
46
        self._handler_changed = self.connect_after("changed",
 
47
                                                   self._on_changed)
 
48
        self.connect("icon-pressed", self._on_icon_pressed)
 
49
        # Does not work - known bug in libsexy
 
50
        # image = gtk.image_new_from_icon_name(gtk.STOCK_CLEAR,
 
51
        #                                      gtk.ICON_SIZE_MENU)
 
52
        # primary
 
53
        image_find = gtk.Image()
 
54
        pixbuf = icon_theme.load_icon(gtk.STOCK_FIND,
 
55
                                      gtk.ICON_SIZE_MENU,
 
56
                                      0)
 
57
        image_find.set_from_pixbuf(pixbuf)
 
58
        self.set_icon(sexy.ICON_ENTRY_PRIMARY, image_find)
 
59
 
 
60
        self.empty_image = gtk.Image()
 
61
        self.clear_image = gtk.Image()
 
62
        pixbuf = icon_theme.load_icon(gtk.STOCK_CLEAR,
 
63
                                      gtk.ICON_SIZE_MENU,
 
64
                                      0)
 
65
        self.clear_image.set_from_pixbuf(pixbuf)
 
66
        self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.clear_image)
 
67
        self.set_icon_highlight(sexy.ICON_ENTRY_PRIMARY, True)
 
68
 
 
69
        # Do not draw a yellow bg if an a11y theme is used
 
70
        settings = gtk.settings_get_default()
 
71
        theme = settings.get_property("gtk-theme-name")
 
72
        self._a11y = (theme.startswith("HighContrast") or
 
73
                      theme.startswith("LowContrast"))
 
74
        self._timeout_id = 0
 
75
 
 
76
    def _on_icon_pressed(self, widget, icon, mouse_button):
 
77
        """
 
78
        Emit the terms-changed signal without any time out when the clear
 
79
        button was clicked
 
80
        """
 
81
        if icon == sexy.ICON_ENTRY_SECONDARY:
 
82
            self.handler_block(self._handler_changed)
 
83
            self.grab_focus()
 
84
            self.set_text("")
 
85
            self._check_style()
 
86
            self.handler_unblock(self._handler_changed)
 
87
            self.emit("terms-changed", self.get_text())
 
88
 
 
89
    def clear(self):
 
90
        self.set_text("")
 
91
        self._check_style()
 
92
 
 
93
    def clear_with_no_signal(self):
 
94
        """Clear and do not send a term-changed signal"""
 
95
        self.handler_block(self._handler_changed)
 
96
        self.clear()
 
97
        self.handler_unblock(self._handler_changed)
 
98
 
 
99
    def _on_changed(self, widget):
 
100
        """
 
101
        Call the actual search method after a small timeout to allow the user
 
102
        to enter a longer search term
 
103
        """
 
104
        self._check_style()
 
105
        if self._timeout_id > 0:
 
106
            gobject.source_remove(self._timeout_id)
 
107
        self._timeout_id = gobject.timeout_add(self.SEARCH_TIMEOUT,
 
108
                                               lambda: self.emit("terms-changed", self.get_text()))
 
109
 
 
110
    def _check_style(self):
 
111
        """
 
112
        Use a different background colour if a search is active
 
113
        """
 
114
        # show/hide icon
 
115
        if self.get_text() != "":
 
116
            self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.clear_image)
 
117
        else:
 
118
            self.set_icon(sexy.ICON_ENTRY_SECONDARY, self.empty_image)
 
119
        # Based on the Rhythmbox code
 
120
        yellowish = gtk.gdk.Color(63479, 63479, 48830)
 
121
        if self._a11y == True:
 
122
            return
 
123
        if self.get_text() == "":
 
124
            self.modify_base(gtk.STATE_NORMAL, None)
 
125
        else:
 
126
            self.modify_base(gtk.STATE_NORMAL, yellowish)
 
127
 
 
128
def on_entry_changed(self, terms):
 
129
    print terms
 
130
 
 
131
if __name__ == "__main__":
 
132
 
 
133
    icons = gtk.icon_theme_get_default()
 
134
    entry = SearchEntry(icons)
 
135
    entry.connect("terms-changed", on_entry_changed)
 
136
 
 
137
    win = gtk.Window()
 
138
    win.add(entry)
 
139
    win.set_size_request(400,400)
 
140
    win.show_all()
 
141
 
 
142
    gtk.main()
 
143