LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
LuaQObject Class Reference

QObject wrapper for Lua Allow connecting Qt signals with Lua functions. More...

#include <luaqobject.h>

Inheritance diagram for LuaQObject:
Collaboration diagram for LuaQObject:

Public Member Functions

 LuaQObject (QObject *object)
 Create QObject wrapper. More...
 
 ~LuaQObject ()
 
bool connect (int signalId, LuaIntf::LuaRef slot)
 Connect Qt slot to Lua function. More...
 
void pushArg (LuaIntf::LuaState s, int const type, void const *arg)
 Add signal parameter. More...
 
std::string objectName ()
 Get object name. More...
 
QObject * findChild (std::string name)
 Find children by name. More...
 
bool valid ()
 Return object validity. More...
 
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. More...
 

Static Public Member Functions

static std::string objectName (QObject *object)
 Get object name. More...
 
static QObject * findChild (QObject *object, std::string name)
 Find children by name. More...
 

Private Attributes

QObject * _object
 
int _slotId
 
int _signalId
 
LuaIntf::LuaRef _slotFunction
 
bool _valid
 

Detailed Description

QObject wrapper for Lua Allow connecting Qt signals with Lua functions.

Definition at line 19 of file luaqobject.h.

Constructor & Destructor Documentation

LuaQObject::LuaQObject ( QObject *  object)

Create QObject wrapper.

Parameters
objectPointer to QObject to wrap

Definition at line 3 of file luaqobject.cpp.

3  :
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 }
bool _valid
Definition: luaqobject.h:91
QObject * _object
Definition: luaqobject.h:85
LuaQObject::~LuaQObject ( )

Definition at line 12 of file luaqobject.cpp.

12  {
13  _object = 0;
14 }
QObject * _object
Definition: luaqobject.h:85

Member Function Documentation

bool LuaQObject::connect ( int  signalId,
LuaIntf::LuaRef  slot 
)

Connect Qt slot to Lua function.

Parameters
signalIdObject signal ID
slotLua function
Returns
true if connected, false if an error happened

Definition at line 16 of file luaqobject.cpp.

16  {
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 }
int _slotId
Definition: luaqobject.h:87
LuaIntf::LuaRef _slotFunction
Definition: luaqobject.h:89
int _signalId
Definition: luaqobject.h:88
bool _valid
Definition: luaqobject.h:91
QObject * _object
Definition: luaqobject.h:85
QObject * LuaQObject::findChild ( std::string  name)

Find children by name.

Parameters
nameChildren name
Returns
Pointer to QObject

Definition at line 86 of file luaqobject.cpp.

86  {
87  if(_object) {
88  return findChild(_object, name);
89  }
90 
91  return 0;
92 }
QObject * findChild(std::string name)
Find children by name.
Definition: luaqobject.cpp:86
QObject * _object
Definition: luaqobject.h:85
QObject * LuaQObject::findChild ( QObject *  object,
std::string  name 
)
static

Find children by name.

Parameters
objectPointer to QObject
nameChildren name
Returns
Pointer to QObject

Definition at line 74 of file luaqobject.cpp.

74  {
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 }
std::string objectName()
Get object name.
Definition: luaqobject.cpp:66
std::string LuaQObject::objectName ( )

Get object name.

Returns
QObject name

Definition at line 66 of file luaqobject.cpp.

66  {
67  if(_object) {
68  return objectName(_object);
69  }
70 
71  return "null";
72 }
std::string objectName()
Get object name.
Definition: luaqobject.cpp:66
QObject * _object
Definition: luaqobject.h:85
std::string LuaQObject::objectName ( QObject *  object)
static

Get object name.

Parameters
objectPointer to QObject
Returns
QObject name

Definition at line 61 of file luaqobject.cpp.

61  {
62  return object->objectName().toStdString();
63 }
void LuaQObject::pushArg ( LuaIntf::LuaState  s,
int const  type,
void const *  arg 
)

Add signal parameter.

Parameters
sLua state
typeQt QMetaType ID
argPointer to arg Push a signal parameter to Lua stack

Definition at line 94 of file luaqobject.cpp.

94  {
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 }
int LuaQObject::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 at line 33 of file luaqobject.cpp.

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 }
int _slotId
Definition: luaqobject.h:87
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
QObject * _object
Definition: luaqobject.h:85
bool LuaQObject::valid ( )

Return object validity.

Returns
true if QOaject still exists

Definition at line 152 of file luaqobject.cpp.

152  {
153  return _valid;
154 }
bool _valid
Definition: luaqobject.h:91

Member Data Documentation

QObject* LuaQObject::_object
private

Definition at line 85 of file luaqobject.h.

int LuaQObject::_signalId
private

Definition at line 88 of file luaqobject.h.

LuaIntf::LuaRef LuaQObject::_slotFunction
private

Definition at line 89 of file luaqobject.h.

int LuaQObject::_slotId
private

Definition at line 87 of file luaqobject.h.

bool LuaQObject::_valid
private

Definition at line 91 of file luaqobject.h.


The documentation for this class was generated from the following files: