Commit c1de9e1f authored by Marinus Schraal's avatar Marinus Schraal

utils: Move SortListModel PyGI wrapper here

parent 1cd26baa
......@@ -27,7 +27,6 @@ import math
import gi
gi.require_version("Gfm", "0.1")
from gi.repository import GObject, Gio, Gfm, Gtk
from gi._gi import pygobject_new_full
from gnomemusic.coreartist import CoreArtist
from gnomemusic.coregrilo import CoreGrilo
......@@ -94,12 +93,12 @@ class CoreModel(GObject.GObject):
self._album_model = Gio.ListStore()
self._album_model_sort = Gfm.SortListModel.new(self._album_model)
self._album_model_sort.set_sort_func(
self._wrap_list_store_sort_func(self._albums_sort))
utils.wrap_list_store_sort_func(self._albums_sort))
self._artist_model = Gio.ListStore.new(CoreArtist)
self._artist_model_sort = Gfm.SortListModel.new(self._artist_model)
self._artist_model_sort.set_sort_func(
self._wrap_list_store_sort_func(self._artist_sort))
utils.wrap_list_store_sort_func(self._artist_sort))
self._playlist_model = Gio.ListStore.new(CoreSong)
self._playlist_model_sort = Gfm.SortListModel.new(self._playlist_model)
......@@ -128,14 +127,14 @@ class CoreModel(GObject.GObject):
self._playlists_model_sort = Gfm.SortListModel.new(
self._playlists_model_filter)
self._playlists_model_sort.set_sort_func(
self._wrap_list_store_sort_func(self._playlists_sort))
utils.wrap_list_store_sort_func(self._playlists_sort))
self._user_playlists_model_filter = Gfm.FilterListModel.new(
self._playlists_model)
self._user_playlists_model_sort = Gfm.SortListModel.new(
self._user_playlists_model_filter)
self._user_playlists_model_sort.set_sort_func(
self._wrap_list_store_sort_func(self._playlists_sort))
utils.wrap_list_store_sort_func(self._playlists_sort))
self.props.grilo = CoreGrilo(self, application)
# FIXME: Not all instances of internal _grilo use have been
......@@ -184,15 +183,6 @@ class CoreModel(GObject.GObject):
playlist_a.props.creation_date)
return math.copysign(1, date_diff)
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 get_album_model(self, media):
disc_model = Gio.ListStore()
disc_model_sort = Gfm.SortListModel.new(disc_model)
......@@ -201,7 +191,7 @@ class CoreModel(GObject.GObject):
return disc_a.props.disc_nr - disc_b.props.disc_nr
disc_model_sort.set_sort_func(
self._wrap_list_store_sort_func(_disc_order_sort))
utils.wrap_list_store_sort_func(_disc_order_sort))
self.props.grilo.get_album_discs(media, disc_model)
......@@ -219,7 +209,7 @@ class CoreModel(GObject.GObject):
return album_a.props.year > album_b.props.year
albums_model_sort.set_sort_func(
self._wrap_list_store_sort_func(_album_sort))
utils.wrap_list_store_sort_func(_album_sort))
return albums_model_sort
......
......@@ -29,11 +29,11 @@ import time
import gi
gi.require_version('GstPbutils', '1.0')
from gi.repository import GObject, GstPbutils
from gi._gi import pygobject_new_full
from gnomemusic.coresong import CoreSong
from gnomemusic.gstplayer import GstPlayer, Playback
from gnomemusic.widgets.songwidget import SongWidget
import gnomemusic.utils as utils
class RepeatMode(IntEnum):
......@@ -247,22 +247,13 @@ class PlayerPlaylist(GObject.GObject):
return None
def _on_repeat_mode_changed(self, klass, param):
def _wrap_list_store_sort_func(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
# FIXME: This shuffle is too simple.
def _shuffle_sort(song_a, song_b):
return randint(-1, 1)
if self.props.repeat_mode == RepeatMode.SHUFFLE:
self._model.set_sort_func(
_wrap_list_store_sort_func(_shuffle_sort))
utils.wrap_list_store_sort_func(_shuffle_sort))
elif self.props.repeat_mode in [RepeatMode.NONE, RepeatMode.ALL]:
self._model.set_sort_func(None)
......
......@@ -23,7 +23,6 @@
# delete this exception statement from your version.
from gi.repository import Gfm, Gio, GObject, Gtk
from gi._gi import pygobject_new_full
import gnomemusic.utils as utils
......@@ -39,7 +38,7 @@ class SongListStore(Gtk.ListStore):
self._model = Gfm.SortListModel.new(model)
self._model.set_sort_func(
self._wrap_list_store_sort_func(self._songs_sort))
utils.wrap_list_store_sort_func(self._songs_sort))
self.set_column_types([
GObject.TYPE_STRING, # play or invalid icon
......@@ -56,15 +55,6 @@ class SongListStore(Gtk.ListStore):
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):
title_a = song_a.props.title
title_b = song_b.props.title
......
......@@ -28,6 +28,7 @@ import unicodedata
from gettext import gettext as _
from gi.repository import Gio
from gi._gi import pygobject_new_full
class SongStateIcon(Enum):
......@@ -167,3 +168,14 @@ def natural_sort_names(name_a, name_b):
for tmp in re.split(r"(\d+)", normalize_caseless(text))]
return _extract_numbers(name_b) < _extract_numbers(name_a)
def wrap_list_store_sort_func(func):
"""PyGI wrapper for SortListModel set_sort_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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment