| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2008 Nikolaj Hald Nielsen <[email protected]> * |
| 3 |
* Copyright (c) 2008 Soren Harward <[email protected]> * |
| 4 |
* Copyright (c) 2009 Téo Mrnjavac <[email protected]> * |
| 5 |
* Copyright (c) 2010 Nanno Langstraat <[email protected]> * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or modify it under * |
| 8 |
* the terms of the GNU General Public License as published by the Free Software * |
| 9 |
* Foundation; either version 2 of the License, or (at your option) version 3 or * |
| 10 |
* any later version accepted by the membership of KDE e.V. (or its successor approved * |
| 11 |
* by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of * |
| 12 |
* version 3 of the license. * |
| 13 |
* * |
| 14 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 15 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 16 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU General Public License along with * |
| 19 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 20 |
****************************************************************************************/ |
| 21 |
|
| 22 |
#define DEBUG_PREFIX "Playlist::RandomAlbumNavigator" |
| 23 |
|
| 24 |
#include "RandomAlbumNavigator.h" |
| 25 |
|
| 26 |
#include "core/support/Debug.h" |
| 27 |
#include "core/meta/Meta.h" |
| 28 |
|
| 29 |
#include <QRandomGenerator> |
| 30 |
|
| 31 |
#include <algorithm> // For std::shuffle |
| 32 |
#include <cmath> |
| 33 |
|
| 34 |
|
| 35 |
Playlist::RandomAlbumNavigator::RandomAlbumNavigator() |
| 36 |
{ |
| 37 |
loadFromSourceModel(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
void |
| 42 |
Playlist::RandomAlbumNavigator::planOne() |
| 43 |
{ |
| 44 |
DEBUG_BLOCK |
| 45 |
|
| 46 |
// Try to find next item in same album |
| 47 |
if ( m_plannedItems.isEmpty() ) |
| 48 |
{ |
| 49 |
ItemList itemsInAlbum = m_itemsPerAlbum.value( currentAlbum() ); // May be default-constructed empty list. |
| 50 |
|
| 51 |
int currentRow = itemsInAlbum.indexOf( currentItem() ); // -1 if currentItem() == 0. |
| 52 |
if ( currentRow != -1 ) |
| 53 |
{ |
| 54 |
int nextRow = currentRow + 1; |
| 55 |
if ( nextRow < itemsInAlbum.size() ) |
| 56 |
m_plannedItems.append( itemsInAlbum.at( nextRow ) ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Try to find first item in next album |
| 61 |
if ( m_plannedItems.isEmpty() ) |
| 62 |
{ |
| 63 |
if ( m_plannedAlbums.isEmpty() ) // Handle end of planned album list |
| 64 |
notifyAlbumsInserted( m_itemsPerAlbum.keys() ); |
| 65 |
|
| 66 |
if ( !m_plannedAlbums.isEmpty() ) |
| 67 |
{ |
| 68 |
AlbumId newAlbum = m_plannedAlbums.takeFirst(); |
| 69 |
quint64 newCurrentItem = m_itemsPerAlbum.value( newAlbum ).first(); |
| 70 |
m_plannedItems.append( newCurrentItem ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
Playlist::RandomAlbumNavigator::notifyAlbumsInserted( const QList<AlbumId> &insertedAlbums ) |
| 77 |
{ |
| 78 |
DEBUG_BLOCK |
| 79 |
|
| 80 |
m_plannedAlbums.append( insertedAlbums ); |
| 81 |
std::shuffle( m_plannedAlbums.begin(), m_plannedAlbums.end(), std::default_random_engine( QDateTime::currentMSecsSinceEpoch() ) ); |
| 82 |
if ( !m_plannedAlbums.isEmpty() ) |
| 83 |
if ( m_plannedAlbums.first() == currentAlbum() ) |
| 84 |
m_plannedAlbums.append( m_plannedAlbums.takeFirst() ); // Try to avoid playing same album twice. |
| 85 |
} |