File indexing completed on 2024-04-28 05:52:37

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "touchonlytapandholdgesturerecognizer.hpp"
0010 
0011 // lib
0012 #include "touchonlytapandholdgesture.hpp"
0013 // Qt
0014 #include <QWidget>
0015 #include <QTouchEvent>
0016 
0017 namespace Okteta {
0018 
0019 constexpr int tapRadius = 40; // value used by Qt's tap gestures
0020 
0021 TouchOnlyTapAndHoldGestureRecognizer::TouchOnlyTapAndHoldGestureRecognizer() = default;
0022 
0023 QGesture *TouchOnlyTapAndHoldGestureRecognizer::create(QObject* target)
0024 {
0025     Q_UNUSED(target);
0026 
0027     return new TouchOnlyTapAndHoldGesture;
0028 }
0029 
0030 QGestureRecognizer::Result TouchOnlyTapAndHoldGestureRecognizer::recognize(QGesture* state, QObject* object,
0031                                                                            QEvent* event)
0032 {
0033     auto* tapAndHoldGesture = static_cast<TouchOnlyTapAndHoldGesture *>(state);
0034 
0035     if ((object == state) && (event->type() == QEvent::Timer)) {
0036         tapAndHoldGesture->cancelHoldTimer();
0037         return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
0038     }
0039 
0040     switch (event->type()) {
0041     case QEvent::TouchBegin: {
0042         const auto* touchEvent = static_cast<const QTouchEvent*>(event);
0043         const QTouchEvent::TouchPoint touchPoint = touchEvent->touchPoints().at(0);
0044         const QPointF screenPosition = touchPoint.screenPos();
0045         const QPointF widgetPosition = touchPoint.pos();
0046         tapAndHoldGesture->setHotSpot(screenPosition);
0047         tapAndHoldGesture->setPosition(widgetPosition);
0048         tapAndHoldGesture->beginHoldTimeer();
0049         return QGestureRecognizer::MayBeGesture;
0050     }
0051     case QEvent::TouchUpdate: {
0052         if (tapAndHoldGesture->isHoldTimerActive()) {
0053             const auto* touchEvent = static_cast<const QTouchEvent*>(event);
0054             if (touchEvent->touchPoints().size() == 1) {
0055                 const QTouchEvent::TouchPoint touchPoint = touchEvent->touchPoints().at(0);
0056                 const QPoint delta = touchPoint.pos().toPoint() - touchPoint.startPos().toPoint();
0057                 if (delta.manhattanLength() <= tapRadius) {
0058                     return QGestureRecognizer::MayBeGesture;
0059                 }
0060             }
0061         }
0062         return QGestureRecognizer::CancelGesture;
0063     }
0064     case QEvent::TouchEnd:
0065         return QGestureRecognizer::CancelGesture;
0066     default:
0067         return QGestureRecognizer::Ignore;
0068     }
0069 }
0070 
0071 void TouchOnlyTapAndHoldGestureRecognizer::reset(QGesture* state)
0072 {
0073     auto* tapAndHoldGesture = static_cast<TouchOnlyTapAndHoldGesture*>(state);
0074     tapAndHoldGesture->reset();
0075 
0076     QGestureRecognizer::reset(state);
0077 }
0078 
0079 }