diff options
9 files changed, 133 insertions, 41 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); diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index 35b4d99..098ac36 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -1382,7 +1382,6 @@ void tst_qdeclarativeecmascript::callQtInvokables() o.reset(); { QScriptValue ret = engine->evaluate("object.method_NoArgs_QPointF()"); - QVERIFY(ret.isVariant()); QCOMPARE(ret.toVariant(), QVariant(QPointF(123, 4.5))); QCOMPARE(o.error(), false); QCOMPARE(o.invoked(), 3); diff --git a/tests/auto/declarative/qdeclarativevaluetypes/data/returnValues.qml b/tests/auto/declarative/qdeclarativevaluetypes/data/returnValues.qml new file mode 100644 index 0000000..185e7ba --- /dev/null +++ b/tests/auto/declarative/qdeclarativevaluetypes/data/returnValues.qml @@ -0,0 +1,17 @@ +import Test 1.0 +import Qt 4.6 + +MyTypeObject { + property bool test1: false; + property bool test2: false; + + Component.onCompleted: { + var a = method(); + + test1 = (a.width == 13) + test2 = (a.height == 14) + + size = a; + } +} + diff --git a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h index 9057b4f..dd13429 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h +++ b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h @@ -129,6 +129,9 @@ public: signals: void changed(); void runScript(); + +public slots: + QSize method() { return QSize(13, 14); } }; QML_DECLARE_TYPE(MyTypeObject); diff --git a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp index e653abf..b733b10 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp +++ b/tests/auto/declarative/qdeclarativevaluetypes/tst_qdeclarativevaluetypes.cpp @@ -79,6 +79,7 @@ private slots: void cppClasses(); void enums(); void conflictingBindings(); + void returnValues(); private: QDeclarativeEngine engine; @@ -763,6 +764,19 @@ void tst_qdeclarativevaluetypes::conflictingBindings() } } +void tst_qdeclarativevaluetypes::returnValues() +{ + QDeclarativeComponent component(&engine, TEST_FILE("returnValues.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test1").toBool(), true); + QCOMPARE(object->property("test2").toBool(), true); + QCOMPARE(object->property("size").toSize(), QSize(13, 14)); + + delete object; +} + QTEST_MAIN(tst_qdeclarativevaluetypes) #include "tst_qdeclarativevaluetypes.moc" |