[gnome-music/wip/mschraal/gtk4-v3: 240/248] coremodel: Sort the songs model
- From: Marinus Schraal <mschraal src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music/wip/mschraal/gtk4-v3: 240/248] coremodel: Sort the songs model
- Date: Mon, 14 Feb 2022 02:24:03 +0000 (UTC)
commit 85796f9aa0f16e63d9655699b2fb17213c7f5568
Author: Marinus Schraal <mschraal gnome org>
Date: Mon Feb 14 00:56:34 2022 +0100
coremodel: Sort the songs model
gnomemusic/coremodel.py | 30 +++++++++++++++++++++++++-----
gnomemusic/views/songsview.py | 25 ++-----------------------
2 files changed, 27 insertions(+), 28 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 447a1268e..5e1f6d6e5 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -23,7 +23,7 @@
# delete this exception statement from your version.
from __future__ import annotations
-from typing import Optional, Union
+from typing import Any, Optional, Union
import typing
from gi.repository import GLib, GObject, Gio, Gtk
@@ -82,6 +82,7 @@ class CoreModel(GObject.GObject):
"""
super().__init__()
+ self._application = application
self._flatten_model: Optional[Gtk.FlattenListModel] = None
self._player_signal_id = 0
self._current_playlist_model: Optional[Union[
@@ -91,11 +92,12 @@ class CoreModel(GObject.GObject):
self._songs_model_proxy: Gio.ListStore = Gio.ListStore.new(
Gio.ListModel)
- self._songs_model: Gtk.FlattenListModel = Gtk.FlattenListModel.new(
+ self._flatten_songs_model = Gtk.FlattenListModel.new(
self._songs_model_proxy)
- self._songliststore = SongListStore(self._songs_model)
-
- self._application = application
+ self._songs_model = Gtk.SortListModel.new(self._flatten_songs_model)
+ sorter = Gtk.CustomSorter()
+ sorter.set_sort_func(self._songs_sort)
+ self._songs_model.set_sorter(sorter)
self._albums_model_proxy: Gio.ListStore = Gio.ListStore.new(
Gio.ListModel)
@@ -182,6 +184,24 @@ class CoreModel(GObject.GObject):
else:
self.props.songs_available = False
+ def _songs_sort(
+ self, song_a: CoreSong, song_b: CoreSong, data: Any = None) -> int:
+ title_a = song_a.props.title
+ title_b = song_b.props.title
+ song_cmp = (utils.normalize_caseless(title_a)
+ == utils.normalize_caseless(title_b))
+ if not song_cmp:
+ return utils.natural_sort_names(title_a, title_b)
+
+ artist_a = song_a.props.artist
+ artist_b = song_b.props.artist
+ artist_cmp = (utils.normalize_caseless(artist_a)
+ == utils.normalize_caseless(artist_b))
+ if not artist_cmp:
+ return utils.natural_sort_names(artist_a, artist_b)
+
+ return utils.natural_sort_names(song_a.props.album, song_b.props.album)
+
def _filter_selected(self, coresong):
return coresong.props.selected
diff --git a/gnomemusic/views/songsview.py b/gnomemusic/views/songsview.py
index e86531204..c6521660c 100644
--- a/gnomemusic/views/songsview.py
+++ b/gnomemusic/views/songsview.py
@@ -73,12 +73,8 @@ class SongsView(Gtk.Box):
self._list_item_star_controllers: Dict[
Gtk.ListItem, List[GObject.Binding]] = {}
- self._model = Gtk.SortListModel.new(self._coremodel.props.songs)
- sorter = Gtk.CustomSorter()
- sorter.set_sort_func(self._songs_sort)
- self._model.set_sorter(sorter)
-
- self._selection_model = Gtk.MultiSelection.new(self._model)
+ self._selection_model = Gtk.MultiSelection.new(
+ self._coremodel.props.songs)
list_item_factory = Gtk.SignalListItemFactory()
list_item_factory.connect("setup", self._setup_list_item)
@@ -103,23 +99,6 @@ class SongsView(Gtk.Box):
"selection-mode", self, "selection-mode",
GObject.BindingFlags.BIDIRECTIONAL)
- def _songs_sort(self, song_a, song_b, data=None):
- title_a = song_a.props.title
- title_b = song_b.props.title
- song_cmp = (utils.normalize_caseless(title_a)
- == utils.normalize_caseless(title_b))
- if not song_cmp:
- return utils.natural_sort_names(title_a, title_b)
-
- artist_a = song_a.props.artist
- artist_b = song_b.props.artist
- artist_cmp = (utils.normalize_caseless(artist_a)
- == utils.normalize_caseless(artist_b))
- if not artist_cmp:
- return utils.natural_sort_names(artist_a, artist_b)
-
- return utils.natural_sort_names(song_a.props.album, song_b.props.album)
-
def _on_song_activated(self, widget, position):
coresong = widget.props.model[position]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]