[gnome-music/freeze-break: 1/107] Add inital searchbar integration



commit 436d9d24942c958661edac485d9f81e98c2655b9
Author: Seif Lotfy <seif lotfy com>
Date:   Mon Aug 5 00:25:34 2013 +0200

    Add inital searchbar integration

 gnomemusic/searchbar.py |   48 +++++++++++++++++++++++++++++++++++++++++++++++
 gnomemusic/toolbar.py   |    3 +-
 gnomemusic/window.py    |    5 +++-
 3 files changed, 54 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/searchbar.py b/gnomemusic/searchbar.py
new file mode 100644
index 0000000..8df5a5c
--- /dev/null
+++ b/gnomemusic/searchbar.py
@@ -0,0 +1,48 @@
+from gi.repository import Gtk
+from gi.repository import Gdk
+from gi.repository import Gd
+from gi.repository import GObject
+
+
+class Searchbar(Gtk.SearchBar):
+
+    __gsignals__ = {
+        'item-activated': (GObject.SIGNAL_RUN_FIRST, None, ())
+    }
+
+    def __init__(self):
+        Gtk.SearchBar.__init__(self)
+        #this.parent({show_close_button: false});
+        self._search_entry = Gtk.SearchEntry()
+        self.connect_entry(self._search_entry)
+        self._search_entry.connect("changed", self.search_entry_changed)
+        self._search_entry.show()
+        self.add(self._search_entry)
+
+    def set_view_filter(self, model, itr, user_data):
+        if self._searchEntry.get_property("visible"):
+            search_string = self._search_entry.get_text().lower()
+            media = model.get_value(itr, 5)
+            searchable_fields = []
+            artist = media.get_artist()
+            if (media and artist):
+                searchable_fields = [media.get_artist(),
+                                     media.get_album(),
+                                     media.get_title()]
+            else:
+                searchable_fields = [model.get_value(itr, 2),
+                                     model.get_value(itr, 3)]
+            for field in searchable_fields:
+                if field and search_string in field.lower():
+                    return True
+            return False
+        return True
+
+    def _on_item_activated(self):
+        self.emit('item-activated')
+
+    def search_entry_changed(self):
+        self.search_term = self._searchEntry.text;
+        if self.view:
+            self.view.filter.refilter()
+
diff --git a/gnomemusic/toolbar.py b/gnomemusic/toolbar.py
index bcd970f..045bcc6 100644
--- a/gnomemusic/toolbar.py
+++ b/gnomemusic/toolbar.py
@@ -34,7 +34,7 @@ if Gtk.get_minor_version() > 8:
     from gi.repository.Gtk import StackSwitcher
 else:
     from gi.repository.Gd import StackSwitcher
-
+from gnomemusic.searchbar import Searchbar
 
 class ToolbarState:
     SINGLE = 0
@@ -58,6 +58,7 @@ class Toolbar(GObject.GObject):
         self._ui = Gtk.Builder()
         self._ui.add_from_resource('/org/gnome/Music/headerbar.ui')
         self.header_bar = self._ui.get_object('header-bar')
+        self.searchbar = Searchbar()
         self._select_button = self._ui.get_object('select-button')
         self._cancel_button = self._ui.get_object('done-button')
         self._back_button = self._ui.get_object('back-button')
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 98fd005..aefb234 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -30,7 +30,7 @@
 # delete this exception statement from your version.
 
 
-from gi.repository import Gtk, Gio, GLib, Tracker
+from gi.repository import Gtk, Gio, GLib, Tracker, GObject
 from gettext import gettext as _
 
 from gnomemusic.toolbar import Toolbar, ToolbarState
@@ -106,6 +106,7 @@ class Window(Gtk.ApplicationWindow):
             self._box.pack_start(self.toolbar.header_bar, False, False, 0)
             self.set_hide_titlebar_when_maximized(True)
         self._box.pack_start(self._stack, True, True, 0)
+        self._box.pack_start(self.toolbar.searchbar, False, False, 0)
         self._box.pack_start(self.player.eventBox, False, False, 0)
         self._box.pack_start(self.selection_toolbar.eventbox, False, False, 0)
         self.add(self._box)
@@ -123,6 +124,8 @@ class Window(Gtk.ApplicationWindow):
                 self._stack.add_titled(i, i.title, i.title)
 
             self.toolbar.set_stack(self._stack)
+            self.toolbar.searchbar.show()
+            self.toolbar._search_button.bind_property("active", self.toolbar.searchbar, 
"search-mode-enabled", GObject.BindingFlags.BIDIRECTIONAL)
 
             self._on_notify_model_id = self._stack.connect('notify::visible-child', self._on_notify_mode)
             self.connect('destroy', self._notify_mode_disconnect)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]