Merge branch 'master' of https://github.com/huderlem/porymap into connections

This commit is contained in:
GriffinR 2024-08-13 21:21:45 -04:00
commit 7c73161ad0
6 changed files with 54 additions and 17 deletions

View file

@ -30,8 +30,9 @@ class Scripting
{ {
public: public:
Scripting(MainWindow *mainWindow); Scripting(MainWindow *mainWindow);
static void stop(); ~Scripting();
static void init(MainWindow *mainWindow); static void init(MainWindow *mainWindow);
static void stop();
static void populateGlobalObject(MainWindow *mainWindow); static void populateGlobalObject(MainWindow *mainWindow);
static QJSEngine *getEngine(); static QJSEngine *getEngine();
static void invokeAction(int actionIndex); static void invokeAction(int actionIndex);

View file

@ -10,7 +10,8 @@ class ScriptUtility : public QObject
public: public:
ScriptUtility(MainWindow *mainWindow); ScriptUtility(MainWindow *mainWindow);
void clearActions(); ~ScriptUtility();
QString getActionFunctionName(int actionIndex); QString getActionFunctionName(int actionIndex);
Q_INVOKABLE bool registerAction(QString functionName, QString actionName, QString shortcut = ""); Q_INVOKABLE bool registerAction(QString functionName, QString actionName, QString shortcut = "");
Q_INVOKABLE bool registerToggleAction(QString functionName, QString actionName, QString shortcut = "", bool checked = false); Q_INVOKABLE bool registerToggleAction(QString functionName, QString actionName, QString shortcut = "", bool checked = false);
@ -59,6 +60,7 @@ private:
MainWindow *window; MainWindow *window;
QList<QAction *> registeredActions; QList<QAction *> registeredActions;
QSet<QTimer *> activeTimers;
QHash<int, QString> actionMap; QHash<int, QString> actionMap;
}; };

View file

@ -14,6 +14,8 @@ public:
void setNumberItem(int value); void setNumberItem(int value);
void setHexItem(uint32_t value); void setHexItem(uint32_t value);
void setClearButtonEnabled(bool enabled); void setClearButtonEnabled(bool enabled);
void setEditable(bool editable);
void setLineEdit(QLineEdit *edit);
private: private:
void setItem(int index, const QString &text); void setItem(int index, const QString &text);

View file

@ -7,6 +7,18 @@ ScriptUtility::ScriptUtility(MainWindow *mainWindow) {
this->window = mainWindow; this->window = mainWindow;
} }
ScriptUtility::~ScriptUtility() {
if (window && window->ui && window->ui->menuTools) {
for (auto action : this->registeredActions) {
window->ui->menuTools->removeAction(action);
}
}
for (auto timer : this->activeTimers) {
timer->stop();
delete timer;
}
}
bool ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) { bool ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) {
if (!window || !window->ui || !window->ui->menuTools) if (!window || !window->ui || !window->ui->menuTools)
return false; return false;
@ -44,12 +56,6 @@ bool ScriptUtility::registerToggleAction(QString functionName, QString actionNam
return true; return true;
} }
void ScriptUtility::clearActions() {
for (auto action : this->registeredActions) {
window->ui->menuTools->removeAction(action);
}
}
QString ScriptUtility::getActionFunctionName(int actionIndex) { QString ScriptUtility::getActionFunctionName(int actionIndex) {
return this->actionMap.value(actionIndex); return this->actionMap.value(actionIndex);
} }
@ -58,11 +64,15 @@ void ScriptUtility::setTimeout(QJSValue callback, int milliseconds) {
if (!callback.isCallable() || milliseconds < 0) if (!callback.isCallable() || milliseconds < 0)
return; return;
QTimer *timer = new QTimer(0); QTimer *timer = new QTimer();
connect(timer, &QTimer::timeout, [=](){ connect(timer, &QTimer::timeout, [=](){
this->callTimeoutFunction(callback); if (this->activeTimers.remove(timer)) {
this->callTimeoutFunction(callback);
timer->deleteLater();
}
}); });
connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
this->activeTimers.insert(timer);
timer->setSingleShot(true); timer->setSingleShot(true);
timer->start(milliseconds); timer->start(milliseconds);
} }

View file

