1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

add context menu

This commit is contained in:
Laserlicht
2025-02-15 00:35:08 +01:00
parent 2d7535eb00
commit 45bb09d9df
5 changed files with 86 additions and 22 deletions

View File

@@ -12,28 +12,11 @@
#include "ui_aboutproject_moc.h" #include "ui_aboutproject_moc.h"
#include "../updatedialog_moc.h" #include "../updatedialog_moc.h"
#include "../helper.h"
#include "../../lib/GameConstants.h" #include "../../lib/GameConstants.h"
#include "../../lib/VCMIDirs.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) void AboutProjectView::hideAndStretchWidget(QGridLayout * layout, QWidget * toHide, QWidget * toStretch)
{ {
toHide->hide(); toHide->hide();
@@ -88,22 +71,22 @@ void AboutProjectView::on_updatesButton_clicked()
void AboutProjectView::on_openGameDataDir_clicked() void AboutProjectView::on_openGameDataDir_clicked()
{ {
revealDirectoryInFileBrowser(ui->lineEditGameDir); Helper::revealDirectoryInFileBrowser(ui->lineEditGameDir->text());
} }
void AboutProjectView::on_openUserDataDir_clicked() void AboutProjectView::on_openUserDataDir_clicked()
{ {
revealDirectoryInFileBrowser(ui->lineEditUserDataDir); Helper::revealDirectoryInFileBrowser(ui->lineEditUserDataDir->text());
} }
void AboutProjectView::on_openTempDir_clicked() void AboutProjectView::on_openTempDir_clicked()
{ {
revealDirectoryInFileBrowser(ui->lineEditTempDir); Helper::revealDirectoryInFileBrowser(ui->lineEditTempDir->text());
} }
void AboutProjectView::on_openConfigDir_clicked() void AboutProjectView::on_openConfigDir_clicked()
{ {
revealDirectoryInFileBrowser(ui->lineEditConfigDir); Helper::revealDirectoryInFileBrowser(ui->lineEditConfigDir->text());
} }
void AboutProjectView::on_pushButtonDiscord_clicked() void AboutProjectView::on_pushButtonDiscord_clicked()

View File

@@ -20,6 +20,10 @@
#include <QtAndroid> #include <QtAndroid>
#endif #endif
#ifdef VCMI_IOS
#include "ios/revealdirectoryinfiles.h"
#endif
#ifdef VCMI_MOBILE #ifdef VCMI_MOBILE
static QScrollerProperties generateScrollerProperties() static QScrollerProperties generateScrollerProperties()
{ {
@@ -75,4 +79,14 @@ void performNativeCopy(QString src, QString dst)
QFile::copy(src, dst); QFile::copy(src, dst);
#endif #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
}
} }

View File

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

View File

@@ -95,6 +95,11 @@ void CModListView::setupModsView()
ui->allModsView->setUniformRowHeights(true); 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&)), connect(ui->allModsView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&,const QModelIndex&)),
this, SLOT(modSelected(const QModelIndex&,const QModelIndex&))); this, SLOT(modSelected(const QModelIndex&,const QModelIndex&)));
@@ -417,6 +422,66 @@ void CModListView::disableModInfo()
ui->updateButton->setVisible(false); 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) void CModListView::dataChanged(const QModelIndex & topleft, const QModelIndex & bottomRight)
{ {
selectMod(ui->allModsView->currentIndex()); selectMod(ui->allModsView->currentIndex());

View File

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