diff options
Diffstat (limited to 'examples/gestures')
-rw-r--r-- | examples/gestures/imageviewer/imageviewer.pro | 13 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.cpp | 80 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.h | 15 | ||||
-rw-r--r-- | examples/gestures/imageviewer/tapandholdgesture.cpp | 155 | ||||
-rw-r--r-- | examples/gestures/imageviewer/tapandholdgesture.h | 74 |
5 files changed, 64 insertions, 273 deletions
diff --git a/examples/gestures/imageviewer/imageviewer.pro b/examples/gestures/imageviewer/imageviewer.pro index 124175e..68c1f1c 100644 --- a/examples/gestures/imageviewer/imageviewer.pro +++ b/examples/gestures/imageviewer/imageviewer.pro @@ -1,11 +1,14 @@ -HEADERS += imagewidget.h \ - tapandholdgesture.h +HEADERS += imagewidget.h SOURCES += imagewidget.cpp \ - tapandholdgesture.cpp \ main.cpp # install target.path = $$[QT_INSTALL_EXAMPLES]/gestures/imageviewer -sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS imageviewer.pro +sources.files = $$SOURCES \ + $$HEADERS \ + $$RESOURCES \ + $$FORMS \ + imageviewer.pro sources.path = $$[QT_INSTALL_EXAMPLES]/gestures/imageviewer -INSTALLS += target sources +INSTALLS += target \ + sources diff --git a/examples/gestures/imageviewer/imagewidget.cpp b/examples/gestures/imageviewer/imagewidget.cpp index 3489b5b..5633942 100644 --- a/examples/gestures/imageviewer/imagewidget.cpp +++ b/examples/gestures/imageviewer/imagewidget.cpp @@ -59,24 +59,16 @@ ImageWidget::ImageWidget(QWidget *parent) setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_NoSystemBackground); - QGesture *panGesture = new QPanGesture(this); - connect(panGesture, SIGNAL(started()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(finished()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(canceled()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(triggered()), this, SLOT(panTriggered())); - - QGesture *pinchGesture = new QPinchGesture(this); - connect(pinchGesture, SIGNAL(started()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(finished()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(canceled()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(triggered()), this, SLOT(pinchTriggered())); - -//! [construct swipe gesture] - QGesture *swipeGesture = new QSwipeGesture(this); -//! [construct swipe gesture] -//! [connect swipe gesture] - connect(swipeGesture, SIGNAL(triggered()), this, SLOT(swipeTriggered())); -//! [connect swipe gesture] + grabGesture(Qt::PanGesture); + grabGesture(Qt::PinchGesture); + grabGesture(Qt::SwipeGesture); +} + +bool ImageWidget::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast<QGestureEvent*>(event)); + return QWidget::event(event); } void ImageWidget::paintEvent(QPaintEvent*) @@ -106,11 +98,25 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) update(); } -void ImageWidget::panTriggered() +bool ImageWidget::gestureEvent(QGestureEvent *event) +{ + if (QGesture *pan = event->gesture(Qt::PanGesture)) { + panTriggered(static_cast<QPanGesture*>(pan)); + return true; + } else if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { + pinchTriggered(static_cast<QPinchGesture*>(pan)); + return true; + } else if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) { + swipeTriggered(static_cast<QSwipeGesture*>(pan)); + return true; + } + return false; +} + +void ImageWidget::panTriggered(QPanGesture *gesture) { - QPanGesture *pg = qobject_cast<QPanGesture*>(sender()); #ifndef QT_NO_CURSOR - switch (pg->state()) { + switch (gesture->state()) { case Qt::GestureStarted: case Qt::GestureUpdated: setCursor(Qt::SizeAllCursor); @@ -119,33 +125,37 @@ void ImageWidget::panTriggered() setCursor(Qt::ArrowCursor); } #endif - horizontalOffset += pg->lastOffset().width(); - verticalOffset += pg->lastOffset().height(); + QSizeF lastOffset = gesture->property("lastOffset").toSizeF(); + horizontalOffset += lastOffset.width(); + verticalOffset += lastOffset.height(); update(); } -void ImageWidget::pinchTriggered() +void ImageWidget::pinchTriggered(QPinchGesture *gesture) { - QPinchGesture *pg = qobject_cast<QPinchGesture*>(sender()); - if (pg->whatChanged() & QPinchGesture::RotationAngleChanged) - rotationAngle += pg->rotationAngle() - pg->lastRotationAngle(); - if (pg->whatChanged() & QPinchGesture::ScaleFactorChanged) - scaleFactor += pg->scaleFactor() - pg->lastScaleFactor(); + QPinchGesture::WhatChanged whatChanged = gesture->property("whatChanged").value<QPinchGesture::WhatChanged>(); + if (whatChanged & QPinchGesture::RotationAngleChanged) { + qreal value = gesture->property("rotationAngle").toReal(); + qreal lastValue = gesture->property("lastRotationAngle").toReal(); + rotationAngle += value - lastValue; + } + if (whatChanged & QPinchGesture::ScaleFactorChanged) { + qreal value = gesture->property("scaleFactor").toReal(); + qreal lastValue = gesture->property("lastScaleFactor").toReal(); + scaleFactor += value - lastValue; + } update(); } -//! [swipe slot start] -void ImageWidget::swipeTriggered() +void ImageWidget::swipeTriggered(QSwipeGesture *gesture) { - QSwipeGesture *pg = qobject_cast<QSwipeGesture*>(sender()); - if (pg->horizontalDirection() == QSwipeGesture::Left - || pg->verticalDirection() == QSwipeGesture::Up) + if (gesture->horizontalDirection() == QSwipeGesture::Left + || gesture->verticalDirection() == QSwipeGesture::Up) goPrevImage(); else goNextImage(); update(); } -//! [swipe slot start] void ImageWidget::resizeEvent(QResizeEvent*) { diff --git a/examples/gestures/imageviewer/imagewidget.h b/examples/gestures/imageviewer/imagewidget.h index 2a1bfca..e05a67a 100644 --- a/examples/gestures/imageviewer/imagewidget.h +++ b/examples/gestures/imageviewer/imagewidget.h @@ -46,6 +46,11 @@ #include <QImage> #include <QtGui> +class QGestureEvent; +class QPanGesture; +class QPinchGesture; +class QSwipeGesture; + class ImageWidget : public QWidget { Q_OBJECT @@ -56,14 +61,16 @@ public: void openDirectory(const QString &path); protected: + bool event(QEvent*); + bool gestureEvent(QGestureEvent*); void paintEvent(QPaintEvent*); void resizeEvent(QResizeEvent*); void mouseDoubleClickEvent(QMouseEvent*); -private slots: - void panTriggered(); - void pinchTriggered(); - void swipeTriggered(); +private: + void panTriggered(QPanGesture*); + void pinchTriggered(QPinchGesture*); + void swipeTriggered(QSwipeGesture*); private: void updateImage(); diff --git a/examples/gestures/imageviewer/tapandholdgesture.cpp b/examples/gestures/imageviewer/tapandholdgesture.cpp deleted file mode 100644 index a10c192..0000000 --- a/examples/gestures/imageviewer/tapandholdgesture.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "tapandholdgesture.h" - -#include <QtGui/qevent.h> - -// #define TAPANDHOLD_USING_MOUSE - -/*! - \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) -{ - const QTouchEvent *ev = static_cast<const QTouchEvent*>(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; -#ifdef TAPANDHOLD_USING_MOUSE - case QEvent::MouseButtonPress: { - if (timer.isActive()) - timer.stop(); - timer.start(TapAndHoldGesture::iterationTimeout, this); - const QPoint p = static_cast<QMouseEvent*>(event)->pos(); - position = startPosition = p; - break; - } - case QEvent::MouseMove: { - const QPoint startPos = startPosition; - const QPoint pos = static_cast<QMouseEvent*>(event)->pos(); - if ((startPos - pos).manhattanLength() > 15) - reset(); - break; - } - case QEvent::MouseButtonRelease: - reset(); - break; -#endif // TAPANDHOLD_USING_MOUSE - default: - break; - } - return false; -} - -/*! \internal */ -void TapAndHoldGesture::timerEvent(QTimerEvent *event) -{ - if (event->timerId() != timer.timerId()) - return; - if (iteration == TapAndHoldGesture::iterationCount) { - timer.stop(); - updateState(Qt::GestureFinished); - } else { - updateState(Qt::GestureUpdated); - } - ++iteration; -} - -/*! \internal */ -void TapAndHoldGesture::reset() -{ - timer.stop(); - iteration = 0; - position = startPosition = QPoint(); - updateState(Qt::NoGesture); -} - -/*! - \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 deleted file mode 100644 index 682342e..0000000 --- a/examples/gestures/imageviewer/tapandholdgesture.h +++ /dev/null @@ -1,74 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef TAPANDHOLDGESTURE_H -#define TAPANDHOLDGESTURE_H - -#include <QtCore/QBasicTimer> -#include <QtGui/QGesture> -#include <QtGui/QWidget> - -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; - QPoint startPosition; - static const int iterationCount; - static const int iterationTimeout; -}; - -#endif // TAPANDHOLDGESTURE_H |