From 314e6fff53cf52010700af42010ef950e1dda3de Mon Sep 17 00:00:00 2001 From: GriffinR Date: Thu, 9 Feb 2023 13:55:25 -0500 Subject: [PATCH] Allow script actions with the same name --- include/scripting.h | 2 +- include/scriptutility.h | 4 ++-- src/scriptapi/apiutility.cpp | 21 +++++++++++++++------ src/scriptapi/scripting.cpp | 4 ++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/scripting.h b/include/scripting.h index 28354d9b..7a0b1a67 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -35,7 +35,7 @@ public: static QJSEngine *getEngine(); static void registerAction(QString functionName, QString actionName); static int numRegisteredActions(); - static void invokeAction(QString actionName); + static void invokeAction(int actionIndex); static void cb_ProjectOpened(QString projectPath); static void cb_ProjectClosed(QString projectPath); static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock); diff --git a/include/scriptutility.h b/include/scriptutility.h index bcc475d5..5b6882c6 100644 --- a/include/scriptutility.h +++ b/include/scriptutility.h @@ -11,7 +11,7 @@ class ScriptUtility : public QObject public: ScriptUtility(MainWindow *mainWindow); void clearActions(); - QString getActionFunctionName(QString actionName); + QString getActionFunctionName(int actionIndex); Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = ""); Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds); Q_INVOKABLE void log(QString message); @@ -58,7 +58,7 @@ private: MainWindow *window; QList registeredActions; - QMap actionMap; + QHash actionMap; }; #endif // SCRIPTUTILITY_H diff --git a/src/scriptapi/apiutility.cpp b/src/scriptapi/apiutility.cpp index 7445a67d..0678a5e0 100644 --- a/src/scriptapi/apiutility.cpp +++ b/src/scriptapi/apiutility.cpp @@ -11,17 +11,26 @@ void ScriptUtility::registerAction(QString functionName, QString actionName, QSt if (!window || !window->ui || !window->ui->menuTools) return; - this->actionMap.insert(actionName, functionName); - if (this->actionMap.size() == 1) { + if (functionName.isEmpty() || actionName.isEmpty()) { + logError("Failed to register script action. 'functionName' and 'actionName' must be non-empty."); + return; + } + + if (this->registeredActions.size() == 0) { QAction *section = window->ui->menuTools->addSection("Custom Actions"); this->registeredActions.append(section); } - QAction *action = window->ui->menuTools->addAction(actionName, [actionName](){ - Scripting::invokeAction(actionName); + + const int actionIndex = this->registeredActions.size(); + QAction *action = window->ui->menuTools->addAction(actionName, [actionIndex](){ + Scripting::invokeAction(actionIndex); }); + if (!shortcut.isEmpty()) { action->setShortcut(QKeySequence(shortcut)); } + + this->actionMap.insert(actionIndex, functionName); this->registeredActions.append(action); } @@ -31,8 +40,8 @@ void ScriptUtility::clearActions() { } } -QString ScriptUtility::getActionFunctionName(QString actionName) { - return this->actionMap.value(actionName); +QString ScriptUtility::getActionFunctionName(int actionIndex) { + return this->actionMap.value(actionIndex); } void ScriptUtility::setTimeout(QJSValue callback, int milliseconds) { diff --git a/src/scriptapi/scripting.cpp b/src/scriptapi/scripting.cpp index 2680dc83..1310a99c 100644 --- a/src/scriptapi/scripting.cpp +++ b/src/scriptapi/scripting.cpp @@ -142,9 +142,9 @@ void Scripting::invokeCallback(CallbackType type, QJSValueList args) { } } -void Scripting::invokeAction(QString actionName) { +void Scripting::invokeAction(int actionIndex) { if (!instance || !instance->scriptUtility) return; - QString functionName = instance->scriptUtility->getActionFunctionName(actionName); + QString functionName = instance->scriptUtility->getActionFunctionName(actionIndex); if (functionName.isEmpty()) return; bool foundFunction = false;