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

220 lines
6.3 KiB
C++
Raw Permalink 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:10:03 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include <algorithm>
2024-04-23 17:15:42 +02:00
#include <utility>
#include <QWidget>
#include <QAbstractItemModel>
#include <QFileInfo>
2018-02-27 18:06:05 +01:00
#include <QFileSystemModel>
#include <QDir>
2018-02-27 18:06:05 +01:00
#include <QMenu>
#include <QUrl>
#include <QCollator>
#include <QtEvents>
2018-02-27 18:06:05 +01:00
#include "core/iconloader.h"
2018-02-27 18:06:05 +01:00
#include "core/mimedata.h"
#include "utilities/filemanagerutils.h"
#include "fileviewlist.h"
2018-02-27 18:06:05 +01:00
FileViewList::FileViewList(QWidget *parent)
: QListView(parent),
2021-01-26 16:48:04 +01:00
menu_(new QMenu(this)) {
2018-02-27 18:06:05 +01:00
2024-04-09 23:20:26 +02:00
menu_->addAction(IconLoader::Load(QStringLiteral("media-playback-start")), tr("Append to current playlist"), this, &FileViewList::AddToPlaylistSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("media-playback-start")), tr("Replace current playlist"), this, &FileViewList::LoadSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("document-new")), tr("Open in new playlist"), this, &FileViewList::OpenInNewPlaylistSlot);
2018-02-27 18:06:05 +01:00
menu_->addSeparator();
2024-04-09 23:20:26 +02:00
menu_->addAction(IconLoader::Load(QStringLiteral("edit-copy")), tr("Copy to collection..."), this, &FileViewList::CopyToCollectionSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("go-jump")), tr("Move to collection..."), this, &FileViewList::MoveToCollectionSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("device")), tr("Copy to device..."), this, &FileViewList::CopyToDeviceSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("edit-delete")), tr("Delete from disk..."), this, &FileViewList::DeleteSlot);
2018-02-27 18:06:05 +01:00
menu_->addSeparator();
2024-04-09 23:20:26 +02:00
menu_->addAction(IconLoader::Load(QStringLiteral("edit-rename")), tr("Edit track information..."), this, &FileViewList::EditTagsSlot);
menu_->addAction(IconLoader::Load(QStringLiteral("document-open-folder")), tr("Show in file browser..."), this, &FileViewList::ShowInBrowser);
2018-02-27 18:06:05 +01:00
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;
2021-06-12 20:53:23 +02:00
const QModelIndexList indexes = menu_selection_.indexes();
for (const QModelIndex &index : indexes) {
2021-08-23 21:21:08 +02:00
if (index.column() == 0) {
filenames << QDir::cleanPath(qobject_cast<QFileSystemModel*>(model())->fileInfo(index).filePath());
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
QCollator collator;
collator.setNumericMode(true);
std::sort(filenames.begin(), filenames.end(), collator);
QList<QUrl> urls;
2023-03-19 20:32:06 +01:00
urls.reserve(filenames.count());
2024-04-23 17:15:42 +02:00
for (const QString &filename : std::as_const(filenames)) {
urls << QUrl::fromLocalFile(filename);
}
2018-02-27 18:06:05 +01:00
return urls;
}
MimeData *FileViewList::MimeDataFromSelection() const {
2020-04-23 21:08:28 +02:00
MimeData *mimedata = new MimeData;
mimedata->setUrls(UrlListFromSelection());
2018-02-27 18:06:05 +01:00
2024-04-23 16:51:42 +02:00
const QStringList filenames = FilenamesFromSelection();
2022-08-28 02:44:37 +02:00
// if just one folder selected - use its path as the new playlist's name
2018-02-27 18:06:05 +01:00
if (filenames.size() == 1 && QFileInfo(filenames.first()).isDir()) {
if (filenames.first().length() > 20) {
2020-04-23 21:08:28 +02:00
mimedata->name_for_new_playlist_ = QDir(filenames.first()).dirName();
}
else {
2020-04-23 21:08:28 +02:00
mimedata->name_for_new_playlist_ = filenames.first();
}
2018-02-27 18:06:05 +01:00
}
// otherwise, use the current root path
2018-02-27 18:06:05 +01:00
else {
QString path = qobject_cast<QFileSystemModel*>(model())->rootPath();
if (path.length() > 20) {
QFileInfo info(path);
if (info.isDir()) {
2020-04-23 21:08:28 +02:00
mimedata->name_for_new_playlist_ = QDir(info.filePath()).dirName();
}
else {
mimedata->name_for_new_playlist_ = info.completeBaseName();
}
}
else {
2020-04-23 21:08:28 +02:00
mimedata->name_for_new_playlist_ = path;
}
2018-02-27 18:06:05 +01:00
}
2020-04-23 21:08:28 +02:00
return mimedata;
2018-02-27 18:06:05 +01:00
}
QStringList FileViewList::FilenamesFromSelection() const {
QStringList filenames;
2021-06-12 20:53:23 +02:00
const QModelIndexList indexes = menu_selection_.indexes();
for (const QModelIndex &index : indexes) {
2021-08-23 21:21:08 +02:00
if (index.column() == 0) {
filenames << qobject_cast<QFileSystemModel*>(model())->filePath(index);
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
QCollator collator;
collator.setNumericMode(true);
std::sort(filenames.begin(), filenames.end(), collator);
2018-02-27 18:06:05 +01:00
return filenames;
}
void FileViewList::LoadSlot() {
2020-04-23 21:08:28 +02:00
MimeData *mimedata = MimeDataFromSelection();
mimedata->clear_first_ = true;
emit AddToPlaylist(mimedata);
2018-02-27 18:06:05 +01:00
}
void FileViewList::AddToPlaylistSlot() {
emit AddToPlaylist(MimeDataFromSelection());
}
void FileViewList::OpenInNewPlaylistSlot() {
2020-04-23 21:08:28 +02:00
MimeData *mimedata = MimeDataFromSelection();
mimedata->open_in_new_playlist_ = true;
emit AddToPlaylist(mimedata);
2018-02-27 18:06:05 +01:00
}
void FileViewList::CopyToCollectionSlot() {
emit CopyToCollection(UrlListFromSelection());
}
void FileViewList::MoveToCollectionSlot() {
emit MoveToCollection(UrlListFromSelection());
}
void FileViewList::CopyToDeviceSlot() {
emit CopyToDevice(UrlListFromSelection());
}
void FileViewList::DeleteSlot() {
emit Delete(FilenamesFromSelection());
}
void FileViewList::EditTagsSlot() {
emit EditTags(UrlListFromSelection());
}
void FileViewList::mousePressEvent(QMouseEvent *e) {
switch (e->button()) {
case Qt::XButton1:
emit Back();
break;
case Qt::XButton2:
emit Forward();
break;
// enqueue to playlist with middleClick
case Qt::MiddleButton:{
2018-02-27 18:06:05 +01:00
QListView::mousePressEvent(e);
// we need to update the menu selection
menu_selection_ = selectionModel()->selection();
2020-04-23 21:08:28 +02:00
MimeData *mimedata = new MimeData;
mimedata->setUrls(UrlListFromSelection());
mimedata->enqueue_now_ = true;
emit AddToPlaylist(mimedata);
2018-02-27 18:06:05 +01:00
break;
}
default:
QListView::mousePressEvent(e);
break;
}
}
void FileViewList::ShowInBrowser() {
Utilities::OpenInFileBrowser(UrlListFromSelection());
}