1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquicktimer_p.h"
44 #include <QtCore/qcoreapplication.h>
45 #include "private/qpauseanimationjob_p.h"
48 #include <private/qobject_p.h>
54 class QQuickTimerPrivate : public QObjectPrivate, public QAnimationJobChangeListener
56 Q_DECLARE_PUBLIC(QQuickTimer)
59 : interval(1000), running(false), repeating(false), triggeredOnStart(false)
60 , classBegun(false), componentComplete(false), firstTick(true) {}
62 virtual void animationFinished(QAbstractAnimationJob *);
63 virtual void animationCurrentLoopChanged(QAbstractAnimationJob *) { Q_Q(QQuickTimer); q->ticked(); }
66 QPauseAnimationJob pause;
69 bool triggeredOnStart : 1;
71 bool componentComplete : 1;
76 \qmlclass Timer QQuickTimer
77 \inqmlmodule QtQuick 2
78 \ingroup qml-utility-elements
79 \brief The Timer item triggers a handler at a specified interval.
81 A Timer can be used to trigger an action either once, or repeatedly
84 Here is a Timer that shows the current date and time, and updates
85 the text every 500 milliseconds. It uses the JavaScript \c Date
86 object to access the current time.
93 interval: 500; running: true; repeat: true
94 onTriggered: time.text = Date().toString()
101 The Timer element is synchronized with the animation timer. Since the animation
102 timer is usually set to 60fps, the resolution of Timer will be
105 If the Timer is running and one of its properties is changed, the
106 elapsed time will be reset. For example, if a Timer with interval of
107 1000ms has its \e repeat property changed 500ms after starting, the
108 elapsed time will be reset to 0, and the Timer will be triggered
111 \sa {declarative/toys/clocks}{Clocks example}
114 QQuickTimer::QQuickTimer(QObject *parent)
115 : QObject(*(new QQuickTimerPrivate), parent)
118 d->pause.addAnimationChangeListener(d, QAbstractAnimationJob::Completion | QAbstractAnimationJob::CurrentLoop);
119 d->pause.setLoopCount(1);
120 d->pause.setDuration(d->interval);
124 \qmlproperty int QtQuick2::Timer::interval
126 Sets the \a interval between triggers, in milliseconds.
128 The default interval is 1000 milliseconds.
130 void QQuickTimer::setInterval(int interval)
133 if (interval != d->interval) {
134 d->interval = interval;
136 emit intervalChanged();
140 int QQuickTimer::interval() const
142 Q_D(const QQuickTimer);
147 \qmlproperty bool QtQuick2::Timer::running
149 If set to true, starts the timer; otherwise stops the timer.
150 For a non-repeating timer, \a running is set to false after the
151 timer has been triggered.
153 \a running defaults to false.
157 bool QQuickTimer::isRunning() const
159 Q_D(const QQuickTimer);
163 void QQuickTimer::setRunning(bool running)
166 if (d->running != running) {
167 d->running = running;
169 emit runningChanged();
175 \qmlproperty bool QtQuick2::Timer::repeat
177 If \a repeat is true the timer is triggered repeatedly at the
178 specified interval; otherwise, the timer will trigger once at the
179 specified interval and then stop (i.e. running will be set to false).
181 \a repeat defaults to false.
185 bool QQuickTimer::isRepeating() const
187 Q_D(const QQuickTimer);
191 void QQuickTimer::setRepeating(bool repeating)
194 if (repeating != d->repeating) {
195 d->repeating = repeating;
197 emit repeatChanged();
202 \qmlproperty bool QtQuick2::Timer::triggeredOnStart
204 When a timer is started, the first trigger is usually after the specified
205 interval has elapsed. It is sometimes desirable to trigger immediately
206 when the timer is started; for example, to establish an initial
209 If \a triggeredOnStart is true, the timer is triggered immediately
210 when started, and subsequently at the specified interval. Note that if
211 \e repeat is set to false, the timer is triggered twice; once on start,
212 and again at the interval.
214 \a triggeredOnStart defaults to false.
218 bool QQuickTimer::triggeredOnStart() const
220 Q_D(const QQuickTimer);
221 return d->triggeredOnStart;
224 void QQuickTimer::setTriggeredOnStart(bool triggeredOnStart)
227 if (d->triggeredOnStart != triggeredOnStart) {
228 d->triggeredOnStart = triggeredOnStart;
230 emit triggeredOnStartChanged();
235 \qmlmethod QtQuick2::Timer::start()
236 \brief Starts the timer.
238 If the timer is already running, calling this method has no effect. The
239 \c running property will be true following a call to \c start().
241 void QQuickTimer::start()
247 \qmlmethod QtQuick2::Timer::stop()
248 \brief Stops the timer.
250 If the timer is not running, calling this method has no effect. The
251 \c running property will be false following a call to \c stop().
253 void QQuickTimer::stop()
259 \qmlmethod QtQuick2::Timer::restart()
260 \brief Restarts the timer.
262 If the Timer is not running it will be started, otherwise it will be
263 stopped, reset to initial state and started. The \c running property
264 will be true following a call to \c restart().
266 void QQuickTimer::restart()
272 void QQuickTimer::update()
275 if (d->classBegun && !d->componentComplete)
279 d->pause.setCurrentTime(0);
280 d->pause.setLoopCount(d->repeating ? -1 : 1);
281 d->pause.setDuration(d->interval);
283 if (d->triggeredOnStart && d->firstTick) {
284 QCoreApplication::removePostedEvents(this, QEvent::MetaCall);
285 QMetaObject::invokeMethod(this, "ticked", Qt::QueuedConnection);
290 void QQuickTimer::classBegin()
293 d->classBegun = true;
296 void QQuickTimer::componentComplete()
299 d->componentComplete = true;
304 \qmlsignal QtQuick2::Timer::onTriggered()
306 This handler is called when the Timer is triggered.
308 void QQuickTimer::ticked()
311 if (d->running && (d->pause.currentTime() > 0 || (d->triggeredOnStart && d->firstTick)))
313 d->firstTick = false;
316 void QQuickTimerPrivate::animationFinished(QAbstractAnimationJob *)
319 if (repeating || !running)
324 emit q->runningChanged();