| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickvelocitycalculator_p_p.h" |
| 5 | |
| 6 | #include <QtCore/qdebug.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | /* |
| 11 | Usage: |
| 12 | |
| 13 | QQuickVelocityCalculator velocityCalculator; |
| 14 | |
| 15 | // ... |
| 16 | |
| 17 | velocityCalcular.startMeasuring(event->pos(), event->timestamp()); |
| 18 | velocityCalcular.stopMeasuring(event->pos(), event->timestamp()); |
| 19 | |
| 20 | // ... |
| 21 | |
| 22 | if (velocityCalculator.velocity().x() > someAmount) |
| 23 | doSomething(); |
| 24 | else if (velocityCalculator.velocity().x() < -someAmount) |
| 25 | doSomethingElse(); |
| 26 | */ |
| 27 | |
| 28 | void QQuickVelocityCalculator::startMeasuring(const QPointF &point1, qint64 timestamp) |
| 29 | { |
| 30 | m_point1 = point1; |
| 31 | m_point1Timestamp = timestamp; |
| 32 | } |
| 33 | |
| 34 | void QQuickVelocityCalculator::stopMeasuring(const QPointF &point2, qint64 timestamp) |
| 35 | { |
| 36 | if (timestamp == 0) { |
| 37 | qWarning() << "QQuickVelocityCalculator: a call to stopMeasuring() must be preceded by a call to startMeasuring()"; |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | m_point2 = point2; |
| 42 | m_point2Timestamp = timestamp; |
| 43 | } |
| 44 | |
| 45 | QPointF QQuickVelocityCalculator::velocity() const |
| 46 | { |
| 47 | if (m_point2Timestamp == 0 || m_point1Timestamp == m_point2Timestamp) |
| 48 | return QPointF(); |
| 49 | |
| 50 | const qreal secondsElapsed = (m_point2Timestamp - m_point1Timestamp) / 1000.0; |
| 51 | const QPointF distance = m_point2 - m_point1; |
| 52 | return distance / secondsElapsed; |
| 53 | } |
| 54 | |
| 55 | QT_END_NAMESPACE |
| 56 |
