[gnome-music/wip/mschraal/core: 122/164] Move AlbumWidget2 back as AlbumWidget



commit 0f92ad0859b210f45cb93e6fc87bf9e6898e6e53
Author: Marinus Schraal <mschraal gnome org>
Date:   Wed Jun 26 15:27:57 2019 +0200

    Move AlbumWidget2 back as AlbumWidget

 data/org.gnome.Music.gresource.xml |   1 -
 data/ui/AlbumWidget.ui             |   6 +-
 data/ui/AlbumWidget2.ui            | 246 -------------------------------------
 gnomemusic/views/albumsview.py     |   4 +-
 gnomemusic/views/searchview.py     |  15 ++-
 gnomemusic/widgets/albumwidget.py  | 236 +++++++++++------------------------
 gnomemusic/widgets/albumwidget2.py | 192 -----------------------------
 7 files changed, 83 insertions(+), 617 deletions(-)
---
diff --git a/data/org.gnome.Music.gresource.xml b/data/org.gnome.Music.gresource.xml
index 953c5c03..3992ffa4 100644
--- a/data/org.gnome.Music.gresource.xml
+++ b/data/org.gnome.Music.gresource.xml
@@ -7,7 +7,6 @@
     <file preprocess="xml-stripblanks">ui/AboutDialog.ui</file>
     <file preprocess="xml-stripblanks">ui/AlbumCover.ui</file>
     <file preprocess="xml-stripblanks">ui/AlbumWidget.ui</file>
-    <file preprocess="xml-stripblanks">ui/AlbumWidget2.ui</file>
     <file preprocess="xml-stripblanks">ui/AppMenu.ui</file>
     <file preprocess="xml-stripblanks">ui/ArtistAlbumWidget.ui</file>
     <file preprocess="xml-stripblanks">ui/ArtistAlbumsWidget.ui</file>
diff --git a/data/ui/AlbumWidget.ui b/data/ui/AlbumWidget.ui
index 49e7b7f9..1cad2407 100644
--- a/data/ui/AlbumWidget.ui
+++ b/data/ui/AlbumWidget.ui
@@ -224,15 +224,13 @@
                   <!-- position as the top of the album songs, however -->
                   <!-- since we set a top margins for the discbox -->
                   <!-- subtract that margin here. A cleaner solution is appreciated. -->
-                  <object class="DiscListBox" id="_disc_listbox">
+                  <object class="DiscListBox" id="_listbox">
                     <property name="can_focus">False</property>
                     <property name="margin_top">48</property>
                     <property name="margin_bottom">64</property>
                     <property name="margin_end">32</property>
-                    <!-- <property name="orientation">vertical</property> -->
-                    <property name="selection_mode_allowed">True</property>
+                    <property name="selection_mode">0</property>
                     <property name="visible">True</property>
-                    <!-- <signal name="selection-changed" handler="_on_selection_changed" swapped="no"/> -->
                   </object>
                 </child>
               </object>
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index fe205de5..c591d81e 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -30,7 +30,7 @@ from gnomemusic.grilo import grilo
 from gnomemusic.views.baseview import BaseView
 from gnomemusic.widgets.headerbar import HeaderBar
 from gnomemusic.widgets.albumcover import AlbumCover
-from gnomemusic.widgets.albumwidget2 import AlbumWidget2
+from gnomemusic.widgets.albumwidget import AlbumWidget
 
 
 class AlbumsView(BaseView):
@@ -46,7 +46,7 @@ class AlbumsView(BaseView):
         super().__init__('albums', _("Albums"), window)
 
         self.player = player
-        self._album_widget = AlbumWidget2(player, self)
+        self._album_widget = AlbumWidget(player, self)
         self._album_widget.bind_property(
             "selection-mode", self, "selection-mode",
             GObject.BindingFlags.BIDIRECTIONAL)
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 4e33e306..6f11a08d 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -36,7 +36,6 @@ from gnomemusic.utils import View
 from gnomemusic.search import Search
 from gnomemusic.views.baseview import BaseView
 from gnomemusic.widgets.headerbar import HeaderBar
