diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2021-01-03 18:15:21 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2021-01-03 18:15:21 +0000 |
| commit | 773c8c68cf30960a2dd6e67561dd40bd766930bc (patch) | |
| tree | 9a546efa6cb418890adcc73d7925a90907fd3eea /src/plugins/General | |
| parent | 48c7312f4ba78ccf2947a9b4b0e6fd2bcdcb3066 (diff) | |
| download | qmmp-773c8c68cf30960a2dd6e67561dd40bd766930bc.tar.gz qmmp-773c8c68cf30960a2dd6e67561dd40bd766930bc.tar.bz2 qmmp-773c8c68cf30960a2dd6e67561dd40bd766930bc.zip | |
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
Diffstat (limited to 'src/plugins/General')
| -rw-r--r-- | src/plugins/General/library/librarymodel.cpp | 106 | ||||
| -rw-r--r-- | src/plugins/General/library/librarymodel.h | 8 | ||||
| -rw-r--r-- | src/plugins/General/library/librarywidget.ui | 9 |
3 files changed, 119 insertions, 4 deletions
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 <QSqlQuery> #include <QSqlError> #include <QtDebug> +#include <QMimeData> #include <qmmp/qmmp.h> #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<QUrl> 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<QUrl> LibraryModel::getUrls(const QModelIndex &index) const +{ + QSqlDatabase db = QSqlDatabase::database("qmmp_library_1"); + QList<QUrl> urls; + if(!db.isOpen()) + return urls; + + const LibraryTreeItem *item = static_cast<const LibraryTreeItem *>(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; +} diff --git a/src/plugins/General/library/librarymodel.h b/src/plugins/General/library/librarymodel.h index a13081bdd..a06365330 100644 --- a/src/plugins/General/library/librarymodel.h +++ b/src/plugins/General/library/librarymodel.h @@ -23,6 +23,7 @@ #include <QObject> #include <QStringList> +#include <QUrl> #include <QAbstractItemModel> class QSqlDatabase; @@ -35,6 +36,9 @@ public: LibraryModel(QObject *parent = nullptr); ~LibraryModel(); + Qt::ItemFlags flags(const QModelIndex &index) const; + QStringList mimeTypes() const override; + QMimeData *mimeData(const QModelIndexList &indexes) const override; bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; @@ -42,12 +46,12 @@ public: QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; - void refresh(); private: - LibraryTreeItem *m_rootItem; + QList<QUrl> getUrls(const QModelIndex &index) const; + LibraryTreeItem *m_rootItem; }; #endif // LIBRARYMODEL_H diff --git a/src/plugins/General/library/librarywidget.ui b/src/plugins/General/library/librarywidget.ui index 830180bb5..54c9b0b71 100644 --- a/src/plugins/General/library/librarywidget.ui +++ b/src/plugins/General/library/librarywidget.ui @@ -25,6 +25,15 @@ </property> <item> <widget class="QTreeView" name="treeView"> + <property name="dragEnabled"> + <bool>true</bool> + </property> + <property name="dragDropMode"> + <enum>QAbstractItemView::DragOnly</enum> + </property> + <property name="selectionMode"> + <enum>QAbstractItemView::ExtendedSelection</enum> + </property> <attribute name="headerVisible"> <bool>false</bool> </attribute> |
