1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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;
77 \instantiates QQuickTimer
78 \inqmlmodule QtQuick 2
79 \ingroup qtquick-interceptors
80 \brief Triggers a handler at a specified interval
82 A Timer can be used to trigger an action either once, or repeatedly
85 Here is a Timer that shows the current date and time, and updates
86 the text every 500 milliseconds. It uses the JavaScript \c Date
87 object to access the current time.
94 interval: 500; running: true; repeat: true
95 onTriggered: time.text = Date().toString()
102 The Timer type is synchronized with the animation timer. Since the animation
103 timer is usually set to 60fps, the resolution of Timer will be
106 If the Timer is running and one of its properties is changed, the
107 elapsed time will be reset. For example, if a Timer with interval of
108 1000ms has its \e repeat property changed 500ms after starting, the
109 elapsed time will be reset to 0, and the Timer will be triggered
112 \sa {declarative/toys/clocks}{Clocks example}
115 QQuickTimer::QQuickTimer(QObject *parent)
116 : QObject(*(new QQuickTimerPrivate), parent)
119 d->pause.addAnimationChangeListener(d, QAbstractAnimationJob::Completion | QAbstractAnimationJob::CurrentLoop);
120 d->pause.setLoopCount(1);
121 d->pause.setDuration(d->interval);
125 \qmlproperty int QtQuick2::Timer::interval
127 Sets the \a interval between triggers, in milliseconds.
129 The default interval is 1000 milliseconds.
131 void QQuickTimer::setInterval(int interval)
134 if (interval != d->interval) {
135 d->interval = interval;
137 emit intervalChanged();
141 int QQuickTimer::interval() const
143 Q_D(const QQuickTimer);
148 \qmlproperty bool QtQuick2::Timer::running
150 If set to true, starts the timer; otherwise stops the timer.
151 For a non-repeating timer, \a running is set to false after the
152 timer has been triggered.
154 \a running defaults to false.
158 bool QQuickTimer::isRunning() const
160 Q_D(const QQuickTimer);
164 void QQuickTimer::setRunning(bool running)
167 if (d->running != running) {
168 d->running = running;
170 emit runningChanged();
176 \qmlproperty bool QtQuick2::Timer::repeat
178 If \a repeat is true the timer is triggered repeatedly at the
179 specified interval; otherwise, the timer will trigger once at the
180 specified interval and then stop (i.e. running will be set to false).
182 \a repeat defaults to false.
186 bool QQuickTimer::isRepeating() const
188 Q_D(const QQuickTimer);
192 void QQuickTimer::setRepeating(bool repeating)
195 if (repeating != d->repeating) {
196 d->repeating = repeating;
198 emit repeatChanged();
203 \qmlproperty bool QtQuick2::Timer::triggeredOnStart
205 When a timer is started, the first trigger is usually after the specified
206 interval has elapsed. It is sometimes desirable to trigger immediately
207 when the timer is started; for example, to establish an initial
210 If \a triggeredOnStart is true, the timer is triggered immediately
211 when started, and subsequently at the specified interval. Note that if
212 \e repeat is set to false, the timer is triggered twice; once on start,
213 and again at the interval.
215 \a triggeredOnStart defaults to false.
219 bool QQuickTimer::triggeredOnStart() const
221 Q_D(const QQuickTimer);
222 return d->triggeredOnStart;
225 void QQuickTimer::setTriggeredOnStart(bool triggeredOnStart)
228 if (d->triggeredOnStart != triggeredOnStart) {
229 d->triggeredOnStart = triggeredOnStart;
231 emit triggeredOnStartChanged();
236 \qmlmethod QtQuick2::Timer::start()
237 \brief Starts the timer
239 If the timer is already running, calling this method has no effect. The
240 \c running property will be true following a call to \c start().
242 void QQuickTimer::start()
248 \qmlmethod QtQuick2::Timer::stop()
249 \brief Stops the timer
251 If the timer is not running, calling this method has no effect. The
252 \c running property will be false following a call to \c stop().
254 void QQuickTimer::stop()
260 \qmlmethod QtQuick2::Timer::restart()
261 \brief Restarts the timer
263 If the Timer is not running it will be started, otherwise it will be
264 stopped, reset to initial state and started. The \c running property
265 will be true following a call to \c restart().
267 void QQuickTimer::restart()
273 void QQuickTimer::update()
276 if (d->classBegun && !d->componentComplete)
280 d->pause.setCurrentTime(0);
281 d->pause.setLoopCount(d->repeating ? -1 : 1);
282 d->pause.setDuration(d->interval);
284 if (d->triggeredOnStart && d->firstTick) {
285 QCoreApplication::removePostedEvents(this, QEvent::MetaCall);
286 QMetaObject::invokeMethod(this, "ticked", Qt::QueuedConnection);
291 void QQuickTimer::classBegin()
294 d->classBegun = true;
297 void QQuickTimer::componentComplete()
300 d->componentComplete = true;
305 \qmlsignal QtQuick2::Timer::onTriggered()
307 This handler is called when the Timer is triggered.
309 void QQuickTimer::ticked()
312 if (d->running && (d->pause.currentTime() > 0 || (d->triggeredOnStart && d->firstTick)))
314 d->firstTick = false;
317 void QQuickTimerPrivate::animationFinished(QAbstractAnimationJob *)
320 if (repeating || !running)
325 emit q->runningChanged();