diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-15 07:48:56 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-15 07:50:44 (GMT) |
commit | 2c9ff8dc79b69b310a568f7457838753abbf2178 (patch) | |
tree | 974ac90d252e9b748b5e2296faecb00acd4967e8 /src | |
parent | 0b3ade40922ecb06e3f67be96973322f8440bef9 (diff) | |
download | Qt-2c9ff8dc79b69b310a568f7457838753abbf2178.zip Qt-2c9ff8dc79b69b310a568f7457838753abbf2178.tar.gz Qt-2c9ff8dc79b69b310a568f7457838753abbf2178.tar.bz2 |
Support valuetypes as method return values
QTBUG-9818
Diffstat (limited to 'src')
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 4 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativevaluetype.cpp | 6 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativevaluetype_p.h | 5 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativevaluetypescriptclass.cpp | 123 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativevaluetypescriptclass_p.h | 1 |
5 files changed, 99 insertions, 40 deletions
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 1deadb2..96145fb 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -1365,7 +1365,9 @@ QScriptValue QDeclarativeEnginePrivate::scriptValueFromVariant(const QVariant &v rv.setProperty(ii, objectClass->newQObject(object)); } return rv; - } + } else if (QDeclarativeValueType *vt = valueTypes[val.userType()]) { + return valueTypeClass->newObject(val, vt); + } bool objOk; QObject *obj = QDeclarativeMetaType::toQObject(val, &objOk); diff --git a/src/declarative/qml/qdeclarativevaluetype.cpp b/src/declarative/qml/qdeclarativevaluetype.cpp index 261c84a..352a6c0 100644 --- a/src/declarative/qml/qdeclarativevaluetype.cpp +++ b/src/declarative/qml/qdeclarativevaluetype.cpp @@ -99,12 +99,6 @@ void QDeclarativeValueTypeFactory::registerValueTypes() qmlRegisterValueTypeEnums<QDeclarativeFontValueType>("Font"); } -QDeclarativeValueType *QDeclarativeValueTypeFactory::operator[](int idx) const -{ - return valueTypes[idx]; -} - - QDeclarativeValueType *QDeclarativeValueTypeFactory::valueType(int t) { switch (t) { diff --git a/src/declarative/qml/qdeclarativevaluetype_p.h b/src/declarative/qml/qdeclarativevaluetype_p.h index 5bfc27d..d1833bb 100644 --- a/src/declarative/qml/qdeclarativevaluetype_p.h +++ b/src/declarative/qml/qdeclarativevaluetype_p.h @@ -86,7 +86,10 @@ public: static void registerValueTypes(); - QDeclarativeValueType *operator[](int idx) const; + QDeclarativeValueType *operator[](int idx) const { + if (idx >= (int)QVariant::UserType) return 0; + else return valueTypes[idx]; + } private: QDeclarativeValueType *valueTypes[QVariant::UserType - 1]; diff --git a/src/declarative/qml/qdeclarativevaluetypescriptclass.cpp b/src/declarative/qml/qdeclarativevaluetypescriptclass.cpp index fdb71c6..cb1f27d 100644 --- a/src/declarative/qml/qdeclarativevaluetypescriptclass.cpp +++ b/src/declarative/qml/qdeclarativevaluetypescriptclass.cpp @@ -48,12 +48,24 @@ QT_BEGIN_NAMESPACE -struct QDeclarativeValueTypeReference : public QScriptDeclarativeClass::Object { +struct QDeclarativeValueTypeObject : public QScriptDeclarativeClass::Object { + enum Type { Reference, Copy }; + QDeclarativeValueTypeObject(Type t) : objectType(t) {} + Type objectType; QDeclarativeValueType *type; +}; + +struct QDeclarativeValueTypeReference : public QDeclarativeValueTypeObject { + QDeclarativeValueTypeReference() : QDeclarativeValueTypeObject(Reference) {} QDeclarativeGuard<QObject> object; int property; }; +struct QDeclarativeValueTypeCopy : public QDeclarativeValueTypeObject { + QDeclarativeValueTypeCopy() : QDeclarativeValueTypeObject(Copy) {} + QVariant value; +}; + QDeclarativeValueTypeScriptClass::QDeclarativeValueTypeScriptClass(QDeclarativeEngine *bindEngine) : QScriptDeclarativeClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine) { @@ -73,40 +85,67 @@ QScriptValue QDeclarativeValueTypeScriptClass::newObject(QObject *object, int co return QScriptDeclarativeClass::newObject(scriptEngine, this, ref); } +QScriptValue QDeclarativeValueTypeScriptClass::newObject(const QVariant &v, QDeclarativeValueType *type) +{ + QDeclarativeValueTypeCopy *copy = new QDeclarativeValueTypeCopy; + copy->type = type; + copy->value = v; + QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine); + return QScriptDeclarativeClass::newObject(scriptEngine, this, copy); +} + QScriptClass::QueryFlags QDeclarativeValueTypeScriptClass::queryProperty(Object *obj, const Identifier &name, - QScriptClass::QueryFlags) + QScriptClass::QueryFlags) { - QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); + QDeclarativeValueTypeObject *o = static_cast<QDeclarativeValueTypeObject *>(obj); m_lastIndex = -1; - if (!ref->object) - return 0; - QByteArray propName = toString(name).toUtf8(); - m_lastIndex = ref->type->metaObject()->indexOfProperty(propName.constData()); + m_lastIndex = o->type->metaObject()->indexOfProperty(propName.constData()); if (m_lastIndex == -1) return 0; - QMetaProperty prop = ref->object->metaObject()->property(m_lastIndex); + QScriptClass::QueryFlags rv = 0; + + if (o->objectType == QDeclarativeValueTypeObject::Reference) { + QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(o); + + if (!ref->object) + return 0; - QScriptClass::QueryFlags rv = - QScriptClass::HandlesReadAccess; - if (prop.isWritable()) - rv |= QScriptClass::HandlesWriteAccess; + QMetaProperty prop = ref->object->metaObject()->property(m_lastIndex); + + rv = QScriptClass::HandlesReadAccess; + if (prop.isWritable()) + rv |= QScriptClass::HandlesWriteAccess; + } else { + rv = QScriptClass::HandlesReadAccess | QScriptClass::HandlesWriteAccess; + } return rv; } QDeclarativeValueTypeScriptClass::Value QDeclarativeValueTypeScriptClass::property(Object *obj, const Identifier &) { - QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); + QDeclarativeValueTypeObject *o = static_cast<QDeclarativeValueTypeObject *>(obj); - QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); - ref->type->read(ref->object, ref->property); - QVariant rv = p.read(ref->type); + QVariant rv; + if (o->objectType == QDeclarativeValueTypeObject::Reference) { + QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); + + QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); + ref->type->read(ref->object, ref->property); + rv = p.read(ref->type); + } else { + QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj); + + QMetaProperty p = copy->type->metaObject()->property(m_lastIndex); + copy->type->setValue(copy->value); + rv = p.read(copy->type); + } QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine); return Value(scriptEngine, static_cast<QDeclarativeEnginePrivate *>(QObjectPrivate::get(engine))->scriptValueFromVariant(rv)); @@ -115,33 +154,53 @@ QDeclarativeValueTypeScriptClass::Value QDeclarativeValueTypeScriptClass::proper void QDeclarativeValueTypeScriptClass::setProperty(Object *obj, const Identifier &, const QScriptValue &value) { - QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); - - QDeclarativeAbstractBinding *delBinding = - QDeclarativePropertyPrivate::setBinding(ref->object, ref->property, m_lastIndex, 0); - if (delBinding) - delBinding->destroy(); + QDeclarativeValueTypeObject *o = static_cast<QDeclarativeValueTypeObject *>(obj); QVariant v = QDeclarativeEnginePrivate::get(engine)->scriptValueToVariant(value); - ref->type->read(ref->object, ref->property); - QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); - p.write(ref->type, v); - ref->type->write(ref->object, ref->property, 0); + if (o->objectType == QDeclarativeValueTypeObject::Reference) { + QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); + + QDeclarativeAbstractBinding *delBinding = + QDeclarativePropertyPrivate::setBinding(ref->object, ref->property, m_lastIndex, 0); + if (delBinding) + delBinding->destroy(); + + ref->type->read(ref->object, ref->property); + QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); + p.write(ref->type, v); + ref->type->write(ref->object, ref->property, 0); + } else { + QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj); + copy->type->setValue(copy->value); + QMetaProperty p = copy->type->metaObject()->property(m_lastIndex); + p.write(copy->type, v); + copy->value = copy->type->value(); + } } QVariant QDeclarativeValueTypeScriptClass::toVariant(Object *obj, bool *ok) { - QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); + QDeclarativeValueTypeObject *o = static_cast<QDeclarativeValueTypeObject *>(obj); - if (ok) *ok = true; + if (o->objectType == QDeclarativeValueTypeObject::Reference) { + QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); - if (ref->object) { - ref->type->read(ref->object, ref->property); - return ref->type->value(); + if (ok) *ok = true; + + if (ref->object) { + ref->type->read(ref->object, ref->property); + return ref->type->value(); + } } else { - return QVariant(); + QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj); + + if (ok) *ok = true; + + return copy->value; } + + return QVariant(); } QVariant QDeclarativeValueTypeScriptClass::toVariant(const QScriptValue &value) diff --git a/src/declarative/qml/qdeclarativevaluetypescriptclass_p.h b/src/declarative/qml/qdeclarativevaluetypescriptclass_p.h index 2bbb61f..9dafa99 100644 --- a/src/declarative/qml/qdeclarativevaluetypescriptclass_p.h +++ b/src/declarative/qml/qdeclarativevaluetypescriptclass_p.h @@ -67,6 +67,7 @@ public: ~QDeclarativeValueTypeScriptClass(); QScriptValue newObject(QObject *object, int coreIndex, QDeclarativeValueType *); + QScriptValue newObject(const QVariant &, QDeclarativeValueType *); virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags); |