-from gnomemusic.widgets.albumwidget import AlbumWidget
 from gnomemusic.widgets.artistalbumswidget import ArtistAlbumsWidget
 import gnomemusic.utils as utils
 
@@ -63,14 +62,14 @@ class SearchView(BaseView):
         self._albums = {}
         self._albums_index = 0
 
-        self._album_widget = AlbumWidget(player)
-        self._album_widget.bind_property(
-            "selection-mode", self, "selection-mode",
-            GObject.BindingFlags.BIDIRECTIONAL)
-        self._album_widget.bind_property(
-            "selected-items-count", self, "selected-items-count")
+        # self._album_widget = AlbumWidget(player)
+        # self._album_widget.bind_property(
+        #     "selection-mode", self, "selection-mode",
+        #     GObject.BindingFlags.BIDIRECTIONAL)
+        # self._album_widget.bind_property(
+        #     "selected-items-count", self, "selected-items-count")
 
-        self.add(self._album_widget)
+        # self.add(self._album_widget)
 
         self._artists_albums_selected = []
         self._artists_albums_index = 0
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index afbf8116..9f7db434 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -1,38 +1,11 @@
-# Copyright (c) 2016 The GNOME Music Developers
-#
-# GNOME Music is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# GNOME Music is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# The GNOME Music authors hereby grant permission for non-GPL compatible
-# GStreamer plugins to be used and distributed together with GStreamer
-# and GNOME Music.  This permission is above and beyond the permissions
-# granted by the GPL license by which GNOME Music is covered.  If you
-# modify this code, you may extend this exception to your version of the
-# 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 gettext import ngettext
-from gi.repository import GdkPixbuf, GObject, Grl, Gtk
+from gi.repository import GObject, Grl, Gtk
 
 from gnomemusic import log
 from gnomemusic.albumartcache import Art
-from gnomemusic.grilo import grilo
 from gnomemusic.player import PlayerPlaylist
 from gnomemusic.widgets.disclistboxwidget import DiscBox
 from gnomemusic.widgets.disclistboxwidget import DiscListBox  # noqa: F401
-from gnomemusic.widgets.songwidget import SongWidget
-import gnomemusic.utils as utils
 
 
 @Gtk.Template(resource_path='/org/gnome/Music/ui/AlbumWidget.ui')
@@ -49,7 +22,7 @@ class AlbumWidget(Gtk.EventBox):
     _composer_label = Gtk.Template.Child()
     _composer_info_label = Gtk.Template.Child()
     _cover_stack = Gtk.Template.Child()
-    _disc_listbox = Gtk.Template.Child()
+    _listbox = Gtk.Template.Child()
     _released_info_label = Gtk.Template.Child()
     _running_info_label = Gtk.Template.Child()
     _title_label = Gtk.Template.Child()
@@ -63,63 +36,34 @@ class AlbumWidget(Gtk.EventBox):
         return '<AlbumWidget>'
 
     @log
-    def __init__(self, player):
+    def __init__(self, player, parent_view):
         """Initialize the AlbumWidget.
 
         :param player: The player object
+        :param parent_view: The view this widget is part of
         """
         super().__init__()
 
         self._album = None
-        self._songs = []
 
         self._cover_stack.props.size = Art.Size.LARGE
+        self._parent_view = parent_view
         self._player = player
-        self._iter_to_clean = None
 
-        self._create_model()
         self._album_name = None
 
-        self.bind_property(
-            'selection-mode', self._disc_listbox, 'selection-mode',
-            GObject.BindingFlags.BIDIRECTIONAL)
-
-    @log
-    def _create_model(self):
-        """Create the ListStore model for this widget."""
-        self._model = Gtk.ListStore(
-            GObject.TYPE_STRING,  # title
-            GObject.TYPE_STRING,
-            GObject.TYPE_STRING,
-            GObject.TYPE_STRING,
-            GdkPixbuf.Pixbuf,    # icon
-            GObject.TYPE_OBJECT,  # song object
-            GObject.TYPE_BOOLEAN,  # item selected
-            GObject.TYPE_STRING,
-            GObject.TYPE_BOOLEAN,
-            GObject.TYPE_INT,  # icon shown
-            GObject.TYPE_BOOLEAN,
-            GObject.TYPE_INT
-        )
-
     @log
