dynamicplaylist.cpp 5.34 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
    if(setupPlaylist)
        collection->setupPlaylist(this, iconName);
    setName(name);
58
    setAllowDuplicates(false);
59

60
    sortItems(columnCount() + 1, Qt::AscendingOrder);
61

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

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

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

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

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

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

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

    checkUpdateItems();
}

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

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

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

121 122 123 124
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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


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

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

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

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

    updateItems();

174
    m_dirty = false;
175 176
}

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

192
#include "dynamicplaylist.moc"
193 194

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