Commit aa0cd617 authored by Alexander Stippich's avatar Alexander Stippich

Rework shuffle handling

Summary:
Remove deprecated usage of qrand().
Keep a list of the previous, current and next track
when in shuffle mode, to be used in a following patch.
Simplify calling skip and previousTracks

Depends on D22769

Test Plan: all tests pass

Reviewers: ngraham, mgallien

Reviewed By: mgallien

Tags: #elisa

Differential Revision: https://phabricator.kde.org/D22770
parent 311c0a93
......@@ -5428,8 +5428,6 @@ void MediaPlayListTest::randomPlayList()
QCOMPARE(repeatPlayChangedSpy.count(), 0);
QCOMPARE(playListFinishedSpy.count(), 0);
myPlayList.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSpy.count(), 0);
QCOMPARE(randomPlayChangedSpy.count(), 0);
QCOMPARE(repeatPlayChangedSpy.count(), 0);
......@@ -5536,8 +5534,6 @@ void MediaPlayListTest::randomAndContinuePlayList()
QCOMPARE(repeatPlayChangedSpy.count(), 0);
QCOMPARE(playListFinishedSpy.count(), 0);
myPlayList.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSpy.count(), 0);
QCOMPARE(randomPlayChangedSpy.count(), 0);
QCOMPARE(repeatPlayChangedSpy.count(), 0);
......@@ -5655,8 +5651,6 @@ void MediaPlayListTest::continuePlayList()
QCOMPARE(repeatPlayChangedSpy.count(), 0);
QCOMPARE(playListFinishedSpy.count(), 0);
myPlayList.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSpy.count(), 0);
QCOMPARE(randomPlayChangedSpy.count(), 0);
QCOMPARE(repeatPlayChangedSpy.count(), 0);
......@@ -5776,8 +5770,6 @@ void MediaPlayListTest::testRestoreSettings()
QCOMPARE(repeatPlayChangedSpy.count(), 1);
QCOMPARE(playListFinishedSpy.count(), 0);
myPlayList.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSpy.count(), 0);
QCOMPARE(randomPlayChangedSpy.count(), 1);
QCOMPARE(repeatPlayChangedSpy.count(), 1);
......@@ -5868,8 +5860,6 @@ void MediaPlayListTest::testSaveAndRestoreSettings()
QCOMPARE(repeatPlayChangedRestoreSpy.count(), 0);
QCOMPARE(playListFinishedRestoreSpy.count(), 0);
myPlayListSave.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSaveSpy.count(), 0);
QCOMPARE(randomPlayChangedSaveSpy.count(), 0);
QCOMPARE(repeatPlayChangedSaveSpy.count(), 0);
......@@ -6223,7 +6213,7 @@ void MediaPlayListTest::singleTrack()
myPlayList.skipNextTrack();
QCOMPARE(currentTrackChangedSpy.count(), 3);
QCOMPARE(currentTrackChangedSpy.count(), 2);
QCOMPARE(randomPlayChangedSpy.count(), 0);
QCOMPARE(repeatPlayChangedSpy.count(), 0);
QCOMPARE(playListFinishedSpy.count(), 1);
......@@ -6485,8 +6475,6 @@ void MediaPlayListTest::testSaveLoadPlayList()
QCOMPARE(playListLoadedRestoreSpy.count(), 0);
QCOMPARE(playListLoadFailedRestoreSpy.count(), 0);
myPlayListSave.seedRandomGenerator(0);
QCOMPARE(currentTrackChangedSaveSpy.count(), 0);
QCOMPARE(randomPlayChangedSaveSpy.count(), 0);
QCOMPARE(repeatPlayChangedSaveSpy.count(), 0);
......
......@@ -28,6 +28,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include <QRandomGenerator>
#include <algorithm>
......@@ -55,18 +56,14 @@ public:
bool mForceUndo = false;
QList<int> mRandomPositions = {0, 0, 0};
};
MediaPlayList::MediaPlayList(QObject *parent) : QAbstractListModel(parent), d(new MediaPlayListPrivate), dOld(new MediaPlayListPrivate)
{
connect(&d->mLoadPlaylist, &QMediaPlaylist::loaded, this, &MediaPlayList::loadPlayListLoaded);
connect(&d->mLoadPlaylist, &QMediaPlaylist::loadFailed, this, &MediaPlayList::loadPlayListLoadFailed);
auto currentMsecTime = QTime::currentTime().msec();
if (currentMsecTime != -1) {
seedRandomGenerator(static_cast<unsigned int>(currentMsecTime));
}
}
MediaPlayList::~MediaPlayList()
......@@ -304,6 +301,10 @@ bool MediaPlayList::removeRows(int row, int count, const QModelIndex &parent)
resetCurrentTrack();
}
if (d->mRandomPlay) {
createRandomList();
}
Q_EMIT tracksCountChanged();
Q_EMIT remainingTracksChanged();
Q_EMIT persistentStateChanged();
......@@ -878,6 +879,9 @@ void MediaPlayList::removeSelection(QList<int> selection)
for (auto oneItem : selection) {
removeRow(oneItem);
}
if (d->mRandomPlay) {
createRandomList();
}
}
void MediaPlayList::tracksListAdded(qulonglong newDatabaseId,
......@@ -1092,6 +1096,7 @@ void MediaPlayList::setRandomPlay(bool value)
{
if (d->mRandomPlay != value) {
d->mRandomPlay = value;
createRandomList();
Q_EMIT randomPlayChanged();
Q_EMIT remainingTracksChanged();
}
......@@ -1123,27 +1128,19 @@ void MediaPlayList::skipNextTrack()
return;
}
if (!d->mRandomPlay && (d->mCurrentTrack.row() >= (rowCount() - 1))) {
if (!d->mRepeatPlay) {
Q_EMIT playListFinished();
}
if (rowCount() == 1) {
d->mCurrentTrack = QPersistentModelIndex{};
notifyCurrentTrackChanged();
}
resetCurrentTrack();
return;
}
if (d->mRandomPlay) {
int randomValue = qrand();
randomValue = randomValue % (rowCount());
d->mCurrentTrack = index(randomValue, 0);
d->mRandomPositions.removeFirst();
d->mCurrentTrack = index(d->mRandomPositions.at(1), 0);
d->mRandomPositions.append(QRandomGenerator::global()->bounded(rowCount()));
} else {
d->mCurrentTrack = index(d->mCurrentTrack.row() + 1, 0);
if (d->mCurrentTrack.row() >= rowCount() - 1) {
d->mCurrentTrack = index(0, 0);
if (!d->mRepeatPlay) {
Q_EMIT playListFinished();
}
} else {
d->mCurrentTrack = index(d->mCurrentTrack.row() + 1, 0);
}
}
notifyCurrentTrackChanged();
......@@ -1155,31 +1152,23 @@ void MediaPlayList::skipPreviousTrack()
return;
}
if (!d->mRandomPlay && !d->mRepeatPlay && d->mCurrentTrack.row() <= 0) {
return;
}
if (d->mRandomPlay) {
int randomValue = qrand();
randomValue = randomValue % (rowCount());
d->mCurrentTrack = index(randomValue, 0);
d->mRandomPositions.removeLast();
d->mCurrentTrack = index(d->mRandomPositions.at(0), 0);
d->mRandomPositions.prepend(QRandomGenerator::global()->bounded(rowCount()));
} else {
if (d->mRepeatPlay) {
if (d->mCurrentTrack.row() == 0) {
if (d->mCurrentTrack.row() == 0) {
if (d->mRepeatPlay) {
d->mCurrentTrack = index(rowCount() - 1, 0);
} else {
d->mCurrentTrack = index(d->mCurrentTrack.row() - 1, 0);
return;
}
} else {
d->mCurrentTrack = index(d->mCurrentTrack.row() - 1, d->mCurrentTrack.column(), d->mCurrentTrack.parent());
d->mCurrentTrack = index(d->mCurrentTrack.row() - 1, 0);
}
}
notifyCurrentTrackChanged();
}
void MediaPlayList::seedRandomGenerator(uint seed)
{
qsrand(seed);
notifyCurrentTrackChanged();
}
void MediaPlayList::switchTo(int row)
......@@ -1188,7 +1177,12 @@ void MediaPlayList::switchTo(int row)
return;
}
d->mCurrentTrack = index(row, 0);
if (d->mRandomPlay) {
d->mCurrentTrack = index(row, 0);
d->mRandomPositions.replace(1, row);
} else {
d->mCurrentTrack = index(row, 0);
}
notifyCurrentTrackChanged();
}
......@@ -1309,4 +1303,14 @@ int MediaPlayList::remainingTracks() const
}
}
void MediaPlayList::createRandomList()
{
for (auto& position : d->mRandomPositions) {
position = QRandomGenerator::global()->bounded(rowCount());
}
if (d->mCurrentTrack.isValid()) {
d->mRandomPositions.replace(1, d->mCurrentTrack.row());
}
}
#include "moc_mediaplaylist.cpp"
......@@ -242,8 +242,6 @@ public Q_SLOTS:
void skipPreviousTrack();
void seedRandomGenerator(uint seed);
void switchTo(int row);
void loadPlaylist(const QString &localFileName);
......@@ -298,6 +296,8 @@ private:
void restoreRandomPlay();
void createRandomList();
void restoreRepeatPlay();
void enqueueArtist(const QString &artistName);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment