[gnome-music/freeze-break: 5/107] Make some views filterable



commit 48ef950267091bcb3c6bb6b7bfd2729edf50cdea
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Mon Aug 5 16:58:00 2013 +0200

    Make some views filterable

 gnomemusic/searchbar.py |   31 +++++++++++++++----------------
 gnomemusic/toolbar.py   |    2 +-
 gnomemusic/view.py      |   43 +++++++++++++++++++++++++++++++++----------
 gnomemusic/widgets.py   |    2 +-
 4 files changed, 50 insertions(+), 28 deletions(-)
---
diff --git a/gnomemusic/searchbar.py b/gnomemusic/searchbar.py
index 92dc18d..7f8b79f 100644
--- a/gnomemusic/searchbar.py
+++ b/gnomemusic/searchbar.py
@@ -2,30 +2,30 @@ from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import Gd
 from gi.repository import GObject
+from gnomemusic.view import ViewContainer
 
 
 class Searchbar(Gtk.SearchBar):
 
-    __gsignals__ = {
-        'item-activated': (GObject.SIGNAL_RUN_FIRST, None, ())
-    }
-
-    def __init__(self):
+    def __init__(self, stack_switcher):
         Gtk.SearchBar.__init__(self)
-        #this.parent({show_close_button: false});
+        self.stack_switcher = stack_switcher
         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)
+        self.connect("notify::search-mode-enabled", self.prepare_search_filter)
 
     def set_view_filter(self, model, itr, user_data):
-        if self._searchEntry.get_property("visible"):
+        if self._search_entry.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):
+            artist = None
+            if media and media.get_url():
+                artist = media.get_artist()
+            if media and artist:
                 searchable_fields = [media.get_artist(),
                                      media.get_album(),
                                      media.get_title()]
@@ -38,12 +38,11 @@ class Searchbar(Gtk.SearchBar):
             return False
         return True
 
-    def _on_item_activated(self):
-        self.emit('item-activated')
+    def prepare_search_filter(self, widget, data):
+        self.view = self.stack_switcher.get_stack().get_visible_child()
+        self.view.filter.set_visible_func(self.set_view_filter)
 
     def search_entry_changed(self, widget):
-        #print (widget)
-        self.search_term = self._search_entry.get_text();
-        #if self.view:
-        #    self.view.filter.refilter()
-
+        self.search_term = self._search_entry.get_text()
+        if self.view:
+            self.view.filter.refilter()
diff --git a/gnomemusic/toolbar.py b/gnomemusic/toolbar.py
index 045bcc6..652282a 100644
--- a/gnomemusic/toolbar.py
+++ b/gnomemusic/toolbar.py
@@ -58,7 +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.searchbar = Searchbar(self._stack_switcher)
         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/view.py b/gnomemusic/view.py
index b5622c4..b7d8788 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -61,6 +61,7 @@ class ViewContainer(Stack):
     errorIconName = 'dialog-error-symbolic'
     starIconName = 'starred-symbolic'
     countQuery = None
+    filter = None
 
     def __init__(self, title, header_bar, selection_toolbar, useStack=False):
         Stack.__init__(self,
@@ -72,7 +73,7 @@ class ViewContainer(Stack):
         self._adjustmentValueId = 0
         self._adjustmentChangedId = 0
         self._scrollbarVisibleId = 0
-        self._model = Gtk.ListStore(
+        self._model = Gtk.TreeStore(
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,
@@ -90,6 +91,8 @@ class ViewContainer(Stack):
         )
         self.view.set_view_type(Gd.MainViewType.ICON)
         self.view.set_model(self._model)
+        self.filter = self._model.filter_new(None)
+        self.view.set_model(self.filter)
         self.vadjustment = self.view.get_vadjustment()
         self.selection_toolbar = selection_toolbar
         box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
@@ -226,15 +229,23 @@ class ViewContainer(Stack):
         title = albumArtCache.get_media_title(item)
         item.set_title(title)
 
-        icon_name = self.nowPlayingIconName
-        _iter = self._model.insert_with_valuesv(
-            -1,
-            [0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
-            [str(item.get_id()), '', title,
-             artist, self._symbolicIcon, item,
-             -1, icon_name, False, False])
-        self.player.discover_item(item, self._on_discovered, _iter)
-        GLib.idle_add(self._update_album_art, item, _iter)
+        def add_new_item():
+            _iter = self._model.append(None)
+            icon_name = self.nowPlayingIconName
+            if item.get_url():
+                try:
+                    self.player.discoverer.discover_uri(item.get_url())
+                except:
+                    print('failed to discover url ' + item.get_url())
+                    icon_name = self.errorIconName
+            self._model.set(_iter,
+                            [0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
+                            [str(item.get_id()), '', title,
+                             artist, self._symbolicIcon, item,
+                             -1, icon_name, False, icon_name == self.errorIconName])
+            GLib.idle_add(self._update_album_art, item, _iter)
+
+        GLib.idle_add(add_new_item)
 
     def _insert_album_art(self, item, cb_item, itr, x=False):
         if item and cb_item and not item.get_thumbnail():
@@ -353,6 +364,10 @@ class Songs(ViewContainer):
         if not item:
             return
         self._offset += 1
+<<<<<<< HEAD
+=======
+        _iter = self._model.append(None)
+>>>>>>> Make some views filterable
         item.set_title(albumArtCache.get_media_title(item))
         artist = item.get_string(Grl.METADATA_KEY_ARTIST)\
             or item.get_author()\
@@ -505,7 +520,11 @@ class Artists (ViewContainer):
     def _populate(self, data=None):
         selection = self.view.get_generic_view().get_selection()
         if not selection.get_selected()[1]:
+<<<<<<< HEAD
             self._allIter = self._model.insert_with_valuesv(-1, [2], [_("All Artists")])
+=======
+            self._allIter = self._model.append(None)
+>>>>>>> Make some views filterable
             self._last_selection = self._allIter
             self._artists[_("All Artists").lower()] =\
                 {'iter': self._allIter, 'albums': []}
@@ -571,7 +590,11 @@ class Artists (ViewContainer):
             or item.get_author()\
             or _("Unknown Artist")
         if not artist.lower() in self._artists:
+<<<<<<< HEAD
             _iter = self._model.insert_with_valuesv(-1, [2], [artist])
+=======
+            _iter = self._model.append(None)
+>>>>>>> Make some views filterable
             self._artists[artist.lower()] = {'iter': _iter, 'albums': []}
 
         self._artists[artist.lower()]['albums'].append(item)
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 6980d6e..2d32783 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -556,7 +556,7 @@ class ArtistAlbumWidget(Gtk.HBox):
                     int(i % (len(self.tracks) / 2)), 1, 1
                 )
                 track.song_widget = song_widget
-                itr = self.model.append()
+                itr = self.model.append(None)
                 song_widget._iter = itr
                 song_widget.model = self.model
                 song_widget.title = ui.get_object('title')


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