[gnome-music/wip/jfelder/playlistview-random-song: 14/17] player: Automatically select a song if none is requested



commit ca6077a197b2e2acd8e51c7e86f0736ebc1ce599
Author: Jean Felder <jfelder src gnome org>
Date:   Thu Apr 18 16:06:10 2019 +0200

    player: Automatically select a song if none is requested
    
    When clicking on the play button from PlaylistView, no song is
    selected. By default, the first song is automatically selected and
    provided as parameter to the set_playlist method of Player. This
    choice makes sense if the repeat mode is in a linear mode. However, in
    shuffle mode, it is more logic to select a random song.
    
    This commit adds the automatic selection to the set_playlist
    method in PlayerPlaylist. The next one will remove the automatic
    selection of the first song in PlaylistView.

 gnomemusic/player.py | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index dcf42621..a1592a1b 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -115,9 +115,14 @@ class PlayerPlaylist(GObject.GObject):
         self.connect("notify::repeat-mode", self._on_repeat_mode_changed)
 
     @log
-    def set_playlist(self, playlist_type, playlist_id, model, model_iter):
+    def set_playlist(self, playlist_type, playlist_id, model, model_iter=None):
         """Set a new playlist or change the song being played
 
+        If no song is requested (through model_iter), a song will be
+        automatically selected:
+        * the first song in a linear mode
+        * a random song in shuffle mode
+
         :param PlayerPlaylist.Type playlist_type: playlist type
         :param string playlist_id: unique identifer to recognize the playlist
         :param GtkListStore model: list of songs to play
@@ -126,6 +131,13 @@ class PlayerPlaylist(GObject.GObject):
         :return: True if the playlist has been updated. False otherwise
         :rtype: bool
         """
+        if not model_iter:
+            if self.props.repeat_mode == RepeatMode.SHUFFLE:
+                index = randrange(len(model))
+                model_iter = model.get_iter_from_string(str(index))
+            else:
+                model_iter = model.get_iter_first()
+
         path = model.get_path(model_iter)
         self._current_index = int(path.to_string())
 
@@ -690,7 +702,7 @@ class Player(GObject.GObject):
             self.play()
 
     @log
-    def set_playlist(self, playlist_type, playlist_id, model, iter_):
+    def set_playlist(self, playlist_type, playlist_id, model, iter_=None):
         """Set a new playlist or change the song being played.
 
         :param PlayerPlaylist.Type playlist_type: playlist type


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