Use engine where appropriate in QDeclarativeProperty
authorAaron Kennedy <aaron.kennedy@nokia.com>
Mon, 25 Jul 2011 06:03:57 +0000 (16:03 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 25 Jul 2011 06:05:07 +0000 (08:05 +0200)
Task-number: QTBUG-14697

Change-Id: Id15def75271666699b1fe23e39991710afebff59
Reviewed-on: http://codereview.qt.nokia.com/2077
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/qdeclarativeproperty.cpp
src/declarative/qml/qdeclarativeproperty_p.h
tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp

index 9b7431a..98c7582 100644 (file)
@@ -213,6 +213,13 @@ QDeclarativeProperty::QDeclarativeProperty(QObject *obj, const QString &name, QD
 
 Q_GLOBAL_STATIC(QDeclarativeValueTypeFactory, qmlValueTypes);
 
+QDeclarativeContextData *QDeclarativePropertyPrivate::effectiveContext() const 
+{
+    if (context) return context;
+    else if (engine) return QDeclarativeContextData::get(engine->rootContext());
+    else return 0;
+}
+
 void QDeclarativePropertyPrivate::initProperty(QObject *obj, const QString &name)
 {
     if (!obj) return;
@@ -392,7 +399,7 @@ const char *QDeclarativeProperty::propertyTypeName() const
         return 0;
     if (d->isValueType()) {
 
-        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(d->context);
+        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(d->engine);
         QDeclarativeValueType *valueType = 0;
         if (ep) valueType = ep->valueTypes[d->core.propType];
         else valueType = QDeclarativeValueTypeFactory::valueType(d->core.propType);
@@ -980,7 +987,7 @@ QVariant QDeclarativePropertyPrivate::readValueProperty()
 {
     if (isValueType()) {
 
-        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context);
+        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
         QDeclarativeValueType *valueType = 0;
         if (ep) valueType = ep->valueTypes[core.propType];
         else valueType = QDeclarativeValueTypeFactory::valueType(core.propType);
@@ -1065,7 +1072,7 @@ bool QDeclarativePropertyPrivate::writeValueProperty(const QVariant &value, Writ
 
     bool rv = false;
     if (isValueType()) {
-        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context);
+        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
 
         QDeclarativeValueType *writeBack = 0;
         if (ep) {
@@ -1080,14 +1087,14 @@ bool QDeclarativePropertyPrivate::writeValueProperty(const QVariant &value, Writ
         data.setFlags(valueType.flags);
         data.coreIndex = valueType.valueTypeCoreIdx;
         data.propType = valueType.valueTypePropType;
-        rv = write(writeBack, data, value, context, flags);
+        rv = write(writeBack, data, value, effectiveContext(), flags);
 
         writeBack->write(object, core.coreIndex, flags);
         if (!ep) delete writeBack;
 
     } else {
 
-        rv = write(object, core, value, context, flags);
+        rv = write(object, core, value, effectiveContext(), flags);
 
     }
 
index ea3fb56..d05e155 100644 (file)
@@ -75,6 +75,7 @@ public:
     QDeclarativePropertyPrivate()
         : context(0), engine(0), object(0), isNameCached(false) {}
 
+    inline QDeclarativeContextData *effectiveContext() const;
     QDeclarativeContextData *context;
     QDeclarativeEngine *engine;
     QDeclarativeGuard<QObject> object;
index a150329..5218093 100644 (file)
@@ -133,6 +133,7 @@ private slots:
     // Bugs
     void crashOnValueProperty();
     void aliasPropertyBindings();
+    void noContext();
 
     void copy();
 private:
@@ -1481,6 +1482,22 @@ void tst_qdeclarativeproperty::copy()
     QCOMPARE(p2.propertyType(), (int)QVariant::Int);
 }
 
+void tst_qdeclarativeproperty::noContext()
+{
+    QDeclarativeComponent compA(&engine, TEST_FILE("NoContextTypeA.qml"));
+    QDeclarativeComponent compB(&engine, TEST_FILE("NoContextTypeB.qml"));
+
+    QObject *a = compA.create();
+    QVERIFY(a != 0);
+    QObject *b = compB.create();
+    QVERIFY(b != 0);
+
+    QVERIFY(QDeclarativeProperty::write(b, "myTypeA", QVariant::fromValue(a), &engine));
+
+    delete a;
+    delete b;
+}
+
 void tst_qdeclarativeproperty::initTestCase()
 {
     qmlRegisterType<MyQmlObject>("Test",1,0,"MyQmlObject");
@@ -1488,7 +1505,6 @@ void tst_qdeclarativeproperty::initTestCase()
     qmlRegisterType<MyContainer>("Test",1,0,"MyContainer");
 }
 
-
 QTEST_MAIN(tst_qdeclarativeproperty)
 
 #include "tst_qdeclarativeproperty.moc"