Save and restore playlist scrollbar position when switching between playlists

This commit is contained in:
Jonas Kvinge 2020-08-09 14:00:56 +02:00
parent 16a753bd95
commit 15ddf6ff20
4 changed files with 16 additions and 6 deletions

View File

@ -25,6 +25,7 @@
#include <QWidget> #include <QWidget>
#include <QItemSelectionModel> #include <QItemSelectionModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QScrollBar>
#include <QAction> #include <QAction>
#include <QList> #include <QList>
#include <QVariant> #include <QVariant>
@ -160,7 +161,7 @@ void PlaylistContainer::SetManager(PlaylistManager *manager) {
connect(ui_->tab_bar, SIGNAL(PlaylistOrderChanged(QList<int>)), manager, SLOT(ChangePlaylistOrder(QList<int>))); connect(ui_->tab_bar, SIGNAL(PlaylistOrderChanged(QList<int>)), manager, SLOT(ChangePlaylistOrder(QList<int>)));
connect(manager, SIGNAL(CurrentChanged(Playlist*)), SLOT(SetViewModel(Playlist*))); connect(manager, SIGNAL(CurrentChanged(Playlist*, int)), SLOT(SetViewModel(Playlist*, int)));
connect(manager, SIGNAL(PlaylistAdded(int, QString, bool)), SLOT(PlaylistAdded(int, QString, bool))); connect(manager, SIGNAL(PlaylistAdded(int, QString, bool)), SLOT(PlaylistAdded(int, QString, bool)));
connect(manager, SIGNAL(PlaylistManagerInitialized()), SLOT(Started())); connect(manager, SIGNAL(PlaylistManagerInitialized()), SLOT(Started()));
connect(manager, SIGNAL(PlaylistClosed(int)), SLOT(PlaylistClosed(int))); connect(manager, SIGNAL(PlaylistClosed(int)), SLOT(PlaylistClosed(int)));
@ -168,7 +169,7 @@ void PlaylistContainer::SetManager(PlaylistManager *manager) {
} }
void PlaylistContainer::SetViewModel(Playlist *playlist) { void PlaylistContainer::SetViewModel(Playlist *playlist, const int scroll_position) {
if (view()->selectionModel()) { if (view()->selectionModel()) {
disconnect(view()->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(SelectionChanged())); disconnect(view()->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(SelectionChanged()));
@ -191,6 +192,7 @@ void PlaylistContainer::SetViewModel(Playlist *playlist) {
view()->setModel(playlist->proxy()); view()->setModel(playlist->proxy());
view()->SetPlaylist(playlist); view()->SetPlaylist(playlist);
view()->selectionModel()->select(manager_->current_selection(), QItemSelectionModel::ClearAndSelect); view()->selectionModel()->select(manager_->current_selection(), QItemSelectionModel::ClearAndSelect);
if (scroll_position != 0) view()->verticalScrollBar()->setValue(scroll_position);
playlist->IgnoreSorting(false); playlist->IgnoreSorting(false);
connect(view()->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(SelectionChanged())); connect(view()->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(SelectionChanged()));

View File

@ -80,7 +80,7 @@ class PlaylistContainer : public QWidget {
void GoToNextPlaylistTab(); void GoToNextPlaylistTab();
void GoToPreviousPlaylistTab(); void GoToPreviousPlaylistTab();
void SetViewModel(Playlist *playlist); void SetViewModel(Playlist *playlist, const int scroll_position);
void PlaylistAdded(int id, const QString &name, bool favorite); void PlaylistAdded(int id, const QString &name, bool favorite);
void PlaylistClosed(int id); void PlaylistClosed(int id);
void PlaylistRenamed(int id, const QString &new_name); void PlaylistRenamed(int id, const QString &new_name);

View File

@ -39,6 +39,7 @@
#include <QRegularExpression> #include <QRegularExpression>
#include <QUrl> #include <QUrl>
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QScrollBar>
#include <QSettings> #include <QSettings>
#include <QtDebug> #include <QtDebug>
@ -366,8 +367,14 @@ void PlaylistManager::OneOfPlaylistsChanged() {
void PlaylistManager::SetCurrentPlaylist(int id) { void PlaylistManager::SetCurrentPlaylist(int id) {
Q_ASSERT(playlists_.contains(id)); Q_ASSERT(playlists_.contains(id));
// Save the scroll position for the current playlist.
if (playlists_.contains(current_)) {
playlists_[current_].scroll_position = playlist_container_->view()->verticalScrollBar()->value();
}
current_ = id; current_ = id;
emit CurrentChanged(current()); emit CurrentChanged(current(), playlists_[id].scroll_position);
UpdateSummaryText(); UpdateSummaryText();
} }

View File

@ -112,7 +112,7 @@ class PlaylistManagerInterface : public QObject {
void PlaylistClosed(int id); void PlaylistClosed(int id);
void PlaylistRenamed(int id, const QString &new_name); void PlaylistRenamed(int id, const QString &new_name);
void PlaylistFavorited(int id, bool favorite); void PlaylistFavorited(int id, bool favorite);
void CurrentChanged(Playlist *new_playlist); void CurrentChanged(Playlist *new_playlist, const int scroll_position = 0);
void ActiveChanged(Playlist *new_playlist); void ActiveChanged(Playlist *new_playlist);
void Error(const QString &message); void Error(const QString &message);
@ -223,10 +223,11 @@ class PlaylistManager : public PlaylistManagerInterface {
private: private:
struct Data { struct Data {
explicit Data(Playlist *_p = nullptr, const QString& _name = QString()) : p(_p), name(_name) {} explicit Data(Playlist *_p = nullptr, const QString& _name = QString()) : p(_p), name(_name), scroll_position(0) {}
Playlist *p; Playlist *p;
QString name; QString name;
QItemSelection selection; QItemSelection selection;
int scroll_position;
}; };
Application *app_; Application *app_;