1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-01-19 13:01:32 +01:00

When going back and forward in the file browser, remember which item was selected and where the view was scrolled to.

This commit is contained in:
David Sansome 2010-04-21 11:57:35 +00:00
parent 3400f2b947
commit b604324728
2 changed files with 56 additions and 19 deletions

View File

@ -17,7 +17,7 @@
#include "fileview.h"
#include <QFileSystemModel>
#include <QUndoStack>
#include <QScrollBar>
FileView::FileView(QWidget* parent)
: QWidget(parent),
@ -29,8 +29,8 @@ FileView::FileView(QWidget* parent)
ui_.list->setModel(model_);
ChangeFilePathWithoutUndo(QDir::homePath());
connect(ui_.back, SIGNAL(clicked()), SLOT(FileBack()));
connect(ui_.forward, SIGNAL(clicked()), SLOT(FileForward()));
connect(ui_.back, SIGNAL(clicked()), undo_stack_, SLOT(undo()));
connect(ui_.forward, SIGNAL(clicked()), undo_stack_, SLOT(redo()));
connect(ui_.home, SIGNAL(clicked()), SLOT(FileHome()));
connect(ui_.up, SIGNAL(clicked()), SLOT(FileUp()));
connect(ui_.path, SIGNAL(textChanged(QString)), SLOT(ChangeFilePath(QString)));
@ -56,18 +56,6 @@ void FileView::FileUp() {
ChangeFilePath(dir.path());
}
void FileView::FileBack() {
QString new_path(undo_stack_->command(undo_stack_->index()-1)->text());
undo_stack_->undo();
ChangeFilePathWithoutUndo(new_path);
}
void FileView::FileForward() {
QString new_path(undo_stack_->command(undo_stack_->index()+1)->text());
undo_stack_->redo();
ChangeFilePathWithoutUndo(new_path);
}
void FileView::FileHome() {
ChangeFilePath(QDir::homePath());
}
@ -78,9 +66,10 @@ void FileView::ChangeFilePath(const QString& new_path) {
return;
QString old_path(model_->rootPath());
if (old_path == new_path)
return;
ChangeFilePathWithoutUndo(new_path);
undo_stack_->push(new QUndoCommand(old_path));
undo_stack_->push(new UndoCommand(this, new_path));
}
void FileView::ChangeFilePathWithoutUndo(const QString& new_path) {
@ -104,3 +93,31 @@ void FileView::ItemDoubleClick(const QModelIndex& index) {
emit Queue(QList<QUrl>() << QUrl::fromLocalFile(model_->filePath(index)));
}
FileView::UndoCommand::UndoCommand(FileView* view, const QString& new_path)
: view_(view)
{
old_state_.path = view->model_->rootPath();
old_state_.scroll_pos = view_->ui_.list->verticalScrollBar()->value();
old_state_.index = view_->ui_.list->currentIndex();
new_state_.path = new_path;
}
void FileView::UndoCommand::redo() {
view_->ChangeFilePathWithoutUndo(new_state_.path);
if (new_state_.scroll_pos != -1) {
view_->ui_.list->setCurrentIndex(new_state_.index);
view_->ui_.list->verticalScrollBar()->setValue(new_state_.scroll_pos);
}
}
void FileView::UndoCommand::undo() {
new_state_.scroll_pos = view_->ui_.list->verticalScrollBar()->value();
new_state_.index = view_->ui_.list->currentIndex();
view_->ChangeFilePathWithoutUndo(old_state_.path);
view_->ui_.list->setCurrentIndex(old_state_.index);
view_->ui_.list->verticalScrollBar()->setValue(old_state_.scroll_pos);
}

View File

@ -18,6 +18,7 @@
#define FILEVIEW_H
#include <QWidget>
#include <QUndoCommand>
#include "ui_fileview.h"
@ -41,8 +42,6 @@ class FileView : public QWidget {
private slots:
void FileUp();
void FileBack();
void FileForward();
void FileHome();
void ChangeFilePath(const QString& new_path);
void ItemActivated(const QModelIndex& index);
@ -52,6 +51,27 @@ class FileView : public QWidget {
void ChangeFilePathWithoutUndo(const QString& new_path);
private:
class UndoCommand : public QUndoCommand {
public:
UndoCommand(FileView* view, const QString& new_path);
void undo();
void redo();
private:
struct State {
State() : scroll_pos(-1) {}
QString path;
QModelIndex index;
int scroll_pos;
};
FileView* view_;
State old_state_;
State new_state_;
};
Ui::FileView ui_;
QFileSystemModel* model_;