CollectionFilter: Override mimedata function

This commit is contained in:
Jonas Kvinge 2024-07-20 01:55:53 +02:00
parent 92a1173b9e
commit d303e700ae
3 changed files with 76 additions and 3 deletions

View File

@ -19,12 +19,20 @@
#include "config.h"
#include <algorithm>
#include <functional>
#include <QSet>
#include <QList>
#include <QString>
#include <QUrl>
#include "core/logging.h"
#include "core/song.h"
#include "filterparser/filterparser.h"
#include "filterparser/filtertree.h"
#include "playlist/songmimedata.h"
#include "playlist/playlistmanager.h"
#include "collectionbackend.h"
#include "collectionfilter.h"
#include "collectionmodel.h"
#include "collectionitem.h"
@ -73,3 +81,56 @@ void CollectionFilter::SetFilterString(const QString &filter_string) {
setFilterFixedString(filter_string);
}
QMimeData *CollectionFilter::mimeData(const QModelIndexList &indexes) const {
if (indexes.isEmpty()) return nullptr;
CollectionModel *collection_model = qobject_cast<CollectionModel*>(sourceModel());
SongMimeData *data = new SongMimeData;
data->backend = collection_model->backend();
QSet<int> song_ids;
QList<QUrl> urls;
for (const QModelIndex &idx : indexes) {
const QModelIndex source_index = mapToSource(idx);
CollectionItem *item = collection_model->IndexToItem(source_index);
GetChildSongs(item, song_ids, urls, data->songs);
}
data->setUrls(urls);
data->name_for_new_playlist_ = PlaylistManager::GetNameForNewPlaylist(data->songs);
return data;
}
void CollectionFilter::GetChildSongs(CollectionItem *item, QSet<int> &song_ids, QList<QUrl> &urls, SongList &songs) const {
CollectionModel *collection_model = qobject_cast<CollectionModel*>(sourceModel());
switch (item->type) {
case CollectionItem::Type::Container:{
QList<CollectionItem*> children = item->children;
std::sort(children.begin(), children.end(), std::bind(&CollectionModel::CompareItems, collection_model, std::placeholders::_1, std::placeholders::_2));
for (CollectionItem *child : children) {
GetChildSongs(child, song_ids, urls, songs);
}
break;
}
case CollectionItem::Type::Song:{
const QModelIndex idx = collection_model->ItemToIndex(item);
if (filterAcceptsRow(idx.row(), idx.parent())) {
urls << item->metadata.url();
if (!song_ids.contains(item->metadata.id())) {
song_ids.insert(item->metadata.id());
songs << item->metadata;
}
}
break;
}
default:
break;
}
}

View File

@ -24,9 +24,15 @@
#include <QSortFilterProxyModel>
#include <QScopedPointer>
#include <QSet>
#include <QList>
#include <QUrl>
#include "core/song.h"
#include "filterparser/filtertree.h"
class CollectionItem;
class CollectionFilter : public QSortFilterProxyModel {
Q_OBJECT
@ -38,6 +44,10 @@ class CollectionFilter : public QSortFilterProxyModel {
protected:
bool filterAcceptsRow(const int source_row, const QModelIndex &source_parent) const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
private:
void GetChildSongs(CollectionItem *item, QSet<int> &song_ids, QList<QUrl> &urls, SongList &songs) const;
private:
mutable QScopedPointer<FilterTree> filter_tree_;

View File

@ -142,6 +142,7 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
CollectionFilterOptions filter_options;
};
SharedPtr<CollectionBackend> backend() const { return backend_; }
CollectionFilter *filter() const { return filter_; }
void Init();
@ -200,6 +201,8 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
void ExpandAll(CollectionItem *item = nullptr) const;
bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;
signals:
void TotalSongCountUpdated(const int count);
void TotalArtistCountUpdated(const int count);
@ -250,7 +253,6 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
static QUrl AlbumIconPixmapDiskCacheKey(const QString &cache_key);
QVariant AlbumIcon(const QModelIndex &idx);
void ClearItemPixmapCache(CollectionItem *item);
bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;
static qint64 MaximumCacheSize(Settings *s, const char *size_id, const char *size_unit_id, const qint64 cache_size_default);
private slots: