[gnome-music/wip/jfelder/core-playlists-view: 1/3] playlistcontrols: Simplify interaction with playlistsview



commit 3921520e615514f0496f61c90748c252cc686457
Author: Jean Felder <jfelder src gnome org>
Date:   Fri Jul 12 22:45:30 2019 +0200

    playlistcontrols: Simplify interaction with playlistsview
    
    Store current playlist as a GObject property to simplify the logic.

 gnomemusic/views/playlistsview.py      | 14 +---------
 gnomemusic/widgets/playlistcontrols.py | 51 ++++++++++++++++++++++++----------
 2 files changed, 37 insertions(+), 28 deletions(-)
---
diff --git a/gnomemusic/views/playlistsview.py b/gnomemusic/views/playlistsview.py
index b521dd82..52f0dded 100644
--- a/gnomemusic/views/playlistsview.py
+++ b/gnomemusic/views/playlistsview.py
@@ -63,7 +63,6 @@ class PlaylistsView(BaseView):
         self.player = player
 
         self._pl_ctrls = PlaylistControls()
-        self._pl_ctrls.connect('playlist-renamed', self._on_playlist_renamed)
 
         self._song_popover = PlaylistContextMenu(self._view)
 
@@ -125,11 +124,6 @@ class PlaylistsView(BaseView):
 
         self.show_all()
 
-    @log
-    def _update_songs_count(self, songs_count):
-        self._songs_count = songs_count
-        self._pl_ctrls.props.songs_count = songs_count
-
     @log
     def _setup_view(self):
         view_container = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
@@ -220,7 +214,6 @@ class PlaylistsView(BaseView):
     def _on_playlist_activated(self, sidebar, row, data=None):
         """Update view with content from selected playlist"""
         playlist = row.props.playlist
-        playlist_name = playlist.props.title
 
         if self.rename_active:
             self._pl_ctrls.disable_rename_playlist()
@@ -229,16 +222,11 @@ class PlaylistsView(BaseView):
             playlist.props.model, self._create_song_widget, playlist)
 
         self._current_playlist = playlist
-        self._pl_ctrls.props.playlist_name = playlist_name
-        self._update_songs_count(playlist.props.count)
-        playlist.connect("notify::count", self._on_song_count_changed)
+        self._pl_ctrls.props.playlist = playlist
 
         self._playlist_rename_action.set_enabled(not playlist.props.is_smart)
         self._playlist_delete_action.set_enabled(not playlist.props.is_smart)
 
-    def _on_song_count_changed(self, playlist, value):
-        self._update_songs_count(playlist.props.count)
-
     def _create_song_widget(self, coresong, playlist):
         can_dnd = not playlist.props.is_smart
         song_widget = SongWidget(coresong, can_dnd, True)
diff --git a/gnomemusic/widgets/playlistcontrols.py b/gnomemusic/widgets/playlistcontrols.py
index 014b2943..9a08a397 100644
--- a/gnomemusic/widgets/playlistcontrols.py
+++ b/gnomemusic/widgets/playlistcontrols.py
@@ -27,16 +27,13 @@ import gettext
 from gi.repository import Gdk, GObject, Gtk
 
 from gnomemusic import log
+from gnomemusic.grilowrappers.grltrackerplaylists import Playlist
 
 
 @Gtk.Template(resource_path='/org/gnome/Music/ui/PlaylistControls.ui')
 class PlaylistControls(Gtk.Grid):
     """Widget holding the playlist controls"""
 
-    __gsignals__ = {
-        'playlist-renamed': (GObject.SignalFlags.RUN_FIRST, None, (str,))
-    }
-
     __gtype_name__ = "PlaylistControls"
 
     _name_stack = Gtk.Template.Child()
@@ -46,19 +43,15 @@ class PlaylistControls(Gtk.Grid):
     _songs_count_label = Gtk.Template.Child()
     _menubutton = Gtk.Template.Child()
 
-    songs_count = GObject.Property(
-        type=int, default=0, minimum=0, flags=GObject.ParamFlags.READWRITE)
-    playlist_name = GObject.Property(
-        type=str, default="", flags=GObject.ParamFlags.READWRITE)
-
     def __repr__(self):
         return '<PlaylistControls>'
 
     def __init__(self):
         super().__init__()
-        self.bind_property("playlist-name", self._name_label, "label")
 
-        self.connect("notify::songs-count", self._on_songs_count_changed)
+        self._playlist = None
+        self._count_id = 0
+        self._binding_count = None
 
     @Gtk.Template.Callback()
     @log
@@ -81,15 +74,14 @@ class PlaylistControls(Gtk.Grid):
         if not new_name:
             return
 
-        self.props.playlist_name = new_name
+        self.props.playlist.props.title = new_name
         self.disable_rename_playlist()
-        self.emit('playlist-renamed', new_name)
 
     @log
     def _on_songs_count_changed(self, klass, data=None):
         self._songs_count_label.props.label = gettext.ngettext(
-            "{} Song", "{} Songs", self.props.songs_count).format(
-                self.props.songs_count)
+            "{} Song", "{} Songs", self.props.playlist.count).format(
+                self.props.playlist.count)
 
     @log
     def enable_rename_playlist(self, pl_torename):
@@ -128,3 +120,32 @@ class PlaylistControls(Gtk.Grid):
     def _set_rename_entry_text_and_focus(self, text):
         self._rename_entry.props.text = text
         self._rename_entry.grab_focus()
+
+    @GObject.Property(
+        type=Playlist, default=None, flags=GObject.ParamFlags.READWRITE)
+    def playlist(self):
+        """Playlist property getter.
+
+        :returns: current playlist
+        :rtype: Playlist
+        """
+        return self._playlist
+
+    @playlist.setter
+    def playlist(self, new_playlist):
+        """Playlist property setter.
+
+        :param Playlistnew_playlist: new playlist
+        """
+        if self._count_id > 0:
+            self._playlist.disconnect(self._count_id)
+            self._count_id = 0
+            self._binding_count.unbind()
+
+        self._playlist = new_playlist
+        self._binding_count = self._playlist.bind_property(
+            "title", self._name_label, "label",
+            GObject.BindingFlags.SYNC_CREATE)
+        self._count_id = self._playlist.connect(
+            "notify::count", self._on_songs_count_changed)
+        self._on_songs_count_changed(None)


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