Add sourceComponentChanged signal to Loader
authorYann Bodson <yann.bodson@nokia.com>
Fri, 29 Jul 2011 04:41:16 +0000 (14:41 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 29 Jul 2011 07:39:41 +0000 (09:39 +0200)
When setting the 'sourceComponent' property in Loader, we emit
a sourceChanged signal with source == "". We should emit
sourceComponentChanged instead.

Task-number: QTBUG-20628
Change-Id: If5f8f389b7dc214e729b366d82fe4d32827408ee
Reviewed-on: http://codereview.qt.nokia.com/2371
Reviewed-by: Martin Jones <martin.jones@nokia.com>
doc/src/declarative/whatsnew.qdoc
src/declarative/items/qsgloader.cpp
src/declarative/items/qsgloader_p.h
tests/auto/declarative/qsgloader/tst_qsgloader.cpp

index f9b6683..d2326fa 100644 (file)
@@ -45,6 +45,10 @@ QDeclarativeScriptString.
 
 Flickable: added dragging, draggingHorizontally and draggingVerically properties.
 
+The Loader element now only emits the \c sourceChanged signal when the source is changed and the
+\c sourceComponentChanged signal when the sourceComponent is changed. It used to emit both signals when one
+of the properties was changed.
+
 \section2 QtQuick 1 is now a separate library and module
 
 Writing C++ applications using QtQuick 1 specific API, i.e. QDeclarativeView or QDeclarativeItem
index 0d1feba..ccf1689 100644 (file)
@@ -165,7 +165,7 @@ void QSGLoader::setSourceComponent(QDeclarativeComponent *comp)
     d->component = comp;
     d->ownComponent = false;
     if (!d->component) {
-        emit sourceChanged();
+        emit sourceComponentChanged();
         emit statusChanged();
         emit progressChanged();
         emit itemChanged();
@@ -197,7 +197,10 @@ void QSGLoaderPrivate::load()
                 q, SIGNAL(progressChanged()));
         emit q->statusChanged();
         emit q->progressChanged();
-        emit q->sourceChanged();
+        if (ownComponent)
+            emit q->sourceChanged();
+        else
+            emit q->sourceComponentChanged();
         emit q->itemChanged();
     }
 }
@@ -209,7 +212,10 @@ void QSGLoaderPrivate::_q_sourceLoaded()
     if (component) {
         if (!component->errors().isEmpty()) {
             QDeclarativeEnginePrivate::warning(qmlEngine(q), component->errors());
-            emit q->sourceChanged();
+            if (ownComponent)
+                emit q->sourceChanged();
+            else
+                emit q->sourceComponentChanged();
             emit q->statusChanged();
             emit q->progressChanged();
             return;
@@ -253,7 +259,10 @@ void QSGLoaderPrivate::_q_sourceLoaded()
             source = QUrl();
         }
         component->completeCreate();
-        emit q->sourceChanged();
+        if (ownComponent)
+            emit q->sourceChanged();
+        else
+            emit q->sourceComponentChanged();
         emit q->statusChanged();
         emit q->progressChanged();
         emit q->itemChanged();
index b8605db..832d3a6 100644 (file)
@@ -58,13 +58,13 @@ class Q_AUTOTEST_EXPORT QSGLoader : public QSGImplicitSizeItem
     Q_ENUMS(Status)
 
     Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
-    Q_PROPERTY(QDeclarativeComponent *sourceComponent READ sourceComponent WRITE setSourceComponent RESET resetSourceComponent NOTIFY sourceChanged)
+    Q_PROPERTY(QDeclarativeComponent *sourceComponent READ sourceComponent WRITE setSourceComponent RESET resetSourceComponent NOTIFY sourceComponentChanged)
     Q_PROPERTY(QSGItem *item READ item NOTIFY itemChanged)
     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
     Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
 
 public:
-    QSGLoader(QSGItem *parent=0);
+    QSGLoader(QSGItem *parent = 0);
     virtual ~QSGLoader();
 
     QUrl source() const;
@@ -83,6 +83,7 @@ public:
 Q_SIGNALS:
     void itemChanged();
     void sourceChanged();
+    void sourceComponentChanged();
     void statusChanged();
     void progressChanged();
     void loaded();
index 89285eb..c7e01da 100644 (file)
@@ -99,6 +99,7 @@ tst_QSGLoader::tst_QSGLoader()
 
 void tst_QSGLoader::sourceOrComponent()
 {
+    QFETCH(QString, sourceOrComponent);
     QFETCH(QString, sourceDefinition);
     QFETCH(QUrl, sourceUrl);
     QFETCH(QString, errorString);
@@ -113,6 +114,7 @@ void tst_QSGLoader::sourceOrComponent()
             "Loader {\n"
             "   property int onItemChangedCount: 0\n"
             "   property int onSourceChangedCount: 0\n"
+            "   property int onSourceComponentChangedCount: 0\n"
             "   property int onStatusChangedCount: 0\n"
             "   property int onProgressChangedCount: 0\n"
             "   property int onLoadedCount: 0\n")
@@ -120,6 +122,7 @@ void tst_QSGLoader::sourceOrComponent()
             + QByteArray(
             "   onItemChanged: onItemChangedCount += 1\n"
             "   onSourceChanged: onSourceChangedCount += 1\n"
+            "   onSourceComponentChanged: onSourceComponentChangedCount += 1\n"
             "   onStatusChanged: onStatusChangedCount += 1\n"
             "   onProgressChanged: onProgressChangedCount += 1\n"
             "   onLoaded: onLoadedCount += 1\n"
@@ -141,7 +144,13 @@ void tst_QSGLoader::sourceOrComponent()
         QCOMPARE(loader->sourceComponent(), c);
     }
 
-    QCOMPARE(loader->property("onSourceChangedCount").toInt(), 1);
+    if (sourceOrComponent == "component") {
+        QCOMPARE(loader->property("onSourceComponentChangedCount").toInt(), 1);
+        QCOMPARE(loader->property("onSourceChangedCount").toInt(), 0);
+    } else {
+        QCOMPARE(loader->property("onSourceComponentChangedCount").toInt(), 0);
+        QCOMPARE(loader->property("onSourceChangedCount").toInt(), 1);
+    }
     QCOMPARE(loader->property("onStatusChangedCount").toInt(), 1);
     QCOMPARE(loader->property("onProgressChangedCount").toInt(), 1);
 
@@ -153,14 +162,14 @@ void tst_QSGLoader::sourceOrComponent()
 
 void tst_QSGLoader::sourceOrComponent_data()
 {
+    QTest::addColumn<QString>("sourceOrComponent");
     QTest::addColumn<QString>("sourceDefinition");
     QTest::addColumn<QUrl>("sourceUrl");
     QTest::addColumn<QString>("errorString");
 
-    QTest::newRow("source") << "source: 'Rect120x60.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml") << "";
-    QTest::newRow("sourceComponent") << "Component { id: comp; Rectangle { width: 100; height: 50 } }\n sourceComponent: comp\n" << QUrl() << "";
-
-    QTest::newRow("invalid source") << "source: 'IDontExist.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml")
+    QTest::newRow("source") << "source" << "source: 'Rect120x60.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml") << "";
+    QTest::newRow("sourceComponent") << "component" << "Component { id: comp; Rectangle { width: 100; height: 50 } }\n sourceComponent: comp\n" << QUrl() << "";
+    QTest::newRow("invalid source") << "source" << "source: 'IDontExist.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml")
             << QString(QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString() + ": File not found");
 }