From c2570adad60bcd772d41aba1d102d5176f5bbccc Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Mon, 25 Dec 2023 18:53:02 +0100 Subject: [PATCH] mod manager drag'n'drop --- launcher/launcherdirs.cpp | 5 ++ launcher/launcherdirs.h | 1 + launcher/modManager/cmodlistview_moc.cpp | 58 ++++++++++++++++++++++++ launcher/modManager/cmodlistview_moc.h | 5 ++ launcher/modManager/cmodmanager.cpp | 3 -- 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/launcher/launcherdirs.cpp b/launcher/launcherdirs.cpp index 97d456bb5..81e474f94 100644 --- a/launcher/launcherdirs.cpp +++ b/launcher/launcherdirs.cpp @@ -34,3 +34,8 @@ QString CLauncherDirs::modsPath() { return pathToQString(VCMIDirs::get().userDataPath() / "Mods"); } + +QString CLauncherDirs::mapsPath() +{ + return pathToQString(VCMIDirs::get().userDataPath() / "Maps"); +} diff --git a/launcher/launcherdirs.h b/launcher/launcherdirs.h index 9117bd9fb..e549fb218 100644 --- a/launcher/launcherdirs.h +++ b/launcher/launcherdirs.h @@ -19,4 +19,5 @@ public: QString downloadsPath(); QString modsPath(); + QString mapsPath(); }; diff --git a/launcher/modManager/cmodlistview_moc.cpp b/launcher/modManager/cmodlistview_moc.cpp index 97913cc40..5aa9b6f34 100644 --- a/launcher/modManager/cmodlistview_moc.cpp +++ b/launcher/modManager/cmodlistview_moc.cpp @@ -48,6 +48,46 @@ void CModListView::changeEvent(QEvent *event) QWidget::changeEvent(event); } +void CModListView::dragEnterEvent(QDragEnterEvent* event) +{ + event->acceptProposedAction(); +} + +void CModListView::dragMoveEvent(QDragMoveEvent* event) +{ + event->acceptProposedAction(); +} + +void CModListView::dragLeaveEvent(QDragLeaveEvent* event) +{ + event->accept(); +} + +void CModListView::dropEvent(QDropEvent* event) +{ + const QMimeData* mimeData = event->mimeData(); + + if(mimeData->hasUrls()) + { + QStringList pathList; + QList urlList = mimeData->urls(); + + for (int i = 0; i < urlList.size(); i++) + { + QString url = urlList.at(i).toString(); + if(url.endsWith(".zip", Qt::CaseInsensitive)) + downloadFile(url.split("/").last().toLower() + // mod name currently comes from zip file -> remove suffixes from github zip download + .replace(QRegularExpression("-[0-9a-f]{40}"), "") + .replace(QRegularExpression("-vcmi-.*\\.zip"), ".zip") + .replace(QRegularExpression("-main.zip"), ".zip") + , url, "mods", 0); + else + downloadFile(url.split("/").last(), url, "mods", 0); + } + } +} + void CModListView::setupFilterModel() { filterModel = new CModFilterModel(modModel, this); @@ -100,6 +140,8 @@ CModListView::CModListView(QWidget * parent) { ui->setupUi(this); + setAcceptDrops(true); + setupModModel(); setupFilterModel(); setupModsView(); @@ -677,6 +719,7 @@ void CModListView::hideProgressBar() void CModListView::installFiles(QStringList files) { QStringList mods; + QStringList maps; QStringList images; QVector repositories; @@ -685,6 +728,8 @@ void CModListView::installFiles(QStringList files) { if(filename.endsWith(".zip")) mods.push_back(filename); + if(filename.endsWith(".h3m") || filename.endsWith(".h3c") || filename.endsWith(".vmap") || filename.endsWith(".vcmp")) + maps.push_back(filename); if(filename.endsWith(".json")) { //download and merge additional files @@ -718,6 +763,9 @@ void CModListView::installFiles(QStringList files) if(!mods.empty()) installMods(mods); + if(!maps.empty()) + installMaps(maps); + if(!images.empty()) loadScreenshots(); } @@ -794,6 +842,16 @@ void CModListView::installMods(QStringList archives) QFile::remove(archive); } +void CModListView::installMaps(QStringList archives) +{ + QString destDir = CLauncherDirs::get().mapsPath() + "/"; + + for(QString archive : archives) + { + QFile(archive).rename(destDir + archive.section('/', -1, -1)); + } +} + void CModListView::on_refreshButton_clicked() { loadRepositories(); diff --git a/launcher/modManager/cmodlistview_moc.h b/launcher/modManager/cmodlistview_moc.h index bf6d07f55..579abd57f 100644 --- a/launcher/modManager/cmodlistview_moc.h +++ b/launcher/modManager/cmodlistview_moc.h @@ -54,12 +54,17 @@ class CModListView : public QWidget void downloadFile(QString file, QString url, QString description, qint64 size = 0); void installMods(QStringList archives); + void installMaps(QStringList archives); void installFiles(QStringList mods); QString genChangelogText(CModEntry & mod); QString genModInfoText(CModEntry & mod); void changeEvent(QEvent *event) override; + void dragEnterEvent(QDragEnterEvent* event) override; + void dragMoveEvent(QDragMoveEvent* event) override; + void dragLeaveEvent(QDragLeaveEvent* event) override; + void dropEvent(QDropEvent *event) override; signals: void modsChanged(); diff --git a/launcher/modManager/cmodmanager.cpp b/launcher/modManager/cmodmanager.cpp index 068518553..959a429c5 100644 --- a/launcher/modManager/cmodmanager.cpp +++ b/launcher/modManager/cmodmanager.cpp @@ -161,9 +161,6 @@ bool CModManager::canInstallMod(QString modname) if(mod.isInstalled()) return addError(modname, "Mod is already installed"); - - if(!mod.isAvailable()) - return addError(modname, "Mod is not available"); return true; }