From 53e8d9abe5d1cd78891d3924b4f87bda4d0a4e84 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 23 Aug 2018 16:58:28 +0200 Subject: [PATCH] delete's added --- src/luaengine/luaengine/LuaEngine.cpp | 48 +++++++++++++++++++++++++-- src/luaengine/luaengine/LuaEngine.h | 8 +++-- src/luaengineapp/app.lua | 9 ++--- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/luaengine/luaengine/LuaEngine.cpp b/src/luaengine/luaengine/LuaEngine.cpp index 9e551d3..27ad235 100644 --- a/src/luaengine/luaengine/LuaEngine.cpp +++ b/src/luaengine/luaengine/LuaEngine.cpp @@ -26,7 +26,11 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) if (loadBaseLibraries) luaL_openlibs(L); pushPointer("__LuaEngine", (void*)this); + pushVariant("DeleteInstant", 0); + pushVariant("DeleteLater", 1); + pushFunction("delete", luaDeletePointer_p); pushFunction("connect", luaTriggerConnect_p); + pushFunction("disconnect", luaTriggerDisconnect_p); pushFunction("luaEngineVersion", luaEngineVersion_p); } @@ -221,10 +225,10 @@ QVariant LuaEngine::getVariant(lua_State *L_p, int index) else if (lua_isuserdata(L_p, index)) { return QVariant::fromValue(lua_touserdata(L_p, index)); } - else if (lua_isnil(L_p, index)) { + else if (lua_isnoneornil(L_p, index)) { return QVariant(); } - QTextStream(stderr) << "Warning: Didn't catch lua_isnil before empty QVariant got returned" << endl; + QTextStream(stderr) << "Warning: Didn't catch lua_isnoneornil before empty QVariant got returned" << endl; return QVariant(); } @@ -285,6 +289,24 @@ int LuaEngine::getArgumentCount(lua_State *L_p) return lua_gettop(L_p); } +int LuaEngine::luaDeletePointer_p(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL && ((QObject*)pointer)->inherits("QObject")) { + switch (getVariant(L_p, 2).toInt()) + { + case 1: + ((QObject*)pointer)->deleteLater(); + break; + default: + delete ((QObject*)pointer); + } + } + } + return 0; +} + int LuaEngine::luaTriggerConnect_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 3) { @@ -307,6 +329,28 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p) return 0; } +int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + 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(), QVariant()); + QObject::disconnect(object, signal, engine, slot); + } + } + } + return 0; +} + void LuaEngine::luaTriggerSlot_p() { QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex()); diff --git a/src/luaengine/luaengine/LuaEngine.h b/src/luaengine/luaengine/LuaEngine.h index 52536ee..d16af0d 100644 --- a/src/luaengine/luaengine/LuaEngine.h +++ b/src/luaengine/luaengine/LuaEngine.h @@ -72,13 +72,15 @@ 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 luaDeletePointer_p(lua_State *L_p); static int luaTriggerConnect_p(lua_State *L_p); + static int luaTriggerDisconnect_p(lua_State *L_p); + +private slots: + void luaTriggerSlot_p(); }; #endif // LUAENGINE_H diff --git a/src/luaengineapp/app.lua b/src/luaengineapp/app.lua index 0d9c9e4..b8a832a 100644 --- a/src/luaengineapp/app.lua +++ b/src/luaengineapp/app.lua @@ -33,30 +33,25 @@ local pushButton1 function main() mainWindow = createMainWindow() mainWidget = createCentralWidget(mainWindow) + mainLayout = createLayout(VerticalLayout, mainWidget) menuBar = createMenuBar(mainWindow) menuFile = createMenu("File", menuBar) menuEntryExit = createMenuEntry("Exit", menuFile) + connect(menuEntryExit, "triggered()", "closeWindow") menuHelp = createMenu("Help", menuBar) menuEntryAbout = createMenuEntry("About LuaEngine", menuHelp) - - connect(menuEntryExit, "triggered()", "closeWindow") connect(menuEntryAbout, "triggered()", "showAboutBox") - mainLayout = createLayout(VerticalLayout, mainWidget) - labelLayout = createLayout(HorizontalLayout, mainLayout) layoutAddLayout(mainLayout, labelLayout) - appLabel1 = createLabel("LuaEngine greets!", mainWidget) layoutAddWidget(labelLayout, appLabel1) - appLabel2 = createLabel("..and not only one time!", mainWidget) layoutAddWidget(labelLayout, appLabel2) pushButton1 = createPushButton("Press me hard please!", mainWidget) layoutAddWidget(mainLayout, pushButton1) - connect(pushButton1, "clicked()", "showHarderBox") showWidget(mainWindow, ShowNormal)