File indexing completed on 2021-12-21 13:27:52

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "dynamicplaylist.h"
0018 #include "collectionlist.h"
0019 #include "playlistcollection.h"
0020 
0021 #include <QTimer>
0022 #include <QObject>
0023 
0024 #include <algorithm>
0025 
0026 ////////////////////////////////////////////////////////////////////////////////
0027 // public methods
0028 ////////////////////////////////////////////////////////////////////////////////
0029 
0030 DynamicPlaylist::DynamicPlaylist(const PlaylistList &playlists,
0031                                  PlaylistCollection *collection,
0032                                  const QString &name,
0033                                  const QString &iconName,
0034                                  bool setupPlaylist,
0035                                  bool synchronizePlaying)
0036   : Playlist(collection, true)
0037   , m_dirty(true)
0038   , m_synchronizePlaying(synchronizePlaying)
0039 {
0040     if(setupPlaylist)
0041         collection->setupPlaylist(this, iconName);
0042 
0043     setName(name);
0044     setAllowDuplicates(false);
0045     setSortingEnabled(false);
0046 
0047     for(const auto playlist : playlists) {
0048         m_playlists << QPointer<Playlist>(playlist);
0049 
0050         connect(&(playlist->signaller), &PlaylistInterfaceSignaller::playingItemDataChanged,
0051                 this, &DynamicPlaylist::slotSetDirty);
0052     }
0053 
0054     connect(CollectionList::instance(), &CollectionList::signalCollectionChanged,
0055             this, &DynamicPlaylist::slotSetDirty);
0056 }
0057 
0058 DynamicPlaylist::~DynamicPlaylist()
0059 {
0060     // Subclasses need to ensure they update items in their own destructor, or
0061     // any other virtual calls they may need to make.
0062 
0063     lower();
0064 }
0065 
0066 void DynamicPlaylist::setPlaylists(const PlaylistList &playlists)
0067 {
0068     m_playlists.clear();
0069 
0070     for(const auto playlist : playlists) {
0071         m_playlists << QPointer<Playlist>(playlist);
0072     }
0073 
0074     updateItems();
0075 }
0076 
0077 ////////////////////////////////////////////////////////////////////////////////
0078 // public slots
0079 ////////////////////////////////////////////////////////////////////////////////
0080 
0081 void DynamicPlaylist::slotReload()
0082 {
0083     for(const auto &playlist : qAsConst(m_playlists)) {
0084         if(!playlist)
0085             continue;
0086         playlist->slotReload();
0087     }
0088 
0089     checkUpdateItems();
0090 }
0091 
0092 void DynamicPlaylist::lower(QWidget *top)
0093 {
0094     if(top == this || !playing())
0095         return;
0096 
0097     PlaylistList l;
0098     l.append(this);
0099 
0100     for(const auto &playlist : qAsConst(m_playlists)) {
0101         if(!playlist)
0102             continue;
0103         playlist->synchronizePlayingItems(l, true);
0104     }
0105 }
0106 
0107 ////////////////////////////////////////////////////////////////////////////////
0108 // protected methods
0109 ////////////////////////////////////////////////////////////////////////////////
0110 
0111 PlaylistItemList DynamicPlaylist::items()
0112 {
0113     checkUpdateItems();
0114     return Playlist::items();
0115 }
0116 
0117 void DynamicPlaylist::showEvent(QShowEvent *e)
0118 {
0119     checkUpdateItems();
0120     Playlist::showEvent(e);
0121 }
0122 
0123 void DynamicPlaylist::paintEvent(QPaintEvent *e)
0124 {
0125     checkUpdateItems();
0126     Playlist::paintEvent(e);
0127 }
0128 
0129 void DynamicPlaylist::updateItems()
0130 {
0131     PlaylistItemList siblings;
0132 
0133     for(const auto &playlist : qAsConst(m_playlists)) {
0134         if(!playlist)
0135             continue;
0136         siblings += playlist->items();
0137     }
0138 
0139     if(m_siblings != siblings) {
0140         m_siblings = siblings;
0141         this->synchronizeItemsTo(siblings);
0142 
0143         if(m_synchronizePlaying) {
0144             for(const auto &playlist : qAsConst(m_playlists)) {
0145                 synchronizePlayingItems(playlist, true);
0146             }
0147         }
0148     }
0149 }
0150 
0151 bool DynamicPlaylist::synchronizePlaying() const
0152 {
0153     return m_synchronizePlaying;
0154 }
0155 
0156 ////////////////////////////////////////////////////////////////////////////////
0157 // private methods
0158 ////////////////////////////////////////////////////////////////////////////////
0159 
0160 void DynamicPlaylist::checkUpdateItems()
0161 {
0162     if(!m_dirty)
0163         return;
0164 
0165     updateItems();
0166 
0167     m_dirty = false;
0168 }
0169 
0170 // vim: set et sw=4 tw=0 sta: