dynamicplaylist.cpp 5.49 KB
Newer Older
1 2
/***************************************************************************
    begin                : Mon May 5 2003
3
    copyright            : (C) 2003 - 2004 by Scott Wheeler
4 5 6 7 8 9 10 11 12 13 14 15 16 17
    email                : 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.                                   *
 *                                                                         *
 ***************************************************************************/

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

21
#include <QTimer>
22

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

    }
32 33
    virtual void updateData() { m_parent->slotSetDirty(); }
    virtual void updateCurrent() {}
34 35 36 37 38

private:
    DynamicPlaylist *m_parent;
};

39 40 41 42
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

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

58 59
    setSorting(columns() + 1);

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

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

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

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

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

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

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

    checkUpdateItems();
}

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

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

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

119 120 121 122
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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


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

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

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

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

    updateItems();

172
    m_dirty = false;
173 174
}

175 176 177 178 179 180 181 182 183 184
////////////////////////////////////////////////////////////////////////////////
// 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();
185
    createItems(m_siblings);
186 187
    if(m_synchronizePlaying)
        synchronizePlayingItems(m_playlists, true);
188 189
}

190
#include "dynamicplaylist.moc"
191 192

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