From 6b85ec8790aad8ac01c04f7e7bd4913d8cd8d104 Mon Sep 17 00:00:00 2001 From: Denis Dzyubenko Date: Mon, 3 Aug 2009 17:02:58 +0200 Subject: Removed the QTapAndHoldGesture Moved the gesture implementation to the imageviewer example as it cannot be fully implemented in a crossplatform way - for example on Windows tap and hold is a system gesture that is transparent to the application. Reviewed-by: trustme --- examples/gestures/imageviewer/imageviewer.pro | 11 ++- examples/gestures/imageviewer/imagewidget.cpp | 15 +++- examples/gestures/imageviewer/imagewidget.h | 4 +- .../gestures/imageviewer/tapandholdgesture.cpp | 97 ++++++++++++++++++++++ examples/gestures/imageviewer/tapandholdgesture.h | 32 +++++++ 5 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 examples/gestures/imageviewer/tapandholdgesture.cpp create mode 100644 examples/gestures/imageviewer/tapandholdgesture.h (limited to 'examples/gestures') diff --git a/examples/gestures/imageviewer/imageviewer.pro b/examples/gestures/imageviewer/imageviewer.pro index 4c35dcec20..efbca004c4 100644 --- a/examples/gestures/imageviewer/imageviewer.pro +++ b/examples/gestures/imageviewer/imageviewer.pro @@ -1,12 +1,11 @@ -###################################################################### -# Automatically generated by qmake (2.01a) Thu Sep 11 17:18:17 2008 -###################################################################### - TEMPLATE = app TARGET = DEPENDPATH += . INCLUDEPATH += . # Input -HEADERS += imagewidget.h -SOURCES += imagewidget.cpp main.cpp +HEADERS += imagewidget.h \ + tapandholdgesture.h +SOURCES += imagewidget.cpp \ + tapandholdgesture.cpp \ + main.cpp diff --git a/examples/gestures/imageviewer/imagewidget.cpp b/examples/gestures/imageviewer/imagewidget.cpp index 0b399970dd..99889ed4f2 100644 --- a/examples/gestures/imageviewer/imagewidget.cpp +++ b/examples/gestures/imageviewer/imagewidget.cpp @@ -65,7 +65,7 @@ ImageWidget::ImageWidget(QWidget *parent) panGesture = new QPanGesture(this); connect(panGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered())); - tapAndHoldGesture = new QTapAndHoldGesture(this); + tapAndHoldGesture = new TapAndHoldGesture(this); connect(tapAndHoldGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered())); } @@ -188,6 +188,19 @@ void ImageWidget::gestureTriggered() } } feedbackFadeOutTimer.start(500, this); + } else if (sender() == tapAndHoldGesture) { + if (tapAndHoldGesture->state() == Qt::GestureFinished) { + qDebug() << "tap and hold detected"; + touchFeedback.reset(); + update(); + + QMenu menu; + menu.addAction("Action 1"); + menu.addAction("Action 2"); + menu.addAction("Action 3"); + menu.exec(mapToGlobal(tapAndHoldGesture->pos())); + } + feedbackFadeOutTimer.start(500, this); } } diff --git a/examples/gestures/imageviewer/imagewidget.h b/examples/gestures/imageviewer/imagewidget.h index e12634d6c6..d8d5f8de8f 100644 --- a/examples/gestures/imageviewer/imagewidget.h +++ b/examples/gestures/imageviewer/imagewidget.h @@ -48,6 +48,8 @@ #include +#include "tapandholdgesture.h" + class ImageWidget : public QWidget { Q_OBJECT @@ -79,7 +81,7 @@ private: void goToImage(int index); QPanGesture *panGesture; - QTapAndHoldGesture *tapAndHoldGesture; + TapAndHoldGesture *tapAndHoldGesture; QString path; QStringList files; diff --git a/examples/gestures/imageviewer/tapandholdgesture.cpp b/examples/gestures/imageviewer/tapandholdgesture.cpp new file mode 100644 index 0000000000..c6f6779fe0 --- /dev/null +++ b/examples/gestures/imageviewer/tapandholdgesture.cpp @@ -0,0 +1,97 @@ +#include "tapandholdgesture.h" + +#include + +/*! + \class TapAndHoldGesture + \since 4.6 + + \brief The TapAndHoldGesture class represents a Tap-and-Hold gesture, + providing additional information. +*/ + +const int TapAndHoldGesture::iterationCount = 40; +const int TapAndHoldGesture::iterationTimeout = 50; + +/*! + Creates a new Tap and Hold gesture handler object and marks it as a child + of \a parent. + + On some platforms like Windows there is a system-wide tap and hold gesture + that cannot be overriden, hence the gesture might never trigger and default + context menu will be shown instead. +*/ +TapAndHoldGesture::TapAndHoldGesture(QWidget *parent) + : QGesture(parent), iteration(0) +{ +} + +/*! \internal */ +bool TapAndHoldGesture::filterEvent(QEvent *event) +{ + if (!event->spontaneous()) + return false; + const QTouchEvent *ev = static_cast(event); + switch (event->type()) { + case QEvent::TouchBegin: { + if (timer.isActive()) + timer.stop(); + timer.start(TapAndHoldGesture::iterationTimeout, this); + const QPoint p = ev->touchPoints().at(0).pos().toPoint(); + position = p; + break; + } + case QEvent::TouchUpdate: + if (ev->touchPoints().size() == 1) { + const QPoint startPos = ev->touchPoints().at(0).startPos().toPoint(); + const QPoint pos = ev->touchPoints().at(0).pos().toPoint(); + if ((startPos - pos).manhattanLength() > 15) + reset(); + } else { + reset(); + } + break; + case QEvent::TouchEnd: + reset(); + break; + default: + break; + } + return false; +} + +/*! \internal */ +void TapAndHoldGesture::timerEvent(QTimerEvent *event) +{ + if (event->timerId() != timer.timerId()) + return; + if (iteration == TapAndHoldGesture::iterationCount) { + timer.stop(); + setState(Qt::GestureFinished); + emit triggered(); + } else { + setState(Qt::GestureStarted); + emit triggered(); + } + ++iteration; +} + +/*! \internal */ +void TapAndHoldGesture::reset() +{ + if (state() != Qt::NoGesture) + emit cancelled(); + setState(Qt::NoGesture); + timer.stop(); + iteration = 0; +} + +/*! + \property TapAndHoldGesture::pos + + \brief The position of the gesture. +*/ +QPoint TapAndHoldGesture::pos() const +{ + return position; +} diff --git a/examples/gestures/imageviewer/tapandholdgesture.h b/examples/gestures/imageviewer/tapandholdgesture.h new file mode 100644 index 0000000000..711a1af4cc --- /dev/null +++ b/examples/gestures/imageviewer/tapandholdgesture.h @@ -0,0 +1,32 @@ +#ifndef TAPANDHOLDGESTURE_H +#define TAPANDHOLDGESTURE_H + +#include +#include +#include + +class TapAndHoldGesture : public QGesture +{ + Q_OBJECT + Q_PROPERTY(QPoint pos READ pos) + +public: + TapAndHoldGesture(QWidget *parent); + + bool filterEvent(QEvent *event); + void reset(); + + QPoint pos() const; + +protected: + void timerEvent(QTimerEvent *event); + +private: + QBasicTimer timer; + int iteration; + QPoint position; + static const int iterationCount; + static const int iterationTimeout; +}; + +#endif // TAPANDHOLDGESTURE_H -- cgit v1.2.3