Commit 2d6663ca authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

starts to restore previous state for the PlayListControler

does not work well
parent f6515c1c
......@@ -52,6 +52,8 @@ ApplicationWindow {
property int height
property var playListState
property var playListControlerState
}
property string globalBrowseFlag: 'BrowseDirectChildren'
......@@ -109,6 +111,7 @@ ApplicationWindow {
persistentSettings.height = mainWindow.height;
persistentSettings.playListState = playListModelItem.persistentState;
persistentSettings.playListControlerState = playListControlerItem.persistentState;
}
}
......@@ -180,6 +183,8 @@ ApplicationWindow {
audioDuration: audioPlayer.duration
playerIsSeekable: audioPlayer.seekable
persistentState: persistentSettings.playListControlerState
onPlayMusic: audioPlayer.play()
onPauseMusic: audioPlayer.pause()
onStopMusic: audioPlayer.stop()
......@@ -336,9 +341,11 @@ ApplicationWindow {
MediaPlayListView {
id: playList
playControl: playControlItem
playListModel: playListModelItem
randomPlayChecked: playListControlerItem.randomPlayControl
repeatPlayChecked: playListControlerItem.repeatPlayControl
artistName: playListControlerItem.artist
albumName: playListControlerItem.album
albumArtUrl: playListControlerItem.image
......
......@@ -22,6 +22,7 @@
#include <QtCore/QTime>
#include <QtCore/QTimer>
#include <QtCore/QDebug>
#include <QtCore/QDataStream>
#include <cstdlib>
......@@ -255,6 +256,7 @@ void PlayListControler::setRandomPlay(bool value)
{
mRandomPlay = value;
Q_EMIT randomPlayChanged();
setRandomPlayControl(mRandomPlay);
}
bool PlayListControler::randomPlay() const
......@@ -266,6 +268,7 @@ void PlayListControler::setRepeatPlay(bool value)
{
mRepeatPlay = value;
Q_EMIT repeatPlayChanged();
setRepeatPlayControl(mRepeatPlay);
}
bool PlayListControler::repeatPlay() const
......@@ -293,6 +296,29 @@ int PlayListControler::isValidRole() const
return mIsValidRole;
}
QVariantMap PlayListControler::persistentState() const
{
auto persistentStateValue = QVariantMap();
persistentStateValue[QStringLiteral("currentTrack")] = mCurrentTrack.row();
persistentStateValue[QStringLiteral("playControlPosition")] = mPlayControlPosition;
persistentStateValue[QStringLiteral("randomPlay")] = mRandomPlay;
persistentStateValue[QStringLiteral("repeatPlay")] = mRepeatPlay;
persistentStateValue[QStringLiteral("playerState")].setValue(mPlayerState);
return persistentStateValue;
}
bool PlayListControler::randomPlayControl() const
{
return mRandomPlayControl;
}
bool PlayListControler::repeatPlayControl() const
{
return mRepeatPlayControl;
}
void PlayListControler::playListReset()
{
if (!mCurrentTrack.isValid()) {
......@@ -323,20 +349,38 @@ void PlayListControler::tracksInserted(const QModelIndex &parent, int first, int
if (!mCurrentTrack.isValid()) {
resetCurrentTrack();
if (mCurrentTrack.isValid() && mCurrentTrack.data(mIsValidRole).toBool()) {
Q_EMIT playControlEnabledChanged();
}
restorePlayListPosition();
restorePlayerState();
}
Q_EMIT remainingTracksChanged();
Q_EMIT playControlEnabledChanged();
Q_EMIT skipForwardControlEnabledChanged();
if (first <= mCurrentTrack.row() && mCurrentTrack.row() < last) {
Q_EMIT remainingTracksChanged();
Q_EMIT skipForwardControlEnabledChanged();
}
}
void PlayListControler::tracksDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
if (!mCurrentTrack.isValid()) {
resetCurrentTrack();
}
for (auto oneRole : roles) {
if (oneRole == mIsPlayingRole) {
signaTrackChange();
signaTrackChange();
continue;
}
if (!mCurrentTrack.isValid()) {
resetCurrentTrack();
}
signaTrackChange();
if (mCurrentTrack.isValid() && mCurrentTrack.data(mIsValidRole).toBool()) {
restorePlayerState();
}
}
}
void PlayListControler::tracksMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
......@@ -529,6 +573,112 @@ void PlayListControler::setIsValidRole(int isValidRole)
emit isValidRoleChanged();
}
void PlayListControler::restorePlayListPosition()
{
auto playerCurrentTrack = mPersistentState.find(QStringLiteral("currentTrack"));
if (playerCurrentTrack != mPersistentState.end()) {
if (mPlayListModel) {
mCurrentTrack = mPlayListModel->index(playerCurrentTrack->toInt(), 0);
if (mCurrentTrack.isValid()) {
signaTrackChange();
Q_EMIT playControlPositionChanged();
mPersistentState.erase(playerCurrentTrack);
}
}
}
}
void PlayListControler::restorePlayControlPosition()
{
auto playControlPositionStoredValue = mPersistentState.find(QStringLiteral("playControlPosition"));
if (playControlPositionStoredValue != mPersistentState.end()) {
mPlayControlPosition = playControlPositionStoredValue->toInt();
mPersistentState.erase(playControlPositionStoredValue);
}
}
void PlayListControler::restoreRandomPlay()
{
auto randomPlayStoredValue = mPersistentState.find(QStringLiteral("randomPlay"));
if (randomPlayStoredValue != mPersistentState.end()) {
setRandomPlayControl(randomPlayStoredValue->toBool());
mPersistentState.erase(randomPlayStoredValue);
}
}
void PlayListControler::restoreRepeatPlay()
{
auto repeatPlayStoredValue = mPersistentState.find(QStringLiteral("repeatPlay"));
if (repeatPlayStoredValue != mPersistentState.end()) {
setRepeatPlayControl(repeatPlayStoredValue->toBool());
mPersistentState.erase(repeatPlayStoredValue);
}
}
void PlayListControler::restorePlayerState()
{
qDebug() << "PlayListControler::restorePlayerState" << mPersistentState;
auto playerStateStoredValue = mPersistentState.find(QStringLiteral("playerState"));
if (playerStateStoredValue != mPersistentState.end()) {
qDebug() << playerStateStoredValue.key() << playerStateStoredValue.value();
if (mCurrentTrack.isValid() && mCurrentTrack.data(mIsValidRole).toBool()) {
mPlayerState = playerStateStoredValue->value<PlayerState>();
switch (mPlayerState) {
case PlayerState::Playing:
startPlayer();
break;
case PlayerState::Paused:
pausePlayer();
break;
case PlayerState::Stopped:
stopPlayer();
break;
}
qDebug() << "PlayListControler::restorePlayerState" << mPersistentState;
mPersistentState.erase(playerStateStoredValue);
qDebug() << "PlayListControler::restorePlayerState" << mPersistentState;
}
}
}
void PlayListControler::setPersistentState(QVariantMap persistentStateValue)
{
qDebug() << "PlayListControler::setPersistentState" << persistentStateValue;
mPersistentState = persistentStateValue;
restorePlayListPosition();
restorePlayControlPosition();
restoreRandomPlay();
restoreRepeatPlay();
restorePlayerState();
Q_EMIT persistentStateChanged();
}
void PlayListControler::setRandomPlayControl(bool randomPlayControl)
{
if (mRandomPlayControl == randomPlayControl) {
return;
}
mRandomPlayControl = randomPlayControl;
Q_EMIT randomPlayControlChanged();
}
void PlayListControler::setRepeatPlayControl(bool repeatPlayControl)
{
if (mRepeatPlayControl == repeatPlayControl) {
return;
}
mRepeatPlayControl = repeatPlayControl;
Q_EMIT repeatPlayControlChanged();
}
void PlayListControler::startPlayer()
{
mIsInPlayingState = true;
......@@ -619,5 +769,19 @@ void PlayListControler::resetCurrentTrack()
}
}
QDataStream &operator<<(QDataStream &out, const PlayListControler::PlayerState &state)
{
out << static_cast<int>(state);
return out;
}
QDataStream &operator>>(QDataStream &in, PlayListControler::PlayerState &state)
{
int value;
in >> value;
state = static_cast<PlayListControler::PlayerState>(value);
return in;
}
#include "moc_playlistcontroler.cpp"
......@@ -27,6 +27,8 @@
#include <QtCore/QModelIndex>
#include <QtCore/QUrl>
class QDataStream;
class PlayListControler : public QObject
{
Q_OBJECT
......@@ -144,11 +146,26 @@ class PlayListControler : public QObject
WRITE setRandomPlay
NOTIFY randomPlayChanged)
Q_PROPERTY(bool randomPlayControl
READ randomPlayControl
WRITE setRandomPlayControl
NOTIFY randomPlayControlChanged)
Q_PROPERTY(bool repeatPlay
READ repeatPlay
WRITE setRepeatPlay
NOTIFY repeatPlayChanged)
Q_PROPERTY(bool repeatPlayControl
READ repeatPlayControl
WRITE setRepeatPlayControl
NOTIFY repeatPlayControlChanged)
Q_PROPERTY(QVariantMap persistentState
READ persistentState
WRITE setPersistentState
NOTIFY persistentStateChanged)
public:
enum class PlayerState
......@@ -242,6 +259,12 @@ public:
int isValidRole() const;
QVariantMap persistentState() const;
bool randomPlayControl() const;
bool repeatPlayControl() const;
Q_SIGNALS:
void playMusic();
......@@ -302,6 +325,12 @@ Q_SIGNALS:
void isValidRoleChanged();
void persistentStateChanged();
void randomPlayControlChanged();
void repeatPlayControlChanged();
public Q_SLOTS:
void playListReset();
......@@ -341,6 +370,12 @@ public Q_SLOTS:
void setIsValidRole(int isValidRole);
void setPersistentState(QVariantMap persistentStateValue);
void setRandomPlayControl(bool randomPlayControl);
void setRepeatPlayControl(bool repeatPlayControl);
private:
void startPlayer();
......@@ -355,6 +390,16 @@ private:
void resetCurrentTrack();
void restorePlayListPosition();
void restorePlayControlPosition();
void restoreRandomPlay();
void restoreRepeatPlay();
void restorePlayerState();
QAbstractItemModel *mPlayListModel = nullptr;
QModelIndex mCurrentTrack;
......@@ -390,6 +435,16 @@ private:
bool mPlayerIsSeekable = false;
int mIsValidRole = Qt::DisplayRole;
QVariantMap mPersistentState;
bool mRandomPlayControl = false;
bool mRepeatPlayControl = false;
};
QDataStream& operator<<(QDataStream &out, const PlayListControler::PlayerState &state);
QDataStream& operator>>(QDataStream &in, PlayListControler::PlayerState &state);
#endif // PLAYLISTCONTROLER_H
......@@ -122,6 +122,8 @@ int __attribute__((visibility("default"))) main(int argc, char *argv[])
qRegisterMetaType<QVector<qulonglong>>("QVector<qulonglong>");
qRegisterMetaType<QHash<qulonglong,int>>("QHash<qulonglong,int>");
qRegisterMetaTypeStreamOperators<PlayListControler::PlayerState>("PlayListControler::PlayerState");
#if defined KF5I18n_FOUND && KF5I18n_FOUND
KLocalizedString::setApplicationDomain("elisa");
#endif
......
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