| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2008 Seb Ruiz <[email protected]> * |
| 3 |
* Copyright (c) 2008 Soren Harward <[email protected]> * |
| 4 |
* Copyright (c) 2010 Nanno Langstraat <[email protected]> * |
| 5 |
* * |
| 6 |
* This program is free software; you can redistribute it and/or modify it under * |
| 7 |
* the terms of the GNU General Public License as published by the Free Software * |
| 8 |
* Foundation; either version 2 of the License, or (at your option) version 3 or * |
| 9 |
* any later version accepted by the membership of KDE e.V. (or its successor approved * |
| 10 |
* by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of * |
| 11 |
* version 3 of the license. * |
| 12 |
* * |
| 13 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 14 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 15 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU General Public License along with * |
| 18 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 19 |
****************************************************************************************/ |
| 20 |
|
| 21 |
#ifndef AMAROK_NONLINEARTRACKNAVIGATOR_H |
| 22 |
#define AMAROK_NONLINEARTRACKNAVIGATOR_H |
| 23 |
|
| 24 |
#include "TrackNavigator.h" |
| 25 |
|
| 26 |
#include <QList> |
| 27 |
#include <QSet> |
| 28 |
|
| 29 |
namespace Playlist |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Base class that offers some standard services for non-linear navigators. |
| 33 |
* - Deterministic back/forward in history. |
| 34 |
* - High-performance keep-in-sync with changes in the source model. |
| 35 |
* - 'm_plannedItems', a standard way to plan ahead. |
| 36 |
* |
| 37 |
* The simplest use case for a child class is to provide an implementation of 'planOne()'. |
| 38 |
* |
| 39 |
* A slightly more advanced use case for a child class is to override |
| 40 |
* 'notifyItemsInserted()', 'notifyItemsRemoved()', and 'setCurrentItem()'. |
| 41 |
* |
| 42 |
* If a child class really needs to do something special, it can override |
| 43 |
* 'requestNextTrack()' etc., as long as it tries the return value of the base |
| 44 |
* implementation first. |
| 45 |
*/ |
| 46 |
class NonlinearTrackNavigator : public TrackNavigator |
| 47 |
{ |
| 48 |
Q_OBJECT |
| 49 |
|
| 50 |
public: |
| 51 |
NonlinearTrackNavigator(); |
| 52 |
|
| 53 |
//! Overrides from 'TrackNavigator' |
| 54 |
quint64 likelyNextTrack() override; |
| 55 |
quint64 requestNextTrack() override; |
| 56 |
quint64 requestUserNextTrack() override { return requestNextTrack(); } |
| 57 |
|
| 58 |
quint64 likelyLastTrack() override; |
| 59 |
quint64 requestLastTrack() override; |
| 60 |
|
| 61 |
static const int MAX_HISTORY_SIZE = 1000; // This is probably enough for even the most crazed user. |
| 62 |
|
| 63 |
/** |
| 64 |
* 'setCurrentItem()' is guaranteed to be called whenever an item is removed from 'm_plannedItems'. |
| 65 |
*/ |
| 66 |
quint64 currentItem(); |
| 67 |
virtual void setCurrentItem( const quint64 newItem, bool goingBackward = false ); |
| 68 |
|
| 69 |
/** |
| 70 |
* All items currently in the source model. |
| 71 |
* |
| 72 |
* The QList is not kept in the same order as the source model. We could add |
| 73 |
* that guarantee, but currently no navigator needs it. |
| 74 |
* |
| 75 |
* The current reason for the QList variant is that RandomTrackNavigator needs |
| 76 |
* a list, and it would give bad performance to convert the whole playlist |
| 77 |
* from QSet to QList on every 'planOne()' call. |
| 78 |
* |
| 79 |
* The QSet variant currently has O(n) performance. (Nothing needs O(1).) |
| 80 |
*/ |
| 81 |
QList<quint64> allItemsList() { doItemListsMaintenance(); return m_allItemsList; } |
| 82 |
QSet<quint64> allItemsSet() { doItemListsMaintenance(); QSet<quint64> r_allItemsSet(m_allItemsList.begin(), m_allItemsList.end()); return r_allItemsSet; } |
| 83 |
|
| 84 |
/** |
| 85 |
* Items that we've played. |
| 86 |
*/ |
| 87 |
QList<quint64> historyItems() { doItemListsMaintenance(); return m_historyItems; } |
| 88 |
|
| 89 |
protected: |
| 90 |
/** |
| 91 |
* Load the contents of the source model. |
| 92 |
* |
| 93 |
* The bottom-most child class constructor should call this function. We must |
| 94 |
* not do it in our constructor or in intermediate constructors, because it |
| 95 |
* causes calls to the virtual functions below. And virtual functions don't do |
| 96 |
* what you want while you're in a base class constructor. |
| 97 |
*/ |
| 98 |
void loadFromSourceModel(); |
| 99 |
|
| 100 |
/** |
| 101 |
* Request-callback for child classes: try to make sure that there's at least |
| 102 |
* 1 item in 'm_plannedItems'. |
| 103 |
*/ |
| 104 |
virtual void planOne() = 0; |
| 105 |
|
| 106 |
/** |
| 107 |
* Notification-callback for child classes: Playlist items have been inserted. |
| 108 |
*/ |
| 109 |
virtual void notifyItemsInserted( const QSet<quint64> &insertedItems ) = 0; |
| 110 |
|
| 111 |
/** |
| 112 |
* Notification-callback for child classes: Playlist items have been removed. |
| 113 |
* |
| 114 |
* 'currentItem()' still has its old (possibly obsolete) value when this function is called. |
| 115 |
* This function can call 'setCurrentItem()' if it knows a good new choice. |
| 116 |
* |
| 117 |
* Note: by the time this notification gets called, the playlist items are |
| 118 |
* usually no longer present in the source model. |
| 119 |
*/ |
| 120 |
virtual void notifyItemsRemoved( const QSet<quint64> &removedItems ) = 0; |
| 121 |
|
| 122 |
/** |
| 123 |
* List of planned-ahead playlist items. Should be edited by child classes. |
| 124 |
*/ |
| 125 |
QList<quint64> m_plannedItems; |
| 126 |
|
| 127 |
private Q_SLOTS: |
| 128 |
void slotModelReset(); |
| 129 |
void slotRowsInserted( const QModelIndex& parent, int start, int end ); |
| 130 |
void slotRowsAboutToBeRemoved( const QModelIndex& parent, int start, int end ); |
| 131 |
|
| 132 |
void slotActiveTrackChanged( const quint64 ); |
| 133 |
|
| 134 |
private: |
| 135 |
/** |
| 136 |
* Maintain internal tem lists. Call before doing anything with internal state. |
| 137 |
*/ |
| 138 |
void doItemListsMaintenance(); |
| 139 |
|
| 140 |
/** |
| 141 |
* Choose the playlist item list that we'll take our next item from. |
| 142 |
*/ |
| 143 |
ItemList* nextItemChooseDonorList(); |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* These variables hold inserted/removed items until we batch-process them. |
| 148 |
* This is due to performance requirements of slotRowsInserted/slotRowsAboutToBeRemoved. |
| 149 |
*/ |
| 150 |
QSet<quint64> m_insertedItems; |
| 151 |
QSet<quint64> m_removedItems; |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* All items in the source model. See comments at 'allItemsList()'. |
| 156 |
*/ |
| 157 |
QList<quint64> m_allItemsList; |
| 158 |
|
| 159 |
/** |
| 160 |
* List of playlist items we already played. May contain items multiple times. |
| 161 |
*/ |
| 162 |
QList<quint64> m_historyItems; |
| 163 |
|
| 164 |
/** |
| 165 |
* The playlist item we most recently suggested to the rest of Amarok. |
| 166 |
*/ |
| 167 |
quint64 m_currentItem; |
| 168 |
|
| 169 |
/** |
| 170 |
* List of playlist items that had already made it to 'm_history', but have been |
| 171 |
* taken out again and re-played because the user did 'requestLastTrack()'. |
| 172 |
* |
| 173 |
* Makes 'requestNextTrack()' after 'requestLastTrack()' work predictably. |
| 174 |
* |
| 175 |
* We maintain this separately from 'm_plannedItems' because when new item are |
| 176 |
* inserted, they are mixed randomly with 'm_plannedItems'. This list shouldn't |
| 177 |
* change under those circumstances. |
| 178 |
*/ |
| 179 |
QList<quint64> m_replayedItems; |
| 180 |
}; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
#endif |