Browse Source

mod manager drag'n'drop

Laserlicht 1 year ago
parent
commit
c2570adad6

+ 5 - 0
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");
+}

+ 1 - 0
launcher/launcherdirs.h

@@ -19,4 +19,5 @@ public:
 
 	QString downloadsPath();
 	QString modsPath();
+	QString mapsPath();
 };

+ 58 - 0
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<QUrl> 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<QVariantMap> 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();

+ 5 - 0
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();
 

+ 0 - 3
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;
 }