diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-03-05 08:04:37 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-03-05 08:04:37 (GMT) |
commit | dcd17fa7b77cb6adfb8b21ea90c113915cab9bd5 (patch) | |
tree | 37dca0549529fe43b6e98af3adab133a9f5991d1 /src/declarative | |
parent | 4a6154ec912d5b84a556aa731c5b7de8f758ba3d (diff) | |
download | Qt-dcd17fa7b77cb6adfb8b21ea90c113915cab9bd5.zip Qt-dcd17fa7b77cb6adfb8b21ea90c113915cab9bd5.tar.gz Qt-dcd17fa7b77cb6adfb8b21ea90c113915cab9bd5.tar.bz2 |
Add support for QtScript connect/disconnect syntax in QML
This support was accidentally removed as a consequence of
4a665ff5da05860f5eb46e3982ef3d8163a6cf59.
QTBUG-8001
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/qml/qdeclarativeobjectscriptclass.cpp | 81 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeobjectscriptclass_p.h | 12 |
2 files changed, 93 insertions, 0 deletions
diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp index 2e4ffa7..e6f6e5f 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp @@ -442,6 +442,13 @@ QDeclarativeObjectMethodScriptClass::QDeclarativeObjectMethodScriptClass(QDeclar engine(bindEngine) { setSupportsCall(true); + + QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine); + + m_connect = scriptEngine->newFunction(connect); + m_connectId = createPersistentIdentifier(QLatin1String("connect")); + m_disconnect = scriptEngine->newFunction(disconnect); + m_disconnectId = createPersistentIdentifier(QLatin1String("disconnect")); } QDeclarativeObjectMethodScriptClass::~QDeclarativeObjectMethodScriptClass() @@ -455,6 +462,80 @@ QScriptValue QDeclarativeObjectMethodScriptClass::newMethod(QObject *object, con return newObject(scriptEngine, this, new MethodData(object, *method)); } +QScriptValue QDeclarativeObjectMethodScriptClass::connect(QScriptContext *context, QScriptEngine *engine) +{ + QDeclarativeEnginePrivate *p = QDeclarativeEnginePrivate::get(engine); + + QScriptValue that = context->thisObject(); + if (&p->objectClass->methods != scriptClass(that)) + return engine->undefinedValue(); + + MethodData *data = (MethodData *)object(that); + + if (!data->object || context->argumentCount() == 0) + return engine->undefinedValue(); + + QByteArray signal("2"); + signal.append(data->object->metaObject()->method(data->data.coreIndex).signature()); + + if (context->argumentCount() == 1) { + qScriptConnect(data->object, signal.constData(), QScriptValue(), context->argument(0)); + } else { + qScriptConnect(data->object, signal.constData(), context->argument(0), context->argument(1)); + } + + return engine->undefinedValue(); +} + +QScriptValue QDeclarativeObjectMethodScriptClass::disconnect(QScriptContext *context, QScriptEngine *engine) +{ + QDeclarativeEnginePrivate *p = QDeclarativeEnginePrivate::get(engine); + + QScriptValue that = context->thisObject(); + if (&p->objectClass->methods != scriptClass(that)) + return engine->undefinedValue(); + + MethodData *data = (MethodData *)object(that); + + if (!data->object || context->argumentCount() == 0) + return engine->undefinedValue(); + + QByteArray signal("2"); + signal.append(data->object->metaObject()->method(data->data.coreIndex).signature()); + + if (context->argumentCount() == 1) { + qScriptDisconnect(data->object, signal.constData(), QScriptValue(), context->argument(0)); + } else { + qScriptDisconnect(data->object, signal.constData(), context->argument(0), context->argument(1)); + } + + return engine->undefinedValue(); +} + +QScriptClass::QueryFlags +QDeclarativeObjectMethodScriptClass::queryProperty(Object *, const Identifier &name, + QScriptClass::QueryFlags flags) +{ + if (name == m_connectId.identifier || name == m_disconnectId.identifier) + return QScriptClass::HandlesReadAccess; + else + return 0; + +} + +QDeclarativeObjectScriptClass::ScriptValue +QDeclarativeObjectMethodScriptClass::property(Object *, const Identifier &name) +{ + QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine); + + if (name == m_connectId.identifier) + return Value(scriptEngine, m_connect); + else if (name == m_disconnectId.identifier) + return Value(scriptEngine, m_disconnect); + else + return Value(); +} + namespace { struct MetaCallArgument { inline MetaCallArgument(); diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass_p.h b/src/declarative/qml/qdeclarativeobjectscriptclass_p.h index 8023756..04e760f 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass_p.h +++ b/src/declarative/qml/qdeclarativeobjectscriptclass_p.h @@ -73,10 +73,21 @@ public: ~QDeclarativeObjectMethodScriptClass(); QScriptValue newMethod(QObject *, const QDeclarativePropertyCache::Data *); + protected: virtual Value call(Object *, QScriptContext *); + virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags); + virtual Value property(Object *, const Identifier &); private: + PersistentIdentifier m_connectId; + PersistentIdentifier m_disconnectId; + QScriptValue m_connect; + QScriptValue m_disconnect; + + static QScriptValue connect(QScriptContext *context, QScriptEngine *engine); + static QScriptValue disconnect(QScriptContext *context, QScriptEngine *engine); + QDeclarativeEngine *engine; }; #endif @@ -119,6 +130,7 @@ protected: private: #if (QT_VERSION > QT_VERSION_CHECK(4, 6, 2)) || defined(QT_HAVE_QSCRIPTDECLARATIVECLASS_VALUE) + friend class QDeclarativeObjectMethodScriptClass; QDeclarativeObjectMethodScriptClass methods; #endif |