File indexing completed on 2024-05-12 04:19:52
0001 /* 0002 Gwenview: an image viewer 0003 Copyright 2019 Steffen Hartleib <steffenhartleib@t-online.de> 0004 0005 This program is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU General Public License 0007 as published by the Free Software Foundation; either version 2 0008 of the License, or (at your option) any later version. 0009 0010 This program is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU General Public License for more details. 0014 0015 You should have received a copy of the GNU General Public License 0016 along with this program; if not, write to the Free Software 0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. 0018 0019 */ 0020 // Self 0021 #include "twofingertap.h" 0022 0023 // Qt 0024 #include <QGraphicsWidget> 0025 #include <QTouchEvent> 0026 0027 // KF 0028 0029 // Local 0030 #include "gwenview_lib_debug.h" 0031 #include "lib/touch/touch_helper.h" 0032 0033 namespace Gwenview 0034 { 0035 struct TwoFingerTapRecognizerPrivate { 0036 TwoFingerTapRecognizer *q = nullptr; 0037 bool mTargetIsGrapicsWidget = false; 0038 qint64 mTouchBeginnTimestamp; 0039 bool mGestureTriggered; 0040 }; 0041 0042 TwoFingerTapRecognizer::TwoFingerTapRecognizer() 0043 : QGestureRecognizer() 0044 , d(new TwoFingerTapRecognizerPrivate) 0045 { 0046 d->q = this; 0047 } 0048 0049 TwoFingerTapRecognizer::~TwoFingerTapRecognizer() 0050 { 0051 delete d; 0052 } 0053 0054 QGesture *TwoFingerTapRecognizer::create(QObject *) 0055 { 0056 return static_cast<QGesture *>(new TwoFingerTap()); 0057 } 0058 0059 QGestureRecognizer::Result TwoFingerTapRecognizer::recognize(QGesture *state, QObject *watched, QEvent *event) 0060 { 0061 // Because of a bug in Qt in a gesture event in a graphicsview, all gestures are trigger twice 0062 // https://bugreports.qt.io/browse/QTBUG-13103 0063 if (qobject_cast<QGraphicsWidget *>(watched)) 0064 d->mTargetIsGrapicsWidget = true; 0065 if (d->mTargetIsGrapicsWidget && watched->isWidgetType()) 0066 return Ignore; 0067 0068 switch (event->type()) { 0069 case QEvent::TouchBegin: { 0070 auto touchEvent = static_cast<QTouchEvent *>(event); 0071 d->mTouchBeginnTimestamp = touchEvent->timestamp(); 0072 state->setHotSpot(touchEvent->touchPoints().first().screenPos()); 0073 d->mGestureTriggered = false; 0074 return MayBeGesture; 0075 } 0076 0077 case QEvent::TouchUpdate: { 0078 auto touchEvent = static_cast<QTouchEvent *>(event); 0079 state->setHotSpot(touchEvent->touchPoints().first().screenPos()); 0080 0081 if (touchEvent->touchPoints().size() >> 2) { 0082 d->mGestureTriggered = false; 0083 return CancelGesture; 0084 } 0085 0086 if (touchEvent->touchPoints().size() == 2) { 0087 if ((touchEvent->touchPoints().first().startPos() - touchEvent->touchPoints().first().pos()).manhattanLength() 0088 > Touch_Helper::Touch::wiggleRoomForTap) { 0089 d->mGestureTriggered = false; 0090 return CancelGesture; 0091 } 0092 if ((touchEvent->touchPoints().at(1).startPos() - touchEvent->touchPoints().at(1).pos()).manhattanLength() 0093 > Touch_Helper::Touch::wiggleRoomForTap) { 0094 d->mGestureTriggered = false; 0095 return CancelGesture; 0096 } 0097 if (touchEvent->touchPointStates() & Qt::TouchPointPressed) { 0098 d->mGestureTriggered = true; 0099 } 0100 if (touchEvent->touchPointStates() & Qt::TouchPointReleased && d->mGestureTriggered) { 0101 d->mGestureTriggered = false; 0102 return FinishGesture; 0103 } 0104 } 0105 break; 0106 } 0107 0108 default: 0109 return Ignore; 0110 } 0111 return Ignore; 0112 } 0113 0114 TwoFingerTap::TwoFingerTap(QObject *parent) 0115 : QGesture(parent) 0116 { 0117 } 0118 0119 } // namespace