@ -1,8 +1,10 @@
#include <QQmlEngine>
#include "scripting.h" #include "scripting.h"
#include "log.h" #include "log.h"
#include "config.h" #include "config.h"
QMap<CallbackType, QString> callbackFunctions = { const QMap<CallbackType, QString> callbackFunctions = {
{OnProjectOpened, "onProjectOpened"}, {OnProjectOpened, "onProjectOpened"},
{OnProjectClosed, "onProjectClosed"}, {OnProjectClosed, "onProjectClosed"},
{OnBlockChanged, "onBlockChanged"}, {OnBlockChanged, "onBlockChanged"},
@ -22,10 +24,6 @@ QMap<CallbackType, QString> callbackFunctions = {
Scripting *instance = nullptr; Scripting *instance = nullptr;
void Scripting::stop() { void Scripting::stop() {
if (!instance) return;
instance->engine->setInterrupted(true);
instance->scriptUtility->clearActions();
qDeleteAll(instance->imageCache);
delete instance; delete instance;
instance = nullptr; instance = nullptr;
} }
@ -39,7 +37,7 @@ void Scripting::init(MainWindow *mainWindow) {
Scripting::Scripting(MainWindow *mainWindow) { Scripting::Scripting(MainWindow *mainWindow) {
this->mainWindow = mainWindow; this->mainWindow = mainWindow;
this->engine = new QJSEngine(mainWindow); this->engine = new QJSEngine();
this->engine->installExtensions(QJSEngine::ConsoleExtension); this->engine->installExtensions(QJSEngine::ConsoleExtension);
const QStringList paths = userConfig.getCustomScriptPaths(); const QStringList paths = userConfig.getCustomScriptPaths();
const QList<bool> enabled = userConfig.getCustomScriptsEnabled(); const QList<bool> enabled = userConfig.getCustomScriptsEnabled();
@ -51,6 +49,13 @@ Scripting::Scripting(MainWindow *mainWindow) {
this->scriptUtility = new ScriptUtility(mainWindow); this->scriptUtility = new ScriptUtility(mainWindow);
} }
Scripting::~Scripting() {
this->engine->setInterrupted(true);
qDeleteAll(this->imageCache);
delete this->engine;
delete this->scriptUtility;
}
void Scripting::loadModules(QStringList moduleFiles) { void Scripting::loadModules(QStringList moduleFiles) {
for (QString filepath : moduleFiles) { for (QString filepath : moduleFiles) {
QString validPath = Project::getExistingFilepath(filepath); QString validPath = Project::getExistingFilepath(filepath);
@ -79,6 +84,11 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) {
instance->engine->globalObject().setProperty("overlay", instance->engine->newQObject(mainWindow->ui->graphicsView_Map)); instance->engine->globalObject().setProperty("overlay", instance->engine->newQObject(mainWindow->ui->graphicsView_Map));
instance->engine->globalObject().setProperty("utility", instance->engine->newQObject(instance->scriptUtility)); instance->engine->globalObject().setProperty("utility", instance->engine->newQObject(instance->scriptUtility));
// Note: QJSEngine also has these functions, but not in Qt 5.15.
QQmlEngine::setObjectOwnership(mainWindow, QQmlEngine::CppOwnership);
QQmlEngine::setObjectOwnership(mainWindow->ui->graphicsView_Map, QQmlEngine::CppOwnership);
QQmlEngine::setObjectOwnership(instance->scriptUtility, QQmlEngine::CppOwnership);
QJSValue constants = instance->engine->newObject(); QJSValue constants = instance->engine->newObject();
// Get version numbers // Get version numbers

View file

@ -23,6 +23,18 @@ NoScrollComboBox::NoScrollComboBox(QWidget *parent)
this->setValidator(validator); this->setValidator(validator);
} }
// On macOS QComboBox::setEditable and QComboBox::setLineEdit will override our changes to the focus policy, so we enforce it here.
void NoScrollComboBox::setEditable(bool editable) {
auto policy = focusPolicy();
QComboBox::setEditable(editable);
setFocusPolicy(policy);
}
void NoScrollComboBox::setLineEdit(QLineEdit *edit) {
auto policy = focusPolicy();
QComboBox::setLineEdit(edit);
setFocusPolicy(policy);
}
void NoScrollComboBox::wheelEvent(QWheelEvent *event) void NoScrollComboBox::wheelEvent(QWheelEvent *event)
{ {
// Only allow scrolling to modify contents when it explicitly has focus. // Only allow scrolling to modify contents when it explicitly has focus.