diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-06 02:44:19 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-06 02:44:19 (GMT) |
commit | 8e94e7d95f7fbc142a1a5faf3347aa165ff2426e (patch) | |
tree | fec7142714ee52afcb19e41fc1d66d787e6d4c09 /src/declarative/qml | |
parent | bce122040d2df5ae650e6de17d41568f1ba7c184 (diff) | |
download | Qt-8e94e7d95f7fbc142a1a5faf3347aa165ff2426e.zip Qt-8e94e7d95f7fbc142a1a5faf3347aa165ff2426e.tar.gz Qt-8e94e7d95f7fbc142a1a5faf3347aa165ff2426e.tar.bz2 |
Update valuetype tests
Add a new test, and make the old variantCopy pass
Diffstat (limited to 'src/declarative/qml')
-rw-r--r-- | src/declarative/qml/qmlengine.cpp | 17 | ||||
-rw-r--r-- | src/declarative/qml/qmlvaluetypescriptclass.cpp | 101 | ||||
-rw-r--r-- | src/declarative/qml/qmlvaluetypescriptclass_p.h | 18 |
3 files changed, 56 insertions, 80 deletions
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index e46205d..177818f 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -908,7 +908,7 @@ QVariant QmlEnginePrivate::scriptValueToVariant(const QScriptValue &val) else if (dc == contextClass) return QVariant(); - QScriptClass *sc = val.scriptClass(); + QScriptDeclarativeClass *sc = QScriptDeclarativeClass::scriptClass(val); if (!sc) { return val.toVariant(); } else if (sc == valueTypeClass) { @@ -929,20 +929,7 @@ QVariant QmlScriptClass::toVariant(QmlEngine *engine, const QScriptValue &val) QmlEnginePrivate *ep = static_cast<QmlEnginePrivate *>(QObjectPrivate::get(engine)); - QScriptDeclarativeClass *dc = QScriptDeclarativeClass::scriptClass(val); - if (dc == ep->objectClass) - return QVariant::fromValue(ep->objectClass->toQObject(val)); - else if (dc == ep->contextClass) - return QVariant(); - - QScriptClass *sc = val.scriptClass(); - if (!sc) { - return val.toVariant(); - } else if (sc == ep->valueTypeClass) { - return ep->valueTypeClass->toVariant(val); - } - - return QVariant(); + return ep->scriptValueToVariant(val); } // XXX this beyonds in QUrl::toLocalFile() diff --git a/src/declarative/qml/qmlvaluetypescriptclass.cpp b/src/declarative/qml/qmlvaluetypescriptclass.cpp index 0c30992..e939e80 100644 --- a/src/declarative/qml/qmlvaluetypescriptclass.cpp +++ b/src/declarative/qml/qmlvaluetypescriptclass.cpp @@ -44,15 +44,14 @@ QT_BEGIN_NAMESPACE -struct QmlValueTypeReference { +struct QmlValueTypeReference : public QScriptDeclarativeClass::Object { QmlValueType *type; QGuard<QObject> object; int property; }; -Q_DECLARE_METATYPE(QmlValueTypeReference); QmlValueTypeScriptClass::QmlValueTypeScriptClass(QmlEngine *bindEngine) -: QScriptClass(QmlEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine) +: QScriptDeclarativeClass(QmlEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine) { } @@ -62,89 +61,83 @@ QmlValueTypeScriptClass::~QmlValueTypeScriptClass() QScriptValue QmlValueTypeScriptClass::newObject(QObject *object, int coreIndex, QmlValueType *type) { - QmlValueTypeReference ref = { type, object, coreIndex }; - QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(engine); - return scriptEngine->newObject(this, scriptEngine->newVariant(qVariantFromValue(ref))); + QmlValueTypeReference *ref = new QmlValueTypeReference; + ref->type = type; + ref->object = object; + ref->property = coreIndex; + return QScriptDeclarativeClass::newObject(QmlEnginePrivate::getScriptEngine(engine), this, ref); } -QmlValueTypeScriptClass::QueryFlags -QmlValueTypeScriptClass::queryProperty(const QScriptValue &object, - const QScriptString &name, - QueryFlags flags, uint *id) +QScriptClass::QueryFlags +QmlValueTypeScriptClass::queryProperty(Object *obj, const Identifier &name, + QScriptClass::QueryFlags) { - Q_UNUSED(flags); - QmlValueTypeReference ref = - qvariant_cast<QmlValueTypeReference>(object.data().toVariant()); + QmlValueTypeReference *ref = static_cast<QmlValueTypeReference *>(obj); - if (!ref.object) + m_lastIndex = -1; + + if (!ref->object) return 0; - QByteArray propName = name.toString().toUtf8(); + QByteArray propName = toString(name).toUtf8(); - int idx = ref.type->metaObject()->indexOfProperty(propName.constData()); - if (idx == -1) + m_lastIndex = ref->type->metaObject()->indexOfProperty(propName.constData()); + if (m_lastIndex == -1) return 0; - *id = idx; - QMetaProperty prop = ref.object->metaObject()->property(idx); + QMetaProperty prop = ref->object->metaObject()->property(m_lastIndex); - QmlValueTypeScriptClass::QueryFlags rv = - QmlValueTypeScriptClass::HandlesReadAccess; + QScriptClass::QueryFlags rv = + QScriptClass::HandlesReadAccess; if (prop.isWritable()) - rv |= QmlValueTypeScriptClass::HandlesWriteAccess; + rv |= QScriptClass::HandlesWriteAccess; return rv; } -QScriptValue QmlValueTypeScriptClass::property(const QScriptValue &object, - const QScriptString &name, - uint id) +QScriptValue QmlValueTypeScriptClass::property(Object *obj, const Identifier &) { - Q_UNUSED(name); - QmlValueTypeReference ref = - qvariant_cast<QmlValueTypeReference>(object.data().toVariant()); - - if (!ref.object) - return QScriptValue(); + QmlValueTypeReference *ref = static_cast<QmlValueTypeReference *>(obj); - ref.type->read(ref.object, ref.property); - - QMetaProperty p = ref.type->metaObject()->property(id); - QVariant rv = p.read(ref.type); + QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); + ref->type->read(ref->object, ref->property); + QVariant rv = p.read(ref->type); return static_cast<QmlEnginePrivate *>(QObjectPrivate::get(engine))->scriptValueFromVariant(rv); } -void QmlValueTypeScriptClass::setProperty(QScriptValue &object, - const QScriptString &name, - uint id, +void QmlValueTypeScriptClass::setProperty(Object *obj, const Identifier &, const QScriptValue &value) { - Q_UNUSED(name); - QmlValueTypeReference ref = - qvariant_cast<QmlValueTypeReference>(object.data().toVariant()); - - if (!ref.object) - return; + QmlValueTypeReference *ref = static_cast<QmlValueTypeReference *>(obj); QVariant v = QmlScriptClass::toVariant(engine, value); - ref.type->read(ref.object, ref.property); - QMetaProperty p = ref.type->metaObject()->property(id); - p.write(ref.type, v); - ref.type->write(ref.object, ref.property, 0); + 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); } -QVariant QmlValueTypeScriptClass::toVariant(const QScriptValue &val) +QVariant QmlValueTypeScriptClass::toVariant(Object *obj, bool *ok) { - QmlValueTypeReference ref = - qvariant_cast<QmlValueTypeReference>(val.data().toVariant()); + QmlValueTypeReference *ref = static_cast<QmlValueTypeReference *>(obj); - if (!ref.object) + if (ok) *ok = true; + + if (ref->object) { + ref->type->read(ref->object, ref->property); + return ref->type->value(); + } else { return QVariant(); + } +} + +QVariant QmlValueTypeScriptClass::toVariant(const QScriptValue &value) +{ + Q_ASSERT(scriptClass(value) == this); - QMetaProperty p = ref.object->metaObject()->property(ref.property); - return p.read(ref.object); + return toVariant(object(value), 0); } QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlvaluetypescriptclass_p.h b/src/declarative/qml/qmlvaluetypescriptclass_p.h index bd31ec1..19020b2 100644 --- a/src/declarative/qml/qmlvaluetypescriptclass_p.h +++ b/src/declarative/qml/qmlvaluetypescriptclass_p.h @@ -59,7 +59,7 @@ QT_BEGIN_NAMESPACE class QmlEngine; class QmlValueType; -class QmlValueTypeScriptClass : public QScriptClass +class QmlValueTypeScriptClass : public QScriptDeclarativeClass { public: QmlValueTypeScriptClass(QmlEngine *); @@ -67,20 +67,16 @@ public: QScriptValue newObject(QObject *object, int coreIndex, QmlValueType *); - virtual QueryFlags queryProperty(const QScriptValue &object, - const QScriptString &name, - QueryFlags flags, uint *id); - virtual QScriptValue property(const QScriptValue &object, - const QScriptString &name, - uint id); - virtual void setProperty(QScriptValue &object, - const QScriptString &name, - uint id, - const QScriptValue &value); + virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, + QScriptClass::QueryFlags flags); + virtual QScriptValue property(Object *, const Identifier &); + virtual void setProperty(Object *, const Identifier &name, const QScriptValue &); + virtual QVariant toVariant(Object *, bool *ok = 0); QVariant toVariant(const QScriptValue &); private: QmlEngine *engine; + int m_lastIndex; }; QT_END_NAMESPACE |