diff --git a/lcUI/cadmdichild.cpp b/lcUI/cadmdichild.cpp index 2da28f99d..6ea680657 100644 --- a/lcUI/cadmdichild.cpp +++ b/lcUI/cadmdichild.cpp @@ -89,6 +89,9 @@ CadMdiChild::CadMdiChild(QWidget* parent) : } CadMdiChild::~CadMdiChild() { + if(_destroyCallback) { + _destroyCallback(_id); + } } int CadMdiChild::randInt(int low, int high) { @@ -396,4 +399,8 @@ unsigned int CadMdiChild::id() { } void CadMdiChild::setId(unsigned int id) { _id = id; +} + +void CadMdiChild::setDestroyCallback(LuaIntf::LuaRef destroyCallback) { + _destroyCallback = destroyCallback; } \ No newline at end of file diff --git a/lcUI/cadmdichild.h b/lcUI/cadmdichild.h index bedd7d599..f18732b66 100644 --- a/lcUI/cadmdichild.h +++ b/lcUI/cadmdichild.h @@ -18,6 +18,15 @@ #include #include "cad/dochelpers/undomanagerimpl.h" +extern "C" +{ +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} + +#include "lua-intf/LuaIntf/LuaIntf.h" + class CadMdiChild : public QWidget { Q_OBJECT @@ -27,6 +36,8 @@ class CadMdiChild : public QWidget { void newDocument(); + void setDestroyCallback(LuaIntf::LuaRef callback); + public slots: void on_actionAdd_Random_Lines_triggered(); @@ -57,6 +68,8 @@ class CadMdiChild : public QWidget { int randInt(int low, int high); unsigned int _id; + LuaIntf::LuaRef _destroyCallback; + std::shared_ptr _document; lc::UndoManagerImpl_SPtr _undoManager; diff --git a/lcUI/lua/luaqobject.cpp b/lcUI/lua/luaqobject.cpp index 80e7dc9ac..e42f7e22a 100644 --- a/lcUI/lua/luaqobject.cpp +++ b/lcUI/lua/luaqobject.cpp @@ -1,9 +1,8 @@ #include "luaqobject.h" #include -LuaQObject::LuaQObject(QObject* object, lua_State* L): - _object(object), - _L(L) +LuaQObject::LuaQObject(QObject* object): + _object(object) { //Connect QObject destroyed() signal const int destroySignalId = _object->metaObject()->indexOfSignal("destroyed()"); @@ -15,17 +14,19 @@ LuaQObject::~LuaQObject() { } -int LuaQObject::connect(int signalId, std::string luaFunction) { - _slotId = 1; - - if(QMetaObject::connect(_object, signalId, this, this->metaObject()->methodCount() + _slotId)) { - _slotFunction = LuaIntf::LuaRef(_L, luaFunction.c_str()); +bool LuaQObject::connect(int signalId, LuaIntf::LuaRef slot) { + if(slot.isFunction()) { + _slotId = 1; - lua_pushboolean(_L, true); - return true; + if(QMetaObject::connect(_object, signalId, this, this->metaObject()->methodCount() + _slotId)) { + _slotFunction = slot; + + return true; + } + } + else { + std::cerr << "Given slot is not a function" << std::endl; } - - lua_pushboolean(_L, false); return false; } @@ -33,7 +34,7 @@ int LuaQObject::connect(int signalId, std::string luaFunction) { int LuaQObject::qt_metacall(QMetaObject::Call c, int id, void **a) { id = QObject::qt_metacall(c, id, a); - if(id == _slotId) { + if(id == _slotId && _slotFunction) { _slotFunction(); } diff --git a/lcUI/lua/luaqobject.h b/lcUI/lua/luaqobject.h index e4a82c8e5..5584b6be6 100644 --- a/lcUI/lua/luaqobject.h +++ b/lcUI/lua/luaqobject.h @@ -14,10 +14,10 @@ extern "C" class LuaQObject : public QObject { public: - LuaQObject(QObject *object, lua_State* L); + LuaQObject(QObject *object); ~LuaQObject(); - int connect(int signalId, std::string luaFunction); + bool connect(int signalId, LuaIntf::LuaRef slot); std::string objectName(); static std::string objectName(QObject* object); @@ -31,7 +31,8 @@ class LuaQObject : public QObject { private: QObject* _object; - lua_State* _L; + + int _slotId; LuaIntf::LuaRef _slotFunction; diff --git a/lcUI/lua/qtbridge.cpp b/lcUI/lua/qtbridge.cpp index bb9012e4f..371af33b0 100644 --- a/lcUI/lua/qtbridge.cpp +++ b/lcUI/lua/qtbridge.cpp @@ -154,6 +154,7 @@ void addLCBindings(lua_State *L) { .addProperty("id", &CadMdiChild::id, &CadMdiChild::setId) .addFunction("import", &CadMdiChild::import) .addFunction("newDocument", &CadMdiChild::newDocument) + .addFunction("setDestroyCallback", &CadMdiChild::setDestroyCallback) .addFunction("undoManager", &CadMdiChild::undoManager) .addFunction("view", &CadMdiChild::view) .addFunction("viewer", &CadMdiChild::viewer) diff --git a/lcUI/luainterface.cpp b/lcUI/luainterface.cpp index 7aba7c185..10d5cacc6 100644 --- a/lcUI/luainterface.cpp +++ b/lcUI/luainterface.cpp @@ -29,11 +29,10 @@ void LuaInterface::initLua() { std::cout << out << std::endl; } -int LuaInterface::qtConnect( - lua_State* L, +bool LuaInterface::qtConnect( QObject *sender, std::string signalName, - std::string luaFunction) + LuaIntf::LuaRef slot) { int signalId = sender->metaObject()->indexOfSignal(signalName.c_str()); @@ -41,12 +40,12 @@ int LuaInterface::qtConnect( std::cout << "No such signal " << signalName << std::endl; } else { - LuaQObject_SPtr lqo(new LuaQObject(sender, L)); + LuaQObject_SPtr lqo(new LuaQObject(sender)); _luaQObjects.push_back(lqo); - return lqo->connect(signalId, luaFunction); + return lqo->connect(signalId, slot); } - return 0; + return false; } std::shared_ptr LuaInterface::loadUiFile(const char* fileName) { diff --git a/lcUI/luainterface.h b/lcUI/luainterface.h index ccf40338a..9a74825a4 100644 --- a/lcUI/luainterface.h +++ b/lcUI/luainterface.h @@ -30,11 +30,10 @@ class LuaInterface { void initLua(); - int qtConnect( - lua_State *L, + bool qtConnect( QObject *sender, std::string signalName, - std::string luaFunction + LuaIntf::LuaRef slot ); static std::shared_ptr loadUiFile(const char* fileName); diff --git a/lcUILua/ui/init.lua b/lcUILua/ui/init.lua index c082475a6..4d2a49d58 100644 --- a/lcUILua/ui/init.lua +++ b/lcUILua/ui/init.lua @@ -8,11 +8,11 @@ function new_file() window = mdiArea:addSubWindow(cadMdiChild) cadMdiChild:showMaximized() cadMdiChild:viewer():autoScale() + cadMdiChild:setDestroyCallback(onMdiChildDestroyed) - luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", "click") - luaInterface:luaConnect(cadMdiChild:view(), "destroyed()", "onMdiChildDestroyed") + luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", click) - id = #op + 1 + id = nextTableId(op) op[id] = Operations:new() op[id].id = id window:widget().id = id @@ -52,10 +52,9 @@ function load_file(fileName) cadMdiChild:import(fileName:toStdString()) window = mdiArea:addSubWindow(cadMdiChild) cadMdiChild:showMaximized() - luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", "click") - luaInterface:luaConnect(cadMdiChild:view(), "destroyed()", "onMdiChildDestroyed") + luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", click) - id = #op + 1 + id = nextTableId(op) op[id] = Operations:new() op[id].id = id window:widget().id = id @@ -75,6 +74,22 @@ function create_line() op[widget.id]:create_line() end +function onMdiChildDestroyed(id) + op[id] = nil +end + +function nextTableId(table) + count = 0 + for id, v in pairs(table) do + count = count + 1 + if(v == nil) then + return count + end + end + + return count +end + --UI mainWindow = qt.loadUi(ui_path .. "/mainwindow.ui") mainWindow:setWindowTitle(qt.QObject.tr("LibreCAD", "", -1)) -- Todo: optional arguments @@ -93,11 +108,11 @@ mainWindow:setCentralWidget(mdiArea) new_file() -luaInterface:luaConnect(mainWindow:findChild("actionNew"), "triggered(bool)", "new_file") -luaInterface:luaConnect(mainWindow:findChild("actionOpen"), "triggered(bool)", "open_file") -luaInterface:luaConnect(mainWindow:findChild("actionUndo"), "triggered(bool)", "undo") -luaInterface:luaConnect(mainWindow:findChild("actionRedo"), "triggered(bool)", "redo") -luaInterface:luaConnect(lineAction, "triggered(bool)", "create_line") +luaInterface:luaConnect(mainWindow:findChild("actionNew"), "triggered(bool)", new_file) +luaInterface:luaConnect(mainWindow:findChild("actionOpen"), "triggered(bool)", open_file) +luaInterface:luaConnect(mainWindow:findChild("actionUndo"), "triggered(bool)", undo) +luaInterface:luaConnect(mainWindow:findChild("actionRedo"), "triggered(bool)", redo) +luaInterface:luaConnect(lineAction, "triggered(bool)", create_line) luaScript = lc.LuaScript(mdiArea) mainWindow:addDockWidget(2, luaScript)