diff options
Diffstat (limited to 'examples/gestures/imageviewer')
-rw-r--r-- | examples/gestures/imageviewer/imageviewer.pro | 11 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.cpp | 52 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.h | 4 | ||||
-rw-r--r-- | examples/gestures/imageviewer/main.cpp | 3 | ||||
-rw-r--r-- | examples/gestures/imageviewer/tapandholdgesture.cpp | 157 | ||||
-rw-r--r-- | examples/gestures/imageviewer/tapandholdgesture.h | 74 |
6 files changed, 268 insertions, 33 deletions
diff --git a/examples/gestures/imageviewer/imageviewer.pro b/examples/gestures/imageviewer/imageviewer.pro index 4c35dce..efbca00 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 717bb09..c0d1e2d 100644 --- a/examples/gestures/imageviewer/imagewidget.cpp +++ b/examples/gestures/imageviewer/imagewidget.cpp @@ -65,8 +65,9 @@ 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())); + connect(tapAndHoldGesture, SIGNAL(finished()), this, SLOT(gestureTriggered())); } void ImageWidget::paintEvent(QPaintEvent*) @@ -100,7 +101,7 @@ void ImageWidget::paintEvent(QPaintEvent*) p.setPen(QPen(Qt::gray, 2)); p.drawEllipse(touchFeedback.position, 5, 5); if (touchFeedback.doubleTapped) { - p.setPen(QPen(Qt::gray, 2, Qt::DotLine)); + p.setPen(QPen(Qt::darkGray, 2, Qt::DotLine)); p.drawEllipse(touchFeedback.position, 15, 15); } else if (touchFeedback.tapAndHoldState != 0) { QPoint pts[8] = { @@ -112,7 +113,7 @@ void ImageWidget::paintEvent(QPaintEvent*) touchFeedback.position + QPoint(-10, 10), touchFeedback.position + QPoint(-15, 0) }; - for (int i = 0; i < (touchFeedback.tapAndHoldState-20)/10; ++i) + for (int i = 0; i < touchFeedback.tapAndHoldState/5; ++i) p.drawEllipse(pts[i], 3, 3); } } else if (touchFeedback.sliding) { @@ -156,43 +157,39 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event) void ImageWidget::gestureTriggered() { - touchFeedback.tapped = false; - touchFeedback.doubleTapped = false; - - QGesture *g = qobject_cast<QGesture*>(sender()); if (sender() == panGesture) { + touchFeedback.tapped = false; + touchFeedback.doubleTapped = false; + QPanGesture *pg = qobject_cast<QPanGesture*>(sender()); if (zoomedIn) { - // usual panning #ifndef QT_NO_CURSOR - if (g->state() == Qt::GestureStarted) - setCursor(Qt::SizeAllCursor); - else - setCursor(Qt::ArrowCursor); + switch (pg->state()) { + case Qt::GestureStarted: + case Qt::GestureUpdated: + setCursor(Qt::SizeAllCursor); + break; + default: + setCursor(Qt::ArrowCursor); + } #endif - const int dx = g->pos().x() - g->lastPos().x(); - const int dy = g->pos().y() - g->lastPos().y(); - horizontalOffset += dx; - verticalOffset += dy; - update(); + horizontalOffset += pg->lastOffset().width(); + verticalOffset += pg->lastOffset().height(); } else { // only slide gesture should be accepted - const QPanGesture *pg = static_cast<const QPanGesture*>(g); - if (g->state() == Qt::GestureFinished) { + if (pg->state() == Qt::GestureFinished) { touchFeedback.sliding = false; zoomed = rotated = false; - if (pg->totalOffset().width() > 0) { - qDebug() << "slide right"; + if (pg->totalOffset().width() > 0) goNextImage(); - } else { - qDebug() << "slide left"; + else goPrevImage(); - } updateImage(); } } + update(); feedbackFadeOutTimer.start(500, this); } else if (sender() == tapAndHoldGesture) { - if (g->state() == Qt::GestureFinished) { + if (tapAndHoldGesture->state() == Qt::GestureFinished) { qDebug() << "tap and hold detected"; touchFeedback.reset(); update(); @@ -201,7 +198,10 @@ void ImageWidget::gestureTriggered() menu.addAction("Action 1"); menu.addAction("Action 2"); menu.addAction("Action 3"); - menu.exec(mapToGlobal(g->pos())); + menu.exec(mapToGlobal(tapAndHoldGesture->pos())); + } else { + ++touchFeedback.tapAndHoldState; + update(); } feedbackFadeOutTimer.start(500, this); } diff --git a/examples/gestures/imageviewer/imagewidget.h b/examples/gestures/imageviewer/imagewidget.h index e12634d..d8d5f8d 100644 --- a/examples/gestures/imageviewer/imagewidget.h +++ b/examples/gestures/imageviewer/imagewidget.h @@ -48,6 +48,8 @@ #include <QtGui> +#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/main.cpp b/examples/gestures/imageviewer/main.cpp index 6bda418..cd1928b 100644 --- a/examples/gestures/imageviewer/main.cpp +++ b/examples/gestures/imageviewer/main.cpp @@ -81,6 +81,9 @@ int main(int argc, char *argv[]) if (QApplication::arguments().size() > 1) w.openDirectory(QApplication::arguments().at(1)); + else + w.openDirectory(QFileDialog::getExistingDirectory(0, "Select image folder")); + return app.exec(); } diff --git a/examples/gestures/imageviewer/tapandholdgesture.cpp b/examples/gestures/imageviewer/tapandholdgesture.cpp new file mode 100644 index 0000000..5fe52cc --- /dev/null +++ b/examples/gestures/imageviewer/tapandholdgesture.cpp @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** 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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $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) +{ + if (!event->spontaneous()) + return false; + 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 new file mode 100644 index 0000000..61fabc2 --- /dev/null +++ b/examples/gestures/imageviewer/tapandholdgesture.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** 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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $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 |