[gnome-music/wip/gbsneto/contained-playlists: 32/35] playlistsdialog: Sort playlists
- From: Georges Basile Stavracas Neto <gbsneto src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music/wip/gbsneto/contained-playlists: 32/35] playlistsdialog: Sort playlists
- Date: Tue, 6 Dec 2016 16:40:54 +0000 (UTC)
commit e8b7e898e83fd9f1338a560707533612fd1346bb
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date: Thu Dec 1 21:07:38 2016 -0200
playlistsdialog: Sort playlists
By sharing the sorting function from the Playlists view,
we can also sort the playlists list inside the dialog.
gnomemusic/utils.py | 24 ++++++++++++++++++++++++
gnomemusic/views/playlistview.py | 19 +------------------
gnomemusic/widgets/playlistdialog.py | 1 +
3 files changed, 26 insertions(+), 18 deletions(-)
---
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index 5e0152a..f448237 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -22,6 +22,8 @@
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
+from gi.repository import GLib
+
from gettext import gettext as _
@@ -87,3 +89,25 @@ def seconds_to_string(duration):
seconds %= 60
return '{:d}:{:02d}'.format(minutes, seconds)
+
+
+def compare_playlists_by_name(row1, row2, user_data):
+ """Compares two playlist rows by: static, title and id.
+
+ :param row1: a Gtk.ListBoxRow
+ :param row2: a Gtk.ListBoxRow
+ :param user_data: not used
+ :return: 0 if they're equal, 1 if row1 comes before row2, -1 otherwise
+ :rtype: int
+ """
+ if row1.playlist.is_static != row2.playlist.is_static:
+ return row2.playlist.is_static - row1.playlist.is_static
+
+ retval = GLib.strcmp0(row1.playlist.title.lower(),
+ row2.playlist.title.lower())
+
+ if retval != 0:
+ return retval
+
+ return GLib.strcmp0(row1.playlist.id.lower(),
+ row2.playlist.id.lower())
diff --git a/gnomemusic/views/playlistview.py b/gnomemusic/views/playlistview.py
index dc1bf7a..ded0a4a 100644
--- a/gnomemusic/views/playlistview.py
+++ b/gnomemusic/views/playlistview.py
@@ -35,23 +35,6 @@ import gnomemusic.utils as utils
playlists = Playlists.get_default()
-def playlist_listbox_sort_func(row1, row2, user_data):
- """Compares two playlist rows by: static, title and id.
-
- :return: 0 if they're equal, 1 if row1 comes before row2, -1 otherwise
- :rtype: int
- """
- if row1.playlist.is_static != row2.playlist.is_static:
- return row2.playlist.is_static - row1.playlist.is_static
-
- retval = GLib.strcmp0(row1.playlist.title, row2.playlist.title)
-
- if retval != 0:
- return retval
-
- return GLib.strcmp0(row1.playlist.id, row2.playlist.id)
-
-
class PlaylistView(BaseView):
__gsignals__ = {
'playlist-songs-loaded': (GObject.SignalFlags.RUN_FIRST, None, ()),
@@ -65,7 +48,7 @@ class PlaylistView(BaseView):
# The playlist sidebar is a GtkListBox, but we pass a scrolled window
# to the parent class
self.playlists_sidebar = Gtk.ListBox(selection_mode=Gtk.SelectionMode.SINGLE)
- self.playlists_sidebar.set_sort_func(playlist_listbox_sort_func, self)
+ self.playlists_sidebar.set_sort_func(utils.compare_playlists_by_name, self)
swin = Gtk.ScrolledWindow(hscrollbar_policy=Gtk.PolicyType.NEVER,
width_request=220)
diff --git a/gnomemusic/widgets/playlistdialog.py b/gnomemusic/widgets/playlistdialog.py
index 8d37736..eba1394 100644
--- a/gnomemusic/widgets/playlistdialog.py
+++ b/gnomemusic/widgets/playlistdialog.py
@@ -49,6 +49,7 @@ class PlaylistDialog():
self.listbox = self.ui.get_object('listbox')
self.listbox.connect('row-selected', self._on_row_selected)
+ self.listbox.set_sort_func(utils.compare_playlists_by_name, self)
self.title_bar = self.ui.get_object('headerbar1')
self.dialog_box.set_titlebar(self.title_bar)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]