From d277e0e8c5d097845fd5e9faa7dad925f780f76f Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 23 Aug 2018 16:09:44 +0200 Subject: [PATCH] move connect to LuaEngine --- src/luaengine/luaengine/LuaEngine.cpp | 34 ++++++++++++++++++ src/luaengine/luaengine/LuaEngine.h | 4 +++ src/luaenginegui/luaengine/LuaEngineGui.cpp | 40 --------------------- src/luaenginegui/luaengine/LuaEngineGui.h | 5 --- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/luaengine/luaengine/LuaEngine.cpp b/src/luaengine/luaengine/LuaEngine.cpp index aab3eaf..9e551d3 100644 --- a/src/luaengine/luaengine/LuaEngine.cpp +++ b/src/luaengine/luaengine/LuaEngine.cpp @@ -18,12 +18,15 @@ #define LUA_LIB #include "LuaEngine.h" #include +#include LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) { L = luaL_newstate(); if (loadBaseLibraries) luaL_openlibs(L); + pushPointer("__LuaEngine", (void*)this); + pushFunction("connect", luaTriggerConnect_p); pushFunction("luaEngineVersion", luaEngineVersion_p); } @@ -281,3 +284,34 @@ int LuaEngine::getArgumentCount(lua_State *L_p) { return lua_gettop(L_p); } + +int LuaEngine::luaTriggerConnect_p(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 3) { + QObject *object = (QObject*)getPointer(L_p, 1); + QString signalString = getVariant(L_p, 2).toString(); + int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); + if (signalIndex != -1) { + LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); + int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); + if (slotIndex != -1) { + QMetaMethod signal = object->metaObject()->method(signalIndex); + QMetaMethod slot = engine->metaObject()->method(slotIndex); + QString funcStorage; + QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); + pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); + QObject::connect(object, signal, engine, slot); + } + } + } + return 0; +} + +void LuaEngine::luaTriggerSlot_p() +{ + QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex()); + QString funcStorage; + QTextStream(&funcStorage) << "__ConnectFunc_" << sender() << "_" << signal.name(); + QString luaConnectFunc = getVariant(funcStorage.toUtf8().data()).toString(); + executeLuaFunction(luaConnectFunc.toUtf8().data(), QVariant::fromValue((void*)sender())); +} diff --git a/src/luaengine/luaengine/LuaEngine.h b/src/luaengine/luaengine/LuaEngine.h index 22078dc..52536ee 100644 --- a/src/luaengine/luaengine/LuaEngine.h +++ b/src/luaengine/luaengine/LuaEngine.h @@ -72,9 +72,13 @@ public: static QVariantList getArguments(lua_State *L_p); static int getArgumentCount(lua_State *L_p); +private slots: + void luaTriggerSlot_p(); + private: lua_State *L; static int luaEngineVersion_p(lua_State *L_p); + static int luaTriggerConnect_p(lua_State *L_p); }; #endif // LUAENGINE_H diff --git a/src/luaenginegui/luaengine/LuaEngineGui.cpp b/src/luaenginegui/luaengine/LuaEngineGui.cpp index 57a7a21..4c5fef7 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.cpp +++ b/src/luaenginegui/luaengine/LuaEngineGui.cpp @@ -34,12 +34,6 @@ LuaEngineGui::LuaEngineGui(QObject *parent, bool loadBaseLibraries) : LuaEngine( { L = luaState(); pushClass(L); - pushEngine(L); -} - -void LuaEngineGui::pushEngine(lua_State *L_p) -{ - pushPointer(L_p, "__LuaEngineGui", (void*)this); } void LuaEngineGui::pushClass(lua_State *L_p) @@ -81,9 +75,6 @@ void LuaEngineGui::pushClass(lua_State *L_p) pushFunction(L_p, "createMenu", createMenu); pushFunction(L_p, "createMenuBar", createMenuBar); pushFunction(L_p, "createMenuEntry", createMenuEntry); - - // Connect - pushFunction(L_p, "connect", connect_p); } void LuaEngineGui::pushClass(LuaEngine *luaEngine) @@ -356,37 +347,6 @@ int LuaEngineGui::setWidgetLayout(lua_State *L_p) return 0; } -int LuaEngineGui::connect_p(lua_State *L_p) -{ - if (getArgumentCount(L_p) >= 3) { - QObject *object = (QObject*)getPointer(L_p, 1); - QString signalString = getVariant(L_p, 2).toString(); - int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); - if (signalIndex != -1) { - LuaEngineGui *engine = (LuaEngineGui*)getPointer(L_p, "__LuaEngineGui"); - int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot()"); - if (slotIndex != -1) { - QMetaMethod signal = object->metaObject()->method(signalIndex); - QMetaMethod slot = engine->metaObject()->method(slotIndex); - QString funcStorage; - QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); - pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); - QObject::connect(object, signal, engine, slot); - } - } - } - return 0; -} - -void LuaEngineGui::luaTriggerSlot() -{ - QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex()); - QString funcStorage; - QTextStream(&funcStorage) << "__ConnectFunc_" << sender() << "_" << signal.name(); - QString luaConnectFunc = getVariant(funcStorage.toUtf8().data()).toString(); - executeLuaFunction(luaConnectFunc.toUtf8().data(), QVariant::fromValue((void*)sender())); -} - QString LuaEngineGui::nameForPointer(void *pointer) { QString nameStorage; diff --git a/src/luaenginegui/luaengine/LuaEngineGui.h b/src/luaenginegui/luaengine/LuaEngineGui.h index fd5ab5f..4c6f756 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.h +++ b/src/luaenginegui/luaengine/LuaEngineGui.h @@ -28,7 +28,6 @@ class LUAENGINEGUISHARED_EXPORT LuaEngineGui : public LuaEngine Q_OBJECT public: LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true); - void pushEngine(lua_State *L_p); static void pushClass(lua_State *L_p); static void pushClass(LuaEngine *luaEngine); static int showMessageBox(lua_State *L_p); @@ -43,10 +42,6 @@ public: static int createMenuBar(lua_State *L_p); static int createMenuEntry(lua_State *L_p); static int setWidgetLayout(lua_State *L_p); - static int connect_p(lua_State *L_p); - -public slots: - void luaTriggerSlot(); private: lua_State *L;