dynamicplaylist.cpp 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
 *
 * This program 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.
 *
 * This program 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
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16 17 18

#include "dynamicplaylist.h"
#include "collectionlist.h"
19
#include "playlistcollection.h"
20
#include "tracksequencemanager.h"
21

22
#include <QTimer>
23

24 25 26 27
class PlaylistDirtyObserver : public PlaylistObserver
{
public:
    PlaylistDirtyObserver(DynamicPlaylist *parent, Playlist *playlist) :
28 29
        PlaylistObserver(playlist),
        m_parent(parent)
30 31 32
    {

    }
33 34

    virtual void playlistItemDataHasChanged() Q_DECL_FINAL override
35 36 37
    {
        m_parent->slotSetDirty();
    }
38 39 40 41 42

private:
    DynamicPlaylist *m_parent;
};

43 44 45 46
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

47 48 49
DynamicPlaylist::DynamicPlaylist(const PlaylistList &playlists,
                                 PlaylistCollection *collection,
                                 const QString &name,
50
                                 const QString &iconName,
51 52
                                 bool setupPlaylist,
                                 bool synchronizePlaying) :
53
    Playlist(collection, true),
54
    m_playlists(playlists),
55 56
    m_dirty(true),
    m_synchronizePlaying(synchronizePlaying)
57
{
58 59 60
    if(setupPlaylist)
        collection->setupPlaylist(this, iconName);
    setName(name);
61
    setAllowDuplicates(false);
62

Kacper Kasper's avatar
Kacper Kasper committed
63
    setSortingEnabled(false);
64

Laurent Montel's avatar
Laurent Montel committed
65
    for(PlaylistList::ConstIterator it = playlists.constBegin(); it != playlists.constEnd(); ++it)
66 67
        m_observers.append(new PlaylistDirtyObserver(this, *it));

68
    connect(CollectionList::instance(), SIGNAL(signalCollectionChanged()), this, SLOT(slotSetDirty()));
69 70
}

71 72
DynamicPlaylist::~DynamicPlaylist()
{
73
    lower();
74

75 76
    foreach(PlaylistObserver *observer, m_observers)
        delete observer;
77 78
}

79 80 81 82 83 84
void DynamicPlaylist::setPlaylists(const PlaylistList &playlists)
{
    m_playlists = playlists;
    updateItems();
}

85 86 87 88 89 90 91
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void DynamicPlaylist::slotReload()
{
    for(PlaylistList::Iterator it = m_playlists.begin(); it != m_playlists.end(); ++it)
92
        (*it)->slotReload();
93 94 95 96

    checkUpdateItems();
}

97 98 99
void DynamicPlaylist::lower(QWidget *top)
{
    if(top == this)
100
        return;
101 102 103 104 105 106 107 108 109 110 111 112 113 114

    if(playing()) {
        PlaylistList l;
        l.append(this);
        for(PlaylistList::Iterator it = m_playlists.begin();
            it != m_playlists.end(); ++it)
        {
            (*it)->synchronizePlayingItems(l, true);
        }
    }

    PlaylistItemList list = PlaylistItem::playingItems();
    for(PlaylistItemList::Iterator it = list.begin(); it != list.end(); ++it) {
        if((*it)->playlist() == this) {
115
            list.erase(it);
116 117 118 119 120 121 122 123
            break;
        }
    }

    if(!list.isEmpty())
        TrackSequenceManager::instance()->setCurrentPlaylist(list.front()->playlist());
}

124 125 126 127
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

128
PlaylistItemList DynamicPlaylist::items()
129
{
130 131
    checkUpdateItems();
    return Playlist::items();
132 133 134
}

void DynamicPlaylist::showEvent(QShowEvent *e)
135 136 137 138 139
{
    checkUpdateItems();
    Playlist::showEvent(e);
}

140 141 142 143 144 145
void DynamicPlaylist::paintEvent(QPaintEvent *e)
{
    checkUpdateItems();
    Playlist::paintEvent(e);
}

146
void DynamicPlaylist::updateItems()
147
{
148 149
    PlaylistItemList siblings;

Laurent Montel's avatar
Laurent Montel committed
150
    for(PlaylistList::ConstIterator it = m_playlists.constBegin(); it != m_playlists.constEnd(); ++it)
151 152
        siblings += (*it)->items();

153 154
    if(m_siblings != siblings) {
        m_siblings = siblings;
155 156 157 158 159
        this->synchronizeItemsTo(siblings);

        if(m_synchronizePlaying) {
            synchronizePlayingItems(m_playlists, true);
        }
160
    }
161 162
}

163 164 165 166 167
bool DynamicPlaylist::synchronizePlaying() const
{
    return m_synchronizePlaying;
}

168 169 170 171 172 173 174
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void DynamicPlaylist::checkUpdateItems()
{
    if(!m_dirty)
175
        return;
176 177 178

    updateItems();

179
    m_dirty = false;
180 181
}

182
// vim: set et sw=4 tw=0 sta: