Warning, file /frameworks/kwidgetsaddons/tests/ktwofingertap_test.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2021 Steffen Hartleib <steffenhartleib@t-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "ktwofingertap_test.h"
0009 
0010 #include <QApplication>
0011 #include <QVBoxLayout>
0012 #include <QCheckBox>
0013 #include <QLineEdit>
0014 #include <QLabel>
0015 #include <QDebug>
0016 #include <QTouchEvent>
0017 #include <QSpinBox>
0018 
0019 #include <ktwofingertap.h>
0020 
0021 
0022 MainWindow::MainWindow()
0023     : QMainWindow()
0024 {
0025     mWidget = new QWidget(this);
0026     setCentralWidget(mWidget);
0027 
0028     QVBoxLayout *vLayout = new QVBoxLayout (mWidget);
0029 
0030     QLabel *msgLabel = new QLabel(QStringLiteral("Make a two finger tap gesture in this window."), mWidget);
0031     msgLabel->setAlignment(Qt::AlignHCenter);
0032 
0033     mGStarted = new QCheckBox(QStringLiteral("gesture started"), mWidget);
0034     mGStarted->setEnabled(false);
0035 
0036     mGUpdated = new QCheckBox(QStringLiteral("gesture updated"), mWidget);
0037     mGUpdated->setEnabled(false);
0038 
0039     mGCanceled = new QCheckBox(QStringLiteral("gesture canceled"), mWidget);
0040     mGCanceled->setEnabled(false);
0041 
0042     mGFinished = new QCheckBox(QStringLiteral("gesture finished"), mWidget);
0043     mGFinished->setEnabled(false);
0044 
0045     mGPos = new QLineEdit(mWidget);
0046     mGPos->setReadOnly(true);
0047 
0048     QLabel *radiusLabel = new QLabel(QStringLiteral("current tapRadius:"), mWidget);
0049     QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0050     radiusLabel->setSizePolicy(sizePolicy);
0051     mGTapRadius = new QSpinBox(mWidget);
0052     mGTapRadius->setMaximum(1000);
0053 
0054     vLayout->addWidget(msgLabel);
0055     vLayout->addWidget(mGStarted);
0056     vLayout->addWidget(mGUpdated);
0057     vLayout->addWidget(mGCanceled);
0058     vLayout->addWidget(mGFinished);
0059     vLayout->addWidget(mGPos);
0060     vLayout->addWidget(radiusLabel);
0061     vLayout->addWidget(mGTapRadius);
0062 
0063     mTwoFingerRec = new KTwoFingerTapRecognizer();
0064     kTwoFingerTapGesture = QGestureRecognizer::registerRecognizer(mTwoFingerRec);
0065 
0066     mGTapRadius->setValue(mTwoFingerRec->tapRadius());
0067     mWidget->grabGesture(kTwoFingerTapGesture);
0068     mWidget->setAttribute(Qt::WA_AcceptTouchEvents);
0069     mWidget->installEventFilter(this);
0070 
0071     connect (mGTapRadius, &QSpinBox::valueChanged, this, &MainWindow::slotSwipeTimeChanged);
0072 }
0073 
0074 MainWindow::~MainWindow()
0075 {
0076 }
0077 
0078 bool MainWindow::eventFilter(QObject *watched, QEvent *e)
0079 {
0080     if (e->type() == QEvent::TouchBegin) {
0081         resetAll();
0082     }
0083 
0084     if (e->type() == QEvent::Gesture) {
0085         QGestureEvent *gEvent = static_cast<QGestureEvent *>(e);
0086         KTwoFingerTap *tap = static_cast<KTwoFingerTap *>(gEvent->gesture(kTwoFingerTapGesture));
0087 
0088         if (tap) {
0089             const Qt::GestureState state = tap->state();
0090 
0091             if (state == Qt::GestureFinished) {
0092                 mGFinished->setCheckState(Qt::Checked);
0093             } else if (state == Qt::GestureStarted) {
0094                 // We need to map the local position, because the tap->pos() is the local position which on received the gesture.
0095                 // In this case it is local to the Widgets in the layout.
0096                 const QPoint lokalPos = mWidget->mapFromGlobal(tap->scenePos().toPoint());
0097                 QString posStr = QStringLiteral("tap Position = ");
0098                 posStr += QString::number(lokalPos.x()) + QStringLiteral(",") + QString::number(lokalPos.y());
0099                 mGPos->setText(posStr);
0100                 mGStarted->setCheckState(Qt::Checked);
0101             } else if (state == Qt::GestureUpdated) {
0102                 mGUpdated->setCheckState(Qt::Checked);
0103             } else if (state == Qt::GestureCanceled) {
0104                 mGCanceled->setCheckState(Qt::Checked);
0105             } else if (mGCanceled->isChecked() && mGFinished->isChecked()) {
0106                 qWarning()<<"ERROR: The gesture state is canceled and finished at the same time";
0107             }
0108         }
0109         e->accept();
0110         return true;
0111     }
0112     return QObject::eventFilter(watched, e);
0113 }
0114 
0115 void MainWindow::resetAll()
0116 {
0117     mGStarted->setCheckState(Qt::Unchecked);
0118     mGUpdated->setCheckState(Qt::Unchecked);
0119     mGCanceled->setCheckState(Qt::Unchecked);
0120     mGFinished->setCheckState(Qt::Unchecked);
0121 
0122     mGPos->setText(QString());
0123 }
0124 
0125 void MainWindow::slotSwipeTimeChanged(int value)
0126 {
0127     mTwoFingerRec->setTapRadius(value);
0128 }
0129 
0130 
0131 int main(int argc, char **argv)
0132 {
0133     QApplication::setApplicationName(QStringLiteral("ktwofingertap_test"));
0134     QApplication app(argc, argv);
0135 
0136     MainWindow mainWindow;
0137     mainWindow.setGeometry(500, 200, 500, 500);
0138     mainWindow.show();
0139 
0140     return app.exec();
0141 }
0142 
0143 #include "moc_ktwofingertap_test.cpp"