LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
luaqobject.cpp
Go to the documentation of this file.
1 #include "luaqobject.h"
2 
3 LuaQObject::LuaQObject(QObject* object):
4  _object(object),
5  _valid(false)
6 {
7  //Connect QObject destroyed() signal
8  const int destroySignalId = _object->metaObject()->indexOfSignal("destroyed()");
9  QMetaObject::connect(_object, destroySignalId, this, metaObject()->methodCount());
10 }
11 
13  _object = 0;
14 }
15 
16 bool LuaQObject::connect(int signalId, LuaIntf::LuaRef slot) {
17  _signalId = signalId;
18  if(slot.isFunction()) {
19  _slotId = 1;
20 
21  if(QMetaObject::connect(_object, signalId, this, this->metaObject()->methodCount() + _slotId)) {
22  _slotFunction = slot;
23  _valid = true;
24  }
25  }
26  else {
27  std::cerr << "Given slot is not a function" << std::endl;
28  }
29 
30  return _valid;
31 }
32 
33 int LuaQObject::qt_metacall(QMetaObject::Call c, int id, void **a)
34 {
35  id = QObject::qt_metacall(c, id, a);
36 
37  if(id == 0) {
38  _valid = false;
39  }
40  else if(id == _slotId) {
41  if(_slotFunction) {
42  LuaIntf::LuaState s = _slotFunction.state();
43  auto parameters = _object->metaObject()->method(_signalId).parameterTypes();
44  unsigned int i = 0;
45 
46  _slotFunction.pushToStack();
47  for (auto parameter : parameters) {
48  i++;
49  pushArg(s, QMetaType::type(parameter.constData()), a[i]);
50  }
51 
52  s.call(i, 0);
53  }
54 
55  return -1;
56  }
57 
58  return id;
59 }
60 
61 std::string LuaQObject::objectName(QObject* object) {
62  return object->objectName().toStdString();
63 }
64 
65 
66 std::string LuaQObject::objectName() {
67  if(_object) {
68  return objectName(_object);
69  }
70 
71  return "null";
72 }
73 
74 QObject* LuaQObject::findChild(QObject* object, std::string name) {
75  for(auto child : object->children()) {
76  if(objectName(child) == name) {
77  return child;
78  }
79  }
80 
81  std::cout << "Child " << name << " not found." << std::endl;
82 
83  return 0;
84 }
85 
86 QObject* LuaQObject::findChild(std::string name) {
87  if(_object) {
88  return findChild(_object, name);
89  }
90 
91  return 0;
92 }
93 
94 void LuaQObject::pushArg(LuaIntf::LuaState s, int const type, void const* arg) {
95  if(type == qRegisterMetaType<lc::geo::Coordinate>()) {
96  s.push(*(lc::geo::Coordinate*) arg);
97  return;
98  }
99 
100  switch (type) {
101  case QMetaType::Void:
102  s.push(nullptr);
103  break;
104  case QMetaType::Bool:
105  s.push(*(bool *) arg);
106  break;
107  case QMetaType::Int:
108  s.push(*(int *) arg);
109  break;
110  case QMetaType::UInt:
111  s.push(*(unsigned int *) arg);
112  break;
113  case QMetaType::Long:
114  s.push(*(long *) arg);
115  break;
116  case QMetaType::LongLong:
117  s.push(*(long long *) arg);
118  break;
119  case QMetaType::Short:
120  s.push(*(short *) arg);
121  break;
122  case QMetaType::Char:
123  s.push(*(char *) arg);
124  break;
125  case QMetaType::ULong:
126  s.push(*(unsigned long *) arg);
127  break;
128  case QMetaType::ULongLong:
129  s.push(*(unsigned long long *) arg);
130  break;
131  case QMetaType::UShort:
132  s.push(*(unsigned short *) arg);
133  break;
134  case QMetaType::UChar:
135  s.push(*(unsigned char *) arg);
136  break;
137  case QMetaType::Double:
138  s.push(*(double *) arg);
139  break;
140  case QMetaType::Float:
141  s.push(*(float *) arg);
142  break;
143  case QMetaType::QString:
144  s.push(reinterpret_cast<const QString *>(arg));
145  break;
146  default:
147  s.push(nullptr);
148  break;
149  }
150 }
151 
153  return _valid;
154 }
int _slotId
Definition: luaqobject.h:87
bool connect(int signalId, LuaIntf::LuaRef slot)
Connect Qt slot to Lua function.
Definition: luaqobject.cpp:16
std::string objectName()
Get object name.
Definition: luaqobject.cpp:66
bool valid()
Return object validity.
Definition: luaqobject.cpp:152
LuaIntf::LuaRef _slotFunction
Definition: luaqobject.h:89
int _signalId
Definition: luaqobject.h:88
bool _valid
Definition: luaqobject.h:91
void pushArg(LuaIntf::LuaState s, int const type, void const *arg)
Add signal parameter.
Definition: luaqobject.cpp:94
LuaQObject(QObject *object)
Create QObject wrapper.
Definition: luaqobject.cpp:3
QObject * findChild(std::string name)
Find children by name.
Definition: luaqobject.cpp:86
QObject * _object
Definition: luaqobject.h:85
int qt_metacall(QMetaObject::Call c, int id, void **a)
Internal Qt function This function is called when a slot of this object is called. It gets the arguments and call the Lua function.
Definition: luaqobject.cpp:33