diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-10-09 17:09:09 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-10-09 17:09:09 (GMT) |
commit | f2437d3aa6cc274b9e663801892fd4e40e693546 (patch) | |
tree | 7a5b1425ac403336eb77c80ca0a551e7222f60f8 /src/gui/kernel/qgesture.cpp | |
parent | 7566a1f15ea32504c10d9467fb69a6399a06c325 (diff) | |
download | Qt-f2437d3aa6cc274b9e663801892fd4e40e693546.zip Qt-f2437d3aa6cc274b9e663801892fd4e40e693546.tar.gz Qt-f2437d3aa6cc274b9e663801892fd4e40e693546.tar.bz2 |
A new implementation of the Gesture API.
Implemented gestures using gesture events and separate
QGesture/QGestureRecognizer classes.
Reviewed-by: trustme
Diffstat (limited to 'src/gui/kernel/qgesture.cpp')
-rw-r--r-- | src/gui/kernel/qgesture.cpp | 428 |
1 files changed, 222 insertions, 206 deletions
diff --git a/src/gui/kernel/qgesture.cpp b/src/gui/kernel/qgesture.cpp index 237ce46..bb74aec 100644 --- a/src/gui/kernel/qgesture.cpp +++ b/src/gui/kernel/qgesture.cpp @@ -40,274 +40,290 @@ ****************************************************************************/ #include "qgesture.h" -#include <private/qgesture_p.h> -#include "qgraphicsitem.h" +#include "private/qgesture_p.h" QT_BEGIN_NAMESPACE + /*! + \class QGesture + \since 4.6 -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*) { } + \brief The QGesture class represents a gesture, containing all + properties that describe a gesture. -private: - QGesture *gesture; -}; + The widget receives a QGestureEvent with a list of QGesture + objects that represent gestures that are occuring on it. The class + has a list of properties that can be queried by the user to get + some gesture-specific arguments (i.e. position of the tap in the + DoubleTap gesture). -/*! - \class QGesture - \since 4.6 - \preliminary - - \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 + When creating custom gesture recognizers, they might add new + properties to the gesture object, or custom gesture developers + might subclass the QGesture objects to provide some extended + information. + + \sa QGestureEvent, QGestureRecognizer */ -/*! \fn bool QGesture::filterEvent(QEvent *event) +QGesture::QGesture(Qt::GestureType type, QObject *parent) + : QObject(*new QGesturePrivate, parent) +{ + d_func()->gestureType = type; +} - Parses input \a event and emits a signal when detects a gesture. +QGesture::QGesture(QObject *parent) + : QObject(*new QGesturePrivate, parent) +{ + d_func()->gestureType = Qt::CustomGesture; +} - 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; +QGesture::QGesture(QGesturePrivate &dd, Qt::GestureType type, QObject *parent) + : QObject(dd, parent) +{ + d_func()->gestureType = type; +} - This is a pure virtual function that needs to be implemented in subclasses. -*/ +QGesture::~QGesture() +{ +} -/*! \fn void QGesture::started() +Qt::GestureType QGesture::gestureType() const +{ + return d_func()->gestureType; +} - The signal is emitted when the gesture is started. Extended information - about the gesture is contained in the signal sender object. +Qt::GestureState QGesture::state() const +{ + return d_func()->state; +} - In addition to started(), a triggered() signal should also be emitted. -*/ +QObject* QGesture::targetObject() const +{ + return d_func()->targetObject; +} -/*! \fn void QGesture::triggered() +void QGesture::setTargetObject(QObject *value) +{ + d_func()->targetObject = value; +} - The signal is emitted when the gesture is detected. Extended information - about the gesture is contained in the signal sender object. -*/ +QPointF QGesture::hotSpot() const +{ + return d_func()->hotSpot; +} -/*! \fn void QGesture::finished() +void QGesture::setHotSpot(const QPointF &value) +{ + Q_D(QGesture); + d->hotSpot = value; + d->isHotSpotSet = true; +} - The signal is emitted when the gesture is finished. Extended information - about the gesture is contained in the signal sender object. -*/ +bool QGesture::hasHotSpot() const +{ + return d_func()->isHotSpotSet; +} -/*! \fn void QGesture::canceled() +void QGesture::unsetHotSpot() +{ + d_func()->isHotSpotSet = false; +} - The signal is emitted when the gesture is canceled, 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. -*/ +// QPanGesture -/*! - Creates a new gesture handler object and marks it as a child of \a - parent. \a gestureTarget is the object that the gesture will watch - for events. +QPanGesture::QPanGesture(QObject *parent) + : QGesture(*new QPanGesturePrivate, Qt::PanGesture, 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 *gestureTarget, QObject *parent) - : QObject(*new QGesturePrivate, parent) +QSizeF QPanGesture::totalOffset() const { - setGestureTarget(gestureTarget); + return d_func()->totalOffset; } -/*! \internal - */ -QGesture::QGesture(QGesturePrivate &dd, QObject *gestureTarget, QObject *parent) - : QObject(dd, parent) +QSizeF QPanGesture::lastOffset() const { - setGestureTarget(gestureTarget); + return d_func()->lastOffset; } -/*! - Destroys the gesture object. -*/ -QGesture::~QGesture() +QSizeF QPanGesture::offset() const { + return d_func()->offset; } -/*! - \property QGesture::gestureTarget +qreal QPanGesture::acceleration() const +{ + return d_func()->acceleration; +} - Gesture target is the object that the gesture will watch for events. - Typically this means that the gesture installs an event filter on the - target object. -*/ -void QGesture::setGestureTarget(QObject *object) + +void QPanGesture::setTotalOffset(const QSizeF &value) { - d_func()->setupGestureTarget(object); + d_func()->totalOffset = value; } -QObject* QGesture::gestureTarget() const +void QPanGesture::setLastOffset(const QSizeF &value) { - return d_func()->gestureTarget; + d_func()->lastOffset = value; } -void QGesturePrivate::setupGestureTarget(QObject *object) +void QPanGesture::setOffset(const QSizeF &value) { - Q_Q(QGesture); - if (gestureTarget) - gestureTarget->removeEventFilter(q); - if (object) - object->installEventFilter(q); - gestureTarget = object; + d_func()->offset = value; } -/*! \internal - */ -bool QGesture::eventFilter(QObject *receiver, QEvent *event) +void QPanGesture::setAcceleration(qreal value) { - Q_D(QGesture); - if (d->graphicsItem && receiver == parent()) - return false; - return filterEvent(event); + d_func()->acceleration = value; } -/*! - \property QGesture::state +// QPinchGesture - \brief The current state of the gesture. -*/ +QPinchGesture::QPinchGesture(QObject *parent) + : QGesture(*new QPinchGesturePrivate, Qt::PinchGesture, parent) +{ -/*! - Returns the gesture recognition state. - */ -Qt::GestureState QGesture::state() const +} + +QPinchGesture::WhatChanged QPinchGesture::whatChanged() const { - return d_func()->state; + return d_func()->whatChanged; } -/*! - Sets this gesture's recognition state to \a state and emits appropriate - signals. +void QPinchGesture::setWhatChanged(QPinchGesture::WhatChanged value) +{ + d_func()->whatChanged = value; +} - 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(), canceled() - */ -void QGesture::updateState(Qt::GestureState state) +QPointF QPinchGesture::startCenterPoint() const { - Q_D(QGesture); - if (d->state == state) { - if (state == Qt::GestureUpdated) - emit triggered(); - return; - } - const Qt::GestureState oldState = d->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. - d->state = state; - qWarning("QGesture::updateState: incorrect new state"); - return; - } - if (oldState == Qt::NoGesture) { - d->state = Qt::GestureStarted; - emit started(); - } - d->state = state; - if (state == Qt::GestureUpdated) - emit triggered(); - else if (state == Qt::GestureFinished) - emit finished(); - else if (state == Qt::NoGesture) - emit canceled(); - - 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) + return d_func()->startCenterPoint; +} + +QPointF QPinchGesture::lastCenterPoint() const { - 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); + return d_func()->lastCenterPoint; } -/*! - Returns the graphics item the gesture is filtering events for. +QPointF QPinchGesture::centerPoint() const +{ + return d_func()->centerPoint; +} - \sa setGraphicsItem() -*/ -QGraphicsItem* QGesture::graphicsItem() const +void QPinchGesture::setStartCenterPoint(const QPointF &value) { - return d_func()->graphicsItem; + d_func()->startCenterPoint = value; } -/*! \fn void QGesture::reset() +void QPinchGesture::setLastCenterPoint(const QPointF &value) +{ + d_func()->lastCenterPoint = value; +} - 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 canceled() - signal if the state() of the gesture indicated it was active. -*/ -void QGesture::reset() +void QPinchGesture::setCenterPoint(const QPointF &value) +{ + d_func()->centerPoint = value; +} + + +qreal QPinchGesture::totalScaleFactor() const +{ + return d_func()->totalScaleFactor; +} + +qreal QPinchGesture::lastScaleFactor() const +{ + return d_func()->lastScaleFactor; +} + +qreal QPinchGesture::scaleFactor() const +{ + return d_func()->scaleFactor; +} + +void QPinchGesture::setTotalScaleFactor(qreal value) +{ + d_func()->totalScaleFactor = value; +} + +void QPinchGesture::setLastScaleFactor(qreal value) +{ + d_func()->lastScaleFactor = value; +} + +void QPinchGesture::setScaleFactor(qreal value) +{ + d_func()->scaleFactor = value; +} + + +qreal QPinchGesture::totalRotationAngle() const +{ + return d_func()->totalRotationAngle; +} + +qreal QPinchGesture::lastRotationAngle() const +{ + return d_func()->lastRotationAngle; +} + +qreal QPinchGesture::rotationAngle() const +{ + return d_func()->rotationAngle; +} + +void QPinchGesture::setTotalRotationAngle(qreal value) +{ + d_func()->totalRotationAngle = value; +} + +void QPinchGesture::setLastRotationAngle(qreal value) +{ + d_func()->lastRotationAngle = value; +} + +void QPinchGesture::setRotationAngle(qreal value) +{ + d_func()->rotationAngle = value; +} + +// QSwipeGesture + +QSwipeGesture::QSwipeGesture(QObject *parent) + : QGesture(*new QSwipeGesturePrivate, Qt::SwipeGesture, parent) +{ +} + +QSwipeGesture::SwipeDirection QSwipeGesture::horizontalDirection() const +{ + return d_func()->horizontalDirection; +} + +QSwipeGesture::SwipeDirection QSwipeGesture::verticalDirection() const +{ + return d_func()->verticalDirection; +} + +void QSwipeGesture::setHorizontalDirection(QSwipeGesture::SwipeDirection value) +{ + d_func()->horizontalDirection = value; +} + +void QSwipeGesture::setVerticalDirection(QSwipeGesture::SwipeDirection value) +{ + d_func()->verticalDirection = value; +} + +qreal QSwipeGesture::swipeAngle() const +{ + return d_func()->swipeAngle; +} + +void QSwipeGesture::setSwipeAngle(qreal value) { - updateState(Qt::NoGesture); + d_func()->swipeAngle = value; } QT_END_NAMESPACE |