strawberry-audio-player-win.../src/widgets/fileview.cpp

302 lines
8.5 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include <QWidget>
#include <QUndoStack>
#include <QDir>
#include <QFileInfo>
2018-02-27 18:06:05 +01:00
#include <QFileSystemModel>
#include <QFileIconProvider>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QSettings>
2018-02-27 18:06:05 +01:00
#include <QMessageBox>
#include <QScrollBar>
#include <QLineEdit>
#include <QToolButton>
#include <QtEvents>
2018-02-27 18:06:05 +01:00
#include "core/deletefiles.h"
#include "core/filesystemmusicstorage.h"
#include "core/iconloader.h"
#include "core/mimedata.h"
#include "dialogs/deleteconfirmationdialog.h"
#include "fileview.h"
#include "fileviewlist.h"
#include "ui_fileview.h"
2020-10-27 17:17:12 +01:00
#include "organize/organizeerrordialog.h"
#include "settings/appearancesettingspage.h"
2018-02-27 18:06:05 +01:00
const char *FileView::kFileFilter =
2019-07-07 21:36:05 +02:00
"*.wav *.flac *.wv *.ogg *.oga *.opus *.spx *.ape *.mpc "
"*.mp2 *.mp3 *.m4a *.mp4 *.aac *.asf *.asx *.wma "
"*.aif *.aiff *.mka *.tta *.dsf *.dsd "
"*.cue *.m3u *.m3u8 *.pls *.xspf *.asxini "
"*.ac3 *.dts "
"*.mod *.s3m *.xm *.it"
"*.spc *.vgm";
2018-02-27 18:06:05 +01:00
FileView::FileView(QWidget *parent)
: QWidget(parent),
ui_(new Ui_FileView),
model_(nullptr),
undo_stack_(new QUndoStack(this)),
task_manager_(nullptr),
2022-10-19 18:36:33 +02:00
storage_(new FilesystemMusicStorage(Song::Source_LocalFile, "/")) {
2018-02-27 18:06:05 +01:00
ui_->setupUi(this);
// Icons
ui_->back->setIcon(IconLoader::Load("go-previous"));
ui_->forward->setIcon(IconLoader::Load("go-next"));
ui_->home->setIcon(IconLoader::Load("go-home"));
ui_->up->setIcon(IconLoader::Load("go-up"));
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->back, &QToolButton::clicked, undo_stack_, &QUndoStack::undo);
QObject::connect(ui_->forward, &QToolButton::clicked, undo_stack_, &QUndoStack::redo);
QObject::connect(ui_->home, &QToolButton::clicked, this, &FileView::FileHome);
QObject::connect(ui_->up, &QToolButton::clicked, this, &FileView::FileUp);
QObject::connect(ui_->path, &QLineEdit::textChanged, this, &FileView::ChangeFilePath);
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
QObject::connect(undo_stack_, &QUndoStack::canUndoChanged, ui_->back, &FileView::setEnabled);
QObject::connect(undo_stack_, &QUndoStack::canRedoChanged, ui_->forward, &FileView::setEnabled);
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->list, &FileViewList::activated, this, &FileView::ItemActivated);
QObject::connect(ui_->list, &FileViewList::doubleClicked, this, &FileView::ItemDoubleClick);
QObject::connect(ui_->list, &FileViewList::AddToPlaylist, this, &FileView::AddToPlaylist);
QObject::connect(ui_->list, &FileViewList::CopyToCollection, this, &FileView::CopyToCollection);
QObject::connect(ui_->list, &FileViewList::MoveToCollection, this, &FileView::MoveToCollection);
QObject::connect(ui_->list, &FileViewList::CopyToDevice, this, &FileView::CopyToDevice);
QObject::connect(ui_->list, &FileViewList::Delete, this, &FileView::Delete);
QObject::connect(ui_->list, &FileViewList::EditTags, this, &FileView::EditTags);
2018-02-27 18:06:05 +01:00
QString filter(FileView::kFileFilter);
filter_list_ << filter.split(" ");
ReloadSettings();
2018-02-27 18:06:05 +01:00
}
FileView::~FileView() {
delete ui_;
}
void FileView::ReloadSettings() {
QSettings s;
s.beginGroup(AppearanceSettingsPage::kSettingsGroup);
int iconsize = s.value(AppearanceSettingsPage::kIconSizeLeftPanelButtons, 22).toInt();
s.endGroup();
ui_->back->setIconSize(QSize(iconsize, iconsize));
ui_->forward->setIconSize(QSize(iconsize, iconsize));
ui_->home->setIconSize(QSize(iconsize, iconsize));
ui_->up->setIconSize(QSize(iconsize, iconsize));
}
2018-02-27 18:06:05 +01:00
void FileView::SetPath(const QString &path) {
2021-08-23 21:21:08 +02:00
if (model_) {
2018-02-27 18:06:05 +01:00
ChangeFilePathWithoutUndo(path);
2021-08-23 21:21:08 +02:00
}
else {
lazy_set_path_ = path;
}
2018-02-27 18:06:05 +01:00
}
void FileView::SetTaskManager(TaskManager *task_manager) {
task_manager_ = task_manager;
}
void FileView::FileUp() {
QDir dir(model_->rootDirectory());
dir.cdUp();
// Is this the same as going back? If so just go back, so we can keep the view scroll position.
2018-02-27 18:06:05 +01:00
if (undo_stack_->canUndo()) {
2021-07-11 09:49:38 +02:00
const UndoCommand *last_dir = static_cast<const UndoCommand*>(undo_stack_->command(undo_stack_->index() - 1));
2018-02-27 18:06:05 +01:00
if (last_dir->undo_path() == dir.path()) {
undo_stack_->undo();
return;
}
}
ChangeFilePath(dir.path());
}
void FileView::FileHome() {
ChangeFilePath(QDir::homePath());
}
void FileView::ChangeFilePath(const QString &new_path_native) {
QString new_path = QDir::fromNativeSeparators(new_path_native);
QFileInfo info(new_path);
2021-08-23 21:21:08 +02:00
if (!info.exists() || !info.isDir()) {
2018-02-27 18:06:05 +01:00
return;
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
QString old_path(model_->rootPath());
2021-08-23 21:21:08 +02:00
if (old_path == new_path) {
2018-02-27 18:06:05 +01:00
return;
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
undo_stack_->push(new UndoCommand(this, new_path));
}
void FileView::ChangeFilePathWithoutUndo(const QString &new_path) {
ui_->list->setRootIndex(model_->setRootPath(new_path));
ui_->path->setText(QDir::toNativeSeparators(new_path));
QDir dir(new_path);
ui_->up->setEnabled(dir.cdUp());
emit PathChanged(new_path);
}
2020-04-23 21:08:28 +02:00
void FileView::ItemActivated(const QModelIndex &idx) {
if (model_->isDir(idx))
ChangeFilePath(model_->filePath(idx));
2018-02-27 18:06:05 +01:00
}
2020-04-23 21:08:28 +02:00
void FileView::ItemDoubleClick(const QModelIndex &idx) {
2018-02-27 18:06:05 +01:00
2021-08-23 21:21:08 +02:00
if (model_->isDir(idx)) {
2018-02-27 18:06:05 +01:00
return;
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
2020-04-23 21:08:28 +02:00
QString file_path = model_->filePath(idx);
2018-02-27 18:06:05 +01:00
2020-04-23 21:08:28 +02:00
MimeData *mimedata = new MimeData;
mimedata->from_doubleclick_ = true;
mimedata->setUrls(QList<QUrl>() << QUrl::fromLocalFile(file_path));
mimedata->name_for_new_playlist_ = file_path;
2018-02-27 18:06:05 +01:00
2020-04-23 21:08:28 +02:00
emit AddToPlaylist(mimedata);
2018-02-27 18:06:05 +01:00
}
2021-07-11 09:49:38 +02:00
FileView::UndoCommand::UndoCommand(FileView *view, const QString &new_path) : view_(view) {
2018-02-27 18:06:05 +01:00
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);
}
void FileView::Delete(const QStringList &filenames) {
if (filenames.isEmpty()) return;
2018-02-27 18:06:05 +01:00
if (DeleteConfirmationDialog::warning(filenames) != QDialogButtonBox::Yes) return;
2018-02-27 18:06:05 +01:00
bool use_trash = false;
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
use_trash = true;
#endif
DeleteFiles *delete_files = new DeleteFiles(task_manager_, storage_, use_trash);
2021-01-26 16:48:04 +01:00
QObject::connect(delete_files, &DeleteFiles::Finished, this, &FileView::DeleteFinished);
2018-02-27 18:06:05 +01:00
delete_files->Start(filenames);
}
void FileView::DeleteFinished(const SongList &songs_with_errors) {
if (songs_with_errors.isEmpty()) return;
OrganizeErrorDialog *dialog = new OrganizeErrorDialog(this);
dialog->Show(OrganizeErrorDialog::Type_Delete, songs_with_errors);
2018-02-27 18:06:05 +01:00
// It deletes itself when the user closes it
}
2018-02-27 18:06:05 +01:00
void FileView::showEvent(QShowEvent *e) {
QWidget::showEvent(e);
if (model_) return;
model_ = new QFileSystemModel(this);
if (!model_->iconProvider() || model_->iconProvider()->icon(QFileIconProvider::Folder).isNull()) {
file_icon_provider_ = std::make_unique<QFileIconProvider>();
model_->setIconProvider(file_icon_provider_.get());
}
2018-02-27 18:06:05 +01:00
model_->setNameFilters(filter_list_);
// if an item fails the filter, hide it
model_->setNameFilterDisables(false);
ui_->list->setModel(model_);
ChangeFilePathWithoutUndo(QDir::homePath());
if (!lazy_set_path_.isEmpty()) ChangeFilePathWithoutUndo(lazy_set_path_);
}
void FileView::keyPressEvent(QKeyEvent *e) {
switch (e->key()) {
case Qt::Key_Back:
case Qt::Key_Backspace:
ui_->up->click();
break;
case Qt::Key_Enter:
case Qt::Key_Return:
ItemDoubleClick(ui_->list->currentIndex());
break;
}
QWidget::keyPressEvent(e);
}