move connect to LuaEngine

This commit is contained in:
Syping 2018-08-23 16:09:44 +02:00
parent 6cd4614371
commit d277e0e8c5
4 changed files with 38 additions and 45 deletions

View file

@ -18,12 +18,15 @@
#define LUA_LIB #define LUA_LIB
#include "LuaEngine.h" #include "LuaEngine.h"
#include <QTextStream> #include <QTextStream>
#include <QMetaMethod>
LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
{ {
L = luaL_newstate(); L = luaL_newstate();
if (loadBaseLibraries) if (loadBaseLibraries)
luaL_openlibs(L); luaL_openlibs(L);
pushPointer("__LuaEngine", (void*)this);
pushFunction("connect", luaTriggerConnect_p);
pushFunction("luaEngineVersion", luaEngineVersion_p); pushFunction("luaEngineVersion", luaEngineVersion_p);
} }
@ -281,3 +284,34 @@ int LuaEngine::getArgumentCount(lua_State *L_p)
{ {
return lua_gettop(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()));
}

View file

@ -72,9 +72,13 @@ public:
static QVariantList getArguments(lua_State *L_p); static QVariantList getArguments(lua_State *L_p);
static int getArgumentCount(lua_State *L_p); static int getArgumentCount(lua_State *L_p);
private slots:
void luaTriggerSlot_p();
private: private:
lua_State *L; lua_State *L;
static int luaEngineVersion_p(lua_State *L_p); static int luaEngineVersion_p(lua_State *L_p);
static int luaTriggerConnect_p(lua_State *L_p);
}; };
#endif // LUAENGINE_H #endif // LUAENGINE_H

View file

@ -34,12 +34,6 @@ LuaEngineGui::LuaEngineGui(QObject *parent, bool loadBaseLibraries) : LuaEngine(
{ {
L = luaState(); L = luaState();
pushClass(L); pushClass(L);
pushEngine(L);
}
void LuaEngineGui::pushEngine(lua_State *L_p)
{
pushPointer(L_p, "__LuaEngineGui", (void*)this);
} }
void LuaEngineGui::pushClass(lua_State *L_p) 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, "createMenu", createMenu);
pushFunction(L_p, "createMenuBar", createMenuBar); pushFunction(L_p, "createMenuBar", createMenuBar);
pushFunction(L_p, "createMenuEntry", createMenuEntry); pushFunction(L_p, "createMenuEntry", createMenuEntry);
// Connect
pushFunction(L_p, "connect", connect_p);
} }
void LuaEngineGui::pushClass(LuaEngine *luaEngine) void LuaEngineGui::pushClass(LuaEngine *luaEngine)
@ -356,37 +347,6 @@ int LuaEngineGui::setWidgetLayout(lua_State *L_p)
return 0; 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 LuaEngineGui::nameForPointer(void *pointer)
{ {
QString nameStorage; QString nameStorage;

View file

@ -28,7 +28,6 @@ class LUAENGINEGUISHARED_EXPORT LuaEngineGui : public LuaEngine
Q_OBJECT Q_OBJECT
public: public:
LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true); LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true);
void pushEngine(lua_State *L_p);
static void pushClass(lua_State *L_p); static void pushClass(lua_State *L_p);
static void pushClass(LuaEngine *luaEngine); static void pushClass(LuaEngine *luaEngine);
static int showMessageBox(lua_State *L_p); static int showMessageBox(lua_State *L_p);
@ -43,10 +42,6 @@ public:
static int createMenuBar(lua_State *L_p); static int createMenuBar(lua_State *L_p);
static int createMenuEntry(lua_State *L_p); static int createMenuEntry(lua_State *L_p);
static int setWidgetLayout(lua_State *L_p); static int setWidgetLayout(lua_State *L_p);
static int connect_p(lua_State *L_p);
public slots:
void luaTriggerSlot();
private: private:
lua_State *L; lua_State *L;