[gnome-music/wip/mschraal/core: 130/164] songliststore: Use the class model directly and sort



commit 022990bd546fe139bd91d0f294e41d5c0a4ea08d
Author: Marinus Schraal <mschraal gnome org>
Date:   Thu Jun 27 09:57:22 2019 +0200

    songliststore: Use the class model directly and sort
    
    Sorting breaks scrolling.

 gnomemusic/coremodel.py     |  2 +-
 gnomemusic/songliststore.py | 33 +++++++++++++++++++++++----------
 2 files changed, 24 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 872b5495..0b717f51 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -210,4 +210,4 @@ class CoreModel(GObject.GObject):
         return albums
 
     def get_songs_model(self):
-        return self._songliststore.props.model
+        return self._songliststore
diff --git a/gnomemusic/songliststore.py b/gnomemusic/songliststore.py
index 7e7382d4..a2a45b28 100644
--- a/gnomemusic/songliststore.py
+++ b/gnomemusic/songliststore.py
@@ -1,16 +1,17 @@
-from gi.repository import GObject, Gtk, GdkPixbuf
+from gi.repository import Gfm, GObject, Gtk, GdkPixbuf
+from gi._gi import pygobject_new_full
 
 
 class SongListStore(Gtk.ListStore):
 
-    model = GObject.Property(type=Gtk.ListStore, default=None)
-
     def __init__(self, model):
         super().__init__()
 
-        self._model = model
+        self._model = Gfm.SortListModel.new(model)
+        self._model.set_sort_func(
+            self._wrap_list_store_sort_func(self._songs_sort))
 
-        self.props.model = Gtk.ListStore(
+        self.set_column_types([
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,
             GObject.TYPE_STRING,    # title
@@ -23,21 +24,33 @@ class SongListStore(Gtk.ListStore):
             GObject.TYPE_INT,       # favorite
             GObject.TYPE_BOOLEAN,   # iter_to_clean
             GObject.TYPE_INT        # validation
-        )
+        ])
 
         self._model.connect("items-changed", self._on_items_changed)
 
+    def _wrap_list_store_sort_func(self, func):
+
+        def wrap(a, b, *user_data):
+            a = pygobject_new_full(a, False)
+            b = pygobject_new_full(b, False)
+            return func(a, b, *user_data)
+
+        return wrap
+
+    def _songs_sort(self, song_a, song_b):
+        return song_a.props.title.lower() > song_b.props.title.lower()
+
     def _on_items_changed(self, model, position, removed, added):
         if removed > 0:
             for i in list(range(removed)):
                 path = Gtk.TreePath.new_from_string("{}".format(position))
-                iter_ = self._gtk_model.get_iter(path)
-                self._gtk_model.remove(iter_)
+                iter_ = self.get_iter(path)
+                self.remove(iter_)
 
         if added > 0:
             for i in list(range(added)):
-                coresong = self._model[position]
-                self.props.model.insert_with_valuesv(
+                coresong = model[position]
+                self.insert_with_valuesv(
                     position, [2, 3, 5, 9],
                     [coresong.props.title, coresong.props.artist, coresong,
                      int(coresong.props.favorite)])


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