Refactoring

This commit is contained in:
Jonas Kvinge
2024-10-22 18:12:33 +02:00
parent dfcf715291
commit 8da2b9cd94
623 changed files with 9071 additions and 5126 deletions

298
src/fileview/fileview.cpp Normal file
View File

@@ -0,0 +1,298 @@
/*
* 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/>.
*
*/
#include <memory>
#include <QWidget>
#include <QUndoStack>
#include <QDir>
#include <QFileInfo>
#include <QFileSystemModel>
#include <QFileIconProvider>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QSettings>
#include <QMessageBox>
#include <QScrollBar>
#include <QLineEdit>
#include <QToolButton>
#include <QtEvents>
#include "includes/shared_ptr.h"
#include "core/deletefiles.h"
#include "core/filesystemmusicstorage.h"
#include "core/iconloader.h"
#include "core/settings.h"
#include "core/mimedata.h"
#include "dialogs/deleteconfirmationdialog.h"
#include "fileview.h"
#include "fileviewlist.h"
#include "ui_fileview.h"
#include "organize/organizeerrordialog.h"
#include "constants/appearancesettings.h"
#include "constants/filefilterconstants.h"
using std::make_unique;
using namespace Qt::Literals::StringLiterals;
FileView::FileView(QWidget *parent)
: QWidget(parent),
ui_(new Ui_FileView),
model_(nullptr),
undo_stack_(new QUndoStack(this)),
task_manager_(nullptr),
storage_(new FilesystemMusicStorage(Song::Source::LocalFile, u"/"_s)) {
ui_->setupUi(this);
// Icons
ui_->back->setIcon(IconLoader::Load(u"go-previous"_s));
ui_->forward->setIcon(IconLoader::Load(u"go-next"_s));
ui_->home->setIcon(IconLoader::Load(u"go-home"_s));
ui_->up->setIcon(IconLoader::Load(u"go-up"_s));
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);
QObject::connect(undo_stack_, &QUndoStack::canUndoChanged, ui_->back, &FileView::setEnabled);
QObject::connect(undo_stack_, &QUndoStack::canRedoChanged, ui_->forward, &FileView::setEnabled);
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);
QString filter = QLatin1String(kFileFilter);
filter_list_ << filter.split(u' ');
ReloadSettings();
}
FileView::~FileView() {
delete ui_;
}
void FileView::ReloadSettings() {
Settings s;
s.beginGroup(AppearanceSettings::kSettingsGroup);
int iconsize = s.value(AppearanceSettings::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));
}
void FileView::SetPath(const QString &path) {
if (model_) {
ChangeFilePathWithoutUndo(path);
}
else {
lazy_set_path_ = path;
}
}
void FileView::SetTaskManager(SharedPtr<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.
if (undo_stack_->canUndo()) {
const UndoCommand *last_dir = static_cast<const UndoCommand*>(undo_stack_->command(undo_stack_->index() - 1));
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);
if (!info.exists() || !info.isDir()) {
return;
}
QString old_path(model_->rootPath());
if (old_path == new_path) {
return;
}
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());
Q_EMIT PathChanged(new_path);
}
void FileView::ItemActivated(const QModelIndex &idx) {
if (model_->isDir(idx))
ChangeFilePath(model_->filePath(idx));
}
void FileView::ItemDoubleClick(const QModelIndex &idx) {
if (model_->isDir(idx)) {
return;
}
QString file_path = model_->filePath(idx);
MimeData *mimedata = new MimeData;
mimedata->from_doubleclick_ = true;
mimedata->setUrls(QList<QUrl>() << QUrl::fromLocalFile(file_path));
mimedata->name_for_new_playlist_ = file_path;
Q_EMIT AddToPlaylist(mimedata);
}
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);
}
void FileView::Delete(const QStringList &filenames) {
if (filenames.isEmpty()) return;
if (DeleteConfirmationDialog::warning(filenames) != QDialogButtonBox::Yes) return;
DeleteFiles *delete_files = new DeleteFiles(task_manager_, storage_, true);
QObject::connect(delete_files, &DeleteFiles::Finished, this, &FileView::DeleteFinished);
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::OperationType::Delete, songs_with_errors);
// It deletes itself when the user closes it
}
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_ = make_unique<QFileIconProvider>();
model_->setIconProvider(&*file_icon_provider_);
}
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;
default:
break;
}
QWidget::keyPressEvent(e);
}

125
src/fileview/fileview.h Normal file
View File

