static int lqtL_methods(lua_State *L) { QObject* self = static_cast<QObject*>(lqtL_toudata(L, 1, "QObject*")); if (self == NULL) return luaL_argerror(L, 1, "expecting QObject*"); const QMetaObject *mo = self->metaObject(); lua_createtable(L, mo->methodCount(), 0); for (int i=0; i < mo->methodCount(); i++) { QMetaMethod m = mo->method(i); lua_pushstring(L, m.signature()); switch (m.access()) { CASE(Private); CASE(Protected); CASE(Public); } switch (m.methodType()) { CASE(Method); CASE(Signal); CASE(Slot); CASE(Constructor); } lua_concat(L, 3); lua_rawseti(L, -2, i+1); } return 1; }
static int lqtL_connect(lua_State *L) { static int methodId = 0; QObject* sender = static_cast<QObject*>(lqtL_toudata(L, 1, "QObject*")); if (sender == NULL) return luaL_argerror(L, 1, "sender not QObject*"); const char *signal = luaL_checkstring(L, 2); const QMetaObject *senderMeta = sender->metaObject(); int idxS = senderMeta->indexOfSignal(signal + 1); if (idxS == -1) return luaL_argerror(L, 2, qPrintable(QString("no such sender signal: '%1'").arg(signal + 1))); QObject* receiver; QString methodName; if (lua_type(L, 3) == LUA_TFUNCTION) { receiver = sender; // simulate sender:__addmethod('LQT_SLOT_X(signature)', function()...end) QMetaMethod m = senderMeta->method(idxS); methodName = QString(m.signature()).replace(QRegExp("^[^\\(]+"), QString("LQT_SLOT_%1").arg(methodId++)); lua_getfield(L, 1, "__addmethod"); lua_pushvalue(L, 1); lua_pushstring(L, qPrintable(methodName)); lua_pushvalue(L, 3); lua_call(L, 3, 0); methodName.prepend("1"); } else { receiver = static_cast<QObject*>(lqtL_toudata(L, 3, "QObject*")); if (receiver == NULL) return luaL_argerror(L, 3, "receiver not QObject*"); const char *method = luaL_checkstring(L, 4); methodName = method; const QMetaObject *receiverMeta = receiver->metaObject(); int idxR = receiverMeta->indexOfMethod(method + 1); if (idxR == -1) return luaL_argerror(L, 4, qPrintable(QString("no such receiver method: '%1'").arg(method + 1))); } bool ok = QObject::connect(sender, signal, receiver, qPrintable(methodName)); lua_pushboolean(L, ok); return 1; }
// converts the value at index `n` to `to_type` and returns a pointer to it void *lqtL_convert(lua_State *L, int n, const char *to_type) { if (lqtL_testudata(L, n, to_type)) return lqtL_toudata(L, n, to_type); int oldtop = lua_gettop(L); luaL_getmetatable(L, to_type); lua_getfield(L, -1, "__convert"); if (lua_isnil(L, -1)) { lua_settop(L, oldtop); return false; } lqt_convertfunc func = (lqt_convertfunc) lua_touserdata(L, -1); lua_settop(L, oldtop); return func(L, n); }
static int lqtL_findchild(lua_State *L) { QObject* self = static_cast<QObject*>(lqtL_toudata(L, 1, "QObject*")); if (self == NULL) return luaL_argerror(L, 1, "expecting QObject*"); QString name = luaL_checkstring(L, 2); QObject * child = self->findChild<QObject*>(name); if (child) { lqtL_pushqobject(L, child); return 1; } else { return 0; } }
static int lqtL_children(lua_State *L) { QObject* self = static_cast<QObject*>(lqtL_toudata(L, 1, "QObject*")); if (self == NULL) return luaL_argerror(L, 1, "expecting QObject*"); const QObjectList & children = self->children(); lua_newtable(L); for (int i=0; i < children.count(); i++) { QObject * object = children[i]; QString name = object->objectName(); if (!name.isEmpty() && lqtL_pushqobject(L, object)) { lua_setfield(L, -2, qPrintable(name)); } } return 1; }
int lqtL_qvariant_value(lua_State *L) { QVariant* self = static_cast<QVariant*>(lqtL_toudata(L, 1, "QVariant*")); lqtL_selfcheck(L, self, "QVariant"); QVariant::Type type; if (lua_isnoneornil(L, 2)) { type = self->type(); } else { type = (QVariant::Type)lqtL_toenum(L, 2, "QVariant.Type"); const char * currentType = self->typeName(); if (!self->canConvert(type) || !self->convert(type)) { lua_pushnil(L); lua_pushfstring(L, "cannot convert %s to %s", currentType, self->typeToName(type)); return 2; } } switch (self->type()) { case QVariant::Invalid: lua_pushnil(L); return 1; /* basic types */ case QVariant::Bool: lua_pushboolean(L, self->toBool()); return 1; case QVariant::Double: lua_pushnumber(L, self->toDouble()); return 1; case QVariant::Int: lua_pushinteger(L, self->toInt()); return 1; case QVariant::UInt: lua_pushinteger(L, self->toUInt()); return 1; case QVariant::LongLong: lua_pushnumber(L, self->toLongLong()); return 1; case QVariant::ULongLong: lua_pushnumber(L, self->toULongLong()); return 1; case QVariant::ByteArray: { const QByteArray &ba = self->toByteArray(); lua_pushlstring(L, ba.data(), ba.size()); return 1; }; /* QtCore types */ case QVariant::BitArray: lqtL_passudata(L, new QBitArray(self->value<QBitArray>()), "QBitArray*"); return 1; case QVariant::Char: lqtL_passudata(L, new QChar(self->value<QChar>()), "QChar*"); return 1; case QVariant::Date: lqtL_passudata(L, new QDate(self->value<QDate>()), "QDate*"); return 1; case QVariant::DateTime: lqtL_passudata(L, new QDateTime(self->value<QDateTime>()), "QDateTime*"); return 1; case QVariant::KeySequence: lqtL_passudata(L, new QKeySequence(self->value<QKeySequence>()), "QKeySequence*"); return 1; case QVariant::Line: lqtL_passudata(L, new QLine(self->value<QLine>()), "QLine*"); return 1; case QVariant::LineF: lqtL_passudata(L, new QLineF(self->value<QLineF>()), "QLineF*"); return 1; case QVariant::List: lqtL_passudata(L, new QList<QVariant>(self->toList()), "QList<QVariant>*"); return 1; case QVariant::Locale: lqtL_passudata(L, new QLocale(self->value<QLocale>()), "QLocale*"); return 1; case QVariant::Point: lqtL_passudata(L, new QPoint(self->value<QPoint>()), "QPoint*"); return 1; case QVariant::PointF: lqtL_passudata(L, new QPointF(self->value<QPointF>()), "QPointF*"); return 1; case QVariant::Rect: lqtL_passudata(L, new QRect(self->value<QRect>()), "QRect*"); return 1; case QVariant::RectF: lqtL_passudata(L, new QRectF(self->value<QRectF>()), "QRectF*"); return 1; case QVariant::RegExp: lqtL_passudata(L, new QRegExp(self->value<QRegExp>()), "QRegExp*"); return 1; case QVariant::Size: lqtL_passudata(L, new QSize(self->value<QSize>()), "QSize*"); return 1; case QVariant::SizeF: lqtL_passudata(L, new QSizeF(self->value<QSizeF>()), "QSizeF*"); return 1; case QVariant::String: lqtL_passudata(L, new QString(self->value<QString>()), "QString*"); return 1; case QVariant::StringList: lqtL_passudata(L, new QStringList(self->value<QStringList>()), "QStringList*"); return 1; case QVariant::Time: lqtL_passudata(L, new QTime(self->value<QTime>()), "QTime*"); return 1; case QVariant::Url: lqtL_passudata(L, new QUrl(self->value<QUrl>()), "QUrl*"); return 1; #ifdef MODULE_qtgui /* QtGui types */ case QVariant::Bitmap: lqtL_passudata(L, new QBitmap(self->value<QBitmap>()), "QBitmap*"); return 1; case QVariant::Brush: lqtL_passudata(L, new QBrush(self->value<QBrush>()), "QBrush*"); return 1; case QVariant::Color: lqtL_passudata(L, new QColor(self->value<QColor>()), "QColor*"); return 1; case QVariant::Cursor: lqtL_passudata(L, new QCursor(self->value<QCursor>()), "QCursor*"); return 1; case QVariant::Font: lqtL_passudata(L, new QFont(self->value<QFont>()), "QFont*"); return 1; case QVariant::Icon: lqtL_passudata(L, new QIcon(self->value<QIcon>()), "QIcon*"); return 1; case QVariant::Image: lqtL_passudata(L, new QImage(self->value<QImage>()), "QImage*"); return 1; case QVariant::Matrix: lqtL_passudata(L, new QMatrix(self->value<QMatrix>()), "QMatrix*"); return 1; case QVariant::Matrix4x4: lqtL_passudata(L, new QMatrix4x4(self->value<QMatrix4x4>()), "QMatrix4x4*"); return 1; case QVariant::Palette: lqtL_passudata(L, new QPalette(self->value<QPalette>()), "QPalette*"); return 1; case QVariant::Pen: lqtL_passudata(L, new QPen(self->value<QPen>()), "QPen*"); return 1; case QVariant::Pixmap: lqtL_passudata(L, new QPixmap(self->value<QPixmap>()), "QPixmap*"); return 1; case QVariant::Polygon: lqtL_passudata(L, new QPolygon(self->value<QPolygon>()), "QPolygon*"); return 1; case QVariant::Quaternion: lqtL_passudata(L, new QQuaternion(self->value<QQuaternion>()), "QQuaternion*"); return 1; case QVariant::Region: lqtL_passudata(L, new QRegion(self->value<QRegion>()), "QRegion*"); return 1; case QVariant::SizePolicy: lqtL_passudata(L, new QSizePolicy(self->value<QSizePolicy>()), "QSizePolicy*"); return 1; case QVariant::Transform: lqtL_passudata(L, new QTransform(self->value<QTransform>()), "QTransform*"); return 1; case QVariant::TextFormat: lqtL_passudata(L, new QTextFormat(self->value<QTextFormat>()), "QTextFormat*"); return 1; case QVariant::TextLength: lqtL_passudata(L, new QTextLength(self->value<QTextLength>()), "QTextLength*"); return 1; case QVariant::Vector2D: lqtL_passudata(L, new QVector2D(self->value<QVector2D>()), "QVector2D*"); return 1; case QVariant::Vector3D: lqtL_passudata(L, new QVector3D(self->value<QVector3D>()), "QVector3D*"); return 1; case QVariant::Vector4D: lqtL_passudata(L, new QVector4D(self->value<QVector4D>()), "QVector4D*"); return 1; #endif } return 0; }
int lqtL_qvariant_setValue(lua_State *L) { QVariant* self = static_cast<QVariant*>(lqtL_toudata(L, 1, "QVariant*")); lqtL_selfcheck(L, self, "QVariant"); /* basic types */ if (lua_isnil(L, 2)) { self->clear(); } else if (lua_isboolean(L, 2)) { self->setValue((bool)lua_toboolean(L, 2)); } else if (lua_isnumber(L, 2)) { self->setValue((double)lua_tonumber(L, 2)); } else if (lua_isstring(L, 2)) { size_t size; const char * str = lua_tolstring(L, 2, &size); self->setValue(QByteArray(str, size)); } /* QtCore types */ else if (lqtL_testudata(L, 2, "QBitArray*")) { self->setValue(*(QBitArray*)lqtL_toudata(L, 2, "QBitArray*")); } else if (lqtL_testudata(L, 2, "QByteArray*")) { self->setValue(*(QByteArray*)lqtL_toudata(L, 2, "QByteArray*")); } else if (lqtL_testudata(L, 2, "QChar*")) { self->setValue(*(QChar*)lqtL_toudata(L, 2, "QChar*")); } else if (lqtL_testudata(L, 2, "QDate*")) { self->setValue(*(QDate*)lqtL_toudata(L, 2, "QDate*")); } else if (lqtL_testudata(L, 2, "QDateTime*")) { self->setValue(*(QDateTime*)lqtL_toudata(L, 2, "QDateTime*")); } else if (lqtL_testudata(L, 2, "QKeySequence*")) { self->setValue(*(QKeySequence*)lqtL_toudata(L, 2, "QKeySequence*")); } else if (lqtL_testudata(L, 2, "QLine*")) { self->setValue(*(QLine*)lqtL_toudata(L, 2, "QLine*")); } else if (lqtL_testudata(L, 2, "QLineF*")) { self->setValue(*(QLineF*)lqtL_toudata(L, 2, "QLineF*")); } else if (lqtL_testudata(L, 2, "QList<QVariant>*")) { self->setValue(*(QList<QVariant>*)lqtL_toudata(L, 2, "QList<QVariant>*")); } else if (lqtL_testudata(L, 2, "QLocale*")) { self->setValue(*(QLocale*)lqtL_toudata(L, 2, "QLocale*")); } else if (lqtL_testudata(L, 2, "QRect*")) { self->setValue(*(QRect*)lqtL_toudata(L, 2, "QRect*")); } else if (lqtL_testudata(L, 2, "QRectF*")) { self->setValue(*(QRectF*)lqtL_toudata(L, 2, "QRectF*")); } else if (lqtL_testudata(L, 2, "QRegExp*")) { self->setValue(*(QRegExp*)lqtL_toudata(L, 2, "QRegExp*")); } else if (lqtL_testudata(L, 2, "QSize*")) { self->setValue(*(QSize*)lqtL_toudata(L, 2, "QSize*")); } else if (lqtL_testudata(L, 2, "QSizeF*")) { self->setValue(*(QSizeF*)lqtL_toudata(L, 2, "QSizeF*")); } else if (lqtL_testudata(L, 2, "QString*")) { self->setValue(*(QString*)lqtL_toudata(L, 2, "QString*")); } else if (lqtL_testudata(L, 2, "QStringList*")) { self->setValue(*(QStringList*)lqtL_toudata(L, 2, "QStringList*")); } else if (lqtL_testudata(L, 2, "QTime*")) { self->setValue(*(QTime*)lqtL_toudata(L, 2, "QTime*")); } else if (lqtL_testudata(L, 2, "QUrl*")) { self->setValue(*(QUrl*)lqtL_toudata(L, 2, "QUrl*")); } #ifdef MODULE_qtgui /* QtGui types */ else if (lqtL_testudata(L, 2, "QBitmap*")) { self->setValue(*(QBitmap*)lqtL_toudata(L, 2, "QBitmap*")); } else if (lqtL_testudata(L, 2, "QBrush*")) { self->setValue(*(QBrush*)lqtL_toudata(L, 2, "QBrush*")); } else if (lqtL_testudata(L, 2, "QColor*")) { self->setValue(*(QColor*)lqtL_toudata(L, 2, "QColor*")); } else if (lqtL_testudata(L, 2, "QCursor*")) { self->setValue(*(QCursor*)lqtL_toudata(L, 2, "QCursor*")); } else if (lqtL_testudata(L, 2, "QFont*")) { self->setValue(*(QFont*)lqtL_toudata(L, 2, "QFont*")); } else if (lqtL_testudata(L, 2, "QIcon*")) { self->setValue(*(QIcon*)lqtL_toudata(L, 2, "QIcon*")); } else if (lqtL_testudata(L, 2, "QImage*")) { self->setValue(*(QImage*)lqtL_toudata(L, 2, "QImage*")); } else if (lqtL_testudata(L, 2, "QMatrix*")) { self->setValue(*(QMatrix*)lqtL_toudata(L, 2, "QMatrix*")); } else if (lqtL_testudata(L, 2, "QMatrix4x4*")) { self->setValue(*(QMatrix4x4*)lqtL_toudata(L, 2, "QMatrix4x4*")); } else if (lqtL_testudata(L, 2, "QPalette*")) { self->setValue(*(QPalette*)lqtL_toudata(L, 2, "QPalette*")); } else if (lqtL_testudata(L, 2, "QPen*")) { self->setValue(*(QPen*)lqtL_toudata(L, 2, "QPen*")); } else if (lqtL_testudata(L, 2, "QPoint*")) { self->setValue(*(QPoint*)lqtL_toudata(L, 2, "QPoint*")); } else if (lqtL_testudata(L, 2, "QPointF*")) { self->setValue(*(QPointF*)lqtL_toudata(L, 2, "QPointF*")); } else if (lqtL_testudata(L, 2, "QPixmap*")) { self->setValue(*(QPixmap*)lqtL_toudata(L, 2, "QPixmap*")); } else if (lqtL_testudata(L, 2, "QPolygon*")) { self->setValue(*(QPolygon*)lqtL_toudata(L, 2, "QPolygon*")); } else if (lqtL_testudata(L, 2, "QQuaternion*")) { self->setValue(*(QQuaternion*)lqtL_toudata(L, 2, "QQuaternion*")); } else if (lqtL_testudata(L, 2, "QRegion*")) { self->setValue(*(QRegion*)lqtL_toudata(L, 2, "QRegion*")); } else if (lqtL_testudata(L, 2, "QSizePolicy*")) { self->setValue(*(QSizePolicy*)lqtL_toudata(L, 2, "QSizePolicy*")); } else if (lqtL_testudata(L, 2, "QTextFormat*")) { self->setValue(*(QTextFormat*)lqtL_toudata(L, 2, "QTextFormat*")); } else if (lqtL_testudata(L, 2, "QTextLength*")) { self->setValue(*(QTextLength*)lqtL_toudata(L, 2, "QTextLength*")); } else if (lqtL_testudata(L, 2, "QTransform*")) { self->setValue(*(QTransform*)lqtL_toudata(L, 2, "QTransform*")); } else if (lqtL_testudata(L, 2, "QVector2D*")) { self->setValue(*(QVector2D*)lqtL_toudata(L, 2, "QVector2D*")); } else if (lqtL_testudata(L, 2, "QVector3D*")) { self->setValue(*(QVector3D*)lqtL_toudata(L, 2, "QVector3D*")); } else if (lqtL_testudata(L, 2, "QVector4D*")) { self->setValue(*(QVector4D*)lqtL_toudata(L, 2, "QVector4D*")); } #endif return 0; }