From 930fbaeac11fe0612916a6eb2b49d2abc4a95d58 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Thu, 24 Dec 2009 20:27:32 +0000 Subject: [PATCH] Move fileview to its own class, remember where it was --- src/fileview.cpp | 87 ++++++++++++++++++++++++++++++++++++++++ src/fileview.h | 44 +++++++++++++++++++++ src/fileview.ui | 99 ++++++++++++++++++++++++++++++++++++++++++++++ src/library.cpp | 4 -- src/library.h | 3 +- src/mainwindow.cpp | 84 +++++++-------------------------------- src/mainwindow.h | 15 +------ src/mainwindow.ui | 99 +++++++++------------------------------------- src/src.pro | 9 +++-- 9 files changed, 272 insertions(+), 172 deletions(-) create mode 100644 src/fileview.cpp create mode 100644 src/fileview.h create mode 100644 src/fileview.ui diff --git a/src/fileview.cpp b/src/fileview.cpp new file mode 100644 index 000000000..fb1643108 --- /dev/null +++ b/src/fileview.cpp @@ -0,0 +1,87 @@ +#include "fileview.h" + +#include +#include + +FileView::FileView(QWidget* parent) + : QWidget(parent), + model_(new QFileSystemModel(this)), + undo_stack_(new QUndoStack(this)) +{ + ui_.setupUi(this); + + ui_.list->setModel(model_); + ChangeFilePathWithoutUndo(QDir::homePath()); + + connect(ui_.back, SIGNAL(clicked()), SLOT(FileBack())); + connect(ui_.forward, SIGNAL(clicked()), SLOT(FileForward())); + connect(ui_.home, SIGNAL(clicked()), SLOT(FileHome())); + connect(ui_.up, SIGNAL(clicked()), SLOT(FileUp())); + connect(ui_.path, SIGNAL(textChanged(QString)), SLOT(ChangeFilePath(QString))); + + connect(undo_stack_, SIGNAL(canUndoChanged(bool)), ui_.back, SLOT(setEnabled(bool))); + connect(undo_stack_, SIGNAL(canRedoChanged(bool)), ui_.forward, SLOT(setEnabled(bool))); + + connect(ui_.list, SIGNAL(activated(QModelIndex)), SLOT(ItemActivated(QModelIndex))); + connect(ui_.list, SIGNAL(doubleClicked(QModelIndex)), SLOT(ItemDoubleClick(QModelIndex))); +} + +void FileView::SetPath(const QString& path) { + ChangeFilePathWithoutUndo(path); +} + +void FileView::FileUp() { + QDir dir(model_->rootDirectory()); + dir.cdUp(); + + 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()); +} + +void FileView::ChangeFilePath(const QString& new_path) { + QFileInfo info(new_path); + if (!info.exists() || !info.isDir()) + return; + + QString old_path(model_->rootPath()); + + ChangeFilePathWithoutUndo(new_path); + undo_stack_->push(new QUndoCommand(old_path)); +} + +void FileView::ChangeFilePathWithoutUndo(const QString& new_path) { + ui_.list->setRootIndex(model_->setRootPath(new_path)); + ui_.path->setText(new_path); + + QDir dir(new_path); + ui_.up->setEnabled(dir.cdUp()); + + emit PathChanged(new_path); +} + +void FileView::ItemActivated(const QModelIndex& index) { + if (model_->isDir(index)) + ChangeFilePath(model_->filePath(index)); +} + +void FileView::ItemDoubleClick(const QModelIndex& index) { + if (model_->isDir(index)) + return; + + emit PlayFile(model_->filePath(index)); +} diff --git a/src/fileview.h b/src/fileview.h new file mode 100644 index 000000000..3d50ce711 --- /dev/null +++ b/src/fileview.h @@ -0,0 +1,44 @@ +#ifndef FILEVIEW_H +#define FILEVIEW_H + +#include + +#include "ui_fileview.h" + +class QFileSystemModel; +class QUndoStack; + +class FileView : public QWidget { + Q_OBJECT + + public: + FileView(QWidget* parent = 0); + + void SetPath(const QString& path); + + signals: + void PathChanged(const QString& path); + + void PlayFile(const QString& path); + void PlayDirectory(const QString& path); + + private slots: + void FileUp(); + void FileBack(); + void FileForward(); + void FileHome(); + void ChangeFilePath(const QString& new_path); + void ItemActivated(const QModelIndex& index); + void ItemDoubleClick(const QModelIndex& index); + + private: + void ChangeFilePathWithoutUndo(const QString& new_path); + + private: + Ui::FileView ui_; + + QFileSystemModel* model_; + QUndoStack* undo_stack_; +}; + +#endif // FILEVIEW_H diff --git a/src/fileview.ui b/src/fileview.ui new file mode 100644 index 000000000..4823a473a --- /dev/null +++ b/src/fileview.ui @@ -0,0 +1,99 @@ + + + FileView + + + + 0 + 0 + 400 + 300 + + + + Form + + + + 0 + + + 0 + + + + + 0 + + + + + false + + + + :/go-previous.png:/go-previous.png + + + true + + + + + + + false + + + ... + + + + :/go-next.png:/go-next.png + + + true + + + + + + + ... + + + + :/go-up.png:/go-up.png + + + true + + + + + + + ... + + + + :/go-home.png:/go-home.png + + + true + + + + + + + + + + + + + + + + diff --git a/src/library.cpp b/src/library.cpp index d52991db9..00eb9839d 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -77,10 +77,6 @@ void Library::Initialise() { Reset(); } -bool Library::IsEmpty() const { - return root_->children.isEmpty(); -} - void Library::SongsDiscovered(const SongList& songs) { foreach (const Song& song, songs) { LibraryItem* artist = NULL; diff --git a/src/library.h b/src/library.h index 261381cea..10dc6c099 100644 --- a/src/library.h +++ b/src/library.h @@ -29,11 +29,10 @@ class Library : public QAbstractItemModel { void StartThreads(); + // Get information about the library void GetChildSongs(LibraryItem* item, QList* urls, SongList* songs) const; SongList GetChildSongs(const QModelIndex& index) const; - bool IsEmpty() const; - // QAbstractItemModel int columnCount(const QModelIndex & parent = QModelIndex()) const; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 779445438..3d33851ae 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -26,8 +26,6 @@ MainWindow::MainWindow(QWidget *parent) player_(new Player(playlist_, this)), library_(new Library(player_->GetEngine(), this)), library_sort_model_(new QSortFilterProxyModel(this)), - file_model_(new QFileSystemModel(this)), - file_undo_stack_(new QUndoStack(this)), tray_icon_(new SystemTrayIcon(this)) { ui_.setupUi(this); @@ -49,21 +47,9 @@ MainWindow::MainWindow(QWidget *parent) ui_.library_view->setModel(library_sort_model_); ui_.library_view->SetLibrary(library_); - // File browser - ui_.file_view->setModel(file_model_); - ChangeFilePathWithoutUndo(QDir::homePath()); - - connect(ui_.file_back, SIGNAL(clicked()), SLOT(FileBack())); - connect(ui_.file_forward, SIGNAL(clicked()), SLOT(FileForward())); - connect(ui_.file_home, SIGNAL(clicked()), SLOT(FileHome())); - connect(ui_.file_up, SIGNAL(clicked()), SLOT(FileUp())); - connect(ui_.file_path, SIGNAL(textChanged(QString)), SLOT(ChangeFilePath(QString))); - - connect(file_undo_stack_, SIGNAL(canUndoChanged(bool)), ui_.file_back, SLOT(setEnabled(bool))); - connect(file_undo_stack_, SIGNAL(canRedoChanged(bool)), ui_.file_forward, SLOT(setEnabled(bool))); - - connect(ui_.file_view, SIGNAL(activated(QModelIndex)), SLOT(FileClicked(QModelIndex))); - connect(ui_.file_view, SIGNAL(doubleClicked(QModelIndex)), SLOT(FileDoubleClicked(QModelIndex))); + // File view connections + connect(ui_.file_view, SIGNAL(PlayFile(QString)), SLOT(PlayFile(QString))); + connect(ui_.file_view, SIGNAL(PathChanged(QString)), SLOT(FilePathChanged(QString))); // Action connections connect(ui_.action_next_track, SIGNAL(triggered()), player_, SLOT(Next())); @@ -168,7 +154,7 @@ MainWindow::MainWindow(QWidget *parent) setStyleSheet(stylesheet.readAll()); } - // Load geometry + // Load settings QSettings settings; settings.beginGroup(kSettingsGroup); @@ -178,6 +164,8 @@ MainWindow::MainWindow(QWidget *parent) tabifyDockWidget(ui_.files_dock, ui_.library_dock); } + ui_.file_view->SetPath(settings.value("file_path", QDir::homePath()).toString()); + if (!settings.value("hidden", false).toBool()) show(); @@ -188,59 +176,9 @@ MainWindow::~MainWindow() { SaveGeometry(); } -void MainWindow::FileUp() { - QDir dir(file_model_->rootDirectory()); - dir.cdUp(); - - ChangeFilePath(dir.path()); -} - -void MainWindow::FileBack() { - QString new_path(file_undo_stack_->command(file_undo_stack_->index()-1)->text()); - file_undo_stack_->undo(); - ChangeFilePathWithoutUndo(new_path); -} - -void MainWindow::FileForward() { - QString new_path(file_undo_stack_->command(file_undo_stack_->index()+1)->text()); - file_undo_stack_->redo(); - ChangeFilePathWithoutUndo(new_path); -} - -void MainWindow::FileHome() { - ChangeFilePath(QDir::homePath()); -} - -void MainWindow::ChangeFilePath(const QString& new_path) { - QFileInfo info(new_path); - if (!info.exists() || !info.isDir()) - return; - - QString old_path(file_model_->rootPath()); - - ChangeFilePathWithoutUndo(new_path); - file_undo_stack_->push(new QUndoCommand(old_path)); -} - -void MainWindow::ChangeFilePathWithoutUndo(const QString& new_path) { - ui_.file_view->setRootIndex(file_model_->setRootPath(new_path)); - ui_.file_path->setText(new_path); - - QDir dir(new_path); - ui_.file_up->setEnabled(dir.cdUp()); -} - -void MainWindow::FileClicked(const QModelIndex& index) { - if (file_model_->isDir(index)) - ChangeFilePath(file_model_->filePath(index)); -} - -void MainWindow::FileDoubleClicked(const QModelIndex& index) { - if (file_model_->isDir(index)) - return; - +void MainWindow::PlayFile(const QString& path) { Song song; - song.InitFromFile(file_model_->filePath(index), -1); + song.InitFromFile(path, -1); if (!song.is_valid()) return; @@ -347,3 +285,9 @@ void MainWindow::ClearLibraryFilter() { ui_.library_filter->clear(); ui_.library_filter->setFocus(); } + +void MainWindow::FilePathChanged(const QString& path) { + QSettings settings; + settings.beginGroup(kSettingsGroup); + settings.setValue("file_path", path); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 5e2a879a4..9510349b3 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -11,9 +11,7 @@ class Player; class Library; class LibraryConfig; -class QFileSystemModel; class QSortFilterProxyModel; -class QUndoStack; class SystemTrayIcon; class MainWindow : public QMainWindow { @@ -30,13 +28,8 @@ class MainWindow : public QMainWindow { void closeEvent(QCloseEvent* event); private slots: - void FileUp(); - void FileBack(); - void FileForward(); - void FileHome(); - void ChangeFilePath(const QString& new_path); - void FileClicked(const QModelIndex& index); - void FileDoubleClicked(const QModelIndex& index); + void PlayFile(const QString& path); + void FilePathChanged(const QString& path); void ReportError(const QString& message); void MediaStopped(); @@ -53,7 +46,6 @@ class MainWindow : public QMainWindow { void TrayClicked(QSystemTrayIcon::ActivationReason reason); private: - void ChangeFilePathWithoutUndo(const QString& new_path); void SaveGeometry(); private: @@ -68,9 +60,6 @@ class MainWindow : public QMainWindow { QSortFilterProxyModel* library_sort_model_; - QFileSystemModel* file_model_; - QUndoStack* file_undo_stack_; - SystemTrayIcon* tray_icon_; }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index e800ebb5d..acd928d43 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -14,7 +14,7 @@ Tangerine - + :/icon.png:/icon.png @@ -229,7 +229,7 @@ - + :/clear.png:/clear.png @@ -249,7 +249,7 @@ - + :/configure.png:/configure.png @@ -324,83 +324,14 @@ 0 - - - 0 - - - - - false - - - - :/go-previous.png:/go-previous.png - - - true - - - - - - - false - - - ... - - - - :/go-next.png:/go-next.png - - - true - - - - - - - ... - - - - :/go-up.png:/go-up.png - - - true - - - - - - - ... - - - - :/go-home.png:/go-home.png - - - true - - - - - - - - - - + - + :/media-skip-backward.png:/media-skip-backward.png @@ -412,7 +343,7 @@ - + :/media-playback-start.png:/media-playback-start.png @@ -427,7 +358,7 @@ false - + :/media-playback-stop.png:/media-playback-stop.png @@ -439,7 +370,7 @@ - + :/media-skip-forward.png:/media-skip-forward.png @@ -451,7 +382,7 @@ - + :/exit.png:/exit.png @@ -463,7 +394,7 @@ - + :/media-playback-stop.png:/media-playback-stop.png @@ -548,7 +479,15 @@ QTreeView
libraryview.h
+ + FileView + QWidget +
fileview.h
+ 1 +
- + + + diff --git a/src/src.pro b/src/src.pro index bbef9979d..4333f3cb1 100644 --- a/src/src.pro +++ b/src/src.pro @@ -30,7 +30,8 @@ SOURCES += main.cpp \ libraryitem.cpp \ libraryconfig.cpp \ systemtrayicon.cpp \ - libraryquery.cpp + libraryquery.cpp \ + fileview.cpp HEADERS += mainwindow.h \ player.h \ library.h \ @@ -57,9 +58,11 @@ HEADERS += mainwindow.h \ libraryitem.h \ libraryconfig.h \ systemtrayicon.h \ - libraryquery.h + libraryquery.h \ + fileview.h FORMS += mainwindow.ui \ - libraryconfig.ui + libraryconfig.ui \ + fileview.ui RESOURCES += ../data/data.qrc OTHER_FILES += ../data/schema.sql \ ../data/mainwindow.css