Move fileview to its own class, remember where it was

This commit is contained in:
David Sansome 2009-12-24 20:27:32 +00:00
parent 5b0496bf8f
commit 930fbaeac1
9 changed files with 272 additions and 172 deletions

87
src/fileview.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "fileview.h"
#include <QFileSystemModel>
#include <QUndoStack>
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));
}

44
src/fileview.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef FILEVIEW_H
#define FILEVIEW_H
#include <QWidget>
#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

99
src/fileview.ui Normal file
View File

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>FileView</class>
<widget class="QWidget" name="FileView">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="back">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-previous.png</normaloff>:/go-previous.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="forward">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-next.png</normaloff>:/go-next.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="up">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-up.png</normaloff>:/go-up.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="home">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-home.png</normaloff>:/go-home.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="path"/>
</item>
</layout>
</item>
<item>
<widget class="QListView" name="list"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -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;

View File

@ -29,11 +29,10 @@ class Library : public QAbstractItemModel {
void StartThreads();
// Get information about the library
void GetChildSongs(LibraryItem* item, QList<QUrl>* 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;

View File

@ -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);
}

View File

@ -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_;
};

View File

@ -14,7 +14,7 @@
<string>Tangerine</string>
</property>
<property name="windowIcon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/icon.png</normaloff>:/icon.png</iconset>
</property>
<widget class="QWidget" name="centralWidget">
@ -229,7 +229,7 @@
<item>
<widget class="QToolButton" name="library_filter_clear">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/clear.png</normaloff>:/clear.png</iconset>
</property>
<property name="iconSize">
@ -249,7 +249,7 @@
<item>
<widget class="QToolButton" name="library_options">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/configure.png</normaloff>:/configure.png</iconset>
</property>
<property name="iconSize">
@ -324,83 +324,14 @@
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="file_back">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-previous.png</normaloff>:/go-previous.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="file_forward">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-next.png</normaloff>:/go-next.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="file_up">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-up.png</normaloff>:/go-up.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="file_home">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/go-home.png</normaloff>:/go-home.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="file_path"/>
</item>
</layout>
</item>
<item>
<widget class="QListView" name="file_view"/>
<widget class="FileView" name="file_view" native="true"/>
</item>
</layout>
</widget>
</widget>
<action name="action_previous_track">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-skip-backward.png</normaloff>:/media-skip-backward.png</iconset>
</property>
<property name="text">
@ -412,7 +343,7 @@
</action>
<action name="action_play_pause">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-playback-start.png</normaloff>:/media-playback-start.png</iconset>
</property>
<property name="text">
@ -427,7 +358,7 @@
<bool>false</bool>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-playback-stop.png</normaloff>:/media-playback-stop.png</iconset>
</property>
<property name="text">
@ -439,7 +370,7 @@
</action>
<action name="action_next_track">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-skip-forward.png</normaloff>:/media-skip-forward.png</iconset>
</property>
<property name="text">
@ -451,7 +382,7 @@
</action>
<action name="action_quit">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/exit.png</normaloff>:/exit.png</iconset>
</property>
<property name="text">
@ -463,7 +394,7 @@
</action>
<action name="action_stop_after_this_track">
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/media-playback-stop.png</normaloff>:/media-playback-stop.png</iconset>
</property>
<property name="text">
@ -548,7 +479,15 @@
<extends>QTreeView</extends>
<header>libraryview.h</header>
</customwidget>
<customwidget>
<class>FileView</class>
<extends>QWidget</extends>
<header>fileview.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../data/data.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -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