1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickvelocitycalculator_p_p.h"
38
39#include <QtCore/qdebug.h>
40
41QT_BEGIN_NAMESPACE
42
43/*
44 Usage:
45
46 QQuickVelocityCalculator velocityCalculator;
47
48 // ...
49
50 velocityCalcular.startMeasuring(event->pos(), event->timestamp());
51 velocityCalcular.stopMeasuring(event->pos(), event->timestamp());
52
53 // ...
54
55 if (velocityCalculator.velocity().x() > someAmount)
56 doSomething();
57 else if (velocityCalculator.velocity().x() < -someAmount)
58 doSomethingElse();
59*/
60
61void QQuickVelocityCalculator::startMeasuring(const QPointF &point1, qint64 timestamp)
62{
63 m_point1 = point1;
64
65 if (timestamp != 0)
66 m_point1Timestamp = timestamp;
67 else
68 m_timer.start();
69}
70
71void QQuickVelocityCalculator::stopMeasuring(const QPointF &point2, qint64 timestamp)
72{
73 if (timestamp == 0 && !m_timer.isValid()) {
74 qWarning() << "QQuickVelocityCalculator: a call to stopMeasuring() must be preceded by a call to startMeasuring()";
75 return;
76 }
77
78 m_point2 = point2;
79 m_point2Timestamp = timestamp != 0 ? timestamp : m_timer.elapsed();
80 m_timer.invalidate();
81}
82
83void QQuickVelocityCalculator::reset()
84{
85 m_point1 = QPointF();
86 m_point2 = QPointF();
87 m_point1Timestamp = 0;
88 m_point2Timestamp = 0;
89 m_timer.invalidate();
90}
91
92QPointF QQuickVelocityCalculator::velocity() const
93{
94 if ((m_point2Timestamp == 0 || m_point1Timestamp == m_point2Timestamp) && !m_timer.isValid())
95 return QPointF();
96
97 const qreal secondsElapsed = (m_point2Timestamp != 0 ? m_point2Timestamp - m_point1Timestamp : m_timer.elapsed()) / 1000.0;
98 const QPointF distance = m_point2 - m_point1;
99 return distance / secondsElapsed;
100}
101
102QT_END_NAMESPACE
103

source code of qtquickcontrols2/src/quicktemplates2/qquickvelocitycalculator.cpp