From da56d7c25ce344128d827cfa2ed26f9eea437e4d Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 9 Apr 2010 13:42:22 +1000 Subject: Fix url resolution in PropertyChanges. Make sure bindings in PropertyChanges resolve urls correctly. Also refactor the code so that PropertyChanges will always use the standard url resolution support provided by QDeclarativeProperty. Task-number: QTBUG-9571 --- .../util/qdeclarativepropertychanges.cpp | 13 +++------ src/declarative/util/qdeclarativestate.cpp | 12 ++++++++ src/declarative/util/qdeclarativestate_p.h | 2 ++ .../data/Implementation/MyType.qml | 32 +++++++++++++++++++++ .../data/Implementation/images/qt-logo.png | Bin 0 -> 5149 bytes .../qdeclarativestates/data/urlResolution.qml | 12 ++++++++ .../qdeclarativestates/tst_qdeclarativestates.cpp | 23 +++++++++++++++ 7 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativestates/data/Implementation/MyType.qml create mode 100644 tests/auto/declarative/qdeclarativestates/data/Implementation/images/qt-logo.png create mode 100644 tests/auto/declarative/qdeclarativestates/data/urlResolution.qml diff --git a/src/declarative/util/qdeclarativepropertychanges.cpp b/src/declarative/util/qdeclarativepropertychanges.cpp index ecbd71e..9c3ee9f 100644 --- a/src/declarative/util/qdeclarativepropertychanges.cpp +++ b/src/declarative/util/qdeclarativepropertychanges.cpp @@ -377,7 +377,7 @@ QDeclarativeProperty QDeclarativePropertyChangesPrivate::property(const QByteArray &property) { Q_Q(QDeclarativePropertyChanges); - QDeclarativeProperty prop(object, QString::fromUtf8(property)); + QDeclarativeProperty prop(object, QString::fromUtf8(property), qmlContext(q)); if (!prop.isValid()) { qmlInfo(q) << QDeclarativePropertyChanges::tr("Cannot assign to non-existent property \"%1\"").arg(QString::fromUtf8(property)); return QDeclarativeProperty(); @@ -400,16 +400,11 @@ QDeclarativePropertyChanges::ActionList QDeclarativePropertyChanges::actions() QByteArray property = d->properties.at(ii).first; - QDeclarativeAction a(d->object, QString::fromLatin1(property), - d->properties.at(ii).second); + QDeclarativeAction a(d->object, QString::fromUtf8(property), + qmlContext(this), d->properties.at(ii).second); if (a.property.isValid()) { a.restore = restoreEntryValues(); - - if (a.property.propertyType() == QVariant::Url && - (a.toValue.userType() == QVariant::String || a.toValue.userType() == QVariant::ByteArray) && !a.toValue.isNull()) - a.toValue.setValue(qmlContext(this)->resolvedUrl(QUrl(a.toValue.toString()))); - list << a; } } @@ -436,7 +431,7 @@ QDeclarativePropertyChanges::ActionList QDeclarativePropertyChanges::actions() a.property = prop; a.fromValue = a.property.read(); a.specifiedObject = d->object; - a.specifiedProperty = QString::fromLatin1(property); + a.specifiedProperty = QString::fromUtf8(property); if (d->isExplicit) { a.toValue = d->expressions.at(ii).second->value(); diff --git a/src/declarative/util/qdeclarativestate.cpp b/src/declarative/util/qdeclarativestate.cpp index e4c968e..78813fa 100644 --- a/src/declarative/util/qdeclarativestate.cpp +++ b/src/declarative/util/qdeclarativestate.cpp @@ -74,6 +74,18 @@ QDeclarativeAction::QDeclarativeAction(QObject *target, const QString &propertyN fromValue = property.read(); } +QDeclarativeAction::QDeclarativeAction(QObject *target, const QString &propertyName, + QDeclarativeContext *context, const QVariant &value) +: restore(true), actionDone(false), reverseEvent(false), deletableToBinding(false), + property(target, propertyName, context), toValue(value), + fromBinding(0), toBinding(0), event(0), + specifiedObject(target), specifiedProperty(propertyName) +{ + if (property.isValid()) + fromValue = property.read(); +} + + QDeclarativeActionEvent::~QDeclarativeActionEvent() { } diff --git a/src/declarative/util/qdeclarativestate_p.h b/src/declarative/util/qdeclarativestate_p.h index ee2b7e8..472897e 100644 --- a/src/declarative/util/qdeclarativestate_p.h +++ b/src/declarative/util/qdeclarativestate_p.h @@ -61,6 +61,8 @@ class Q_DECLARATIVE_EXPORT QDeclarativeAction public: QDeclarativeAction(); QDeclarativeAction(QObject *, const QString &, const QVariant &); + QDeclarativeAction(QObject *, const QString &, + QDeclarativeContext *, const QVariant &); bool restore:1; bool actionDone:1; diff --git a/tests/auto/declarative/qdeclarativestates/data/Implementation/MyType.qml b/tests/auto/declarative/qdeclarativestates/data/Implementation/MyType.qml new file mode 100644 index 0000000..1872de8 --- /dev/null +++ b/tests/auto/declarative/qdeclarativestates/data/Implementation/MyType.qml @@ -0,0 +1,32 @@ +import Qt 4.7 + +Item { + Column { + anchors.centerIn: parent + Image { id: image1; objectName: "image1" } + Image { id: image2; objectName: "image2" } + Image { id: image3; objectName: "image3" } + } + + states: State { + name: "SetImageState" + PropertyChanges { + target: image1 + source: "images/qt-logo.png" + } + PropertyChanges { + target: image2 + source: "images/" + "qt-logo.png" + } + PropertyChanges { + target: image3 + source: "images/" + (true ? "qt-logo.png" : "") + } + } + + MouseArea { + anchors.fill: parent + onClicked: parent.state = "SetImageState" + } + +} diff --git a/tests/auto/declarative/qdeclarativestates/data/Implementation/images/qt-logo.png b/tests/auto/declarative/qdeclarativestates/data/Implementation/images/qt-logo.png new file mode 100644 index 0000000..14ddf2a Binary files /dev/null and b/tests/auto/declarative/qdeclarativestates/data/Implementation/images/qt-logo.png differ diff --git a/tests/auto/declarative/qdeclarativestates/data/urlResolution.qml b/tests/auto/declarative/qdeclarativestates/data/urlResolution.qml new file mode 100644 index 0000000..8995b56 --- /dev/null +++ b/tests/auto/declarative/qdeclarativestates/data/urlResolution.qml @@ -0,0 +1,12 @@ +import Qt 4.7 +import "Implementation" + +Rectangle { + width: 100 + height: 200 + + MyType { + objectName: "MyType" + anchors.fill: parent + } +} diff --git a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp index f0b6759..578bcb4 100644 --- a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp +++ b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -109,6 +110,7 @@ private slots: void reset(); void illegalObjectCreation(); void whenOrdering(); + void urlResolution(); }; void tst_qdeclarativestates::initTestCase() @@ -1016,6 +1018,27 @@ void tst_qdeclarativestates::whenOrdering() QCOMPARE(rect->state(), QLatin1String("")); } +void tst_qdeclarativestates::urlResolution() +{ + QDeclarativeEngine engine; + + QDeclarativeComponent c(&engine, SRCDIR "/data/urlResolution.qml"); + QDeclarativeRectangle *rect = qobject_cast(c.create()); + QVERIFY(rect != 0); + + QDeclarativeItem *myType = rect->findChild("MyType"); + QDeclarativeImage *image1 = rect->findChild("image1"); + QDeclarativeImage *image2 = rect->findChild("image2"); + QDeclarativeImage *image3 = rect->findChild("image3"); + QVERIFY(myType != 0 && image1 != 0 && image2 != 0 && image3 != 0); + + myType->setState("SetImageState"); + QUrl resolved = QUrl::fromLocalFile(SRCDIR "/data/Implementation/images/qt-logo.png"); + QCOMPARE(image1->source(), resolved); + QCOMPARE(image2->source(), resolved); + QCOMPARE(image3->source(), resolved); +} + QTEST_MAIN(tst_qdeclarativestates) #include "tst_qdeclarativestates.moc" -- cgit v0.12