summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/declarative/qml/qmlengine.cpp17
-rw-r--r--src/declarative/qml/qmlvaluetypescriptclass.cpp101
-rw-r--r--src/declarative/qml/qmlvaluetypescriptclass_p.h18
-rw-r--r--tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml (renamed from tests/auto/declarative/valuetypes/data/variantCopy.qml)0
-rw-r--r--tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml14
-rw-r--r--tests/auto/declarative/valuetypes/tst_valuetypes.cpp26
6 files changed, 92 insertions, 84 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
diff --git a/tests/auto/declarative/valuetypes/data/variantCopy.qml b/tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml
index 691a56c..691a56c 100644
--- a/tests/auto/declarative/valuetypes/data/variantCopy.qml
+++ b/tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml
diff --git a/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml b/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml
new file mode 100644
index 0000000..29157e8
--- /dev/null
+++ b/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml
@@ -0,0 +1,14 @@
+import Test 1.0
+
+MyTypeObject {
+ property var object
+ object: MyTypeObject {
+ rect.x: 19
+ rect.y: 33
+ rect.width: 5
+ rect.height: 99
+ }
+
+ onRunScript: rect = object.rect
+}
+
diff --git a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
index 9821a01..d42bfc5 100644
--- a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
+++ b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp
@@ -71,7 +71,8 @@ private slots:
void valueInterceptors();
void bindingConflict();
void deletedObject();
- void variantCopy();
+ void bindingVariantCopy();
+ void scriptVariantCopy();
void cppClasses();
private:
@@ -524,10 +525,10 @@ void tst_valuetypes::deletedObject()
delete object;
}
-// Test that value types can be assigned to another value type property
-void tst_valuetypes::variantCopy()
+// Test that value types can be assigned to another value type property in a binding
+void tst_valuetypes::bindingVariantCopy()
{
- QmlComponent component(&engine, TEST_FILE("variantCopy.qml"));
+ QmlComponent component(&engine, TEST_FILE("bindingVariantCopy.qml"));
MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
QVERIFY(object != 0);
@@ -536,6 +537,23 @@ void tst_valuetypes::variantCopy()
delete object;
}
+// Test that value types can be assigned to another value type property in script
+void tst_valuetypes::scriptVariantCopy()
+{
+ QmlComponent component(&engine, TEST_FILE("scriptVariantCopy.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+
+ QCOMPARE(object->rect(), QRect(2, 3, 109, 102));
+
+ object->emitRunScript();
+
+ QCOMPARE(object->rect(), QRect(19, 33, 5, 99));
+
+ delete object;
+}
+
+
// Test that the value type classes can be used manually
void tst_valuetypes::cppClasses()
{