diff --git a/autotests/mediaplaylisttest.cpp b/autotests/mediaplaylisttest.cpp --- a/autotests/mediaplaylisttest.cpp +++ b/autotests/mediaplaylisttest.cpp @@ -5428,8 +5428,6 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ QCOMPARE(playListLoadedRestoreSpy.count(), 0); QCOMPARE(playListLoadFailedRestoreSpy.count(), 0); - myPlayListSave.seedRandomGenerator(0); - QCOMPARE(currentTrackChangedSaveSpy.count(), 0); QCOMPARE(randomPlayChangedSaveSpy.count(), 0); QCOMPARE(repeatPlayChangedSaveSpy.count(), 0); diff --git a/src/mediaplaylist.h b/src/mediaplaylist.h --- a/src/mediaplaylist.h +++ b/src/mediaplaylist.h @@ -242,8 +242,6 @@ void skipPreviousTrack(); - void seedRandomGenerator(uint seed); - void switchTo(int row); void loadPlaylist(const QString &localFileName); @@ -298,6 +296,8 @@ void restoreRandomPlay(); + void createRandomList(); + void restoreRepeatPlay(); void enqueueArtist(const QString &artistName); diff --git a/src/mediaplaylist.cpp b/src/mediaplaylist.cpp --- a/src/mediaplaylist.cpp +++ b/src/mediaplaylist.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -55,18 +56,14 @@ bool mForceUndo = false; + QList 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(currentMsecTime)); - } } MediaPlayList::~MediaPlayList() @@ -304,6 +301,10 @@ resetCurrentTrack(); } + if (d->mRandomPlay) { + createRandomList(); + } + Q_EMIT tracksCountChanged(); Q_EMIT remainingTracksChanged(); Q_EMIT persistentStateChanged(); @@ -878,6 +879,9 @@ for (auto oneItem : selection) { removeRow(oneItem); } + if (d->mRandomPlay) { + createRandomList(); + } } void MediaPlayList::tracksListAdded(qulonglong newDatabaseId, @@ -1092,6 +1096,7 @@ { if (d->mRandomPlay != value) { d->mRandomPlay = value; + createRandomList(); Q_EMIT randomPlayChanged(); Q_EMIT remainingTracksChanged(); } @@ -1123,27 +1128,19 @@ 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,40 +1152,37 @@ 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) { if (!d->mCurrentTrack.isValid()) { 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 @@ } } +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"