1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2021 Steffen Hartleib <steffenhartleib@t-online.de>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "ktwofingerswipe_test.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QTouchEvent>
#include <QSpinBox>
#include <ktwofingerswipe.h>
MainWindow::MainWindow()
: QMainWindow()
{
mWidget = new QWidget(this);
setCentralWidget(mWidget);
QVBoxLayout *vLayout = new QVBoxLayout (mWidget);
QLabel *msgLabel = new QLabel(QStringLiteral("Make a two finger swipe gesture in this window."), mWidget);
msgLabel->setAlignment(Qt::AlignHCenter);
mGStarted = new QCheckBox(QStringLiteral("gesture started"), mWidget);
mGStarted->setEnabled(false);
mGUpdated = new QCheckBox(QStringLiteral("gesture updated"), mWidget);
mGUpdated->setEnabled(false);
mGCanceled = new QCheckBox(QStringLiteral("gesture canceled"), mWidget);
mGCanceled->setEnabled(false);
mGFinished = new QCheckBox(QStringLiteral("gesture finished"), mWidget);
mGFinished->setEnabled(false);
mGPos = new QLineEdit(mWidget);
mGPos->setReadOnly(true);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QLabel *timeLabel = new QLabel(QStringLiteral("maximum duration:"), mWidget);
timeLabel->setSizePolicy(sizePolicy);
mGSwipeTime = new QSpinBox(mWidget);
mGSwipeTime->setMaximum(1000);
QLabel *distanceLabel = new QLabel(QStringLiteral("minimum distance:"), mWidget);
distanceLabel->setSizePolicy(sizePolicy);
mGSwipeDistance = new QSpinBox(mWidget);
mGSwipeDistance->setMaximum(1000);
vLayout->addWidget(msgLabel);
vLayout->addWidget(mGStarted);
vLayout->addWidget(mGUpdated);
vLayout->addWidget(mGCanceled);
vLayout->addWidget(mGFinished);
vLayout->addWidget(mGPos);
vLayout->addWidget(timeLabel);
vLayout->addWidget(mGSwipeTime);
vLayout->addWidget(distanceLabel);
vLayout->addWidget(mGSwipeDistance);
mTwoFingerRec = new KTwoFingerSwipeRecognizer();
mKTwoFingerSwipeGesture = QGestureRecognizer::registerRecognizer(mTwoFingerRec);
mGSwipeTime->setValue(mTwoFingerRec->maxSwipeTime());
mGSwipeDistance->setValue(mTwoFingerRec->minSswipeDistance());
mWidget->grabGesture(mKTwoFingerSwipeGesture);
mWidget->setAttribute(Qt::WA_AcceptTouchEvents);
mWidget->installEventFilter(this);
connect (mGSwipeTime, &QSpinBox::valueChanged, this, &MainWindow::slotSwipeTimeChanged);
connect (mGSwipeDistance, &QSpinBox::valueChanged, this, &MainWindow::slotSwipeDistanceChanged);
}
MainWindow::~MainWindow()
{
}
bool MainWindow::eventFilter(QObject *watched, QEvent *e)
{
if (e->type() == QEvent::TouchBegin) {
resetAll();
}
if (e->type() == QEvent::Gesture) {
QGestureEvent *gEvent = static_cast<QGestureEvent *>(e);
KTwoFingerSwipe *swipe = static_cast<KTwoFingerSwipe *>(gEvent->gesture(mKTwoFingerSwipeGesture));
if (swipe) {
// We need to map the local position, because the tap->pos() is the local position which on received the gesture.
// In this case it is local to the Widgets in the layout.
const QPoint lokalPos = mWidget->mapFromGlobal(swipe->scenePos().toPoint());
QString posStr = QStringLiteral("tap Position = ");
posStr += QString::number(lokalPos.x()) + QStringLiteral(",") + QString::number(lokalPos.y());
const Qt::GestureState state = swipe->state();
if (state == Qt::GestureFinished) {
posStr += QStringLiteral(" angle = ");
posStr += QString::number(swipe->swipeAngle());
mGPos->setText(posStr);
mGFinished->setCheckState(Qt::Checked);
} else if (state == Qt::GestureStarted) {
mGPos->setText(posStr);
mGStarted->setCheckState(Qt::Checked);
} else if (state == Qt::GestureUpdated) {
mGUpdated->setCheckState(Qt::Checked);
} else if (state == Qt::GestureCanceled) {
mGCanceled->setCheckState(Qt::Checked);
}
}
e->accept();
return true;
}
return QObject::eventFilter(watched, e);
}
void MainWindow::resetAll()
{
mGStarted->setCheckState(Qt::Unchecked);
mGUpdated->setCheckState(Qt::Unchecked);
mGCanceled->setCheckState(Qt::Unchecked);
mGFinished->setCheckState(Qt::Unchecked);
mGPos->setText(QString());
}
void MainWindow::slotSwipeTimeChanged(int value)
{
mTwoFingerRec->setMaxSwipeTime(value);
}
void MainWindow::slotSwipeDistanceChanged(int value)
{
mTwoFingerRec->setSwipeDistance(value);
}
int main(int argc, char **argv)
{
QApplication::setApplicationName(QStringLiteral("ktwofingerswipe_test"));
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setGeometry(500, 200, 500, 500);
mainWindow.show();
return app.exec();
}
#include "moc_ktwofingerswipe_test.cpp"
|