Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent some crashes #52

Merged
merged 2 commits into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lcUI/cadmdichild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ CadMdiChild::CadMdiChild(QWidget* parent) :
}

CadMdiChild::~CadMdiChild() {
if(_destroyCallback) {
_destroyCallback(_id);
}
}

int CadMdiChild::randInt(int low, int high) {
Expand Down Expand Up @@ -396,4 +399,8 @@ unsigned int CadMdiChild::id() {
}
void CadMdiChild::setId(unsigned int id) {
_id = id;
}

void CadMdiChild::setDestroyCallback(LuaIntf::LuaRef destroyCallback) {
_destroyCallback = destroyCallback;
}
13 changes: 13 additions & 0 deletions lcUI/cadmdichild.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
#include <managers/snapmanagerimpl.h>
#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

Expand All @@ -27,6 +36,8 @@ class CadMdiChild : public QWidget {

void newDocument();

void setDestroyCallback(LuaIntf::LuaRef callback);

public slots:
void on_actionAdd_Random_Lines_triggered();

Expand Down Expand Up @@ -57,6 +68,8 @@ class CadMdiChild : public QWidget {
int randInt(int low, int high);
unsigned int _id;

LuaIntf::LuaRef _destroyCallback;

std::shared_ptr<lc::Document> _document;
lc::UndoManagerImpl_SPtr _undoManager;

Expand Down
27 changes: 14 additions & 13 deletions lcUI/lua/luaqobject.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "luaqobject.h"
#include <iostream>

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()");
Expand All @@ -15,25 +14,27 @@ 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;
}

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();
}

Expand Down
7 changes: 4 additions & 3 deletions lcUI/lua/luaqobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,7 +31,8 @@ class LuaQObject : public QObject {
private:

QObject* _object;
lua_State* _L;



int _slotId;
LuaIntf::LuaRef _slotFunction;
Expand Down
1 change: 1 addition & 0 deletions lcUI/lua/qtbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions lcUI/luainterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,23 @@ 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());

if(signalId < 0) {
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<QWidget> LuaInterface::loadUiFile(const char* fileName) {
Expand Down
5 changes: 2 additions & 3 deletions lcUI/luainterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QWidget> loadUiFile(const char* fileName);
Expand Down
37 changes: 26 additions & 11 deletions lcUILua/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down