diff --git a/src/qobject.cpp b/src/qobject.cpp deleted file mode 100644 index f0d0d62..0000000 --- a/src/qobject.cpp +++ /dev/null @@ -1,428 +0,0 @@ -#include -#include -#include -#include -#include "qobject.h" -#include "signal_handler.h" -#include "callback.h" - -namespace Brig { - - using namespace v8; - using namespace node; - - Persistent QObjectWrap::constructor; - - QObjectWrap::QObjectWrap() : ObjectWrap() - { - SignalHandler *signalHandler = new SignalHandler(obj_wrap); - } - - QObjectWrap::QObjectWrap(QObject *object) : ObjectWrap() - { - obj = object; - } - - QObjectWrap::~QObjectWrap() - { - - delete obj; - } - - void QObjectWrap::Initialize(Handle target) - { - HandleScope scope; - - Local name = String::NewSymbol("QObject"); - - /* Constructor template */ - Persistent tpl = Persistent::New(FunctionTemplate::New(QObjectWrap::New)); - tpl->InstanceTemplate()->SetInternalFieldCount(1); - tpl->SetClassName(name); - - /* Prototype */ - NODE_SET_PROTOTYPE_METHOD(tpl, "toQuickWindow", QObjectWrap::toQuickWindow); - NODE_SET_PROTOTYPE_METHOD(tpl, "toQuickItem", QObjectWrap::toQuickItem); - NODE_SET_PROTOTYPE_METHOD(tpl, "setParent", QObjectWrap::setParent); - NODE_SET_PROTOTYPE_METHOD(tpl, "getProperty", QObjectWrap::getProperty); - NODE_SET_PROTOTYPE_METHOD(tpl, "setProperty", QObjectWrap::setProperty); - NODE_SET_PROTOTYPE_METHOD(tpl, "getPropertyNames", QObjectWrap::getPropertyNames); - NODE_SET_PROTOTYPE_METHOD(tpl, "getMethods", QObjectWrap::getMethods); - NODE_SET_PROTOTYPE_METHOD(tpl, "emit", QObjectWrap::emitEvent); - NODE_SET_PROTOTYPE_METHOD(tpl, "invokeMethod", QObjectWrap::invokeMethod); - - constructor = Persistent::New(tpl->GetFunction()); - - target->Set(name, constructor); - } - - Handle QObjectWrap::New(const Arguments& args) - { - HandleScope scope; - - if (args[0]->ToObject()->InternalFieldCount() >= 2) { - - BrigContainerType type = static_cast(args[0]->ToObject()->GetInternalField(1)->ToInteger()->Value()); - - if (type == BRIG_CONTAINER_NATIVE) { - QObject *object = ObjectWrap::Unwrap(args[0]->ToObject()); - QObjectWrap *obj_wrap = new QObjectWrap(object); - obj_wrap->Wrap(args.This()); - return args.This(); - } - } - - QObjectWrap *obj_wrap = new QObjectWrap(); - obj_wrap->Wrap(args.This()); - - return scope.Close(args[0]); - } - - Handle QObjectWrap::NewInstance(QObject *qobject) - { - HandleScope scope; - - // Create a container - Handle object_template = ObjectTemplate::New(); - object_template->SetInternalFieldCount(2); - Persistent object_instance = Persistent::New(object_template); - Local obj = object_instance->NewInstance(); - - // Wrap - obj->SetInternalField(0, External::New(qobject)); - obj->SetInternalField(1, Integer::New(BRIG_CONTAINER_NATIVE)); - - const unsigned argc = 1; - Handle argv[argc] = { obj }; - - Local instance = constructor->NewInstance(argc, argv); - - return scope.Close(instance); - } - - Handle QObjectWrap::setParent(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - QObjectWrap *wrap = ObjectWrap::Unwrap(args[0]->ToObject()); - - obj_wrap->GetObject()->setParent(wrap->GetObject()); - - return scope.Close(Undefined()); - } - - Handle QObjectWrap::toQuickWindow(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - // It's not a window component - if (!obj_wrap->GetObject()->isWindowType()) - return ThrowException(Exception::Error(String::New("Not a QuickWindow object"))); - - return scope.Close(QuickWindowWrap::NewInstance(args.This())); - } - - Handle QObjectWrap::toQuickItem(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - // It's not a window component - if (!obj_wrap->GetObject()->inherits("QQuickItem")) - return ThrowException(Exception::Error(String::New("Not a QuickItem object"))); - - return scope.Close(QuickItemWrap::NewInstance(args.This())); - } - - Handle QObjectWrap::getProperty(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - if (!args[0]->IsString()) - return ThrowException(Exception::Error(String::New("First argument must be a string"))); - - String::Utf8Value name(args[0]->ToString()); - - // Get property - QVariant v = obj_wrap->GetObject()->property(*name); - - // Convert Qvariant to V8 data type - if (v.isNull()) - return scope.Close(Null()); - - switch(v.userType()) { - case QMetaType::Bool: - return scope.Close(Boolean::New(v.toBool())); - case QMetaType::Int: - return scope.Close(Number::New(v.toInt())); - case QMetaType::UInt: - return scope.Close(Number::New(v.toUInt())); - case QMetaType::Float: - return scope.Close(Number::New(v.toFloat())); - case QMetaType::Double: - return scope.Close(Number::New(v.toDouble())); - case QMetaType::LongLong: - return scope.Close(Number::New(v.toLongLong())); - case QMetaType::ULongLong: - return scope.Close(Number::New(v.toULongLong())); - case QMetaType::QString: - return scope.Close(String::New(v.toString().toUtf8().constData())); - case QMetaType::QColor: - return scope.Close(String::New(v.value().name(QColor::HexArgb).toUtf8().constData())); - } - - return scope.Close(Undefined()); - } - - Handle QObjectWrap::setProperty(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - if (!args[0]->IsString()) - return ThrowException(Exception::Error(String::New("First argument must be a string"))); - - String::Utf8Value name(args[0]->ToString()); - Handle value(args[1]); - QVariant v; - - // Check data type - if (value->IsTrue() || value->IsFalse() || value->IsBoolean() ) { - v.setValue(QVariant(value->ToBoolean()->Value())); - } else if (value->IsNumber()) { - v.setValue(QVariant(value->NumberValue())); - } else if (value->IsInt32()) { - v.setValue(QVariant(value->ToInt32()->Value())); - } else if (value->IsString()) { - String::Utf8Value _v(value->ToString()); - v.setValue(QVariant(static_cast(*_v))); - } - - // Set property - obj_wrap->GetObject()->setProperty(*name, v); - - return scope.Close(Undefined()); - } - - Handle QObjectWrap::getPropertyNames(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - Handle keys = Array::New(); - - // Getting property names - static const QMetaObject *meta = obj_wrap->GetObject()->metaObject(); - for (int i = 0; i < meta->propertyCount(); i++) { - keys->Set(i, String::New(QString(meta->property(i).name()).toUtf8().constData())); - } - - return scope.Close(keys); - } - - Handle QObjectWrap::getMethods(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - Handle obj = Object::New(); - - static const QMetaObject *meta = obj_wrap->GetObject()->metaObject(); - for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) -// for(int i = 0; i < meta->methodCount(); ++i) - obj->Set(i, String::New(meta->method(i).methodSignature().data())); - - return scope.Close(obj); - } - - Handle QObjectWrap::emitEvent(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - if (!args[0]->IsString()) - return ThrowException(Exception::Error(String::New("First argument must be a string"))); - - // Method name - String::Utf8Value methodSig(args[0]->ToString()); - - QVariant returnedValue; - int argsLen = args.Length() - 1; - - static const QMetaObject *meta = obj_wrap->GetObject()->metaObject(); -// int methodIndex = meta->indexOfMethod(*methodSig); -// QMetaMethod method = meta->method(methodIndex); -// - // Getting currect context - QQmlContext *thisContext = QQmlEngine::contextForObject(obj_wrap->GetObject()); - - // Preparing parameter and ensure QJSValue have individual memory - QJSValue val[argsLen]; - QList parameters; - QList dataList; - - // Getting signature - for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { - QMetaMethod method = meta->method(i); - const char *methodName = method.name().data(); - if (strcmp(*methodSig, methodName) != 0) - continue; - - if (!method.isValid()) - break; - - // Getting parameter types - for (int j = 0; j < method.parameterCount(); j++) { - int type = method.parameterType(j); - - if (j >= args.Length()) - break; - - Handle value = args[j + 1]; - - // Type is "var" in QML, which is different from QVariant - if (type == qMetaTypeId()) { - val[j] = Utils::V8ToQJSValue(thisContext->engine(), value); - - // Undefined - parameters << QGenericArgument("QJSValue", static_cast(&val[j])); - continue; - } - - // Making arguments - Utils::ParamData *data = Utils::MakeParameter(type, value); - if (data == NULL) { - // Unknown type, set Undefined to this parameter - parameters << QGenericArgument(); - continue; - } - - dataList << data; - parameters << QGenericArgument(QMetaType::typeName(type), data->ptr); - } - - // Invoke - method.invoke(obj_wrap->GetObject(), - Qt::QueuedConnection, - (argsLen > 0) ? parameters[0] : QGenericArgument(), - (argsLen > 1) ? parameters[1] : QGenericArgument(), - (argsLen > 2) ? parameters[2] : QGenericArgument(), - (argsLen > 3) ? parameters[3] : QGenericArgument(), - (argsLen > 4) ? parameters[4] : QGenericArgument(), - (argsLen > 5) ? parameters[5] : QGenericArgument(), - (argsLen > 6) ? parameters[6] : QGenericArgument(), - (argsLen > 7) ? parameters[7] : QGenericArgument(), - (argsLen > 8) ? parameters[8] : QGenericArgument(), - (argsLen > 9) ? parameters[9] : QGenericArgument()); - - // Release - dataList.clear(); - parameters.clear(); - - break; - } - - return Undefined(); - } - - Handle QObjectWrap::invokeMethod(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - - if (!args[0]->IsString()) - return ThrowException(Exception::Error(String::New("First argument must be a string"))); - - // Method name - String::Utf8Value methodSig(args[0]->ToString()); - - QVariant returnedValue; - int argsLen = args.Length() - 1; - - // It supports only 10 arguments with limitation of Qt - QMetaObject::invokeMethod(obj_wrap->GetObject(), *methodSig, - Qt::AutoConnection, - Q_RETURN_ARG(QVariant, returnedValue), - (argsLen > 0) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[1])) : QGenericArgument(), - (argsLen > 1) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[2])) : QGenericArgument(), - (argsLen > 2) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[3])) : QGenericArgument(), - (argsLen > 3) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[4])) : QGenericArgument(), - (argsLen > 4) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[5])) : QGenericArgument(), - (argsLen > 5) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[6])) : QGenericArgument(), - (argsLen > 6) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[7])) : QGenericArgument(), - (argsLen > 7) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[8])) : QGenericArgument(), - (argsLen > 8) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[8])) : QGenericArgument(), - (argsLen > 9) ? Q_ARG(QVariant, Utils::V8ToQVariant(args[9])) : QGenericArgument()); - - // Convert Qvariant to V8 data type - if (returnedValue.isNull()) - return scope.Close(Null()); - - switch(returnedValue.userType()) { - case QMetaType::Bool: - return scope.Close(Boolean::New(returnedValue.toBool())); - case QMetaType::Int: - return scope.Close(Number::New(returnedValue.toInt())); - case QMetaType::UInt: - return scope.Close(Number::New(returnedValue.toUInt())); - case QMetaType::Float: - return scope.Close(Number::New(returnedValue.toFloat())); - case QMetaType::Double: - return scope.Close(Number::New(returnedValue.toDouble())); - case QMetaType::LongLong: - return scope.Close(Number::New(returnedValue.toLongLong())); - case QMetaType::ULongLong: - return scope.Close(Number::New(returnedValue.toULongLong())); - case QMetaType::QString: - return scope.Close(String::New(returnedValue.toString().toUtf8().constData())); - case QMetaType::QColor: - return scope.Close(String::New(returnedValue.value().name(QColor::HexArgb).toUtf8().constData())); - } - - return scope.Close(Undefined()); - } - - Handle QObjectWrap::connect(const Arguments& args) - { - HandleScope scope; - - QObjectWrap *obj_wrap = ObjectWrap::Unwrap(args.This()); - QObject *obj = obj_wrap->GetObject(); - - // Signal name - String::Utf8Value methodSig(args[0]->ToString()); - - // Callback - Callback *callback = new Callback(); - callback->handler = Persistent::New(Handle::Cast(args[1])); - - // Finding index of signal - static const QMetaObject *meta = obj->metaObject(); - for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { - QMetaMethod method = meta->method(i); - const char *methodName = method.name().data(); - if (strcmp(*methodSig, methodName) != 0) - continue; - - // Create signal handler - SignalHandler *signalHandler = new SignalHandler(obj_wrap); - - // Setup handler - QMetaObject::connect(obj, i, signalHandler, QObject::metaObject()->methodCount() + callbacks.count()); - - } - - return Undefined(); - } -} diff --git a/src/qobject.h b/src/qobject.h deleted file mode 100644 index 69dc17f..0000000 --- a/src/qobject.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef BRIG_QOBJECT_WRAP_H -#define BRIG_QOBJECT_WRAP_H - -#include -#include -#include -#include "brig.h" - -namespace Brig { - - using namespace v8; - using namespace node; - - class QObjectWrap : public ObjectWrap { - - public: - QObjectWrap(); - QObjectWrap(QObject *object); - ~QObjectWrap(); - - static Persistent constructor; - static void Initialize(Handle target); - static Handle NewInstance(QObject *object); - - QObject *GetObject() const { return obj; }; - - private: - - static Handle New(const Arguments& args); - - /* Methods */ - static Handle setParent(const Arguments& args); - static Handle toQuickWindow(const Arguments& args); - static Handle toQuickItem(const Arguments& args); - static Handle getProperty(const Arguments& args); - static Handle setProperty(const Arguments& args); - static Handle getPropertyNames(const Arguments& args); - static Handle getMethods(const Arguments& args); - static Handle emitEvent(const Arguments& args); - static Handle invokeMethod(const Arguments& args); - - QObject *obj; - SignalHandler *signalHandler; - }; - -} - -#endif