From 20098f931b6c19f6caf1fc1fc23297d14cb9e707 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Tue, 12 Jan 2010 11:13:55 +1000 Subject: Don't double enter states at startup. QT-2697 --- src/declarative/util/qmlstategroup.cpp | 34 +++++++++++++++------- src/declarative/util/qmlstategroup_p.h | 2 +- .../states/data/autoStateAtStartupRestoreBug.qml | 18 ++++++++++++ tests/auto/declarative/states/tst_states.cpp | 18 ++++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 tests/auto/declarative/states/data/autoStateAtStartupRestoreBug.qml diff --git a/src/declarative/util/qmlstategroup.cpp b/src/declarative/util/qmlstategroup.cpp index 373d457..d4db2b9 100644 --- a/src/declarative/util/qmlstategroup.cpp +++ b/src/declarative/util/qmlstategroup.cpp @@ -88,7 +88,7 @@ public: QmlTransition *findTransition(const QString &from, const QString &to); void setCurrentStateInternal(const QString &state, bool = false); - void updateAutoState(); + bool updateAutoState(); }; /*! @@ -231,25 +231,30 @@ void QmlStateGroup::componentComplete() { Q_D(QmlStateGroup); d->componentComplete = true; - d->updateAutoState(); - if (!d->currentState.isEmpty()) { + + if (d->updateAutoState()) { + return; + } else if (!d->currentState.isEmpty()) { QString cs = d->currentState; d->currentState = QString(); d->setCurrentStateInternal(cs, true); } } -void QmlStateGroup::updateAutoState() +/*! + Returns true if the state was changed, otherwise false. +*/ +bool QmlStateGroup::updateAutoState() { Q_D(QmlStateGroup); - d->updateAutoState(); + return d->updateAutoState(); } -void QmlStateGroupPrivate::updateAutoState() +bool QmlStateGroupPrivate::updateAutoState() { Q_Q(QmlStateGroup); if (!componentComplete) - return; + return false; bool revert = false; for (int ii = 0; ii < states.count(); ++ii) { @@ -260,16 +265,25 @@ void QmlStateGroupPrivate::updateAutoState() if (stateChangeDebug()) qWarning() << "Setting auto state due to:" << state->when()->expression(); - q->setState(state->name()); - return; + if (currentState != state->name()) { + q->setState(state->name()); + return true; + } else { + return false; + } } else if (state->name() == currentState) { revert = true; } } } } - if (revert) + if (revert) { + bool rv = currentState != QString(); q->setState(QString()); + return rv; + } else { + return false; + } } QmlTransition *QmlStateGroupPrivate::findTransition(const QString &from, const QString &to) diff --git a/src/declarative/util/qmlstategroup_p.h b/src/declarative/util/qmlstategroup_p.h index 82cc504..112c9eb 100644 --- a/src/declarative/util/qmlstategroup_p.h +++ b/src/declarative/util/qmlstategroup_p.h @@ -82,7 +82,7 @@ Q_SIGNALS: private: friend class QmlState; - void updateAutoState(); + bool updateAutoState(); void removeState(QmlState *state); }; diff --git a/tests/auto/declarative/states/data/autoStateAtStartupRestoreBug.qml b/tests/auto/declarative/states/data/autoStateAtStartupRestoreBug.qml new file mode 100644 index 0000000..693a5c5 --- /dev/null +++ b/tests/auto/declarative/states/data/autoStateAtStartupRestoreBug.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Item { + id: root + property int input: 1 + property int test: 9 + + states: [ + State { + name: "portrait" + when: root.input == 1 + PropertyChanges { + target: root + test: 3 + } + } + ] +} diff --git a/tests/auto/declarative/states/tst_states.cpp b/tests/auto/declarative/states/tst_states.cpp index 9d9cf07..3301048 100644 --- a/tests/auto/declarative/states/tst_states.cpp +++ b/tests/auto/declarative/states/tst_states.cpp @@ -70,6 +70,7 @@ private slots: void explicitChanges(); void propertyErrors(); void incorrectRestoreBug(); + void autoStateAtStartupRestoreBug(); void deletingChange(); void deletingState(); void tempState(); @@ -743,6 +744,23 @@ void tst_states::incorrectRestoreBug() QCOMPARE(rect->color(),QColor("green")); } +void tst_states::autoStateAtStartupRestoreBug() +{ + QmlEngine engine; + + QmlComponent component(&engine, SRCDIR "/data/autoStateAtStartupRestoreBug.qml"); + QObject *obj = component.create(); + + QVERIFY(obj != 0); + QCOMPARE(obj->property("test").toInt(), 3); + + obj->setProperty("input", 2); + + QCOMPARE(obj->property("test").toInt(), 9); + + delete obj; +} + void tst_states::deletingChange() { QmlEngine engine; -- cgit v0.12