-    def update(self, album):
+    def update(self, corealbum):
         """Update the album widget.
 
-        :param Grl.Media album: The grilo media album
+        :param CoreAlbum album: The CoreAlbum object
         """
-        # reset view
-        self._songs = []
-        self._create_model()
-        for widget in self._disc_listbox.get_children():
-            self._disc_listbox.remove(widget)
-
-        self._cover_stack.update(album)
+        self._cover_stack.update(corealbum.props.media)
 
         self._duration = 0
 
-        self._album_name = utils.get_album_title(album)
-        artist = utils.get_artist_name(album)
+        self._album_name = corealbum.props.title
+        artist = corealbum.props.artist
 
         self._title_label.props.label = self._album_name
         self._title_label.props.tooltip_text = self._album_name
@@ -127,22 +71,47 @@ class AlbumWidget(Gtk.EventBox):
         self._artist_label.props.label = artist
         self._artist_label.props.tooltip_text = artist
 
-        year = utils.get_media_year(album)
-        if not year:
-            year = '----'
-        self._released_info_label.props.label = year
+        self._released_info_label.props.label = corealbum.props.year
+
+        self._set_composer_label(corealbum)
+
+        self._album = corealbum.props.media
+        self._album_model = corealbum.props.model
+        self._listbox.bind_model(self._album_model, self._create_widget)
+
+        def non_selectable(child):
+            child.props.selectable = False
+
+        self._listbox.forall(non_selectable)
 
-        self._set_composer_label(album)
+        corealbum.connect("notify::duration", self._on_duration_changed)
 
-        self._album = album
+    def _create_widget(self, disc):
+        disc_box = self._create_disc_box(
+            disc.props.disc_nr, disc.model)
 
-        self._player.connect('song-changed', self._update_model)
+        self.bind_property(
+            "selection-mode", disc_box, "selection-mode",
+            GObject.BindingFlags.BIDIRECTIONAL
+            | GObject.BindingFlags.SYNC_CREATE)
+
+        return disc_box
+
+    def _create_disc_box(self, disc_nr, album_model):
+        disc_box = DiscBox(None, album_model)
+        # disc_box.set_songs(disc_songs)
+        disc_box.set_disc_number(disc_nr)
+        # disc_box.props.columns = 2
+        disc_box.props.show_durations = False
+        disc_box.props.show_favorites = False
+        disc_box.props.show_song_numbers = True
+        disc_box.connect('song-activated', self._song_activated)
 
-        grilo.populate_album_songs(album, self.add_item)
+        return disc_box
 
     @log
-    def _set_composer_label(self, album):
-        composer = album.get_composer()
+    def _set_composer_label(self, corealbum):
+        composer = corealbum.props.composer
         show = False
 
         if composer:
@@ -154,115 +123,48 @@ class AlbumWidget(Gtk.EventBox):
         self._composer_label.props.visible = show
         self._composer_info_label.props.visible = show
 
