From e755868b744233c6ab2491d949d0edecba8700ee Mon Sep 17 00:00:00 2001 From: David Sansome Date: Mon, 12 Apr 2010 00:40:03 +0000 Subject: [PATCH] Remember which track was being played last. Fixes issue #56 --- src/player.cpp | 13 +++++++------ src/playlist.cpp | 10 ++++++++++ src/playlist.h | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/player.cpp b/src/player.cpp index 584321e44..fb423d362 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -141,12 +141,13 @@ void Player::PlayPause() { case Engine::Empty: case Engine::Idle: { - int i = playlist_->current_index(); - if (i == -1) { - if (playlist_->rowCount() == 0) - break; - i = 0; - } + if (playlist_->rowCount() == 0) + break; + + int i = playlist_->current_index(); + if (i == -1) i = playlist_->last_played_index(); + if (i == -1) i = 0; + PlayAt(i, false); break; } diff --git a/src/playlist.cpp b/src/playlist.cpp index 4fa6e43c0..cbf06253d 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -190,6 +190,10 @@ int Playlist::current_index() const { return current_item_.isValid() ? current_item_.row() : -1; } +int Playlist::last_played_index() const { + return last_played_item_.isValid() ? last_played_item_.row() : -1; +} + void Playlist::ShuffleModeChanged(PlaylistSequence::ShuffleMode mode) { is_shuffled_ = (mode != PlaylistSequence::Shuffle_Off); ReshuffleIndices(); @@ -266,6 +270,8 @@ void Playlist::set_current_index(int i) { ClearStreamMetadata(); current_item_ = QPersistentModelIndex(index(i, 0, QModelIndex())); + last_played_item_ = old_current; + Save(); if (old_current.isValid()) emit dataChanged(old_current, old_current.sibling(old_current.row(), ColumnCount)); @@ -624,6 +630,8 @@ void Playlist::Save() const { items_.at(i)->Save(s); } s.endArray(); + + s.setValue("last_index", last_played_index()); } void Playlist::Restore() { @@ -650,6 +658,8 @@ void Playlist::Restore() { s.endArray(); reset(); + + last_played_item_ = index(s.value("last_index", -1).toInt(), 0, QModelIndex()); } bool Playlist::removeRows(int row, int count, const QModelIndex& parent) { diff --git a/src/playlist.h b/src/playlist.h index aff5652eb..dcacfbe01 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -77,10 +77,12 @@ class Playlist : public QAbstractListModel { // Persistence void Save() const; + void SaveLast() const; void Restore(); // Accessors int current_index() const; + int last_played_index() const; int next_index() const; int previous_index() const; bool stop_after_current() const; @@ -156,6 +158,7 @@ class Playlist : public QAbstractListModel { // that they will be played. QPersistentModelIndex current_item_; + QPersistentModelIndex last_played_item_; QPersistentModelIndex stop_after_; bool current_is_paused_; int current_virtual_index_;