@@ -0,0 +1,125 @@
/*
* 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/>.
*
*/
#ifndef FILEVIEW_H
#define FILEVIEW_H
#include <QObject>
#include <QWidget>
#include <QAbstractItemModel>
#include <QList>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QUndoCommand>
#include "includes/scoped_ptr.h"
#include "includes/shared_ptr.h"
#include "core/song.h"
class QMimeData;
class QFileSystemModel;
class QFileIconProvider;
class QUndoStack;
class QKeyEvent;
class QShowEvent;
class MusicStorage;
class TaskManager;
class Ui_FileView;
class FileView : public QWidget {
Q_OBJECT
public:
explicit FileView(QWidget *parent = nullptr);
~FileView() override;
void ReloadSettings();
void SetPath(const QString &path);
void SetTaskManager(SharedPtr<TaskManager> task_manager);
protected:
void showEvent(QShowEvent*) override;
void keyPressEvent(QKeyEvent *e) override;
Q_SIGNALS:
void PathChanged(const QString &path);
void AddToPlaylist(QMimeData *data);
void CopyToCollection(const QList<QUrl> &urls);
void MoveToCollection(const QList<QUrl> &urls);
void CopyToDevice(const QList<QUrl> &urls);
void EditTags(const QList<QUrl> &urls);
private Q_SLOTS:
void FileUp();
void FileHome();
void ChangeFilePath(const QString &new_path);
void ItemActivated(const QModelIndex &idx);
void ItemDoubleClick(const QModelIndex &idx);
void Delete(const QStringList &filenames);
void DeleteFinished(const SongList &songs_with_errors);
private:
void ChangeFilePathWithoutUndo(const QString &new_path);
private:
class UndoCommand : public QUndoCommand {
public:
UndoCommand(FileView *view, const QString &new_path);
QString undo_path() const { return old_state_.path; }
void undo() override;
void redo() override;
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_;
QUndoStack *undo_stack_;
SharedPtr<TaskManager> task_manager_;
SharedPtr<MusicStorage> storage_;
QString lazy_set_path_;
QStringList filter_list_;
ScopedPtr<QFileIconProvider> file_icon_provider_;
};
#endif // FILEVIEW_H

133
src/fileview/fileview.ui Normal file
View File

@@ -0,0 +1,133 @@
<?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="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<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="iconSize">
<size>
<width>22</width>
<height>22</height>
</size>
</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="iconSize">
<size>
<width>22</width>
<height>22</height>
</size>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="up">
<property name="iconSize">
<size>
<width>22</width>
<height>22</height>
</size>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="home">
<property name="iconSize">
<size>
<width>22</width>
<height>22</height>
</size>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="path"/>
</item>
</layout>
</item>
<item>
<widget class="FileViewList" name="list">
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragOnly</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FileViewList</class>
<extends>QListView</extends>
<header>fileview/fileviewlist.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,221 @@
/*
* 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/>.
*
*/
#include <algorithm>
#include <utility>
#include <QWidget>
#include <QAbstractItemModel>
#include <QFileInfo>
#include <QFileSystemModel>
#include <QDir>
#include <QMenu>
#include <QUrl>
#include <QCollator>
#include <QtEvents>
#include "core/iconloader.h"
#include "core/mimedata.h"
#include "utilities/filemanagerutils.h"
#include "fileviewlist.h"
using namespace Qt::Literals::StringLiterals;
FileViewList::FileViewList(QWidget *parent)
: QListView(parent),
menu_(new QMenu(this)) {
menu_->addAction(IconLoader::Load(u"media-playback-start"_s), tr("Append to current playlist"), this, &FileViewList::AddToPlaylistSlot);
menu_->addAction(IconLoader::Load(u"media-playback-start"_s), tr("Replace current playlist"), this, &FileViewList::LoadSlot);
menu_->addAction(IconLoader::Load(u"document-new"_s), tr("Open in new playlist"), this, &FileViewList::OpenInNewPlaylistSlot);
menu_->addSeparator();
menu_->addAction(IconLoader::Load(u"edit-copy"_s), tr("Copy to collection..."), this, &FileViewList::CopyToCollectionSlot);
menu_->addAction(IconLoader::Load(u"go-jump"_s), tr("Move to collection..."), this, &FileViewList::MoveToCollectionSlot);
menu_->addAction(IconLoader::Load(u"device"_s), tr("Copy to device..."), this, &FileViewList::CopyToDeviceSlot);
menu_->addAction(IconLoader::Load(u"edit-delete"_s), tr("Delete from disk..."), this, &FileViewList::DeleteSlot);
menu_->addSeparator();
menu_->addAction(IconLoader::Load(u"edit-rename"_s), tr("Edit track information..."), this, &FileViewList::EditTagsSlot);
menu_->addAction(IconLoader::Load(u"document-open-folder"_s), tr("Show in file browser..."), this, &FileViewList::ShowInBrowser);
setAttribute(Qt::WA_MacShowFocusRect, false);
}
void FileViewList::contextMenuEvent(QContextMenuEvent *e) {
menu_selection_ = selectionModel()->selection();
menu_->popup(e->globalPos());
e->accept();
}
QList<QUrl> FileViewList::UrlListFromSelection() const {
QStringList filenames;
const QModelIndexList indexes = menu_selection_.indexes();
for (const QModelIndex &index : indexes) {
if (index.column() == 0) {
filenames << QDir::cleanPath(qobject_cast<QFileSystemModel*>(model())->fileInfo(index).filePath());
}
}
QCollator collator;
collator.setNumericMode(true);
std::sort(filenames.begin(), filenames.end(), collator);
QList<QUrl> urls;
urls.reserve(filenames.count());
for (const QString &filename : std::as_const(filenames)) {
urls << QUrl::fromLocalFile(filename);
}
return urls;
}
MimeData *FileViewList::MimeDataFromSelection() const {
MimeData *mimedata = new MimeData;
mimedata->setUrls(UrlListFromSelection());
const QStringList filenames = FilenamesFromSelection();
// if just one folder selected - use its path as the new playlist's name
if (filenames.size() == 1 && QFileInfo(filenames.first()).isDir()) {
if (filenames.first().length() > 20) {
mimedata->name_for_new_playlist_ = QDir(filenames.first()).dirName();
}
else {
mimedata->name_for_new_playlist_ = filenames.first();
}
}
// otherwise, use the current root path
else {
QString path = qobject_cast<QFileSystemModel*>(model())->rootPath();
if (path.length() > 20) {
QFileInfo info(path);
if (info.isDir()) {
mimedata->name_for_new_playlist_ = QDir(info.filePath()).dirName();
}
else {
mimedata->name_for_new_playlist_ = info.completeBaseName();
}
}
else {
mimedata->name_for_new_playlist_ = path;
}
}
return mimedata;
}
QStringList FileViewList::FilenamesFromSelection() const {
QStringList filenames;
const QModelIndexList indexes = menu_selection_.indexes();
for (const QModelIndex &index : indexes) {
if (index.column() == 0) {
filenames << qobject_cast<QFileSystemModel*>(model())->filePath(index);
}
}
QCollator collator;
collator.setNumericMode(true);
std::sort(filenames.begin(), filenames.end(), collator);
return filenames;
}
void FileViewList::LoadSlot() {
MimeData *mimedata = MimeDataFromSelection();
mimedata->clear_first_ = true;
Q_EMIT AddToPlaylist(mimedata);
}
void FileViewList::AddToPlaylistSlot() {
Q_EMIT AddToPlaylist(MimeDataFromSelection());
}
void FileViewList::OpenInNewPlaylistSlot() {
MimeData *mimedata = MimeDataFromSelection();
mimedata->open_in_new_playlist_ = true;
Q_EMIT AddToPlaylist(mimedata);
}
void FileViewList::CopyToCollectionSlot() {
Q_EMIT CopyToCollection(UrlListFromSelection());
}
void FileViewList::MoveToCollectionSlot() {
Q_EMIT MoveToCollection(UrlListFromSelection());
}
void FileViewList::CopyToDeviceSlot() {
Q_EMIT CopyToDevice(UrlListFromSelection());
}
void FileViewList::DeleteSlot() {
Q_EMIT Delete(FilenamesFromSelection());
}
void FileViewList::EditTagsSlot() {
Q_EMIT EditTags(UrlListFromSelection());
}
void FileViewList::mousePressEvent(QMouseEvent *e) {
switch (e->button()) {
case Qt::XButton1:
Q_EMIT Back();
break;
case Qt::XButton2:
Q_EMIT Forward();
break;
// enqueue to playlist with middleClick
case Qt::MiddleButton:{
QListView::mousePressEvent(e);
// we need to update the menu selection
menu_selection_ = selectionModel()->selection();
MimeData *mimedata = new MimeData;
mimedata->setUrls(UrlListFromSelection());
mimedata->enqueue_now_ = true;
Q_EMIT AddToPlaylist(mimedata);
break;
}
default:
QListView::mousePressEvent(e);
break;
}
}
void FileViewList::ShowInBrowser() {
Utilities::OpenInFileBrowser(UrlListFromSelection());
}

View File

@@ -0,0 +1,82 @@
/*
* 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/>.
*
*/
#ifndef FILEVIEWLIST_H
#define FILEVIEWLIST_H
#include <QObject>
#include <QListView>
#include <QItemSelectionModel>
#include <QList>
#include <QUrl>
#include <QString>
#include <QStringList>
class QWidget;
class QMimeData;
class QMenu;
class QMouseEvent;
class QContextMenuEvent;
class MimeData;
class FileViewList : public QListView {
Q_OBJECT
public:
explicit FileViewList(QWidget *parent = nullptr);
void mousePressEvent(QMouseEvent *e) override;
Q_SIGNALS:
void AddToPlaylist(QMimeData *data);
void CopyToCollection(const QList<QUrl> &urls);
void MoveToCollection(const QList<QUrl> &urls);
void CopyToDevice(const QList<QUrl> &urls);
void Delete(const QStringList &filenames);
void EditTags(const QList<QUrl> &urls);
void Back();
void Forward();
protected:
void contextMenuEvent(QContextMenuEvent *e) override;
private:
QStringList FilenamesFromSelection() const;
QList<QUrl> UrlListFromSelection() const;
MimeData *MimeDataFromSelection() const;
private Q_SLOTS:
void LoadSlot();
void AddToPlaylistSlot();
void OpenInNewPlaylistSlot();
void CopyToCollectionSlot();
void MoveToCollectionSlot();
void CopyToDeviceSlot();
void DeleteSlot();
void EditTagsSlot();
void ShowInBrowser();
private:
QMenu *menu_;
QItemSelection menu_selection_;
};
#endif // FILEVIEWLIST_H