dynamicplaylist.cpp 5.29 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 updateData() { m_parent->slotSetDirty(); }
    virtual void updateCurrent() {}
35 36 37 38 39

private:
    DynamicPlaylist *m_parent;
};

40 41 42 43
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

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

59 60
    setSorting(columns() + 1);

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

64
    connect(CollectionList::instance(), SIGNAL(signalCollectionChanged()), this, SLOT(slotSetDirty()));
65 66
}

67 68
DynamicPlaylist::~DynamicPlaylist()
{
69
    lower();
70

71 72
    foreach(PlaylistObserver *observer, m_observers)
        delete observer;
73 74
}

75 76 77 78 79 80
void DynamicPlaylist::setPlaylists(const PlaylistList &playlists)
{
    m_playlists = playlists;
    updateItems();
}

81 82 83 84 85 86 87
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void DynamicPlaylist::slotReload()
{
    for(PlaylistList::Iterator it = m_playlists.begin(); it != m_playlists.end(); ++it)
88
        (*it)->slotReload();
89 90 91 92

    checkUpdateItems();
}

93 94 95
void DynamicPlaylist::lower(QWidget *top)
{
    if(top == this)
96
        return;
97 98 99 100 101 102 103 104 105 106 107 108 109 110

    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) {
111
            list.erase(it);
112 113 114 115 116 117 118 119
            break;
        }
    }

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

120 121 122 123
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

124
PlaylistItemList DynamicPlaylist::items()
125
{
126 127
    checkUpdateItems();
    return Playlist::items();
128 129 130
}

void DynamicPlaylist::showEvent(QShowEvent *e)
131 132 133 134 135
{
    checkUpdateItems();
    Playlist::showEvent(e);
}

136 137 138 139 140 141
void DynamicPlaylist::paintEvent(QPaintEvent *e)
{
    checkUpdateItems();
    Playlist::paintEvent(e);
}

142
void DynamicPlaylist::updateItems()
143
{
144 145
    PlaylistItemList siblings;

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


    PlaylistItemList newSiblings = siblings;
    if(m_siblings != newSiblings) {
152
        m_siblings = newSiblings;
153
        QTimer::singleShot(0, this, SLOT(slotUpdateItems()));
154
    }
155 156
}

157 158 159 160 161
bool DynamicPlaylist::synchronizePlaying() const
{
    return m_synchronizePlaying;
}

162 163 164 165 166 167 168
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void DynamicPlaylist::checkUpdateItems()
{
    if(!m_dirty)
169
        return;
170 171 172

    updateItems();

173
    m_dirty = false;
174 175
}

176 177 178 179 180 181 182 183 184 185
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void DynamicPlaylist::slotUpdateItems()
{
    // This should be optimized to check to see which items are already in the
    // list and just adding those and removing the ones that aren't.

    clear();
186
    createItems(m_siblings);
187 188
    if(m_synchronizePlaying)
        synchronizePlayingItems(m_playlists, true);
189 190
}

191
#include "dynamicplaylist.moc"
192 193

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