delete's added

This commit is contained in:
Syping 2018-08-23 16:58:28 +02:00
parent ba207a1c8f
commit 53e8d9abe5
3 changed files with 53 additions and 12 deletions

View file

@ -26,7 +26,11 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
if (loadBaseLibraries) if (loadBaseLibraries)
luaL_openlibs(L); luaL_openlibs(L);
pushPointer("__LuaEngine", (void*)this); pushPointer("__LuaEngine", (void*)this);
pushVariant("DeleteInstant", 0);
pushVariant("DeleteLater", 1);
pushFunction("delete", luaDeletePointer_p);
pushFunction("connect", luaTriggerConnect_p); pushFunction("connect", luaTriggerConnect_p);
pushFunction("disconnect", luaTriggerDisconnect_p);
pushFunction("luaEngineVersion", luaEngineVersion_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)) { else if (lua_isuserdata(L_p, index)) {
return QVariant::fromValue(lua_touserdata(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(); 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(); return QVariant();
} }
@ -285,6 +289,24 @@ int LuaEngine::getArgumentCount(lua_State *L_p)
return lua_gettop(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) int LuaEngine::luaTriggerConnect_p(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 3) { if (getArgumentCount(L_p) >= 3) {
@ -307,6 +329,28 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p)
return 0; 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() void LuaEngine::luaTriggerSlot_p()
{ {
QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex()); QMetaMethod signal = sender()->metaObject()->method(senderSignalIndex());

View file

@ -72,13 +72,15 @@ 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 luaDeletePointer_p(lua_State *L_p);
static int luaTriggerConnect_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 #endif // LUAENGINE_H

View file

@ -33,30 +33,25 @@ local pushButton1
function main() function main()
mainWindow = createMainWindow() mainWindow = createMainWindow()
mainWidget = createCentralWidget(mainWindow) mainWidget = createCentralWidget(mainWindow)
mainLayout = createLayout(VerticalLayout, mainWidget)
menuBar = createMenuBar(mainWindow) menuBar = createMenuBar(mainWindow)
menuFile = createMenu("File", menuBar) menuFile = createMenu("File", menuBar)
menuEntryExit = createMenuEntry("Exit", menuFile) menuEntryExit = createMenuEntry("Exit", menuFile)
connect(menuEntryExit, "triggered()", "closeWindow")
menuHelp = createMenu("Help", menuBar) menuHelp = createMenu("Help", menuBar)
menuEntryAbout = createMenuEntry("About LuaEngine", menuHelp) menuEntryAbout = createMenuEntry("About LuaEngine", menuHelp)
connect(menuEntryExit, "triggered()", "closeWindow")
connect(menuEntryAbout, "triggered()", "showAboutBox") connect(menuEntryAbout, "triggered()", "showAboutBox")
mainLayout = createLayout(VerticalLayout, mainWidget)
labelLayout = createLayout(HorizontalLayout, mainLayout) labelLayout = createLayout(HorizontalLayout, mainLayout)
layoutAddLayout(mainLayout, labelLayout) layoutAddLayout(mainLayout, labelLayout)
appLabel1 = createLabel("LuaEngine greets!", mainWidget) appLabel1 = createLabel("LuaEngine greets!", mainWidget)
layoutAddWidget(labelLayout, appLabel1) layoutAddWidget(labelLayout, appLabel1)
appLabel2 = createLabel("..and not only one time!", mainWidget) appLabel2 = createLabel("..and not only one time!", mainWidget)
layoutAddWidget(labelLayout, appLabel2) layoutAddWidget(labelLayout, appLabel2)
pushButton1 = createPushButton("Press me hard please!", mainWidget) pushButton1 = createPushButton("Press me hard please!", mainWidget)
layoutAddWidget(mainLayout, pushButton1) layoutAddWidget(mainLayout, pushButton1)
connect(pushButton1, "clicked()", "showHarderBox") connect(pushButton1, "clicked()", "showHarderBox")
showWidget(mainWindow, ShowNormal) showWidget(mainWindow, ShowNormal)