summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2011-01-27 05:53:45 (GMT)
committerBea Lam <bea.lam@nokia.com>2011-01-27 06:23:53 (GMT)
commit80f74d3801ddece4d34b4a663bf1bdc23ecc4a67 (patch)
tree1777c3533e520dabd7a2635e3ef020f492a6684a
parent43b8305367156c1ceb09eb4a056bdae3f325b5eb (diff)
downloadQt-80f74d3801ddece4d34b4a663bf1bdc23ecc4a67.zip
Qt-80f74d3801ddece4d34b4a663bf1bdc23ecc4a67.tar.gz
Qt-80f74d3801ddece4d34b4a663bf1bdc23ecc4a67.tar.bz2
Don't load components until the Loader component itself is completed
Previously components were loaded as soon as setSource() or setSourceComponent() were called, even if the Loader component was not complete. If a source component had been set, the itemChanged(), statusChanged() etc. signals would be emitted before the Loader would be complete and before the internal item had been sized. If a source had been set via url, these signals were not emitted at all. It also caused loaded() to be emitted twice for components set by setSourceComponent(). Task-number: QTBUG-16319 Reviewed-by: Martin Jones
-rw-r--r--src/declarative/graphicsitems/qdeclarativeloader.cpp57
-rw-r--r--src/declarative/graphicsitems/qdeclarativeloader_p_p.h1
-rw-r--r--tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp94
3 files changed, 86 insertions, 66 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp
index 242435c..871850d 100644
--- a/src/declarative/graphicsitems/qdeclarativeloader.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp
@@ -262,6 +262,7 @@ void QDeclarativeLoader::setSource(const QUrl &url)
d->clear();
d->source = url;
+
if (d->source.isEmpty()) {
emit sourceChanged();
emit statusChanged();
@@ -272,18 +273,9 @@ void QDeclarativeLoader::setSource(const QUrl &url)
d->component = new QDeclarativeComponent(qmlEngine(this), d->source, this);
d->ownComponent = true;
- if (!d->component->isLoading()) {
- d->_q_sourceLoaded();
- } else {
- connect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
- this, SLOT(_q_sourceLoaded()));
- connect(d->component, SIGNAL(progressChanged(qreal)),
- this, SIGNAL(progressChanged()));
- emit statusChanged();
- emit progressChanged();
- emit sourceChanged();
- emit itemChanged();
- }
+
+ if (isComponentComplete())
+ d->load();
}
/*!
@@ -324,6 +316,7 @@ void QDeclarativeLoader::setSourceComponent(QDeclarativeComponent *comp)
d->component = comp;
d->ownComponent = false;
+
if (!d->component) {
emit sourceChanged();
emit statusChanged();
@@ -332,18 +325,8 @@ void QDeclarativeLoader::setSourceComponent(QDeclarativeComponent *comp)
return;
}
- if (!d->component->isLoading()) {
- d->_q_sourceLoaded();
- } else {
- connect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
- this, SLOT(_q_sourceLoaded()));
- connect(d->component, SIGNAL(progressChanged(qreal)),
- this, SIGNAL(progressChanged()));
- emit progressChanged();
- emit sourceChanged();
- emit statusChanged();
- emit itemChanged();
- }
+ if (isComponentComplete())
+ d->load();
}
void QDeclarativeLoader::resetSourceComponent()
@@ -351,6 +334,27 @@ void QDeclarativeLoader::resetSourceComponent()
setSourceComponent(0);
}
+void QDeclarativeLoaderPrivate::load()
+{
+ Q_Q(QDeclarativeLoader);
+
+ if (!q->isComponentComplete() || !component)
+ return;
+
+ if (!component->isLoading()) {
+ _q_sourceLoaded();
+ } else {
+ QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
+ q, SLOT(_q_sourceLoaded()));
+ QObject::connect(component, SIGNAL(progressChanged(qreal)),
+ q, SIGNAL(progressChanged()));
+ emit q->statusChanged();
+ emit q->progressChanged();
+ emit q->sourceChanged();
+ emit q->itemChanged();
+ }
+}
+
void QDeclarativeLoaderPrivate::_q_sourceLoaded()
{
Q_Q(QDeclarativeLoader);
@@ -465,9 +469,10 @@ QDeclarativeLoader::Status QDeclarativeLoader::status() const
void QDeclarativeLoader::componentComplete()
{
+ Q_D(QDeclarativeLoader);
+
QDeclarativeItem::componentComplete();
- if (status() == Ready)
- emit loaded();
+ d->load();
}
diff --git a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h
index 2239b16..e366a14 100644
--- a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h
@@ -72,6 +72,7 @@ public:
void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
void clear();
void initResize();
+ void load();
QUrl source;
QGraphicsObject *item;
diff --git a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp
index bfa81ed..358822e 100644
--- a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp
+++ b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp
@@ -69,9 +69,8 @@ public:
tst_QDeclarativeLoader();
private slots:
- void url();
- void invalidUrl();
- void component();
+ void sourceOrComponent();
+ void sourceOrComponent_data();
void clear();
void urlToComponent();
void componentToUrl();
@@ -100,56 +99,71 @@ tst_QDeclarativeLoader::tst_QDeclarativeLoader()
{
}
-void tst_QDeclarativeLoader::url()
+void tst_QDeclarativeLoader::sourceOrComponent()
{
+ QFETCH(QString, sourceDefinition);
+ QFETCH(QUrl, sourceUrl);
+ QFETCH(QString, errorString);
+
+ bool error = !errorString.isEmpty();
+ if (error)
+ QTest::ignoreMessage(QtWarningMsg, errorString.toUtf8().constData());
+
QDeclarativeComponent component(&engine);
- component.setData(QByteArray("import QtQuick 1.0\nLoader { property int did_load: 0; onLoaded: did_load=123; source: \"Rect120x60.qml\" }"), TEST_FILE(""));
+ component.setData(QByteArray(
+ "import QtQuick 1.0\n"
+ "Loader {\n"
+ " property int onItemChangedCount: 0\n"
+ " property int onSourceChangedCount: 0\n"
+ " property int onStatusChangedCount: 0\n"
+ " property int onProgressChangedCount: 0\n"
+ " property int onLoadedCount: 0\n")
+ + sourceDefinition.toUtf8()
+ + QByteArray(
+ " onItemChanged: onItemChangedCount += 1\n"
+ " onSourceChanged: onSourceChangedCount += 1\n"
+ " onStatusChanged: onStatusChangedCount += 1\n"
+ " onProgressChanged: onProgressChangedCount += 1\n"
+ " onLoaded: onLoadedCount += 1\n"
+ "}")
+ , TEST_FILE(""));
+
QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
QVERIFY(loader != 0);
- QVERIFY(loader->item());
- QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml"));
+ QCOMPARE(loader->item() == 0, error);
+ QCOMPARE(loader->source(), sourceUrl);
QCOMPARE(loader->progress(), 1.0);
- QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
- QCOMPARE(loader->property("did_load").toInt(), 123);
- QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
- delete loader;
-}
+ QCOMPARE(loader->status(), error ? QDeclarativeLoader::Error : QDeclarativeLoader::Ready);
+ QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), error ? 0: 1);
-void tst_QDeclarativeLoader::component()
-{
- QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
- QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
- QVERIFY(item);
+ if (!error) {
+ QDeclarativeComponent *c = qobject_cast<QDeclarativeComponent*>(loader->QGraphicsObject::children().at(0));
+ QVERIFY(c);
+ QCOMPARE(loader->sourceComponent(), c);
+ }
- QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1));
- QVERIFY(loader);
- QVERIFY(loader->item());
- QCOMPARE(loader->progress(), 1.0);
- QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
- QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
+ QCOMPARE(loader->property("onSourceChangedCount").toInt(), 1);
+ QCOMPARE(loader->property("onStatusChangedCount").toInt(), 1);
+ QCOMPARE(loader->property("onProgressChangedCount").toInt(), 1);
- QDeclarativeComponent *c = qobject_cast<QDeclarativeComponent*>(item->QGraphicsObject::children().at(0));
- QVERIFY(c);
- QCOMPARE(loader->sourceComponent(), c);
+ QCOMPARE(loader->property("onItemChangedCount").toInt(), error ? 0 : 1);
+ QCOMPARE(loader->property("onLoadedCount").toInt(), error ? 0 : 1);
- delete item;
+ delete loader;
}
-void tst_QDeclarativeLoader::invalidUrl()
+void tst_QDeclarativeLoader::sourceOrComponent_data()
{
- QTest::ignoreMessage(QtWarningMsg, QString(QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString() + ": File not found").toUtf8().constData());
+ QTest::addColumn<QString>("sourceDefinition");
+ QTest::addColumn<QUrl>("sourceUrl");
+ QTest::addColumn<QString>("errorString");
- QDeclarativeComponent component(&engine);
- component.setData(QByteArray("import QtQuick 1.0\nLoader { source: \"IDontExist.qml\" }"), TEST_FILE(""));
- QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
- QVERIFY(loader != 0);
- QVERIFY(loader->item() == 0);
- QCOMPARE(loader->progress(), 1.0);
- QCOMPARE(loader->status(), QDeclarativeLoader::Error);
- QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
+ 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() << "";
- delete loader;
+ QTest::newRow("invalid source") << "source: 'IDontExist.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml")
+ << QString(QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString() + ": File not found");
}
void tst_QDeclarativeLoader::clear()
@@ -446,7 +460,7 @@ void tst_QDeclarativeLoader::networkRequestUrl()
server.serveDirectory(SRCDIR "/data");
QDeclarativeComponent component(&engine);
- component.setData(QByteArray("import QtQuick 1.0\nLoader { property int did_load : 0; source: \"http://127.0.0.1:14450/Rect120x60.qml\"; onLoaded: did_load=123 }"), QUrl::fromLocalFile(SRCDIR "/dummy.qml"));
+ component.setData(QByteArray("import QtQuick 1.0\nLoader { property int signalCount : 0; source: \"http://127.0.0.1:14450/Rect120x60.qml\"; onLoaded: signalCount += 1 }"), QUrl::fromLocalFile(SRCDIR "/dummy.qml"));
if (component.isError())
qDebug() << component.errors();
QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
@@ -456,7 +470,7 @@ void tst_QDeclarativeLoader::networkRequestUrl()
QVERIFY(loader->item());
QCOMPARE(loader->progress(), 1.0);
- QCOMPARE(loader->property("did_load").toInt(), 123);
+ QCOMPARE(loader->property("signalCount").toInt(), 1);
QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
delete loader;