diff options
author | Martin Jones <martin.jones@nokia.com> | 2009-11-25 14:07:15 +1000 |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2009-11-25 14:07:15 +1000 |
commit | 3c5f26079d20ffb6bac924e3686e717f499afbf7 (patch) | |
tree | 2850f4dc533dbbbe8268b9d90ad1fbd36ec3f9b7 | |
parent | 368aa557ab3cf8a5c241890ff2f21e2b890df114 (diff) | |
download | qt4-tools-3c5f26079d20ffb6bac924e3686e717f499afbf7.tar.gz |
Fix changing Timer interval while running.
Task-number: QTBUG-6201
-rw-r--r-- | src/declarative/util/qmltimer.cpp | 30 | ||||
-rw-r--r-- | src/declarative/util/qmltimer_p.h | 2 | ||||
-rw-r--r-- | tests/auto/declarative/qmltimer/tst_qmltimer.cpp | 34 |
3 files changed, 54 insertions, 12 deletions
diff --git a/src/declarative/util/qmltimer.cpp b/src/declarative/util/qmltimer.cpp index 0be62f47d0..d2198f2869 100644 --- a/src/declarative/util/qmltimer.cpp +++ b/src/declarative/util/qmltimer.cpp @@ -55,7 +55,7 @@ class QmlTimerPrivate : public QObjectPrivate public: QmlTimerPrivate() : interval(1000), running(false), repeating(false), triggeredOnStart(false) - , classBegun(false), componentComplete(false) {} + , classBegun(false), componentComplete(false), firstTick(true) {} int interval; QPauseAnimation pause; bool running : 1; @@ -63,6 +63,7 @@ public: bool triggeredOnStart : 1; bool classBegun : 1; bool componentComplete : 1; + bool firstTick : 1; }; /*! @@ -85,6 +86,12 @@ public: QmlTimer is synchronized with the animation timer. Since the animation timer is usually set to 60fps, the resolution of QmlTimer will be at best 16ms. + + If the Timer is running and one of its properties is changed, the + elapsed time will be reset. For example, if a Timer with interval of + 1000ms has its \e repeat property changed 500ms after starting, the + elapsed time will be reset to 0, and the Timer will be triggered + 1000ms later. */ QmlTimer::QmlTimer(QObject *parent) @@ -92,8 +99,7 @@ QmlTimer::QmlTimer(QObject *parent) { Q_D(QmlTimer); connect(&d->pause, SIGNAL(currentLoopChanged(int)), this, SLOT(ticked())); - connect(&d->pause, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)) - , this, SLOT(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State))); + connect(&d->pause, SIGNAL(finished()), this, SLOT(finished())); d->pause.setLoopCount(1); d->pause.setDuration(d->interval); } @@ -142,6 +148,7 @@ void QmlTimer::setRunning(bool running) Q_D(QmlTimer); if (d->running != running) { d->running = running; + d->firstTick = true; emit runningChanged(); update(); } @@ -236,10 +243,11 @@ void QmlTimer::update() return; d->pause.stop(); if (d->running) { + d->pause.setCurrentTime(0); d->pause.setLoopCount(d->repeating ? -1 : 1); d->pause.setDuration(d->interval); d->pause.start(); - if (d->triggeredOnStart) { + if (d->triggeredOnStart && d->firstTick) { QCoreApplication::removePostedEvents(this, QEvent::MetaCall); QMetaObject::invokeMethod(this, "ticked", Qt::QueuedConnection); } @@ -267,18 +275,18 @@ void QmlTimer::componentComplete() void QmlTimer::ticked() { Q_D(QmlTimer); - if (d->running) + if (d->running && (d->pause.currentTime() > 0 || (d->triggeredOnStart && d->firstTick))) emit triggered(); + d->firstTick = false; } -void QmlTimer::stateChanged(QAbstractAnimation::State state, QAbstractAnimation::State) +void QmlTimer::finished() { Q_D(QmlTimer); - if (d->running && state != QAbstractAnimation::Running) { - d->running = false; - emit triggered(); - emit runningChanged(); - } + if (d->repeating || !d->running) + return; + emit triggered(); + d->firstTick = false; } QT_END_NAMESPACE diff --git a/src/declarative/util/qmltimer_p.h b/src/declarative/util/qmltimer_p.h index bd96d4a8e5..56ef7a6811 100644 --- a/src/declarative/util/qmltimer_p.h +++ b/src/declarative/util/qmltimer_p.h @@ -95,7 +95,7 @@ private: private Q_SLOTS: void ticked(); - void stateChanged(QAbstractAnimation::State,QAbstractAnimation::State); + void finished(); }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/qmltimer/tst_qmltimer.cpp b/tests/auto/declarative/qmltimer/tst_qmltimer.cpp index cf54647901..620d53e147 100644 --- a/tests/auto/declarative/qmltimer/tst_qmltimer.cpp +++ b/tests/auto/declarative/qmltimer/tst_qmltimer.cpp @@ -42,6 +42,7 @@ #include <QtDeclarative/qmlengine.h> #include <QtDeclarative/qmlcomponent.h> #include <private/qmltimer_p.h> +#include <QDebug> class tst_qmltimer : public QObject { @@ -56,6 +57,7 @@ private slots: void noTriggerIfNotRunning(); void triggeredOnStart(); void triggeredOnStartRepeat(); + void changeDuration(); }; class TimerHelper : public QObject @@ -123,6 +125,8 @@ void tst_qmltimer::notRepeatingStart() QCOMPARE(helper.count, 1); QTest::qWait(TIMEOUT_TIMEOUT); QCOMPARE(helper.count, 1); + + delete timer; } void tst_qmltimer::repeat() @@ -147,6 +151,8 @@ void tst_qmltimer::repeat() timer->stop(); QTest::qWait(TIMEOUT_TIMEOUT); QVERIFY(helper.count == oldCount); + + delete timer; } void tst_qmltimer::triggeredOnStart() @@ -166,6 +172,8 @@ void tst_qmltimer::triggeredOnStart() QCOMPARE(helper.count, 2); QTest::qWait(TIMEOUT_TIMEOUT); QCOMPARE(helper.count, 2); + + delete timer; } void tst_qmltimer::triggeredOnStartRepeat() @@ -185,6 +193,8 @@ void tst_qmltimer::triggeredOnStartRepeat() int oldCount = helper.count; QTest::qWait(TIMEOUT_TIMEOUT); QVERIFY(helper.count > oldCount); + + delete timer; } void tst_qmltimer::noTriggerIfNotRunning() @@ -201,6 +211,30 @@ void tst_qmltimer::noTriggerIfNotRunning() QVERIFY(item != 0); QTest::qWait(TIMEOUT_TIMEOUT); QCOMPARE(item->property("ok").toBool(), true); + + delete item; +} + +void tst_qmltimer::changeDuration() +{ + QmlEngine engine; + QmlComponent component(&engine, QByteArray("import Qt 4.6\nTimer { interval: 200; repeat: true; running: true }"), QUrl("file://")); + QmlTimer *timer = qobject_cast<QmlTimer*>(component.create()); + QVERIFY(timer != 0); + + TimerHelper helper; + connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); + QCOMPARE(helper.count, 0); + + QTest::qWait(500); + QCOMPARE(helper.count, 2); + + timer->setInterval(500); + + QTest::qWait(600); + QCOMPARE(helper.count, 3); + + delete timer; } QTEST_MAIN(tst_qmltimer) |