-    @log
-    def _set_duration_label(self):
-        mins = (self._duration // 60) + 1
+    def _on_duration_changed(self, coredisc, duration):
+        mins = (coredisc.props.duration // 60) + 1
         self._running_info_label.props.label = ngettext(
             "{} minute", "{} minutes", mins).format(mins)
 
-    # @Gtk.Template.Callback()
-    # @log
-    # def _on_selection_changed(self, widget):
-    #     n_items = len(self._disc_listbox.get_selected_items())
-    #     self.props.selected_items_count = n_items
-
     @log
-    def _create_disc_box(self, disc_nr, disc_songs):
-        disc_box = DiscBox(self._model)
-        disc_box.set_songs(disc_songs)
-        disc_box.set_disc_number(disc_nr)
-        disc_box.props.columns = 1
-        disc_box.props.show_durations = True
-        disc_box.props.show_favorites = True
-        disc_box.props.show_song_numbers = False
-        disc_box.connect('song-activated', self._song_activated)
+    def _on_selection_changed(self, klass, value):
+        n_items = 0
+        for song in self._model[0]:
+            if song.props.selected:
+                n_items += 1
 
-        return disc_box
+        self.props.selected_items_count = n_items
 
     @log
     def _song_activated(self, widget, song_widget):
+        print("activated", song_widget)
         if self.props.selection_mode:
             song_widget.props.selected = not song_widget.props.selected
             return
 
-        self._player.set_playlist(
-            PlayerPlaylist.Type.ALBUM, self._album_name, song_widget.model,
-            song_widget.itr)
-        self._player.play()
-        return True
-
-    @log
-    def add_item(self, source, prefs, song, remaining, data=None):
-        """Add a song to the item to album list.
-
-        If no song is remaining create DiscBox and display the widget.
-        :param GrlTrackerSource source: The grilo source
-        :param prefs: not used
-        :param GrlMedia song: The grilo media object
-        :param int remaining: Remaining number of items to add
-        :param data: User data
-        """
-        if song:
-            self._songs.append(song)
-            self._duration += song.get_duration()
-            return
-
-        if remaining == 0:
-            discs = {}
-            for song in self._songs:
-                disc_nr = song.get_album_disc_number()
-                if disc_nr not in discs.keys():
-                    discs[disc_nr] = [song]
-                else:
-                    discs[disc_nr].append(song)
+        signal_id = None
 
-            for disc_nr in discs:
-                disc = self._create_disc_box(disc_nr, discs[disc_nr])
-                if len(discs) == 1:
-                    disc.props.show_disc_label = False
-                self._disc_listbox.add(disc)
+        def _on_playlist_loaded(klass):
+            self._player.play(None, None, song_widget._media)
+            self._parent_view._window._app._coremodel.disconnect(signal_id)
 
-            self._set_duration_label()
-            self._update_model(self._player)
-
-    @log
-    def _update_model(self, player):
-        """Updates model when the song changes
-
-        :param Player player: The main player object
-        """
-        if not player.playing_playlist(
-                PlayerPlaylist.Type.ALBUM, self._album_name):
-            return True
-
-        current_song = player.props.current_song
-        self._duration = 0
-        song_passed = False
-
-        for song in self._songs:
-            song_widget = song.song_widget
-            self._duration += song.get_duration()
-
-            if (song.get_id() == current_song.get_id()):
-                song_widget.props.state = SongWidget.State.PLAYING
-                song_passed = True
-            elif (song_passed):
-                # Counter intuitive, but this is due to call order.
-                song_widget.props.state = SongWidget.State.UNPLAYED
-            else:
-                song_widget.props.state = SongWidget.State.PLAYED
-
-        self._set_duration_label()
+        signal_id = self._parent_view._window._app._coremodel.connect(
+            "playlist-loaded", _on_playlist_loaded)
+        self._parent_view._window._app._coremodel.set_playlist_model(
+            PlayerPlaylist.Type.ALBUM, self._album, song_widget._media,
+            self._album_model)
 
         return True
 
     @log
     def select_all(self):
-        self._disc_listbox.select_all()
+        self._listbox.select_all()
 
     @log
     def select_none(self):
-        self._disc_listbox.select_none()
+        self._listbox.select_none()
 
     @log
     def get_selected_songs(self):
@@ -271,7 +173,13 @@ class AlbumWidget(Gtk.EventBox):
         :returns: selected songs
         :rtype: list
         """
-        return self._disc_listbox.get_selected_items()
+        selected_songs = []
+
+        for song in self._model:
+            if song.props.selected:
+                selected_songs.append(song.props.media)
+
+        return selected_songs
 
     @GObject.Property(
         type=Grl.Media, default=False, flags=GObject.ParamFlags.READABLE)


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