| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/handlers/qquickmultipointhandler.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 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 The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #include "qquickmultipointhandler_p.h" | - | ||||||||||||
| 41 | #include <private/qquickitem_p.h> | - | ||||||||||||
| 42 | #include <QLineF> | - | ||||||||||||
| 43 | #include <QMouseEvent> | - | ||||||||||||
| 44 | #include <QDebug> | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | /*! | - | ||||||||||||
| 49 | \qmltype MultiPointHandler | - | ||||||||||||
| 50 | \since 5.10 | - | ||||||||||||
| 51 | \preliminary | - | ||||||||||||
| 52 | \instantiates QQuickMultiPointHandler | - | ||||||||||||
| 53 | \inherits PointerDeviceHandler | - | ||||||||||||
| 54 | \inqmlmodule Qt.labs.handlers | - | ||||||||||||
| 55 | \ingroup qtquick-handlers | - | ||||||||||||
| 56 | \brief Abstract handler for multi-point Pointer Events. | - | ||||||||||||
| 57 | - | |||||||||||||
| 58 | An intermediate class (not registered as a QML type) | - | ||||||||||||
| 59 | for any type of handler which requires and acts upon a specific number | - | ||||||||||||
| 60 | of multiple touchpoints. | - | ||||||||||||
| 61 | */ | - | ||||||||||||
| 62 | QQuickMultiPointHandler::QQuickMultiPointHandler(QObject *parent, int minimumPointCount) | - | ||||||||||||
| 63 | : QQuickPointerDeviceHandler(parent) | - | ||||||||||||
| 64 | , m_minimumPointCount(minimumPointCount) | - | ||||||||||||
| 65 | , m_maximumPointCount(-1) | - | ||||||||||||
| 66 | { | - | ||||||||||||
| 67 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 68 | - | |||||||||||||
| 69 | QQuickMultiPointHandler::~QQuickMultiPointHandler() | - | ||||||||||||
| 70 | { | - | ||||||||||||
| 71 | } | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | bool QQuickMultiPointHandler::wantsPointerEvent(QQuickPointerEvent *event) | - | ||||||||||||
| 74 | { | - | ||||||||||||
| 75 | if (!QQuickPointerDeviceHandler::wantsPointerEvent(event))
| 0-83 | ||||||||||||
| 76 | return false; never executed: return false; | 0 | ||||||||||||
| 77 | - | |||||||||||||
| 78 | #if QT_CONFIG(gestures) | - | ||||||||||||
| 79 | if (event->asPointerNativeGestureEvent())
| 0-83 | ||||||||||||
| 80 | return true; never executed: return true; | 0 | ||||||||||||
| 81 | #endif | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | if (hasCurrentPoints(event))
| 27-56 | ||||||||||||
| 84 | return true; executed 27 times by 1 test: return true;Executed by:
| 27 | ||||||||||||
| 85 | - | |||||||||||||
| 86 | const QVector<QQuickEventPoint *> candidatePoints = eligiblePoints(event); | - | ||||||||||||
| 87 | const bool ret = (candidatePoints.size() >= minimumPointCount() && candidatePoints.size() <= maximumPointCount());
| 2-50 | ||||||||||||
| 88 | if (ret)
| 4-52 | ||||||||||||
| 89 | m_currentPoints = candidatePoints; executed 4 times by 1 test: m_currentPoints = candidatePoints;Executed by:
| 4 | ||||||||||||
| 90 | return ret; executed 56 times by 1 test: return ret;Executed by:
| 56 | ||||||||||||
| 91 | } | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | void QQuickMultiPointHandler::handlePointerEventImpl(QQuickPointerEvent *event) | - | ||||||||||||
| 94 | { | - | ||||||||||||
| 95 | QQuickPointerHandler::handlePointerEventImpl(event); | - | ||||||||||||
| 96 | m_centroid.reset(m_currentPoints); | - | ||||||||||||
| 97 | emit centroidChanged(); | - | ||||||||||||
| 98 | } executed 31 times by 1 test: end of blockExecuted by:
| 31 | ||||||||||||
| 99 | - | |||||||||||||
| 100 | void QQuickMultiPointHandler::onActiveChanged() | - | ||||||||||||
| 101 | { | - | ||||||||||||
| 102 | if (active()) {
| 4 | ||||||||||||
| 103 | m_centroid.m_sceneGrabPosition = m_centroid.m_scenePosition; | - | ||||||||||||
| 104 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 105 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||
| 106 | - | |||||||||||||
| 107 | QVector<QQuickEventPoint *> QQuickMultiPointHandler::eligiblePoints(QQuickPointerEvent *event) | - | ||||||||||||
| 108 | { | - | ||||||||||||
| 109 | QVector<QQuickEventPoint *> ret; | - | ||||||||||||
| 110 | int c = event->pointCount(); | - | ||||||||||||
| 111 | // If one or more points are newly pressed or released, all non-released points are candidates for this handler. | - | ||||||||||||
| 112 | // In other cases however, do not steal the grab: that is, if a point has a grabber, | - | ||||||||||||
| 113 | // it's not a candidate for this handler. | - | ||||||||||||
| 114 | bool stealingAllowed = event->isPressEvent() || event->isReleaseEvent();
| 10-46 | ||||||||||||
| 115 | for (int i = 0; i < c; ++i) {
| 56-96 | ||||||||||||
| 116 | QQuickEventPoint *p = event->point(i); | - | ||||||||||||
| 117 | if (!stealingAllowed) {
| 44-52 | ||||||||||||
| 118 | QObject *exclusiveGrabber = p->exclusiveGrabber(); | - | ||||||||||||
| 119 | if (exclusiveGrabber && exclusiveGrabber != this)
| 4-34 | ||||||||||||
| 120 | continue; executed 14 times by 1 test: continue;Executed by:
| 14 | ||||||||||||
| 121 | } executed 38 times by 1 test: end of blockExecuted by:
| 38 | ||||||||||||
| 122 | if (p->state() != QQuickEventPoint::Released && wantsEventPoint(p))
| 0-72 | ||||||||||||
| 123 | ret << p; executed 72 times by 1 test: ret << p;Executed by:
| 72 | ||||||||||||
| 124 | } executed 82 times by 1 test: end of blockExecuted by:
| 82 | ||||||||||||
| 125 | return ret; executed 56 times by 1 test: return ret;Executed by:
| 56 | ||||||||||||
| 126 | } | - | ||||||||||||
| 127 | - | |||||||||||||
| 128 | /*! | - | ||||||||||||
| 129 | \qmlproperty int MultiPointHandler::minimumPointCount | - | ||||||||||||
| 130 | - | |||||||||||||
| 131 | The minimum number of touchpoints required to activate this handler. | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | If a smaller number of touchpoints are in contact with the | - | ||||||||||||
| 134 | \l {PointerHandler::parent}{parent}, they will be ignored. | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | Any ignored points are eligible to activate other Pointer Handlers that | - | ||||||||||||
| 137 | have different constraints, on the same Item or on other Items. | - | ||||||||||||
| 138 | - | |||||||||||||
| 139 | The default value is 2. | - | ||||||||||||
| 140 | */ | - | ||||||||||||
| 141 | void QQuickMultiPointHandler::setMinimumPointCount(int c) | - | ||||||||||||
| 142 | { | - | ||||||||||||
| 143 | if (m_minimumPointCount == c)
| 0-4 | ||||||||||||
| 144 | return; never executed: return; | 0 | ||||||||||||
| 145 | - | |||||||||||||
| 146 | m_minimumPointCount = c; | - | ||||||||||||
| 147 | emit minimumPointCountChanged(); | - | ||||||||||||
| 148 | if (m_maximumPointCount < 0)
| 0-4 | ||||||||||||
| 149 | emit maximumPointCountChanged(); executed 4 times by 1 test: maximumPointCountChanged();Executed by:
| 4 | ||||||||||||
| 150 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 151 | - | |||||||||||||
| 152 | /*! | - | ||||||||||||
| 153 | \qmlproperty int MultiPointHandler::maximumPointCount | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | The maximum number of touchpoints this handler can utilize. | - | ||||||||||||
| 156 | - | |||||||||||||
| 157 | If a larger number of touchpoints are in contact with the | - | ||||||||||||
| 158 | \l {PointerHandler::parent}{parent}, the required number of points will be | - | ||||||||||||
| 159 | chosen in the order that they are pressed, and the remaining points will | - | ||||||||||||
| 160 | be ignored. | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | Any ignored points are eligible to activate other Pointer Handlers that | - | ||||||||||||
| 163 | have different constraints, on the same Item or on other Items. | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | The default value is the same as \l minimumPointCount. | - | ||||||||||||
| 166 | */ | - | ||||||||||||
| 167 | void QQuickMultiPointHandler::setMaximumPointCount(int maximumPointCount) | - | ||||||||||||
| 168 | { | - | ||||||||||||
| 169 | if (m_maximumPointCount == maximumPointCount)
| 0 | ||||||||||||
| 170 | return; never executed: return; | 0 | ||||||||||||
| 171 | - | |||||||||||||
| 172 | m_maximumPointCount = maximumPointCount; | - | ||||||||||||
| 173 | emit maximumPointCountChanged(); | - | ||||||||||||
| 174 | } never executed: end of block | 0 | ||||||||||||
| 175 | - | |||||||||||||
| 176 | bool QQuickMultiPointHandler::hasCurrentPoints(QQuickPointerEvent *event) | - | ||||||||||||
| 177 | { | - | ||||||||||||
| 178 | bool ret = true; | - | ||||||||||||
| 179 | int c = event->pointCount(); | - | ||||||||||||
| 180 | if (c < m_currentPoints.size())
| 34-49 | ||||||||||||
| 181 | return false; executed 34 times by 1 test: return false;Executed by:
| 34 | ||||||||||||
| 182 | // TODO optimize: either ensure the points are sorted, | - | ||||||||||||
| 183 | // or use std::equal with a predicate | - | ||||||||||||
| 184 | for (int i = 0; ret && i < c; ++i) {
| 16-142 | ||||||||||||
| 185 | if (event->point(i)->state() == QQuickEventPoint::Released)
| 6-109 | ||||||||||||
| 186 | return false; executed 6 times by 1 test: return false;Executed by:
| 6 | ||||||||||||
| 187 | bool found = false; | - | ||||||||||||
| 188 | int pointId = event->point(i)->pointId(); | - | ||||||||||||
| 189 | for (QQuickEventPoint *o : qAsConst(m_currentPoints)) | - | ||||||||||||
| 190 | if (o && pointId == o->pointId())
| 0-285 | ||||||||||||
| 191 | found = true; executed 93 times by 1 test: found = true;Executed by:
| 93 | ||||||||||||
| 192 | if (!found)
| 16-93 | ||||||||||||
| 193 | ret = false; executed 16 times by 1 test: ret = false;Executed by:
| 16 | ||||||||||||
| 194 | } executed 109 times by 1 test: end of blockExecuted by:
| 109 | ||||||||||||
| 195 | return ret; executed 43 times by 1 test: return ret;Executed by:
| 43 | ||||||||||||
| 196 | } | - | ||||||||||||
| 197 | - | |||||||||||||
| 198 | qreal QQuickMultiPointHandler::averageTouchPointDistance(const QPointF &ref) | - | ||||||||||||
| 199 | { | - | ||||||||||||
| 200 | qreal ret = 0; | - | ||||||||||||
| 201 | if (Q_UNLIKELY(m_currentPoints.size() == 0))
| 0-33 | ||||||||||||
| 202 | return ret; never executed: return ret; | 0 | ||||||||||||
| 203 | for (QQuickEventPoint *point : qAsConst(m_currentPoints)) | - | ||||||||||||
| 204 | ret += QVector2D(point->scenePosition() - ref).length(); executed 99 times by 1 test: ret += QVector2D(point->scenePosition() - ref).length();Executed by:
| 99 | ||||||||||||
| 205 | return ret / m_currentPoints.size(); executed 33 times by 1 test: return ret / m_currentPoints.size();Executed by:
| 33 | ||||||||||||
| 206 | } | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | qreal QQuickMultiPointHandler::averageStartingDistance(const QPointF &ref) | - | ||||||||||||
| 209 | { | - | ||||||||||||
| 210 | // TODO cache it in setActive()? | - | ||||||||||||
| 211 | qreal ret = 0; | - | ||||||||||||
| 212 | if (Q_UNLIKELY(m_currentPoints.size() == 0))
| 0 | ||||||||||||
| 213 | return ret; never executed: return ret; | 0 | ||||||||||||
| 214 | for (QQuickEventPoint *point : qAsConst(m_currentPoints)) | - | ||||||||||||
| 215 | ret += QVector2D(point->sceneGrabPosition() - ref).length(); never executed: ret += QVector2D(point->sceneGrabPosition() - ref).length(); | 0 | ||||||||||||
| 216 | return ret / m_currentPoints.size(); never executed: return ret / m_currentPoints.size(); | 0 | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | QVector<QQuickMultiPointHandler::PointData> QQuickMultiPointHandler::angles(const QPointF &ref) const | - | ||||||||||||
| 220 | { | - | ||||||||||||
| 221 | QVector<PointData> angles; | - | ||||||||||||
| 222 | angles.reserve(m_currentPoints.count()); | - | ||||||||||||
| 223 | for (QQuickEventPoint *point : qAsConst(m_currentPoints)) { | - | ||||||||||||
| 224 | qreal angle = QLineF(ref, point->scenePosition()).angle(); | - | ||||||||||||
| 225 | angles.append(PointData(point->pointId(), -angle)); // convert to clockwise, to be consistent with QQuickItem::rotation | - | ||||||||||||
| 226 | } executed 99 times by 1 test: end of blockExecuted by:
| 99 | ||||||||||||
| 227 | return angles; executed 33 times by 1 test: return angles;Executed by:
| 33 | ||||||||||||
| 228 | } | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | qreal QQuickMultiPointHandler::averageAngleDelta(const QVector<PointData> &old, const QVector<PointData> &newAngles) | - | ||||||||||||
| 231 | { | - | ||||||||||||
| 232 | qreal avgAngleDelta = 0; | - | ||||||||||||
| 233 | int numSamples = 0; | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | auto oldBegin = old.constBegin(); | - | ||||||||||||
| 236 | - | |||||||||||||
| 237 | for (PointData newData : newAngles) { | - | ||||||||||||
| 238 | quint64 id = newData.id; | - | ||||||||||||
| 239 | auto it = std::find_if(oldBegin, old.constEnd(), [id] (PointData pd) { return pd.id == id; }); executed 87 times by 1 test: return pd.id == id;Executed by:
| 87 | ||||||||||||
| 240 | qreal angleD = 0; | - | ||||||||||||
| 241 | if (it != old.constEnd()) {
| 0-87 | ||||||||||||
| 242 | PointData oldData = *it; | - | ||||||||||||
| 243 | // We might rotate from 359 degrees to 1 degree. However, this | - | ||||||||||||
| 244 | // should be interpreted as a rotation of +2 degrees instead of | - | ||||||||||||
| 245 | // -358 degrees. Therefore, we call remainder() to translate the angle | - | ||||||||||||
| 246 | // to be in the range [-180, 180] (-350 to +10 etc) | - | ||||||||||||
| 247 | angleD = remainder(newData.angle - oldData.angle, qreal(360)); | - | ||||||||||||
| 248 | // optimization: narrow down the O(n^2) search to optimally O(n) | - | ||||||||||||
| 249 | // if both vectors have the same points and they are in the same order | - | ||||||||||||
| 250 | if (it == oldBegin)
| 0-87 | ||||||||||||
| 251 | ++oldBegin; executed 87 times by 1 test: ++oldBegin;Executed by:
| 87 | ||||||||||||
| 252 | numSamples++; | - | ||||||||||||
| 253 | } executed 87 times by 1 test: end of blockExecuted by:
| 87 | ||||||||||||
| 254 | avgAngleDelta += angleD; | - | ||||||||||||
| 255 | } executed 87 times by 1 test: end of blockExecuted by:
| 87 | ||||||||||||
| 256 | if (numSamples > 1)
| 0-29 | ||||||||||||
| 257 | avgAngleDelta /= numSamples; executed 29 times by 1 test: avgAngleDelta /= numSamples;Executed by:
| 29 | ||||||||||||
| 258 | - | |||||||||||||
| 259 | return avgAngleDelta; executed 29 times by 1 test: return avgAngleDelta;Executed by:
| 29 | ||||||||||||
| 260 | } | - | ||||||||||||
| 261 | - | |||||||||||||
| 262 | void QQuickMultiPointHandler::acceptPoints(const QVector<QQuickEventPoint *> &points) | - | ||||||||||||
| 263 | { | - | ||||||||||||
| 264 | for (QQuickEventPoint* point : points) | - | ||||||||||||
| 265 | point->setAccepted(); executed 87 times by 1 test: point->setAccepted();Executed by:
| 87 | ||||||||||||
| 266 | } executed 29 times by 1 test: end of blockExecuted by:
| 29 | ||||||||||||
| 267 | - | |||||||||||||
| 268 | bool QQuickMultiPointHandler::grabPoints(QVector<QQuickEventPoint *> points) | - | ||||||||||||
| 269 | { | - | ||||||||||||
| 270 | bool allowed = true; | - | ||||||||||||
| 271 | for (QQuickEventPoint* point : points) { | - | ||||||||||||
| 272 | if (!canGrab(point)) {
| 0-12 | ||||||||||||
| 273 | allowed = false; | - | ||||||||||||
| 274 | break; never executed: break; | 0 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | } executed 12 times by 1 test: end of blockExecuted by:
| 12 | ||||||||||||
| 277 | if (allowed) {
| 0-4 | ||||||||||||
| 278 | for (QQuickEventPoint* point : points) | - | ||||||||||||
| 279 | setExclusiveGrab(point); executed 12 times by 1 test: setExclusiveGrab(point);Executed by:
| 12 | ||||||||||||
| 280 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 281 | return allowed; executed 4 times by 1 test: return allowed;Executed by:
| 4 | ||||||||||||
| 282 | } | - | ||||||||||||
| 283 | - | |||||||||||||
| 284 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |