File indexing completed on 2023-05-30 11:30:46

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 <algorithm>
0022 
0023 ////////////////////////////////////////////////////////////////////////////////
0024 // public methods
0025 ////////////////////////////////////////////////////////////////////////////////
0026 
0027 DynamicPlaylist::DynamicPlaylist(const PlaylistList &playlists,
0028                                  PlaylistCollection *collection,
0029                                  const QString &name,
0030                                  const QString &iconName,
0031                                  bool setupPlaylist,
0032                                  bool synchronizePlaying)
0033   : Playlist(collection, true)
0034   , m_dirty(true)
0035   , m_synchronizePlaying(synchronizePlaying)
0036 {
0037     if(setupPlaylist)
0038         collection->setupPlaylist(this, iconName);
0039 
0040     setName(name);
0041     setAllowDuplicates(false);
0042     setSortingEnabled(false);
0043 
0044     for(const auto playlist : playlists) {
0045         m_playlists << QPointer<Playlist>(playlist);
0046 
0047         connect(&(playlist->signaller), &PlaylistInterfaceSignaller::playingItemDataChanged,
0048                 this, &DynamicPlaylist::slotSetDirty);
0049     }
0050 
0051     connect(CollectionList::instance(), &CollectionList::signalCollectionChanged,
0052             this, &DynamicPlaylist::slotSetDirty);
0053 }
0054 
0055 DynamicPlaylist::~DynamicPlaylist()
0056 {
0057     // Subclasses need to ensure they update items in their own destructor, or
0058     // any other virtual calls they may need to make.
0059 
0060     lower();
0061 }
0062 
0063 void DynamicPlaylist::setPlaylists(const PlaylistList &playlists)
0064 {
0065     m_playlists.clear();
0066 
0067     for(const auto playlist : playlists) {
0068         m_playlists << QPointer<Playlist>(playlist);
0069     }
0070 
0071     updateItems();
0072 }
0073 
0074 ////////////////////////////////////////////////////////////////////////////////
0075 // public slots
0076 ////////////////////////////////////////////////////////////////////////////////
0077 
0078 void DynamicPlaylist::slotReload()
0079 {
0080     for(const auto &playlist : qAsConst(m_playlists)) {
0081         if(!playlist)
0082             continue;
0083         playlist->slotReload();
0084     }
0085 
0086     checkUpdateItems();
0087 }
0088 
0089 void DynamicPlaylist::lower(QWidget *top)
0090 {
0091     if(top == this || !playing())
0092         return;
0093 
0094     PlaylistList l;
0095     l.append(this);
0096 
0097     for(const auto &playlist : qAsConst(m_playlists)) {
0098         if(!playlist)
0099             continue;
0100         playlist->synchronizePlayingItems(l, true);
0101     }
0102 }
0103 
0104 ////////////////////////////////////////////////////////////////////////////////
0105 // protected methods
0106 ////////////////////////////////////////////////////////////////////////////////
0107 
0108 PlaylistItemList DynamicPlaylist::items()
0109 {
0110     checkUpdateItems();
0111     return Playlist::items();
0112 }
0113 
0114 void DynamicPlaylist::showEvent(QShowEvent *e)
0115 {
0116     checkUpdateItems();
0117     Playlist::showEvent(e);
0118 }
0119 
0120 void DynamicPlaylist::paintEvent(QPaintEvent *e)
0121 {
0122     checkUpdateItems();
0123     Playlist::paintEvent(e);
0124 }
0125 
0126 void DynamicPlaylist::updateItems()
0127 {
0128     PlaylistItemList siblings;
0129 
0130     for(const auto &playlist : qAsConst(m_playlists)) {
0131         if(!playlist)
0132             continue;
0133         siblings += playlist->items();
0134     }
0135 
0136     if(m_siblings != siblings) {
0137         m_siblings = siblings;
0138         this->synchronizeItemsTo(siblings);
0139 
0140         if(m_synchronizePlaying) {
0141             for(const auto &playlist : qAsConst(m_playlists)) {
0142                 synchronizePlayingItems(playlist, true);
0143             }
0144         }
0145     }
0146 }
0147 
0148 bool DynamicPlaylist::synchronizePlaying() const
0149 {
0150     return m_synchronizePlaying;
0151 }
0152 
0153 ////////////////////////////////////////////////////////////////////////////////
0154 // private methods
0155 ////////////////////////////////////////////////////////////////////////////////
0156 
0157 void DynamicPlaylist::checkUpdateItems()
0158 {
0159     if(!m_dirty)
0160         return;
0161 
0162     updateItems();
0163 
0164     m_dirty = false;
0165 }
0166 
0167 // vim: set et sw=4 tw=0 sta: