From 773c8c68cf30960a2dd6e67561dd40bd766930bc Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Sun, 3 Jan 2021 18:15:21 +0000 Subject: library: drag and drop support git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9616 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/library/librarymodel.cpp | 106 ++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) (limited to 'src/plugins/General/library/librarymodel.cpp') diff --git a/src/plugins/General/library/librarymodel.cpp b/src/plugins/General/library/librarymodel.cpp index b24e64e6a..511927391 100644 --- a/src/plugins/General/library/librarymodel.cpp +++ b/src/plugins/General/library/librarymodel.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "librarymodel.h" @@ -60,6 +61,39 @@ LibraryModel::~LibraryModel() delete m_rootItem; } +Qt::ItemFlags LibraryModel::flags(const QModelIndex &index) const +{ + if(index.isValid()) + return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled; + else + return QAbstractItemModel::flags(index); +} + +QStringList LibraryModel::mimeTypes() const +{ + return QStringList("text/uri-list"); +} + +QMimeData *LibraryModel::mimeData(const QModelIndexList &indexes) const +{ + QList urls; + + for(const QModelIndex &index : indexes) + { + if(index.isValid()) + urls << getUrls(index); + } + + if(!urls.isEmpty()) + { + QMimeData *mimeData = new QMimeData; + mimeData->setUrls(urls); + return mimeData; + } + + return nullptr; +} + bool LibraryModel::canFetchMore(const QModelIndex &parent) const { if(!parent.isValid()) @@ -128,8 +162,6 @@ void LibraryModel::fetchMore(const QModelIndex &parent) item->parent = parentItem; parentItem->children << item; } - - qDebug() << parentItem->children.count(); } } @@ -219,3 +251,73 @@ void LibraryModel::refresh() } endResetModel(); } + +QList LibraryModel::getUrls(const QModelIndex &index) const +{ + QSqlDatabase db = QSqlDatabase::database("qmmp_library_1"); + QList urls; + if(!db.isOpen()) + return urls; + + const LibraryTreeItem *item = static_cast(index.internalPointer()); + + if(item->type == Qmmp::TITLE) + { + QSqlQuery query(db); + query.prepare("SELECT URL from track_library WHERE Artist = :artist AND Album = :album AND Title = :title"); + query.bindValue(":artist", item->parent->parent->name); + query.bindValue(":album", item->parent->name); + query.bindValue(":title", item->name); + + if(!query.exec()) + { + qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); + return urls; + } + + if(query.next()) + { + QString path = query.value("URL").toString(); + urls << (path.contains("://") ? QUrl(path) : QUrl::fromLocalFile(path)); + } + } + else if(item->type == Qmmp::ALBUM) + { + QSqlQuery query(db); + query.prepare("SELECT URL from track_library WHERE Artist = :artist AND Album = :album"); + query.bindValue(":artist", item->parent->name); + query.bindValue(":album", item->name); + + if(!query.exec()) + { + qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); + return urls; + } + + while(query.next()) + { + QString path = query.value("URL").toString(); + urls << (path.contains("://") ? QUrl(path) : QUrl::fromLocalFile(path)); + } + } + else if(item->type == Qmmp::ARTIST) + { + QSqlQuery query(db); + query.prepare("SELECT URL from track_library WHERE Artist = :artist"); + query.bindValue(":artist", item->name); + + if(!query.exec()) + { + qWarning("Library: exec error: %s", qPrintable(query.lastError().text())); + return urls; + } + + while(query.next()) + { + QString path = query.value("URL").toString(); + urls << (path.contains("://") ? QUrl(path) : QUrl::fromLocalFile(path)); + } + } + + return urls; +} -- cgit v1.2.3-13-gbd6f