File indexing completed on 2025-02-16 13:11:55
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 "ktwofingerswipe_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 <ktwofingerswipe.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 swipe 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 QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0049 0050 QLabel *timeLabel = new QLabel(QStringLiteral("maximum duration:"), mWidget); 0051 timeLabel->setSizePolicy(sizePolicy); 0052 mGSwipeTime = new QSpinBox(mWidget); 0053 mGSwipeTime->setMaximum(1000); 0054 0055 QLabel *distanceLabel = new QLabel(QStringLiteral("minimum distance:"), mWidget); 0056 distanceLabel->setSizePolicy(sizePolicy); 0057 mGSwipeDistance = new QSpinBox(mWidget); 0058 mGSwipeDistance->setMaximum(1000); 0059 0060 vLayout->addWidget(msgLabel); 0061 vLayout->addWidget(mGStarted); 0062 vLayout->addWidget(mGUpdated); 0063 vLayout->addWidget(mGCanceled); 0064 vLayout->addWidget(mGFinished); 0065 vLayout->addWidget(mGPos); 0066 vLayout->addWidget(timeLabel); 0067 vLayout->addWidget(mGSwipeTime); 0068 vLayout->addWidget(distanceLabel); 0069 vLayout->addWidget(mGSwipeDistance); 0070 0071 mTwoFingerRec = new KTwoFingerSwipeRecognizer(); 0072 mKTwoFingerSwipeGesture = QGestureRecognizer::registerRecognizer(mTwoFingerRec); 0073 0074 mGSwipeTime->setValue(mTwoFingerRec->maxSwipeTime()); 0075 mGSwipeDistance->setValue(mTwoFingerRec->minSswipeDistance()); 0076 0077 mWidget->grabGesture(mKTwoFingerSwipeGesture); 0078 mWidget->setAttribute(Qt::WA_AcceptTouchEvents); 0079 mWidget->installEventFilter(this); 0080 0081 connect (mGSwipeTime, &QSpinBox::valueChanged, this, &MainWindow::slotSwipeTimeChanged); 0082 connect (mGSwipeDistance, &QSpinBox::valueChanged, this, &MainWindow::slotSwipeDistanceChanged); 0083 } 0084 0085 MainWindow::~MainWindow() 0086 { 0087 } 0088 0089 bool MainWindow::eventFilter(QObject *watched, QEvent *e) 0090 { 0091 if (e->type() == QEvent::TouchBegin) { 0092 resetAll(); 0093 } 0094 if (e->type() == QEvent::Gesture) { 0095 QGestureEvent *gEvent = static_cast<QGestureEvent *>(e); 0096 KTwoFingerSwipe *swipe = static_cast<KTwoFingerSwipe *>(gEvent->gesture(mKTwoFingerSwipeGesture)); 0097 if (swipe) { 0098 // We need to map the local position, because the tap->pos() is the local position which on received the gesture. 0099 // In this case it is local to the Widgets in the layout. 0100 const QPoint lokalPos = mWidget->mapFromGlobal(swipe->scenePos().toPoint()); 0101 QString posStr = QStringLiteral("tap Position = "); 0102 posStr += QString::number(lokalPos.x()) + QStringLiteral(",") + QString::number(lokalPos.y()); 0103 0104 const Qt::GestureState state = swipe->state(); 0105 0106 if (state == Qt::GestureFinished) { 0107 posStr += QStringLiteral(" angle = "); 0108 posStr += QString::number(swipe->swipeAngle()); 0109 mGPos->setText(posStr); 0110 mGFinished->setCheckState(Qt::Checked); 0111 } else if (state == Qt::GestureStarted) { 0112 mGPos->setText(posStr); 0113 mGStarted->setCheckState(Qt::Checked); 0114 } else if (state == Qt::GestureUpdated) { 0115 mGUpdated->setCheckState(Qt::Checked); 0116 } else if (state == Qt::GestureCanceled) { 0117 mGCanceled->setCheckState(Qt::Checked); 0118 } 0119 } 0120 e->accept(); 0121 return true; 0122 } 0123 return QObject::eventFilter(watched, e); 0124 } 0125 0126 void MainWindow::resetAll() 0127 { 0128 mGStarted->setCheckState(Qt::Unchecked); 0129 mGUpdated->setCheckState(Qt::Unchecked); 0130 mGCanceled->setCheckState(Qt::Unchecked); 0131 mGFinished->setCheckState(Qt::Unchecked); 0132 0133 mGPos->setText(QString()); 0134 } 0135 0136 void MainWindow::slotSwipeTimeChanged(int value) 0137 { 0138 mTwoFingerRec->setMaxSwipeTime(value); 0139 } 0140 0141 void MainWindow::slotSwipeDistanceChanged(int value) 0142 { 0143 mTwoFingerRec->setSwipeDistance(value); 0144 } 0145 0146 int main(int argc, char **argv) 0147 { 0148 QApplication::setApplicationName(QStringLiteral("ktwofingerswipe_test")); 0149 QApplication app(argc, argv); 0150 0151 MainWindow mainWindow; 0152 mainWindow.setGeometry(500, 200, 500, 500); 0153 mainWindow.show(); 0154 0155 return app.exec(); 0156 } 0157 0158 #include "moc_ktwofingerswipe_test.cpp"