diff options
-rw-r--r-- | doc/src/frameworks-technologies/gestures.qdoc | 245 | ||||
-rw-r--r-- | doc/src/getting-started/examples.qdoc | 7 | ||||
-rw-r--r-- | doc/src/snippets/gestures/imageviewer/imagewidget.cpp | 364 | ||||
-rw-r--r-- | doc/src/snippets/gestures/imageviewer/imagewidget.h | 137 | ||||
-rw-r--r-- | doc/src/snippets/gestures/imageviewer/tapandholdgesture.cpp | 159 | ||||
-rw-r--r-- | doc/src/snippets/gestures/qgesture.cpp | 283 | ||||
-rw-r--r-- | doc/src/snippets/gestures/qstandardgestures.cpp | 483 | ||||
-rw-r--r-- | doc/src/snippets/gestures/qstandardgestures.h | 126 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imageviewer.pro | 8 | ||||
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.cpp | 10 | ||||
-rw-r--r-- | examples/gestures/imageviewer/main.cpp | 33 | ||||
-rw-r--r-- | examples/gestures/imageviewer/mainwidget.cpp (renamed from doc/src/snippets/gestures/qgesture.h) | 71 | ||||
-rw-r--r-- | examples/gestures/imageviewer/mainwidget.h (renamed from doc/src/snippets/gestures/imageviewer/tapandholdgesture.h) | 33 | ||||
-rw-r--r-- | src/gui/kernel/qgesture.cpp | 26 | ||||
-rw-r--r-- | src/gui/kernel/qgesturerecognizer.cpp | 23 |
15 files changed, 170 insertions, 1838 deletions
diff --git a/doc/src/frameworks-technologies/gestures.qdoc b/doc/src/frameworks-technologies/gestures.qdoc index 158a273..e5947a2 100644 --- a/doc/src/frameworks-technologies/gestures.qdoc +++ b/doc/src/frameworks-technologies/gestures.qdoc @@ -59,176 +59,115 @@ \section1 Overview - QGesture is the central class in Qt's gesture framework, providing - the API used by classes that represent specific gestures, such as - QPanGesture, QPinchGesture, and QSwipeGesture. These standard - classes are ready to use, and each exposes functions and - properties that give gesture-specific information about the user's - input. This is described in the \l{Using Standard Gestures With Widgets} - section. - - QGesture is also designed to be subclassed and extended so that - support for new gestures can be implemented by developers. Adding - support for a new gesture involves implementing code to recognize - the gesture from incoming events. This is described in the + QGesture is the central class in Qt's gesture framework, providing a container + for information about gestures performed by the user, such as panning, pinching + and swiping. QGesture exposes properties that give general information that is + common to all gestures, and these can be extended to provide additional + gesture-specific information. + + Developers can also implement new gestures by subclassing and extending the + QGestureRecognizer class. Adding support for a new gesture involves implementing + code to recognize the gesture from input events. This is described in the \l{Creating Your Own Gesture Recognizer} section. \section1 Using Standard Gestures with Widgets - Gesture objects are applied directly to widgets and other controls that accept - user input \mdash these are the \e{target objects}. When a gesture object is - constructed, the target object is typically passed to the constructor, though - it can also be passed as the argument to the \l{QGesture::}{setGestureTarget()} - function. + Gestures can be enabled for instances of QWidget and QGraphicsObject subclasses. + An object that accepts gesture input is referred to as a \e{target object}. - \snippet examples/gestures/imageviewer/imagewidget.cpp construct swipe gesture + To enable a gesture for a target object, call its QWidget::grabGesture() or + QGraphicsObject::grabGesture() function with an argument describing the + required gesture type. The standard types are defined by the Qt::GestureType + enum and include many commonly used gestures. + + \snippet examples/gestures/imageviewer/imagewidget.cpp enable gestures In the above code, the gesture is set up in the constructor of the target object - itself, so the argument to the QSwipeGesture constructor is \e this. + itself. + + When the user performs a gesture, QGestureEvent events will be delivered to the + target object, and these can be handled by reimplementing the QWidget::event() + handler function for widgets or QGraphicsItem::sceneEvent() for graphics objects. + + For convenience, the \l{Image Gestures Example} reimplements the general + \l{QWidget::}{event()} handler function and delegates gesture events to a + specialized gestureEvent() function: - When the user performs a gesture, various signals may be emitted by the - gesture object. To monitor the user's actions, you need to connect signals - from the gesture object to slots in your code. + \snippet examples/gestures/imageviewer/imagewidget.cpp event handler - \snippet examples/gestures/imageviewer/imagewidget.cpp connect swipe gesture + The gesture events delivered to the target object can be examined individually + and dealt with appropriately: - Here, the \l{QGesture::}{triggered()} signal is used to inform the application - that a gesture was used. More precise monitoring of a gesture can be implemented - by connecting its \l{QGesture::}{started()}, \l{QGesture::}{canceled()} and - \l{QGesture::}{finished()} signals to slots. + \snippet examples/gestures/imageviewer/imagewidget.cpp gesture event handler - Responding to a signal is simply a matter of obtaining the gesture that sent - it and examining the information it contains. + Responding to a gesture is simply a matter of obtaining the QGesture object + delivered in the QGestureEvent sent to the target object and examining the + information it contains. - \snippet examples/gestures/imageviewer/imagewidget.cpp swipe slot start + \snippet examples/gestures/imageviewer/imagewidget.cpp swipe function Here, we examine the direction in which the user swiped the widget and modify its contents accordingly. - \section1 Using Standard Gestures with Graphics Items - - The approach used for applying gestures to widgets can also be used with - graphics items. However, instead of passing a target object to the - gesture object's constructor, you set a target graphics item with the - \l{QGesture::}{setGraphicsItem()} function. \section1 Creating Your Own Gesture Recognizer - QGesture is a base class for a user defined gesture recognizer class. In - order to implement the recognizer you will need to subclass the - QGesture class and implement the pure virtual function - \l{QGesture::}{filterEvent()} to filter out events that are not relevant - to your gesture. - - Once you have implemented the \l{QGesture::}{filterEvent()} function to - make your own recognizer you can process events. A sequence of events may, - according to your own rules, represent a gesture. The events can be singly - passed to the recognizer via the \l{QGesture::}{filterEvent()} function - or as a stream of events by specifying a parent source of events. The events - can be from any source and could result in any action as defined by the user. - The source and action need not be graphical, though that would be the most - likely scenario. To find how to connect a source of events to automatically - feed into the recognizer see the QGesture documentation. - - Recognizers based on QGesture can emit any of the following signals to - indicate their progress in recognizing user input: - - \list - \o \l{QGesture::}{triggered()} is emitted when a gesture is recognized. - \o \l{QGesture::}{started()} indicates that the gesture object has started - to recognize user input. - \o \l{QGesture::}{finished()} is emitted when the gesture object has - recognized the user input as a gesture, and finished handling it. - \o \l{QGesture::}{canceled()} indicates that the gesture was canceled, - either by the user or by the application. - \endlist - - These signals are emitted when the state changes with the call to - \l{QGesture::}{updateState()}, more than one signal may - be emitted when a change of state occurs. There are four GestureStates - - \table - \header \o New State \o Description \o QGesture Actions on Entering this State - \row \o Qt::NoGesture \o Initial value \o emit \l {QGesture::}{canceled()} - \row \o Qt::GestureStarted \o A continuous gesture has started \o emit \l{QGesture::}{started()} and emit \l{QGesture::}{triggered()} - \row \o Qt::GestureUpdated \o A gesture continues \o emit \l{QGesture::}{triggered()} - \row \o Qt::GestureFinished \o A gesture has finished. \o emit \l{QGesture::}{finished()} - \endtable - - \note \l{QGesture::started()}{started()} can be emitted if entering any - state greater than NoGesture if NoGesture was the previous state. This - means that your state machine does not need to explicitly use the - Qt::GestureStarted state, you can simply proceed from NoGesture to - Qt::GestureUpdated to emit a \l{QGesture::started()}{started()} signal - and a \l{QGesture::triggered()}{triggered()} signal. - - You may use some or all of these states when implementing the pure - virtual function \l{QGesture::filterEvent()}{filterEvent()}. - \l{QGesture::filterEvent()}{filterEvent()} will usually implement a - state machine using the GestureState enums, but the details of which - states are used is up to the developer. - - You may also need to reimplement the virtual function \l{QGesture::reset()}{reset()} - if internal data or objects need to be re-initialized. The function must - conclude with a call to \l{QGesture::updateState()}{updateState()} to - change the current state to Qt::NoGesture. - - \section1 The ImageViewer Example - - To illustrate how to use QGesture we will look at the ImageViewer - example. This example uses QPanGesture, a standard gesture, and an - implementation of TapAndHoldGesture. Note that TapAndHoldGesture is - platform-dependent. - - \snippet doc/src/snippets/gestures/imageviewer/tapandholdgesture.cpp tapandhold-reset - - In ImageViewer we see that the ImageWidget class uses two gestures: - \l QPanGesture and TapAndHoldGesture. The - QPanGesture is a standard gesture which is part of Qt. - TapAndHoldGesture is defined and implemented as part of the example. - The ImageWidget listens for signals from the gestures, but is not - interested in the \l{QGesture::started()}{started()} signal. - - \snippet doc/src/snippets/gestures/imageviewer/imagewidget.h imagewidget-slots - - TapAndHoldGesture uses QTouchEvent events and mouse events to detect - start, update and end events that can be mapped onto the GestureState - changes. The implementation in this case uses a timer as well. If the - timeout event occurs a given number of times after the start of the gesture - then the gesture is considered to have finished whether or not the - appropriate touch or mouse event has occurred. Also if a large jump in - the position of the event occurs, as calculated by the \l {QPoint::manhattanLength()}{manhattanLength()} - call, then the gesture is canceled by calling \l{QGesture::reset()}{reset()} - which tidies up and uses \l{QGesture::updateState()}{updateState()} to - change state to NoGesture which will result in the \l{QGesture::canceled()}{canceled()} - signal being emitted by the recognizer. - - ImageWidget handles the signals by connecting the slots to the signals, - although \c canceled() is not connected here. - - \snippet doc/src/snippets/gestures/imageviewer/imagewidget.cpp imagewidget-connect - - These functions in turn will have to be aware of which gesture - object was the source of the signal since we have more than one source - per slot. This is easily done by using the QObject::sender() function - as shown here - - \snippet doc/src/snippets/gestures/imageviewer/imagewidget.cpp imagewidget-triggered-1 - - As \l{QGesture::triggered()}{triggered()} signals are handled by - gestureTriggered() there may be position updates invoking calls to, - for example, goNextImage(), this will cause a change in the image - handling logic of ImageWidget and a call to updateImage() to display - the changed state. - - Following the logic of how the QEvent is processed we can summmarize - it as follows: - \list - \o filterEvent() becomes the event filter of the parent ImageWidget object - for a QPanGesture object and a TapAndHoldGesture object. - \o filterEvent() then calls updateState() to change states - \o updateState() emits the appropriate signal(s) for the state change. - \o The signals are caught by the defined slots in ImageWidget - \o The widget logic changes and an update() results in a paint event. - \endlist + Adding support for a new gesture involves creating and registering a new gesture + recognizer. Depending on the recognition process for the gesture, it may also + involve creating a new gesture object. + + To create a new recognizer, you need to subclass QGestureRecognizer to create a + custom recognizer class. There is one virtual function that you must reimplement + and two others that can be reimplemented as required. + + \section2 Filtering Input Events + + The \l{QGestureRecognizer::}{filterEvent()} function must be reimplemented. + This function handles and filters the incoming input events for the target objects + and determines whether or not they correspond to the gesture the recognizer is + looking for. + + Although the logic for gesture recognition is implemented in this function, + possibly using a state machine based on the Qt::GestureState enums, you can store + persistent information about the state of the recognition process in the QGesture + object supplied. + + Your \l{QGestureRecognizer::}{filterEvent()} function must return a value of + Qt::GestureState that indicates the state of recognition for a given gesture and + target object. This determines whether or not a gesture event will be delivered + to a target object. + + \section2 Custom Gestures + + If you choose to represent a gesture by a custom QGesture subclass, you will need to + reimplement the \l{QGestureRecognizer::}{createGesture()} function to construct + instances of your gesture class instead of standard QGesture instances. Alternatively, + you may want to use standard QGesture instances, but add additional dynamic properties + to them to express specific details of the gesture you want to handle. + + \section2 Resetting Gestures + + If you use custom gesture objects that need to be reset or otherwise specially + handled when a gesture is canceled, you need to reimplement the + \l{QGestureRecognizer::}{reset()} function to perform these special tasks. + + Note that QGesture objects are only created once for each combination of target object + and gesture type, and they are reused every time the user attempts to perform the + same gesture type on the target object. As a result, it can be useful to reimplement + the \l{QGestureRecognizer::}{reset()} function to clean up after each previous attempt + at recognizing a gesture. + + + \section1 Using a New Gesture Recognizer + + To use a gesture recognizer, construct an instance of your QGestureRecognizer + subclass, and register it with the application with + QApplication::registerGestureRecognizer(). A recognizer for a given type of + gesture can be removed with QApplication::unregisterGestureRecognizer(). + + + \section1 Further Reading + + The \l{Image Gestures Example} shows how to enable gestures for a widget in + a simple image viewer application. */ diff --git a/doc/src/getting-started/examples.qdoc b/doc/src/getting-started/examples.qdoc index d6ade22..05940e4 100644 --- a/doc/src/getting-started/examples.qdoc +++ b/doc/src/getting-started/examples.qdoc @@ -1072,18 +1072,17 @@ /*! \page examples-gestures.html \title Gestures Examples - + \previouspage Animation Framework Examples \contentspage Qt Examples \nextpage D-Bus Examples The API of the gesture framework is not yet finalized and still subject to change. -\omit + \list - \o \l{widgets/imageviewer}{Image Viewer} + \o \l{gestures/imagegestures}{Image Gestures} \endlist -\endomit */ /*! diff --git a/doc/src/snippets/gestures/imageviewer/imagewidget.cpp b/doc/src/snippets/gestures/imageviewer/imagewidget.cpp deleted file mode 100644 index 409db2e..0000000 --- a/doc/src/snippets/gestures/imageviewer/imagewidget.cpp +++ /dev/null @@ -1,364 +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 "imagewidget.h" - -#include <QtGui> - -ImageWidget::ImageWidget(QWidget *parent) - : QWidget(parent) -{ - setAttribute(Qt::WA_AcceptTouchEvents); - setAttribute(Qt::WA_PaintOnScreen); - setAttribute(Qt::WA_OpaquePaintEvent); - setAttribute(Qt::WA_NoSystemBackground); - - setObjectName("ImageWidget"); - - setMinimumSize(QSize(100,100)); - - position = 0; - zoomed = rotated = false; - - zoomedIn = false; - horizontalOffset = 0; - verticalOffset = 0; - -//! [imagewidget-connect] - panGesture = new QPanGesture(this); - connect(panGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered())); - - tapAndHoldGesture = new TapAndHoldGesture(this); - connect(tapAndHoldGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered())); - connect(tapAndHoldGesture, SIGNAL(finished()), this, SLOT(gestureFinished())); -//! [imagewidget-connect] -} - -void ImageWidget::paintEvent(QPaintEvent*) -{ - QPainter p(this); - if (currentImage.isNull()) { - p.fillRect(geometry(), Qt::white); - return; - } - int hoffset = 0; - int voffset = 0; - const int w = pixmap.width(); - const int h = pixmap.height(); - p.save(); - if (zoomedIn) { - hoffset = horizontalOffset; - voffset = verticalOffset; - if (horizontalOffset > 0) - p.fillRect(0, 0, horizontalOffset, height(), Qt::white); - if (verticalOffset > 0) - p.fillRect(0, 0, width(), verticalOffset, Qt::white); - } - p.drawPixmap(hoffset, voffset, pixmap); - if (hoffset + w < width()) - p.fillRect(hoffset + w, 0, width() - w - hoffset, height(), Qt::white); - if (voffset + h < height()) - p.fillRect(0, voffset + h, width(), height() - h - voffset, Qt::white); - - // paint touch feedback - if (touchFeedback.tapped || touchFeedback.doubleTapped) { - p.setPen(QPen(Qt::gray, 2)); - p.drawEllipse(touchFeedback.position, 5, 5); - if (touchFeedback.doubleTapped) { - p.setPen(QPen(Qt::darkGray, 2, Qt::DotLine)); - p.drawEllipse(touchFeedback.position, 15, 15); - } else if (touchFeedback.tapAndHoldState != 0) { - QPoint pts[8] = { - touchFeedback.position + QPoint( 0, -15), - touchFeedback.position + QPoint( 10, -10), - touchFeedback.position + QPoint( 15, 0), - touchFeedback.position + QPoint( 10, 10), - touchFeedback.position + QPoint( 0, 15), - touchFeedback.position + QPoint(-10, 10), - touchFeedback.position + QPoint(-15, 0) - }; - for (int i = 0; i < touchFeedback.tapAndHoldState/5; ++i) - p.drawEllipse(pts[i], 3, 3); - } - } else if (touchFeedback.sliding) { - p.setPen(QPen(Qt::red, 3)); - QPoint endPos = QPoint(touchFeedback.position.x(), touchFeedback.slidingStartPosition.y()); - p.drawLine(touchFeedback.slidingStartPosition, endPos); - int dx = 10; - if (touchFeedback.slidingStartPosition.x() < endPos.x()) - dx = -1*dx; - p.drawLine(endPos, endPos + QPoint(dx, 5)); - p.drawLine(endPos, endPos + QPoint(dx, -5)); - } - - for (int i = 0; i < TouchFeedback::MaximumNumberOfTouches; ++i) { - if (touchFeedback.touches[i].isNull()) - break; - p.drawEllipse(touchFeedback.touches[i], 10, 10); - } - p.restore(); -} - -void ImageWidget::mousePressEvent(QMouseEvent *event) -{ - touchFeedback.tapped = true; - touchFeedback.position = event->pos(); -} - -void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event) -{ - touchFeedback.doubleTapped = true; - const QPoint p = event->pos(); - touchFeedback.position = p; - horizontalOffset = p.x() - currentImage.width()*1.0*p.x()/width(); - verticalOffset = p.y() - currentImage.height()*1.0*p.y()/height(); - setZoomedIn(!zoomedIn); - zoomed = rotated = false; - updateImage(); - - feedbackFadeOutTimer.start(500, this); -} - -//! [imagewidget-triggered-1] -void ImageWidget::gestureTriggered() -{ - if (sender() == panGesture) { -//! [imagewidget-triggered-1] - touchFeedback.tapped = false; - touchFeedback.doubleTapped = false; - QPanGesture *pg = qobject_cast<QPanGesture*>(sender()); - if (zoomedIn) { -#ifndef QT_NO_CURSOR - switch (pg->state()) { - case Qt::GestureStarted: - case Qt::GestureUpdated: - setCursor(Qt::SizeAllCursor); - break; - default: - setCursor(Qt::ArrowCursor); - } -#endif - horizontalOffset += pg->lastOffset().width(); - verticalOffset += pg->lastOffset().height(); - } else { - // only slide gesture should be accepted - if (pg->state() == Qt::GestureFinished) { - touchFeedback.sliding = false; - zoomed = rotated = false; - if (pg->totalOffset().width() > 0) - goNextImage(); - else - goPrevImage(); - updateImage(); - } - } - update(); - 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())); - } else { - ++touchFeedback.tapAndHoldState; - update(); - } - feedbackFadeOutTimer.start(500, this); - } -} - -void ImageWidget::gestureFinished() -{ - qDebug() << "gesture finished" << sender(); -} - -void ImageWidget::gestureCancelled() -{ - qDebug() << "gesture cancelled" << sender(); -} - -void ImageWidget::resizeEvent(QResizeEvent*) -{ - updateImage(); -} - -void ImageWidget::updateImage() -{ - // should use qtconcurrent here? - transformation = QTransform(); - if (zoomedIn) { - } else { - if (currentImage.isNull()) - return; - if (zoomed) { - transformation = transformation.scale(zoom, zoom); - } else { - double xscale = (double)width()/currentImage.width(); - double yscale = (double)height()/currentImage.height(); - if (xscale < yscale) - yscale = xscale; - else - xscale = yscale; - transformation = transformation.scale(xscale, yscale); - } - if (rotated) - transformation = transformation.rotate(angle); - } - pixmap = QPixmap::fromImage(currentImage).transformed(transformation); - update(); -} - -void ImageWidget::openDirectory(const QString &path) -{ - this->path = path; - QDir dir(path); - QStringList nameFilters; - nameFilters << "*.jpg" << "*.png"; - files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name); - - position = 0; - goToImage(0); - updateImage(); -} - -QImage ImageWidget::loadImage(const QString &fileName) -{ - QImageReader reader(fileName); - if (!reader.canRead()) { - qDebug() << fileName << ": can't load image"; - return QImage(); - } - QImage image; - if (!reader.read(&image)) { - qDebug() << fileName << ": corrupted image"; - return QImage(); - } - return image; -} - -void ImageWidget::setZoomedIn(bool zoomed) -{ - zoomedIn = zoomed; -} - -void ImageWidget::goNextImage() -{ - if (files.isEmpty()) - return; - if (position < files.size()-1) { - ++position; - prevImage = currentImage; - currentImage = nextImage; - if (position+1 < files.size()) - nextImage = loadImage(path+QLatin1String("/")+files.at(position+1)); - else - nextImage = QImage(); - } - setZoomedIn(false); - updateImage(); -} - -void ImageWidget::goPrevImage() -{ - if (files.isEmpty()) - return; - if (position > 0) { - --position; - nextImage = currentImage; - currentImage = prevImage; - if (position > 0) - prevImage = loadImage(path+QLatin1String("/")+files.at(position-1)); - else - prevImage = QImage(); - } - setZoomedIn(false); - updateImage(); -} - -void ImageWidget::goToImage(int index) -{ - if (files.isEmpty()) - return; - if (index < 0 || index >= files.size()) { - qDebug() << "goToImage: invalid index: " << index; - return; - } - if (index == position+1) { - goNextImage(); - return; - } - if (position > 0 && index == position-1) { - goPrevImage(); - return; - } - position = index; - pixmap = QPixmap(); - if (index > 0) - prevImage = loadImage(path+QLatin1String("/")+files.at(position-1)); - else - prevImage = QImage(); - currentImage = loadImage(path+QLatin1String("/")+files.at(position)); - if (position+1 < files.size()) - nextImage = loadImage(path+QLatin1String("/")+files.at(position+1)); - else - nextImage = QImage(); - setZoomedIn(false); - updateImage(); -} - -void ImageWidget::timerEvent(QTimerEvent *event) -{ - if (event->timerId() == touchFeedback.tapTimer.timerId()) { - touchFeedback.tapTimer.stop(); - } else if (event->timerId() == feedbackFadeOutTimer.timerId()) { - feedbackFadeOutTimer.stop(); - touchFeedback.reset(); - } - update(); -} - -#include "moc_imagewidget.cpp" diff --git a/doc/src/snippets/gestures/imageviewer/imagewidget.h b/doc/src/snippets/gestures/imageviewer/imagewidget.h deleted file mode 100644 index 5553093..0000000 --- a/doc/src/snippets/gestures/imageviewer/imagewidget.h +++ /dev/null @@ -1,137 +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 IMAGEWIDGET_H -#define IMAGEWIDGET_H - -#include <QWidget> -#include <QImage> -#include <QPixmap> - -#include <QtGui> - -#include "tapandholdgesture.h" - -class ImageWidget : public QWidget -{ - Q_OBJECT - -public: - ImageWidget(QWidget *parent = 0); - - void openDirectory(const QString &path); - -protected: - void paintEvent(QPaintEvent*); - void resizeEvent(QResizeEvent*); - void timerEvent(QTimerEvent*); - void mousePressEvent(QMouseEvent*); - void mouseDoubleClickEvent(QMouseEvent*); - -//! [imagewidget-slots] -private slots: - void gestureTriggered(); - void gestureFinished(); - void gestureCancelled(); -//! [imagewidget-slots] - -private: - void updateImage(); - QImage loadImage(const QString &fileName); - void loadImage(); - void setZoomedIn(bool zoomed); - void goNextImage(); - void goPrevImage(); - void goToImage(int index); - - QPanGesture *panGesture; - TapAndHoldGesture *tapAndHoldGesture; - - QString path; - QStringList files; - int position; - - QImage prevImage, nextImage; - QImage currentImage; - QPixmap pixmap; - QTransform transformation; - - bool zoomedIn; - int horizontalOffset; - int verticalOffset; - - bool zoomed; - qreal zoom; - bool rotated; - qreal angle; - - struct TouchFeedback - { - bool tapped; - QPoint position; - bool sliding; - QPoint slidingStartPosition; - QBasicTimer tapTimer; - int tapState; - bool doubleTapped; - int tapAndHoldState; - - enum { MaximumNumberOfTouches = 5 }; - QPoint touches[MaximumNumberOfTouches]; - - inline TouchFeedback() { reset(); } - inline void reset() - { - tapped = false; - sliding = false; - tapTimer.stop(); - tapState = 0; - doubleTapped = false; - tapAndHoldState = 0; - for (int i = 0; i < MaximumNumberOfTouches; ++i) { - touches[i] = QPoint(); - } - } - } touchFeedback; - QBasicTimer feedbackFadeOutTimer; -}; - -#endif diff --git a/doc/src/snippets/gestures/imageviewer/tapandholdgesture.cpp b/doc/src/snippets/gestures/imageviewer/tapandholdgesture.cpp deleted file mode 100644 index 7dd2359..0000000 --- a/doc/src/snippets/gestures/imageviewer/tapandholdgesture.cpp +++ /dev/null @@ -1,159 +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) -{ - 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 */ -//! [tapandhold-reset] -void TapAndHoldGesture::reset() -{ - timer.stop(); - iteration = 0; - position = startPosition = QPoint(); - updateState(Qt::NoGesture); -} -//! [tapandhold-reset] - -/*! - \property TapAndHoldGesture::pos - - \brief The position of the gesture. -*/ -QPoint TapAndHoldGesture::pos() const -{ - return position; -} diff --git a/doc/src/snippets/gestures/qgesture.cpp b/doc/src/snippets/gestures/qgesture.cpp deleted file mode 100644 index 77f5cc2..0000000 --- a/doc/src/snippets/gestures/qgesture.cpp +++ /dev/null @@ -1,283 +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 QtGui module 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 "qgesture.h" -#include <private/qgesture_p.h> -#include "qgraphicsitem.h" - -QT_BEGIN_NAMESPACE - - -class QEventFilterProxyGraphicsItem : public QGraphicsItem -{ -public: - QEventFilterProxyGraphicsItem(QGesture *g) - : gesture(g) - { - } - bool sceneEventFilter(QGraphicsItem *, QEvent *event) - { - return gesture ? gesture->filterEvent(event) : false; - } - QRectF boundingRect() const { return QRectF(); } - void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) { } - -private: - QGesture *gesture; -}; - -/* - \class QGesture - \since 4.6 - - \brief The QGesture class is the base class for implementing custom - gestures. - - This class represents both an object that recognizes a gesture out of a set - of input events (a gesture recognizer), and a gesture object itself that - can be used to get extended information about the triggered gesture. - - The class has a list of properties that can be queried by the user to get - some gesture-specific parameters (for example, an offset of a Pan gesture). - - Usually gesture recognizer implements a state machine, storing its state - internally in the recognizer object. The recognizer receives input events - through the \l{QGesture::}{filterEvent()} virtual function and decides - whether the event should change the state of the recognizer by emitting an - appropriate signal. - - Input events should be either fed to the recognizer one by one with a - filterEvent() function, or the gesture recognizer should be attached to an - object it filters events for by specifying it as a parent object. The - QGesture object installs itself as an event filter to the parent object - automatically, the unsetObject() function should be used to remove an event - filter from the parent object. To make a - gesture that operates on a QGraphicsItem, both the appropriate QGraphicsView - should be passed as a parent object and setGraphicsItem() functions should - be used to attach a gesture to a graphics item. - - This is a base class, to create a custom gesture type, you should subclass - it and implement its pure virtual functions. - - \sa QPanGesture -*/ - -/* \fn bool QGesture::filterEvent(QEvent *event) - - Parses input \a event and emits a signal when detects a gesture. - - In your reimplementation of this function, if you want to filter the \a - event out, i.e. stop it being handled further, return true; otherwise - return false; - - This is a pure virtual function that needs to be implemented in subclasses. -*/ - -/* \fn void QGesture::started() - - The signal is emitted when the gesture is started. Extended information - about the gesture is contained in the signal sender object. - - In addition to started(), a triggered() signal should also be emitted. -*/ - -/* \fn void QGesture::triggered() - - The signal is emitted when the gesture is detected. Extended information - about the gesture is contained in the signal sender object. -*/ - -/* \fn void QGesture::finished() - - The signal is emitted when the gesture is finished. Extended information - about the gesture is contained in the signal sender object. -*/ - -/* \fn void QGesture::cancelled() - - The signal is emitted when the gesture is cancelled, for example the reset() - function is called while the gesture was in the process of emitting a - triggered() signal. Extended information about the gesture is contained in - the sender object. -*/ - - -/* - Creates a new gesture handler object and marks it as a child of \a parent. - - The \a parent object is also the default event source for the gesture, - meaning that the gesture installs itself as an event filter for the \a - parent. - - \sa setGraphicsItem() -*/ -QGesture::QGesture(QObject *parent) - : QObject(*new QGesturePrivate, parent) -{ - if (parent) - parent->installEventFilter(this); -} - -/* \internal - */ -QGesture::QGesture(QGesturePrivate &dd, QObject *parent) - : QObject(dd, parent) -{ - if (parent) - parent->installEventFilter(this); -} - -/* - Destroys the gesture object. -*/ -QGesture::~QGesture() -{ -} - -/* \internal - */ -bool QGesture::eventFilter(QObject *receiver, QEvent *event) -{ - Q_D(QGesture); - if (d->graphicsItem && receiver == parent()) - return false; - return filterEvent(event); -} - -/* - \property QGesture::state - - \brief The current state of the gesture. -*/ - -/* - Returns the gesture recognition state. - */ -Qt::GestureState QGesture::state() const -{ - return d_func()->state; -} - -/* - Sets this gesture's recognition state to \a state and emits appropriate - signals. - - This functions emits the signals according to the old state and the new - \a state, and it should be called after all the internal properties have been - initialized. - - \sa started(), triggered(), finished(), cancelled() - */ -void QGesture::updateState(Qt::GestureState state) -{ - Q_D(QGesture); - if (d->state == state) { - if (state == Qt::GestureUpdated) - emit triggered(); - return; - } - const Qt::GestureState oldState = d->state; - d->state = state; - if (state != Qt::NoGesture && oldState > state) { - // comparing the state as ints: state should only be changed from - // started to (optionally) updated and to finished. - qWarning("QGesture::updateState: incorrect new state"); - return; - } - if (oldState == Qt::NoGesture) - emit started(); - if (state == Qt::GestureUpdated) - emit triggered(); - else if (state == Qt::GestureFinished) - emit finished(); - else if (state == Qt::NoGesture) - emit cancelled(); - - if (state == Qt::GestureFinished) { - // gesture is finished, so we reset the internal state. - d->state = Qt::NoGesture; - } -} - -/* - Sets the \a graphicsItem the gesture is filtering events for. - - The gesture will install an event filter to the \a graphicsItem and - redirect them to the filterEvent() function. - - \sa graphicsItem() -*/ -void QGesture::setGraphicsItem(QGraphicsItem *graphicsItem) -{ - Q_D(QGesture); - if (d->graphicsItem && d->eventFilterProxyGraphicsItem) - d->graphicsItem->removeSceneEventFilter(d->eventFilterProxyGraphicsItem); - d->graphicsItem = graphicsItem; - if (!d->eventFilterProxyGraphicsItem) - d->eventFilterProxyGraphicsItem = new QEventFilterProxyGraphicsItem(this); - if (graphicsItem) - graphicsItem->installSceneEventFilter(d->eventFilterProxyGraphicsItem); -} - -/* - Returns the graphics item the gesture is filtering events for. - - \sa setGraphicsItem() -*/ -QGraphicsItem* QGesture::graphicsItem() const -{ - return d_func()->graphicsItem; -} - -/* \fn void QGesture::reset() - - Resets the internal state of the gesture. This function might be called by - the filterEvent() implementation in a derived class, or by the user to - cancel a gesture. The base class implementation calls - updateState(Qt::NoGesture) which emits the cancelled() - signal if the state() of the gesture indicated it was active. -*/ -void QGesture::reset() -{ - updateState(Qt::NoGesture); -} - -QT_END_NAMESPACE diff --git a/doc/src/snippets/gestures/qstandardgestures.cpp b/doc/src/snippets/gestures/qstandardgestures.cpp deleted file mode 100644 index b22f6ae..0000000 --- a/doc/src/snippets/gestures/qstandardgestures.cpp +++ /dev/null @@ -1,483 +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 QtGui module 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 "qstandardgestures.h" -#include "qstandardgestures_p.h" - -#include <qabstractscrollarea.h> -#include <qscrollbar.h> -#include <private/qapplication_p.h> -#include <private/qevent_p.h> -#include <private/qwidget_p.h> - -QT_BEGIN_NAMESPACE - -#ifdef Q_WS_WIN -QWidgetPrivate *qt_widget_private(QWidget *widget); -#endif - -/*! - \class QPanGesture - \since 4.6 - - \brief The QPanGesture class represents a Pan gesture, - providing additional information related to panning. -*/ - -/*! - Creates a new Pan gesture handler object and marks it as a child of \a - parent. - - On some platform like Windows it's necessary to provide a non-null widget - as \a parent to get native gesture support. -*/ -QPanGesture::QPanGesture(QWidget *parent) - : QGesture(*new QPanGesturePrivate, parent) -{ - if (parent) { - QApplicationPrivate *qAppPriv = QApplicationPrivate::instance(); - qAppPriv->widgetGestures[parent].pan = this; -#ifdef Q_WS_WIN - qt_widget_private(parent)->winSetupGestures(); -#endif - } -} - -/*! \internal */ -bool QPanGesture::event(QEvent *event) -{ - switch (event->type()) { - case QEvent::ParentAboutToChange: - if (QWidget *w = qobject_cast<QWidget*>(parent())) { - QApplicationPrivate::instance()->widgetGestures[w].pan = 0; -#ifdef Q_WS_WIN - qt_widget_private(w)->winSetupGestures(); -#endif - } - break; - case QEvent::ParentChange: - if (QWidget *w = qobject_cast<QWidget*>(parent())) { - QApplicationPrivate::instance()->widgetGestures[w].pan = this; -#ifdef Q_WS_WIN - qt_widget_private(w)->winSetupGestures(); -#endif - } - break; - default: - break; - } - -#if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA) - Q_D(QPanGesture); - if (event->type() == QEvent::Timer) { - const QTimerEvent *te = static_cast<QTimerEvent *>(event); - if (te->timerId() == d->panFinishedTimer) { - killTimer(d->panFinishedTimer); - d->panFinishedTimer = 0; - d->lastOffset = QSize(0, 0); - updateState(Qt::GestureFinished); - } - } -#endif - - return QObject::event(event); -} - -bool QPanGesture::eventFilter(QObject *receiver, QEvent *event) -{ -#ifdef Q_WS_WIN - Q_D(QPanGesture); - if (receiver->isWidgetType() && event->type() == QEvent::NativeGesture) { - QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event); - QApplicationPrivate *qAppPriv = QApplicationPrivate::instance(); - QApplicationPrivate::WidgetStandardGesturesMap::iterator it; - it = qAppPriv->widgetGestures.find(static_cast<QWidget*>(receiver)); - if (it == qAppPriv->widgetGestures.end()) - return false; - if (this != it.value().pan) - return false; - Qt::GestureState nextState = Qt::NoGesture; - switch(ev->gestureType) { - case QNativeGestureEvent::GestureBegin: - // next we might receive the first gesture update event, so we - // prepare for it. - d->state = Qt::NoGesture; - return false; - case QNativeGestureEvent::Pan: - nextState = Qt::GestureUpdated; - event->accept(); - break; - case QNativeGestureEvent::GestureEnd: - if (state() == Qt::NoGesture) - return false; // some other gesture has ended - nextState = Qt::GestureFinished; - break; - default: - return false; - } - if (state() == Qt::NoGesture) { - d->lastOffset = d->totalOffset = QSize(); - } else { - d->lastOffset = QSize(ev->position.x() - d->lastPosition.x(), - ev->position.y() - d->lastPosition.y()); - d->totalOffset += d->lastOffset; - } - d->lastPosition = ev->position; - updateState(nextState); - return true; - } -#endif - return QGesture::eventFilter(receiver, event); -} - -/*! \internal */ -bool QPanGesture::filterEvent(QEvent *event) -{ - Q_D(QPanGesture); - if (!event->spontaneous()) - return false; - const QTouchEvent *ev = static_cast<const QTouchEvent*>(event); - if (event->type() == QEvent::TouchBegin) { - QTouchEvent::TouchPoint p = ev->touchPoints().at(0); - d->lastPosition = p.pos().toPoint(); - d->lastOffset = d->totalOffset = QSize(); - } else if (event->type() == QEvent::TouchEnd) { - if (state() != Qt::NoGesture) { - if (!ev->touchPoints().isEmpty()) { - QTouchEvent::TouchPoint p = ev->touchPoints().at(0); - const QPoint pos = p.pos().toPoint(); - const QPoint lastPos = p.lastPos().toPoint(); - const QPoint startPos = p.startPos().toPoint(); - d->lastOffset = QSize(pos.x() - lastPos.x(), pos.y() - lastPos.y()); - d->totalOffset = QSize(pos.x() - startPos.x(), pos.y() - startPos.y()); - } - updateState(Qt::GestureFinished); - } - reset(); - } else if (event->type() == QEvent::TouchUpdate) { - QTouchEvent::TouchPoint p = ev->touchPoints().at(0); - const QPoint pos = p.pos().toPoint(); - const QPoint lastPos = p.lastPos().toPoint(); - const QPoint startPos = p.startPos().toPoint(); - d->lastOffset = QSize(pos.x() - lastPos.x(), pos.y() - lastPos.y()); - d->totalOffset = QSize(pos.x() - startPos.x(), pos.y() - startPos.y()); - if (d->totalOffset.width() > 10 || d->totalOffset.height() > 10 || - d->totalOffset.width() < -10 || d->totalOffset.height() < -10) { - updateState(Qt::GestureUpdated); - } - } -#ifdef Q_OS_MAC - else if (event->type() == QEvent::Wheel) { - // On Mac, there is really no native panning gesture. Instead, a two - // finger pan is delivered as mouse wheel events. Otoh, on Windows, you - // either get mouse wheel events or pan events. We have decided to make this - // the Qt behaviour as well, meaning that on Mac, wheel - // events will be masked away when listening for pan events. -#ifndef QT_MAC_USE_COCOA - // In Carbon we receive neither touch-, nor pan gesture events. - // So we create pan gestures by converting wheel events. After all, this - // is how things are supposed to work on mac in the first place. - const QWheelEvent *wev = static_cast<const QWheelEvent*>(event); - int offset = wev->delta() / -120; - d->lastOffset = wev->orientation() == Qt::Horizontal ? QSize(offset, 0) : QSize(0, offset); - - if (state() == Qt::NoGesture) { - d->totalOffset = d->lastOffset; - } else { - d->totalOffset += d->lastOffset; - } - - killTimer(d->panFinishedTimer); - d->panFinishedTimer = startTimer(200); - updateState(Qt::GestureUpdated); -#endif - return true; - } -#endif - return false; -} - -/*! \internal */ -void QPanGesture::reset() -{ - Q_D(QPanGesture); - d->lastOffset = d->totalOffset = QSize(); - d->lastPosition = QPoint(); -#if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA) - if (d->panFinishedTimer) { - killTimer(d->panFinishedTimer); - d->panFinishedTimer = 0; - } -#endif - QGesture::reset(); -} - -/*! - \property QPanGesture::totalOffset - - Specifies a total pan offset since the start of the gesture. -*/ -QSize QPanGesture::totalOffset() const -{ - Q_D(const QPanGesture); - return d->totalOffset; -} - -/*! - \property QPanGesture::lastOffset - - Specifies a pan offset since the last time the gesture was - triggered. -*/ -QSize QPanGesture::lastOffset() const -{ - Q_D(const QPanGesture); - return d->lastOffset; -} - - -/*! - \class QPinchGesture - \since 4.6 - - \brief The QPinchGesture class represents a Pinch gesture, - providing additional information related to zooming and/or rotation. -*/ - -/*! - Creates a new Pinch gesture handler object and marks it as a child of \a - parent. - - On some platform like Windows it's necessary to provide a non-null widget - as \a parent to get native gesture support. -*/ -QPinchGesture::QPinchGesture(QWidget *parent) - : QGesture(*new QPinchGesturePrivate, parent) -{ - if (parent) { - QApplicationPrivate *qAppPriv = QApplicationPrivate::instance(); - qAppPriv->widgetGestures[parent].pinch = this; -#ifdef Q_WS_WIN - qt_widget_private(parent)->winSetupGestures(); -#endif - } -} - -/*! \internal */ -bool QPinchGesture::event(QEvent *event) -{ - switch (event->type()) { - case QEvent::ParentAboutToChange: - if (QWidget *w = qobject_cast<QWidget*>(parent())) { - QApplicationPrivate::instance()->widgetGestures[w].pinch = 0; -#ifdef Q_WS_WIN - qt_widget_private(w)->winSetupGestures(); -#endif - } - break; - case QEvent::ParentChange: - if (QWidget *w = qobject_cast<QWidget*>(parent())) { - QApplicationPrivate::instance()->widgetGestures[w].pinch = this; -#ifdef Q_WS_WIN - qt_widget_private(w)->winSetupGestures(); -#endif - } - break; - default: - break; - } - return QObject::event(event); -} - -bool QPinchGesture::eventFilter(QObject *receiver, QEvent *event) -{ -#ifdef Q_WS_WIN - Q_D(QPinchGesture); - if (receiver->isWidgetType() && event->type() == QEvent::NativeGesture) { - QNativeGestureEvent *ev = static_cast<QNativeGestureEvent*>(event); - QApplicationPrivate *qAppPriv = QApplicationPrivate::instance(); - QApplicationPrivate::WidgetStandardGesturesMap::iterator it; - it = qAppPriv->widgetGestures.find(static_cast<QWidget*>(receiver)); - if (it == qAppPriv->widgetGestures.end()) - return false; - if (this != it.value().pinch) - return false; - Qt::GestureState nextState = Qt::NoGesture; - switch(ev->gestureType) { - case QNativeGestureEvent::GestureBegin: - // next we might receive the first gesture update event, so we - // prepare for it. - d->state = Qt::NoGesture; - d->scaleFactor = d->lastScaleFactor = 1; - d->rotationAngle = d->lastRotationAngle = 0; - d->startCenterPoint = d->centerPoint = d->lastCenterPoint = QPoint(); - d->initialDistance = 0; - return false; - case QNativeGestureEvent::Rotate: - d->lastRotationAngle = d->rotationAngle; - d->rotationAngle = -1 * GID_ROTATE_ANGLE_FROM_ARGUMENT(ev->argument); - nextState = Qt::GestureUpdated; - event->accept(); - break; - case QNativeGestureEvent::Zoom: - if (d->initialDistance != 0) { - d->lastScaleFactor = d->scaleFactor; - int distance = int(qint64(ev->argument)); - d->scaleFactor = (qreal) distance / d->initialDistance; - } else { - d->initialDistance = int(qint64(ev->argument)); - } - nextState = Qt::GestureUpdated; - event->accept(); - break; - case QNativeGestureEvent::GestureEnd: - if (state() == Qt::NoGesture) - return false; // some other gesture has ended - nextState = Qt::GestureFinished; - break; - default: - return false; - } - if (d->startCenterPoint.isNull()) - d->startCenterPoint = d->centerPoint; - d->lastCenterPoint = d->centerPoint; - d->centerPoint = static_cast<QWidget*>(receiver)->mapFromGlobal(ev->position); - updateState(nextState); - return true; - } -#endif - return QGesture::eventFilter(receiver, event); -} - -/*! \internal */ -bool QPinchGesture::filterEvent(QEvent *event) -{ - Q_UNUSED(event); - return false; -} - -/*! \internal */ -void QPinchGesture::reset() -{ - Q_D(QPinchGesture); - d->scaleFactor = d->lastScaleFactor = 0; - d->rotationAngle = d->lastRotationAngle = 0; - d->startCenterPoint = d->centerPoint = d->lastCenterPoint = QPoint(); - QGesture::reset(); -} - -/*! - \property QPinchGesture::scaleFactor - - Specifies a scale factor of the pinch gesture. -*/ -qreal QPinchGesture::scaleFactor() const -{ - return d_func()->scaleFactor; -} - -/*! - \property QPinchGesture::lastScaleFactor - - Specifies a previous scale factor of the pinch gesture. -*/ -qreal QPinchGesture::lastScaleFactor() const -{ - return d_func()->lastScaleFactor; -} - -/*! - \property QPinchGesture::rotationAngle - - Specifies a rotation angle of the gesture. -*/ -qreal QPinchGesture::rotationAngle() const -{ - return d_func()->rotationAngle; -} - -/*! - \property QPinchGesture::lastRotationAngle - - Specifies a previous rotation angle of the gesture. -*/ -qreal QPinchGesture::lastRotationAngle() const -{ - return d_func()->lastRotationAngle; -} - -/*! - \property QPinchGesture::centerPoint - - Specifies a center point of the gesture. The point can be used as a center - point that the object is rotated around. -*/ -QPoint QPinchGesture::centerPoint() const -{ - return d_func()->centerPoint; -} - -/*! - \property QPinchGesture::lastCenterPoint - - Specifies a previous center point of the gesture. -*/ -QPoint QPinchGesture::lastCenterPoint() const -{ - return d_func()->lastCenterPoint; -} - -/*! - \property QPinchGesture::startCenterPoint - - Specifies an initial center point of the gesture. Difference between the - startCenterPoint and the centerPoint is the distance at which pinching - fingers has shifted. -*/ -QPoint QPinchGesture::startCenterPoint() const -{ - return d_func()->startCenterPoint; -} - -QT_END_NAMESPACE - -#include "moc_qstandardgestures.cpp" - diff --git a/doc/src/snippets/gestures/qstandardgestures.h b/doc/src/snippets/gestures/qstandardgestures.h deleted file mode 100644 index efd6c6e..0000000 --- a/doc/src/snippets/gestures/qstandardgestures.h +++ /dev/null @@ -1,126 +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 QtGui module 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 QSTANDARDGESTURES_H -#define QSTANDARDGESTURES_H - -#include <QtGui/qevent.h> -#include <QtCore/qbasictimer.h> - -#include <QtGui/qgesture.h> - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -QT_MODULE(Gui) - -class QPanGesturePrivate; -class Q_GUI_EXPORT QPanGesture : public QGesture -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QPanGesture) - - Q_PROPERTY(QSize totalOffset READ totalOffset) - Q_PROPERTY(QSize lastOffset READ lastOffset) - -public: - QPanGesture(QWidget *parent); - - bool filterEvent(QEvent *event); - - QSize totalOffset() const; - QSize lastOffset() const; - -protected: - void reset(); - -private: - bool event(QEvent *event); - bool eventFilter(QObject *receiver, QEvent *event); - - friend class QWidget; -}; - -class QPinchGesturePrivate; -class Q_GUI_EXPORT QPinchGesture : public QGesture -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QPinchGesture) - - Q_PROPERTY(qreal scaleFactor READ scaleFactor) - Q_PROPERTY(qreal lastScaleFactor READ lastScaleFactor) - - Q_PROPERTY(qreal rotationAngle READ rotationAngle) - Q_PROPERTY(qreal lastRotationAngle READ lastRotationAngle) - - Q_PROPERTY(QPoint startCenterPoint READ startCenterPoint) - Q_PROPERTY(QPoint lastCenterPoint READ lastCenterPoint) - Q_PROPERTY(QPoint centerPoint READ centerPoint) - -public: - QPinchGesture(QWidget *parent); - - bool filterEvent(QEvent *event); - void reset(); - - QPoint startCenterPoint() const; - QPoint lastCenterPoint() const; - QPoint centerPoint() const; - - qreal scaleFactor() const; - qreal lastScaleFactor() const; - - qreal rotationAngle() const; - qreal lastRotationAngle() const; - -private: - bool event(QEvent *event); - bool eventFilter(QObject *receiver, QEvent *event); - - friend class QWidget; -}; - -QT_END_NAMESPACE - -QT_END_HEADER - -#endif // QSTANDARDGESTURES_H diff --git a/examples/gestures/imageviewer/imageviewer.pro b/examples/gestures/imageviewer/imageviewer.pro index 68c1f1c..7780ad9 100644 --- a/examples/gestures/imageviewer/imageviewer.pro +++ b/examples/gestures/imageviewer/imageviewer.pro @@ -1,6 +1,8 @@ -HEADERS += imagewidget.h -SOURCES += imagewidget.cpp \ - main.cpp +HEADERS = imagewidget.h \ + mainwidget.h +SOURCES = imagewidget.cpp \ + main.cpp \ + mainwidget.cpp # install target.path = $$[QT_INSTALL_EXAMPLES]/gestures/imageviewer diff --git a/examples/gestures/imageviewer/imagewidget.cpp b/examples/gestures/imageviewer/imagewidget.cpp index f3fd8e4..c4a4e50 100644 --- a/examples/gestures/imageviewer/imagewidget.cpp +++ b/examples/gestures/imageviewer/imagewidget.cpp @@ -59,17 +59,21 @@ ImageWidget::ImageWidget(QWidget *parent) setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_NoSystemBackground); +//! [enable gestures] grabGesture(Qt::PanGesture); grabGesture(Qt::PinchGesture); grabGesture(Qt::SwipeGesture); +//! [enable gestures] } +//! [event handler] bool ImageWidget::event(QEvent *event) { if (event->type() == QEvent::Gesture) return gestureEvent(static_cast<QGestureEvent*>(event)); return QWidget::event(event); } +//! [event handler] void ImageWidget::paintEvent(QPaintEvent*) { @@ -98,6 +102,7 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) update(); } +//! [gesture event handler] bool ImageWidget::gestureEvent(QGestureEvent *event) { if (QGesture *pan = event->gesture(Qt::PanGesture)) { @@ -112,6 +117,7 @@ bool ImageWidget::gestureEvent(QGestureEvent *event) } return false; } +//! [gesture event handler] void ImageWidget::panTriggered(QPanGesture *gesture) { @@ -147,7 +153,7 @@ void ImageWidget::pinchTriggered(QPinchGesture *gesture) update(); } -//! [swipe slot] +//! [swipe function] void ImageWidget::swipeTriggered(QSwipeGesture *gesture) { if (gesture->horizontalDirection() == QSwipeGesture::Left @@ -157,7 +163,7 @@ void ImageWidget::swipeTriggered(QSwipeGesture *gesture) goNextImage(); update(); } -//! [swipe slot] +//! [swipe function] void ImageWidget::resizeEvent(QResizeEvent*) { diff --git a/examples/gestures/imageviewer/main.cpp b/examples/gestures/imageviewer/main.cpp index c3d03f3..9c99f31 100644 --- a/examples/gestures/imageviewer/main.cpp +++ b/examples/gestures/imageviewer/main.cpp @@ -41,36 +41,7 @@ #include <QtGui> -#include "imagewidget.h" - -class MainWidget : public QMainWindow -{ - Q_OBJECT - -public: - MainWidget(QWidget *parent = 0); - -public slots: - void openDirectory(const QString &path); - -private: - bool loadImage(const QString &fileName); - - ImageWidget *imageWidget; -}; - -MainWidget::MainWidget(QWidget *parent) - : QMainWindow(parent) -{ - resize(400, 300); - imageWidget = new ImageWidget(this); - setCentralWidget(imageWidget); -} - -void MainWidget::openDirectory(const QString &path) -{ - imageWidget->openDirectory(path); -} +#include "mainwidget.h" int main(int argc, char *argv[]) { @@ -86,5 +57,3 @@ int main(int argc, char *argv[]) return app.exec(); } - -#include "main.moc" diff --git a/doc/src/snippets/gestures/qgesture.h b/examples/gestures/imageviewer/mainwidget.cpp index 3fc89a1..51e9f1e 100644 --- a/doc/src/snippets/gestures/qgesture.h +++ b/examples/gestures/imageviewer/mainwidget.cpp @@ -4,7 +4,7 @@ ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** -** This file is part of the QtGui module of the Qt Toolkit. +** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage @@ -39,63 +39,18 @@ ** ****************************************************************************/ -#ifndef QGESTURE_H -#define QGESTURE_H +#include "imagewidget.h" +#include "mainwidget.h" -#include <QtCore/qobject.h> -#include <QtCore/qlist.h> -#include <QtCore/qdatetime.h> -#include <QtCore/qpoint.h> -#include <QtCore/qrect.h> -#include <QtCore/qmetatype.h> - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -QT_MODULE(Gui) - -class QGraphicsItem; -class QGesturePrivate; -class Q_GUI_EXPORT QGesture : public QObject +MainWidget::MainWidget(QWidget *parent) + : QMainWindow(parent) { - Q_OBJECT - Q_DECLARE_PRIVATE(QGesture) - - Q_PROPERTY(Qt::GestureState state READ state) - -public: - explicit QGesture(QObject *parent = 0); - ~QGesture(); - - virtual bool filterEvent(QEvent *event) = 0; - - void setGraphicsItem(QGraphicsItem *); - QGraphicsItem *graphicsItem() const; + resize(400, 300); + imageWidget = new ImageWidget(this); + setCentralWidget(imageWidget); +} - Qt::GestureState state() const; - -protected: - QGesture(QGesturePrivate &dd, QObject *parent); - bool eventFilter(QObject*, QEvent*); - - virtual void reset(); - void updateState(Qt::GestureState state); - -//! [qgesture-signals] -Q_SIGNALS: - void started(); - void triggered(); - void finished(); - void cancelled(); -//! [qgesture-signals] - -private: - friend class QWidget; -}; - -QT_END_NAMESPACE - -QT_END_HEADER - -#endif // QGESTURE_H +void MainWidget::openDirectory(const QString &path) +{ + imageWidget->openDirectory(path); +} diff --git a/doc/src/snippets/gestures/imageviewer/tapandholdgesture.h b/examples/gestures/imageviewer/mainwidget.h index 682342e..1a99155 100644 --- a/doc/src/snippets/gestures/imageviewer/tapandholdgesture.h +++ b/examples/gestures/imageviewer/mainwidget.h @@ -39,36 +39,25 @@ ** ****************************************************************************/ -#ifndef TAPANDHOLDGESTURE_H -#define TAPANDHOLDGESTURE_H +#ifndef MAINWIDGET_H +#define MAINWIDGET_H -#include <QtCore/QBasicTimer> -#include <QtGui/QGesture> -#include <QtGui/QWidget> +#include <QMainWindow> -class TapAndHoldGesture : public QGesture +class MainWidget : public QMainWindow { Q_OBJECT - Q_PROPERTY(QPoint pos READ pos) public: - TapAndHoldGesture(QWidget *parent); + MainWidget(QWidget *parent = 0); - bool filterEvent(QEvent *event); - void reset(); - - QPoint pos() const; - -protected: - void timerEvent(QTimerEvent *event); +public slots: + void openDirectory(const QString &path); private: - QBasicTimer timer; - int iteration; - QPoint position; - QPoint startPosition; - static const int iterationCount; - static const int iterationTimeout; + bool loadImage(const QString &fileName); + + ImageWidget *imageWidget; }; -#endif // TAPANDHOLDGESTURE_H +#endif diff --git a/src/gui/kernel/qgesture.cpp b/src/gui/kernel/qgesture.cpp index 3639a45..6231d19 100644 --- a/src/gui/kernel/qgesture.cpp +++ b/src/gui/kernel/qgesture.cpp @@ -51,11 +51,14 @@ QT_BEGIN_NAMESPACE \brief The QGesture class represents a gesture, containing properties that describe the corresponding user input. - QGesture objects are delivered to widgets and \l{QGraphicsObject}s with - \l{QGestureEvent}s. + Gesture objects are not constructed directly by developers. They are created by + the QGestureRecognizer object that is registered with the application; see + QApplication::registerGestureRecognizer(). + + \section1 Gesture Properties The class has a list of properties that can be queried by the user to get - some gesture-specific arguments. For example, the QPinchGesture gesture has a scale + some gesture-specific arguments. For example, the pinch gesture has a scale factor that is exposed as a property. Developers of custom gesture recognizers can add additional properties in @@ -63,6 +66,23 @@ QT_BEGIN_NAMESPACE by adding new dynamic properties to a QGesture object, or by subclassing the QGesture class (or one of its subclasses). + \section1 Lifecycle of a Gesture Object + + A QGesture instance is created when the application calls QWidget::grabGesture() + or QGraphicsObject::grabGesture() to configure a widget or graphics object (the + target object) for gesture input. One gesture object is created for each target + object. + + The registered gesture recognizer monitors the input events for the target + object via its \l{QGestureRecognizer::}{filterEvent()} function, updating the + properties of the gesture object as required. + + The gesture object may be delivered to the target object in a QGestureEvent if + the corresponding gesture is active or has just been canceled. Each event that + is delivered contains a list of gesture objects, since support for more than + one gesture may be enabled for the target object. Due to the way events are + handled in Qt, gesture events may be filtered by other objects. + \sa QGestureEvent, QGestureRecognizer */ diff --git a/src/gui/kernel/qgesturerecognizer.cpp b/src/gui/kernel/qgesturerecognizer.cpp index 2af087f..1998cba 100644 --- a/src/gui/kernel/qgesturerecognizer.cpp +++ b/src/gui/kernel/qgesturerecognizer.cpp @@ -65,9 +65,11 @@ QT_BEGIN_NAMESPACE about the user's input. Gestures are created when the framework calls createGesture() to handle user input - for a particular target QWidget or QGraphicsObject instance. Once a QGesture has been - created for one of these objects, the gesture recognizer will receive events for it - in its filterEvent() handler function. + for a particular instance of a QWidget or QGraphicsObject subclass. A QGesture object + is created for each widget or item that is configured to use gestures. + + Once a QGesture has been created for a target object, the gesture recognizer will + receive events for it in its filterEvent() handler function. When a gesture is canceled, the reset() function is called, giving the recognizer the chance to update the appropriate properties in the corresponding QGesture object. @@ -75,15 +77,18 @@ QT_BEGIN_NAMESPACE \section1 Supporting New Gestures To add support for new gestures, you need to derive from QGestureRecognizer to create - a custom recognizer class and register it with the application by calling - QApplication::registerGestureRecognizer(). You can also derive from QGesture to create - a custom gesture class, or rely on dynamic properties to express specific details - of the gesture you want to handle. + a custom recognizer class, construct an instance of this class, and register it with + the application by calling QApplication::registerGestureRecognizer(). You can also + subclass QGesture to create a custom gesture class, or rely on dynamic properties + to express specific details of the gesture you want to handle. Your custom QGestureRecognizer subclass needs to reimplement the filterEvent() function to handle and filter the incoming input events for QWidget and QGraphicsObject subclasses. - Although the logic for gesture recognition is implemented in this function, the state of - recognition for each target object can be recorded in the QGesture object supplied. + Although the logic for gesture recognition is implemented in this function, you can + store persistent information about the state of the recognition process in the QGesture + object supplied. The filterEvent() function must return a value of Qt::GestureState that + indicates the state of recognition for a given gesture and target object. This determines + whether or not a gesture event will be delivered to a target object. If you choose to represent a gesture by a custom QGesture subclass, you will need to reimplement the createGesture() function to construct instances of your gesture class. |