浏览代码

add context menu

Laserlicht 8 月之前
父节点
当前提交
45bb09d9df

+ 5 - 22
launcher/aboutProject/aboutproject_moc.cpp

@@ -12,28 +12,11 @@
 #include "ui_aboutproject_moc.h"
 
 #include "../updatedialog_moc.h"
+#include "../helper.h"
 
 #include "../../lib/GameConstants.h"
 #include "../../lib/VCMIDirs.h"
 
-#ifdef VCMI_IOS
-#include "ios/revealdirectoryinfiles.h"
-#endif
-
-namespace
-{
-void revealDirectoryInFileBrowser(QLineEdit * dirLineEdit)
-{
-	const auto dirUrl = QUrl::fromLocalFile(QFileInfo{dirLineEdit->text()}.absoluteFilePath());
-#ifdef VCMI_IOS
-	iOS_utils::revealDirectoryInFiles(dirUrl);
-#else
-	QDesktopServices::openUrl(dirUrl);
-#endif
-}
-}
-
-
 void AboutProjectView::hideAndStretchWidget(QGridLayout * layout, QWidget * toHide, QWidget * toStretch)
 {
 	toHide->hide();
@@ -88,22 +71,22 @@ void AboutProjectView::on_updatesButton_clicked()
 
 void AboutProjectView::on_openGameDataDir_clicked()
 {
-	revealDirectoryInFileBrowser(ui->lineEditGameDir);
+	Helper::revealDirectoryInFileBrowser(ui->lineEditGameDir->text());
 }
 
 void AboutProjectView::on_openUserDataDir_clicked()
 {
-	revealDirectoryInFileBrowser(ui->lineEditUserDataDir);
+	Helper::revealDirectoryInFileBrowser(ui->lineEditUserDataDir->text());
 }
 
 void AboutProjectView::on_openTempDir_clicked()
 {
-	revealDirectoryInFileBrowser(ui->lineEditTempDir);
+	Helper::revealDirectoryInFileBrowser(ui->lineEditTempDir->text());
 }
 
 void AboutProjectView::on_openConfigDir_clicked()
 {
-	revealDirectoryInFileBrowser(ui->lineEditConfigDir);
+	Helper::revealDirectoryInFileBrowser(ui->lineEditConfigDir->text());
 }
 
 void AboutProjectView::on_pushButtonDiscord_clicked()

+ 14 - 0
launcher/helper.cpp

@@ -20,6 +20,10 @@
 #include <QtAndroid>
 #endif
 
+#ifdef VCMI_IOS
+#include "ios/revealdirectoryinfiles.h"
+#endif
+
 #ifdef VCMI_MOBILE
 static QScrollerProperties generateScrollerProperties()
 {
@@ -75,4 +79,14 @@ void performNativeCopy(QString src, QString dst)
 	QFile::copy(src, dst);
 #endif
 }
+
+void revealDirectoryInFileBrowser(QString path)
+{
+	const auto dirUrl = QUrl::fromLocalFile(QFileInfo{path}.absoluteFilePath());
+#ifdef VCMI_IOS
+	iOS_utils::revealDirectoryInFiles(dirUrl);
+#else
+	QDesktopServices::openUrl(dirUrl);
+#endif
+}
 }

+ 1 - 0
launcher/helper.h

@@ -19,4 +19,5 @@ void loadSettings();
 void enableScrollBySwiping(QObject * scrollTarget);
 QString getRealPath(QString path);
 void performNativeCopy(QString src, QString dst);
+void revealDirectoryInFileBrowser(QString path);
 }

+ 65 - 0
launcher/modManager/cmodlistview_moc.cpp

@@ -95,6 +95,11 @@ void CModListView::setupModsView()
 
 	ui->allModsView->setUniformRowHeights(true);
 
+	ui->allModsView->setContextMenuPolicy(Qt::CustomContextMenu);
+	
+	connect(ui->allModsView, SIGNAL(customContextMenuRequested(const QPoint &)),
+		this, SLOT(onCustomContextMenu(const QPoint &)));
+
 	connect(ui->allModsView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&,const QModelIndex&)),
 		this, SLOT(modSelected(const QModelIndex&,const QModelIndex&)));
 
@@ -417,6 +422,66 @@ void CModListView::disableModInfo()
 	ui->updateButton->setVisible(false);
 }
 
+void CModListView::onCustomContextMenu(const QPoint &point)
+{
+	QModelIndex index = ui->allModsView->indexAt(point);
+	if(index.isValid())
+	{	
+		const auto modName = index.data(ModRoles::ModNameRole).toString();
+		auto mod = modStateModel->getMod(modName);
+
+		QStringList notInstalledDependencies = getModsToInstall(modName);
+		QStringList unavailableDependencies = findUnavailableMods(notInstalledDependencies);
+		bool translationMismatch = 	mod.isTranslation() && CGeneralTextHandler::getPreferredLanguage() != mod.getBaseLanguage().toStdString();
+		bool modIsBeingDownloaded = enqueuedModDownloads.contains(mod.getID());
+
+		auto contextMenu = new QMenu(tr("Context menu"), this);
+		QList<QAction*> actions;
+
+		auto addContextEntry = [this, &contextMenu, &actions, mod](bool condition, QString name, std::function<void(ModState)> function){
+			if(condition)
+			{
+				actions.append(new QAction(name, this));
+				connect(actions.back(), &QAction::triggered, this, [mod, function](){ function(mod); });
+				contextMenu->addAction(actions.back());
+			}
+		};
+
+		addContextEntry(
+			modStateModel->isModInstalled(mod.getID()) && modStateModel->isModEnabled(mod.getID()),
+			tr("Disable"),
+			[this](ModState mod){ disableModByName(mod.getID()); }
+		);
+		addContextEntry(
+			modStateModel->isModInstalled(mod.getID()) && !modStateModel->isModEnabled(mod.getID()) && notInstalledDependencies.empty() && !translationMismatch,
+			tr("Enable"),
+			[this](ModState mod){ enableModByName(mod.getID());
+		});
+		addContextEntry(
+			mod.isAvailable() && !mod.isSubmod() && unavailableDependencies.empty() && !modIsBeingDownloaded,
+			tr("Install"),
+			[this](ModState mod){ doInstallMod(mod.getID()); }
+		);
+		addContextEntry(
+			mod.isInstalled() && !mod.isSubmod(),
+			tr("Uninstall"),
+			[this](ModState mod){
+				if(modStateModel->isModEnabled(mod.getID()))
+					manager->disableMod(mod.getID());
+				manager->uninstallMod(mod.getID());
+				reload();
+			}
+		);
+		addContextEntry(
+			mod.isUpdateAvailable() && unavailableDependencies.empty() && !modIsBeingDownloaded,
+			tr("Update"),
+			[this](ModState mod){ doUpdateMod(mod.getID()); }
+		);
+
+		contextMenu->exec(ui->allModsView->viewport()->mapToGlobal(point));
+	}
+}
+
 void CModListView::dataChanged(const QModelIndex & topleft, const QModelIndex & bottomRight)
 {
 	selectMod(ui->allModsView->currentIndex());

+ 1 - 0
launcher/modManager/cmodlistview_moc.h

@@ -123,6 +123,7 @@ public slots:
 	void disableModByName(QString modName);
 
 private slots:
+	void onCustomContextMenu(const QPoint &point);
 	void dataChanged(const QModelIndex & topleft, const QModelIndex & bottomRight);
 	void modSelected(const QModelIndex & current, const QModelIndex & previous);
 	void downloadProgress(qint64 current, qint64 max);