diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-06-15 14:00:46 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2009-07-02 15:16:07 (GMT) |
commit | 60e965fd35037f4a27816d2aeccafdff0d6ae9d6 (patch) | |
tree | 8a968022a03057cc29aa3f7cc246ee708d869b8a /src | |
parent | 099a32d121cbc80a1a234c3146f4be9b5237e7e8 (diff) | |
download | Qt-60e965fd35037f4a27816d2aeccafdff0d6ae9d6.zip Qt-60e965fd35037f4a27816d2aeccafdff0d6ae9d6.tar.gz Qt-60e965fd35037f4a27816d2aeccafdff0d6ae9d6.tar.bz2 |
Refactored gesture api
Rewritten the api almost from scratch, making it simplier and more
flexible at the same time.
The current implementation will not have complex gseturemanager class
inside Qt, but the QGesture base class, which represents both a
gesture recognizer and a gesture itself with a set of properties. A
set of common gestures that can use used in third-party applications
(and in Qt itself internally) is supposed to be found in
qstandardgestures.h, and a base class for user-defined gestures is in
qgesture.h
Gesture implementation for Pan on Windows7 has also been added as a
reference implementation for platform gestures.
Diffstat (limited to 'src')
46 files changed, 887 insertions, 3178 deletions
diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index 0006026..e0584e5 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -1562,18 +1562,6 @@ public: }; Q_DECLARE_FLAGS(TouchPointStates, TouchPointState) - enum GestureType - { - UnknownGesture, - TapGesture, - DoubleTapGesture, - TrippleTapGesture, - TapAndHoldGesture, - PanGesture, - PinchGesture - }; - - enum GestureState { NoGesture, @@ -1582,23 +1570,6 @@ public: GestureFinished = 3 }; - enum DirectionType - { - NoDirection = 0, - LeftDownDirection = 1, - DownLeftDirection = LeftDownDirection, - DownDirection = 2, - RightDownDirection = 3, - DownRightDirection = RightDownDirection, - LeftDirection = 4, - RightDirection = 6, - LeftUpDirection = 7, - UpLeftDirection = LeftUpDirection, - UpDirection = 8, - RightUpDirection = 9, - UpRightDirection = RightUpDirection - }; - } #ifdef Q_MOC_RUN ; diff --git a/src/corelib/kernel/qcoreevent.cpp b/src/corelib/kernel/qcoreevent.cpp index 504ae84..c636716 100644 --- a/src/corelib/kernel/qcoreevent.cpp +++ b/src/corelib/kernel/qcoreevent.cpp @@ -225,8 +225,6 @@ QT_BEGIN_NAMESPACE \value TouchBegin Beginning of a sequence of touch-screen and/or track-pad events (QTouchEvent) \value TouchUpdate Touch-screen event (QTouchEvent) \value TouchEnd End of touch-event sequence (QTouchEvent) - \value Gesture A gesture has occured. - \value GraphicsSceneGesture A gesture has occured on a graphics scene. User events should have values between \c User and \c{MaxUser}: @@ -271,6 +269,7 @@ QT_BEGIN_NAMESPACE \omitvalue CocoaRequestModal \omitvalue Wrapped \omitvalue Signal + \omitvalue WinGesture */ /*! diff --git a/src/corelib/kernel/qcoreevent.h b/src/corelib/kernel/qcoreevent.h index 4929c51..1d86f47 100644 --- a/src/corelib/kernel/qcoreevent.h +++ b/src/corelib/kernel/qcoreevent.h @@ -276,8 +276,7 @@ public: TouchUpdate = 195, TouchEnd = 196, - Gesture = 197, - GraphicsSceneGesture = 198, + WinGesture = 197, // 512 reserved for Qt Jambi's MetaCall event // 513 reserved for Qt Jambi's DeleteOnMainThread event diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 2223f5d..c82c2e5 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -574,7 +574,6 @@ #include <private/qtextcontrol_p.h> #include <private/qtextdocumentlayout_p.h> #include <private/qtextengine_p.h> -#include <private/qgesturemanager_p.h> #ifdef Q_WS_X11 #include <private/qt_x11_p.h> @@ -615,8 +614,6 @@ public: }; Q_GLOBAL_STATIC(QGraphicsItemCustomDataStore, qt_dataStore) -QString qt_getStandardGestureTypeName(Qt::GestureType type); - /*! \internal @@ -1131,7 +1128,6 @@ QGraphicsItem::~QGraphicsItem() { d_ptr->inDestructor = 1; d_ptr->removeExtraItemCache(); - d_ptr->removeExtraGestures(); clearFocus(); if (!d_ptr->children.isEmpty()) { @@ -6251,104 +6247,6 @@ QVariant QGraphicsItem::inputMethodQuery(Qt::InputMethodQuery query) const } /*! - \since 4.6 - - Subscribes the graphics item to the specified \a gesture type. - - Returns the id of the gesture. - - \sa releaseGesture(), setGestureEnabled() -*/ -int QGraphicsItem::grabGesture(Qt::GestureType gesture) -{ - /// TODO: if we are QGraphicsProxyWidget we should subscribe the widget to gesture as well. - return grabGesture(qt_getStandardGestureTypeName(gesture)); -} - -/*! - \since 4.6 - - Subscribes the graphics item to the specified \a gesture type. - - Returns the id of the gesture. - - \sa releaseGesture(), setGestureEnabled() -*/ -int QGraphicsItem::grabGesture(const QString &gesture) -{ - int id = QGestureManager::instance()->makeGestureId(gesture); - d_ptr->grabGesture(id); - return id; -} - -void QGraphicsItemPrivate::grabGesture(int id) -{ - Q_Q(QGraphicsItem); - extraGestures()->gestures << id; - if (scene) - scene->d_func()->grabGesture(q, id); -} - -bool QGraphicsItemPrivate::releaseGesture(int id) -{ - Q_Q(QGraphicsItem); - QGestureExtraData *extra = maybeExtraGestures(); - if (extra && extra->gestures.contains(id)) { - if (scene) - scene->d_func()->releaseGesture(q, id); - extra->gestures.remove(id); - return true; - } - return false; -} - -/*! - \since 4.6 - - Unsubscribes the graphics item from a gesture, which is specified - by the \a gestureId. - - \sa grabGesture(), setGestureEnabled() -*/ -void QGraphicsItem::releaseGesture(int gestureId) -{ - /// TODO: if we are QGraphicsProxyWidget we should unsubscribe the widget from gesture as well. - if (d_ptr->releaseGesture(gestureId)) - QGestureManager::instance()->releaseGestureId(gestureId); -} - -/*! - \since 4.6 - - If \a enable is true, the gesture with the given \a gestureId is - enabled; otherwise the gesture is disabled. - - The id of the gesture is returned by the grabGesture(). - - \sa grabGesture(), releaseGesture() -*/ -void QGraphicsItem::setGestureEnabled(int gestureId, bool enable) -{ - Q_UNUSED(gestureId); - Q_UNUSED(enable); - //### -} - -bool QGraphicsItemPrivate::hasGesture(const QString &name) const -{ - if (QGestureExtraData *extra = maybeExtraGestures()) { - QGestureManager *gm = QGestureManager::instance(); - QSet<int>::const_iterator it = extra->gestures.begin(), - e = extra->gestures.end(); - for (; it != e; ++it) { - if (gm->gestureNameFromId(*it) == name) - return true; - } - } - return false; -} - -/*! This virtual function is called by QGraphicsItem to notify custom items that some part of the item's state changes. By reimplementing this function, your can react to a change, and in some cases, (depending on \a @@ -6372,23 +6270,6 @@ bool QGraphicsItemPrivate::hasGesture(const QString &name) const QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) { Q_UNUSED(change); - if (change == QGraphicsItem::ItemSceneChange) { - QGestureExtraData *extra = d_ptr->maybeExtraGestures(); - if (!qVariantValue<QGraphicsScene*>(value) && extra) { - // the item has been removed from a scene, unsubscribe gestures. - Q_ASSERT(d_ptr->scene); - foreach(int id, extra->gestures) - d_ptr->scene->d_func()->releaseGesture(this, id); - } - } else if (change == QGraphicsItem::ItemSceneHasChanged) { - QGraphicsScene *scene = qVariantValue<QGraphicsScene*>(value); - QGestureExtraData *extra = d_ptr->maybeExtraGestures(); - if (scene && extra) { - // item has been added to the scene - foreach(int id, extra->gestures) - scene->d_func()->grabGesture(this, id); - } - } return value; } diff --git a/src/gui/graphicsview/qgraphicsitem.h b/src/gui/graphicsview/qgraphicsitem.h index 3a3d1a1..b0571c2 100644 --- a/src/gui/graphicsview/qgraphicsitem.h +++ b/src/gui/graphicsview/qgraphicsitem.h @@ -379,11 +379,6 @@ public: QVariant data(int key) const; void setData(int key, const QVariant &value); - int grabGesture(Qt::GestureType gesture); - int grabGesture(const QString &gesture); - void releaseGesture(int gestureId); - void setGestureEnabled(int gestureId, bool enable = true); - enum { Type = 1, UserType = 65536 diff --git a/src/gui/graphicsview/qgraphicsitem_p.h b/src/gui/graphicsview/qgraphicsitem_p.h index a0d061b..a977e1e 100644 --- a/src/gui/graphicsview/qgraphicsitem_p.h +++ b/src/gui/graphicsview/qgraphicsitem_p.h @@ -93,12 +93,6 @@ public: void purge(); }; -class QGestureExtraData -{ -public: - QSet<int> gestures; -}; - class Q_AUTOTEST_EXPORT QGraphicsItemPrivate { Q_DECLARE_PUBLIC(QGraphicsItem) @@ -108,8 +102,7 @@ public: ExtraCursor, ExtraCacheData, ExtraMaxDeviceCoordCacheSize, - ExtraBoundingRegionGranularity, - ExtraGestures + ExtraBoundingRegionGranularity }; enum AncestorFlag { @@ -394,30 +387,6 @@ public: int index; int depth; - inline QGestureExtraData* extraGestures() const - { - QGestureExtraData *c = (QGestureExtraData *)qVariantValue<void *>(extra(ExtraGestures)); - if (!c) { - QGraphicsItemPrivate *that = const_cast<QGraphicsItemPrivate *>(this); - c = new QGestureExtraData; - that->setExtra(ExtraGestures, qVariantFromValue<void *>(c)); - } - return c; - } - QGestureExtraData* maybeExtraGestures() const - { - return (QGestureExtraData *)qVariantValue<void *>(extra(ExtraGestures)); - } - inline void removeExtraGestures() - { - QGestureExtraData *c = (QGestureExtraData *)qVariantValue<void *>(extra(ExtraGestures)); - delete c; - unsetExtra(ExtraGestures); - } - bool hasGesture(const QString &gesture) const; - void grabGesture(int id); - bool releaseGesture(int id); - // Packed 32 bytes quint32 acceptedMouseButtons : 5; quint32 visible : 1; diff --git a/src/gui/graphicsview/qgraphicsproxywidget.cpp b/src/gui/graphicsview/qgraphicsproxywidget.cpp index f081222..0b421ad 100644 --- a/src/gui/graphicsview/qgraphicsproxywidget.cpp +++ b/src/gui/graphicsview/qgraphicsproxywidget.cpp @@ -48,7 +48,6 @@ #include "private/qgraphicsproxywidget_p.h" #include "private/qwidget_p.h" #include "private/qapplication_p.h" -#include "private/qgesturemanager_p.h" #include <QtCore/qdebug.h> #include <QtGui/qevent.h> @@ -649,9 +648,6 @@ void QGraphicsProxyWidgetPrivate::setWidget_helper(QWidget *newWidget, bool auto q->setAttribute(Qt::WA_OpaquePaintEvent); widget = newWidget; - foreach(int gestureId, widget->d_func()->gestures) { - grabGesture(gestureId); - } // Changes only go from the widget to the proxy. enabledChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode; @@ -875,15 +871,6 @@ bool QGraphicsProxyWidget::event(QEvent *event) } break; } - case QEvent::GraphicsSceneGesture: { - qDebug() << "QGraphicsProxyWidget: graphicsscenegesture"; - if (d->widget && d->widget->isVisible()) { - //### TODO: widget->childAt(): decompose gesture event and find widget under hotspots. - //QGestureManager::instance()->sendGestureEvent(d->widget, ge->gestures().toSet(), ge->cancelledGestures()); - return true; - } - break; - } default: break; } diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index e50ee94..c3f72e6 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -247,8 +247,6 @@ #ifdef Q_WS_X11 #include <private/qt_x11_p.h> #endif -#include <private/qgesturemanager_p.h> -#include <private/qgesture_p.h> QT_BEGIN_NAMESPACE @@ -4018,47 +4016,6 @@ bool QGraphicsScene::event(QEvent *event) // geometries that do not have an explicit style set. update(); break; - case QEvent::GraphicsSceneGesture: { - QGraphicsSceneGestureEvent *ev = static_cast<QGraphicsSceneGestureEvent*>(event); - QGraphicsView *view = qobject_cast<QGraphicsView*>(ev->widget()); - if (!view) { - qWarning("QGraphicsScene::event: gesture event was received without a view"); - break; - } - - // get a list of gestures that just started. - QSet<QGesture*> startedGestures; - QList<QGesture*> gestures = ev->gestures(); - for(QList<QGesture*>::const_iterator it = gestures.begin(), e = gestures.end(); - it != e; ++it) { - QGesture *g = *it; - QGesturePrivate *gd = g->d_func(); - if (g->state() == Qt::GestureStarted || gd->singleshot) { - startedGestures.insert(g); - } - } - if (!startedGestures.isEmpty()) { - // find a target for each started gesture. - for(QSet<QGesture*>::const_iterator it = startedGestures.begin(), e = startedGestures.end(); - it != e; ++it) { - QGesture *g = *it; - QGesturePrivate *gd = g->d_func(); - gd->graphicsItem = 0; - QList<QGraphicsItem*> itemsInGestureArea = items(g->hotSpot()); - const QString gestureName = g->type(); - foreach(QGraphicsItem *item, itemsInGestureArea) { - if (item->d_func()->hasGesture(gestureName)) { - Q_ASSERT(gd->graphicsItem == 0); - gd->graphicsItem = item; - d->itemsWithGestures[item].insert(g); - break; - } - } - } - } - d->sendGestureEvent(ev->gestures().toSet(), ev->cancelledGestures()); - } - break; case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: @@ -4080,69 +4037,6 @@ bool QGraphicsScene::event(QEvent *event) return true; } -void QGraphicsScenePrivate::sendGestureEvent(const QSet<QGesture*> &gestures, const QSet<QString> &cancelled) -{ - Q_Q(QGraphicsScene); - typedef QMap<QGraphicsItem*, QSet<QGesture*> > ItemGesturesMap; - ItemGesturesMap itemGestures; - QSet<QGesture*> startedGestures; - for(QSet<QGesture*>::const_iterator it = gestures.begin(), e = gestures.end(); - it != e; ++it) { - QGesture *g = *it; - Q_ASSERT(g != 0); - QGesturePrivate *gd = g->d_func(); - if (gd->graphicsItem != 0) { - itemGestures[gd->graphicsItem].insert(g); - if (g->state() == Qt::GestureStarted || gd->singleshot) - startedGestures.insert(g); - } - } - - QSet<QGesture*> ignoredGestures; - for(ItemGesturesMap::const_iterator it = itemGestures.begin(), e = itemGestures.end(); - it != e; ++it) { - QGraphicsItem *receiver = it.key(); - Q_ASSERT(receiver != 0); - QGraphicsSceneGestureEvent event; - event.setGestures(it.value()); - event.setCancelledGestures(cancelled); - bool processed = sendEvent(receiver, &event); - QSet<QGesture*> started = startedGestures.intersect(it.value()); - if (event.isAccepted()) - foreach(QGesture *g, started) - g->accept(); - if (!started.isEmpty() && !(processed && event.isAccepted())) { - // there are started gestures event that weren't - // accepted, so propagating each gesture independently. - QSet<QGesture*>::const_iterator it = started.begin(), - e = started.end(); - for(; it != e; ++it) { - QGesture *g = *it; - if (processed && g->isAccepted()) { - continue; - } - QGesturePrivate *gd = g->d_func(); - gd->graphicsItem = 0; - - //### THIS IS BS, DONT FORGET TO REWRITE THIS CODE - // need to make sure we try to deliver event just once to each widget - const QString gestureType = g->type(); - QList<QGraphicsItem*> itemsUnderGesture = q->items(g->hotSpot()); - for (int i = 0; i < itemsUnderGesture.size(); ++i) { - QGraphicsItem *item = itemsUnderGesture.at(i); - if (item != receiver && item->d_func()->hasGesture(gestureType)) { - ignoredGestures.insert(g); - gd->graphicsItem = item; - break; - } - } - } - } - } - if (!ignoredGestures.isEmpty()) - sendGestureEvent(ignoredGestures, cancelled); -} - /*! \reimp @@ -6018,32 +5912,11 @@ bool QGraphicsScene::sendEvent(QGraphicsItem *item, QEvent *event) void QGraphicsScenePrivate::addView(QGraphicsView *view) { views << view; - foreach(int gestureId, grabbedGestures) - view->d_func()->grabGesture(gestureId); } void QGraphicsScenePrivate::removeView(QGraphicsView *view) { views.removeAll(view); - foreach(int gestureId, grabbedGestures) - view->releaseGesture(gestureId); -} - -void QGraphicsScenePrivate::grabGesture(QGraphicsItem *item, int gestureId) -{ - if (!grabbedGestures.contains(gestureId)) { - foreach(QGraphicsView *view, views) - view->d_func()->grabGesture(gestureId); - } - (void)itemsWithGestures[item]; - grabbedGestures << gestureId; -} - -void QGraphicsScenePrivate::releaseGesture(QGraphicsItem *item, int gestureId) -{ - Q_UNUSED(gestureId); - itemsWithGestures.remove(item); - //### } void QGraphicsScenePrivate::updateTouchPointsForItem(QGraphicsItem *item, QTouchEvent *touchEvent) diff --git a/src/gui/graphicsview/qgraphicsscene.h b/src/gui/graphicsview/qgraphicsscene.h index c8b147f..6aaeb91 100644 --- a/src/gui/graphicsview/qgraphicsscene.h +++ b/src/gui/graphicsview/qgraphicsscene.h @@ -79,7 +79,6 @@ class QGraphicsSceneHelpEvent; class QGraphicsSceneHoverEvent; class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; -class QGraphicsSceneGestureEvent; class QGraphicsSimpleTextItem; class QGraphicsTextItem; class QGraphicsView; diff --git a/src/gui/graphicsview/qgraphicsscene_p.h b/src/gui/graphicsview/qgraphicsscene_p.h index 3c3a811..d2d603a 100644 --- a/src/gui/graphicsview/qgraphicsscene_p.h +++ b/src/gui/graphicsview/qgraphicsscene_p.h @@ -78,7 +78,6 @@ QT_BEGIN_NAMESPACE class QGraphicsView; class QGraphicsWidget; -class QGesture; class QGraphicsScenePrivate : public QObjectPrivate { @@ -309,13 +308,6 @@ public: QStyleOptionGraphicsItem styleOptionTmp; - // items with gestures -> list of started gestures. - QMap<QGraphicsItem*, QSet<QGesture*> > itemsWithGestures; - QSet<int> grabbedGestures; - void grabGesture(QGraphicsItem *item, int gestureId); - void releaseGesture(QGraphicsItem *item, int gestureId); - void sendGestureEvent(const QSet<QGesture*> &gestures, const QSet<QString> &cancelled); - QMap<int, QTouchEvent::TouchPoint> sceneCurrentTouchPoints; QMap<int, QGraphicsItem *> itemForTouchPointId; static void updateTouchPointsForItem(QGraphicsItem *item, QTouchEvent *touchEvent); diff --git a/src/gui/graphicsview/qgraphicssceneevent.cpp b/src/gui/graphicsview/qgraphicssceneevent.cpp index 27a2d7e..53019f2 100644 --- a/src/gui/graphicsview/qgraphicssceneevent.cpp +++ b/src/gui/graphicsview/qgraphicssceneevent.cpp @@ -275,8 +275,6 @@ QT_BEGIN_NAMESPACE -QString qt_getStandardGestureTypeName(Qt::GestureType type); - class QGraphicsSceneEventPrivate { public: @@ -1679,253 +1677,6 @@ void QGraphicsSceneMoveEvent::setNewPos(const QPointF &pos) d->newPos = pos; } -/*! - \class QGraphicsSceneGestureEvent - \brief The QGraphicsSceneGestureEvent class provides gesture events for - the graphics view framework. - \since 4.6 - \ingroup multimedia - \ingroup graphicsview-api - - QGraphicsSceneGestureEvent extends information provided by - QGestureEvent by adding some convenience functions like - \l{QGraphicsSceneEvent::}{widget()} to get a widget that received - original gesture event, and convenience functions mapToScene(), - mapToItem() for converting positions of the gesture into - QGraphicsScene and QGraphicsItem coordinate system respectively. - - The scene sends the event to the first QGraphicsItem under the - mouse cursor that accepts gestures; a graphics item is set to accept - gestures with \l{QGraphicsItem::}{grabGesture()}. - - \sa QGestureEvent -*/ - -/*! - Constructs a QGraphicsSceneGestureEvent. -*/ -QGraphicsSceneGestureEvent::QGraphicsSceneGestureEvent() - : QGraphicsSceneEvent(QEvent::GraphicsSceneGesture) -{ - setAccepted(false); -} - -/*! - Destroys a QGraphicsSceneGestureEvent. -*/ -QGraphicsSceneGestureEvent::~QGraphicsSceneGestureEvent() -{ -} - -/*! - Returns true if the gesture event contains gesture of specific \a - type; returns false otherwise. -*/ -bool QGraphicsSceneGestureEvent::contains(const QString &type) const -{ - return gesture(type) != 0; -} - -/*! - Returns true if the gesture event contains gesture of specific \a - type; returns false otherwise. -*/ -bool QGraphicsSceneGestureEvent::contains(Qt::GestureType type) const -{ - return contains(qt_getStandardGestureTypeName(type)); -} - -/*! - Returns a list of gesture names that this event contains. -*/ -QList<QString> QGraphicsSceneGestureEvent::gestureTypes() const -{ - return m_gestures.keys(); -} - -/*! - Returns extended information about a gesture of specific \a type. -*/ -const QGesture* QGraphicsSceneGestureEvent::gesture(const QString &type) const -{ - return m_gestures.value(type, 0); -} - -/*! - Returns extended information about a gesture of specific \a type. -*/ -const QGesture* QGraphicsSceneGestureEvent::gesture(Qt::GestureType type) const -{ - return gesture(qt_getStandardGestureTypeName(type)); -} - -/*! - Returns extended information about all gestures in the event. -*/ -QList<QGesture*> QGraphicsSceneGestureEvent::gestures() const -{ - return m_gestures.values(); -} - -/*! - Returns a set of gesture names that used to be executed, but were - cancelled (i.e. they were not finished properly). -*/ -QSet<QString> QGraphicsSceneGestureEvent::cancelledGestures() const -{ - return m_cancelledGestures; -} - -/*! - Sets a list of gesture names \a cancelledGestures that used to be - executed, but were cancelled (i.e. they were not finished - properly). -*/ -void QGraphicsSceneGestureEvent::setCancelledGestures(const QSet<QString> &cancelledGestures) -{ - m_cancelledGestures = cancelledGestures; -} - -/*! - Maps the point \a point, which is in a view coordinate system, to - scene coordinate system, and returns the mapped coordinate. - - A \a point is in coordinate system of the widget that received - gesture event. - - \sa mapToItem(), {The Graphics View Coordinate System} -*/ -QPointF QGraphicsSceneGestureEvent::mapToScene(const QPoint &point) const -{ - if (QGraphicsView *view = qobject_cast<QGraphicsView*>(widget())) - return view->mapToScene(point); - return QPointF(); -} - -/*! - Maps the rectangular \a rect, which is in a view coordinate system, to - scene coordinate system, and returns the mapped coordinate. - - A \a rect is in coordinate system of the widget that received - gesture event. - - \sa mapToItem(), {The Graphics View Coordinate System} -*/ -QPolygonF QGraphicsSceneGestureEvent::mapToScene(const QRect &rect) const -{ - if (QGraphicsView *view = qobject_cast<QGraphicsView*>(widget())) - return view->mapToScene(rect); - return QPolygonF(); -} - -/*! - Maps the point \a point, which is in a view coordinate system, to - item's \a item coordinate system, and returns the mapped coordinate. - - If \a item is 0, this function returns the same as mapToScene(). - - \sa mapToScene(), {The Graphics View Coordinate System} -*/ -QPointF QGraphicsSceneGestureEvent::mapToItem(const QPoint &point, QGraphicsItem *item) const -{ - if (item) { - if (QGraphicsView *view = qobject_cast<QGraphicsView*>(widget())) - return item->mapFromScene(view->mapToScene(point)); - } else { - return mapToScene(point); - } - return QPointF(); -} - -/*! - Maps the rectangualar \a rect, which is in a view coordinate system, to - item's \a item coordinate system, and returns the mapped coordinate. - - If \a item is 0, this function returns the same as mapToScene(). - - \sa mapToScene(), {The Graphics View Coordinate System} -*/ -QPolygonF QGraphicsSceneGestureEvent::mapToItem(const QRect &rect, QGraphicsItem *item) const -{ - if (item) { - if (QGraphicsView *view = qobject_cast<QGraphicsView*>(widget())) - return item->mapFromScene(view->mapToScene(rect)); - } else { - return mapToScene(rect); - } - return QPolygonF(); -} - -/*! - Set a list of gesture objects containing extended information about \a gestures. -*/ -void QGraphicsSceneGestureEvent::setGestures(const QList<QGesture*> &gestures) -{ - foreach(QGesture *g, gestures) - m_gestures.insert(g->type(), g); -} - -/*! - Set a list of gesture objects containing extended information about \a gestures. -*/ -void QGraphicsSceneGestureEvent::setGestures(const QSet<QGesture*> &gestures) -{ - foreach(QGesture *g, gestures) - m_gestures.insert(g->type(), g); -} - -/*! - Sets the accept flag of the all gestures for the event object. - This is the equivalent of calling \l{QEvent::accept()} {accept()} - or \l{QEvent::setAccepted()}{setAccepted(true)}. - - Setting the accept flag indicates that the event receiver wants - the gesture. Unwanted gestures might be propagated to the parent - widget. -*/ -void QGraphicsSceneGestureEvent::acceptAll() -{ - QHash<QString, QGesture*>::iterator it = m_gestures.begin(), - e = m_gestures.end(); - for(; it != e; ++it) - it.value()->accept(); - setAccepted(true); -} - -/*! \fn void QGraphicsSceneGestureEvent::accept() - Calls QEvent::accept(). -*/ - -/*! - Sets the accept flag of the gesture specified by \a type. This is - equivalent to calling \l{QGestureEvent::gesture()} {gesture(type)}-> - \l{QGesture::accept()}{accept()} - - Setting the accept flag indicates that the event receiver - wants the gesture. Unwanted gestures might be propagated to the parent - widget. -*/ -void QGraphicsSceneGestureEvent::accept(Qt::GestureType type) -{ - if (QGesture *g = m_gestures.value(qt_getStandardGestureTypeName(type), 0)) - g->accept(); -} - -/*! - - Sets the accept flag of the gesture specified by \a type. This is - equivalent to calling \l{QGestureEvent::gesture()} {gesture(type)}-> - \l{QGesture::accept()}{accept()} - - Setting the accept flag indicates that the event receiver wants the - gesture. Unwanted gestures might be propagated to the parent widget. -*/ -void QGraphicsSceneGestureEvent::accept(const QString &type) -{ - if (QGesture *g = m_gestures.value(type, 0)) - g->accept(); -} - QT_END_NAMESPACE #endif // QT_NO_GRAPHICSVIEW diff --git a/src/gui/graphicsview/qgraphicssceneevent.h b/src/gui/graphicsview/qgraphicssceneevent.h index 8a30bb8..b38e757 100644 --- a/src/gui/graphicsview/qgraphicssceneevent.h +++ b/src/gui/graphicsview/qgraphicssceneevent.h @@ -306,49 +306,6 @@ public: void setNewPos(const QPointF &pos); }; -class QGesture; -class QGraphicsItem; -class QGraphicsSceneGestureEventPrivate; -class Q_GUI_EXPORT QGraphicsSceneGestureEvent : public QGraphicsSceneEvent -{ - Q_DECLARE_PRIVATE(QGraphicsSceneGestureEvent) -public: - QGraphicsSceneGestureEvent(); - ~QGraphicsSceneGestureEvent(); - - bool contains(const QString &type) const; - bool contains(Qt::GestureType type) const; - - QList<QString> gestureTypes() const; - - const QGesture* gesture(Qt::GestureType type) const; - const QGesture* gesture(const QString &type) const; - QList<QGesture*> gestures() const; - void setGestures(const QList<QGesture*> &gestures); - void setGestures(const QSet<QGesture*> &gestures); - - QSet<QString> cancelledGestures() const; - void setCancelledGestures(const QSet<QString> &cancelledGestures); - - void acceptAll(); -#ifndef Q_NO_USING_KEYWORD - using QEvent::accept; -#else - inline void accept() { QEvent::accept(); } -#endif - void accept(Qt::GestureType type); - void accept(const QString &type); - - QPointF mapToScene(const QPoint &point) const; - QPolygonF mapToScene(const QRect &rect) const; - QPointF mapToItem(const QPoint &point, QGraphicsItem *item) const; - QPolygonF mapToItem(const QRect &rect, QGraphicsItem *item) const; - -protected: - QHash<QString, QGesture*> m_gestures; - QSet<QString> m_cancelledGestures; -}; - #endif // QT_NO_GRAPHICSVIEW QT_END_NAMESPACE diff --git a/src/gui/graphicsview/qgraphicsview.cpp b/src/gui/graphicsview/qgraphicsview.cpp index a2adf82..56a69f7 100644 --- a/src/gui/graphicsview/qgraphicsview.cpp +++ b/src/gui/graphicsview/qgraphicsview.cpp @@ -2658,9 +2658,6 @@ bool QGraphicsView::event(QEvent *event) } } break; - case QEvent::Gesture: - viewportEvent(event); - return true; default: break; } @@ -2742,17 +2739,6 @@ bool QGraphicsView::viewportEvent(QEvent *event) d->scene->d_func()->updateAll = false; } break; - case QEvent::Gesture: { - QGraphicsSceneGestureEvent gestureEvent; - gestureEvent.setWidget(this); - QGestureEvent *ev = static_cast<QGestureEvent*>(event); - gestureEvent.setGestures(ev->gestures()); - gestureEvent.setCancelledGestures(ev->cancelledGestures()); - QApplication::sendEvent(d->scene, &gestureEvent); - event->setAccepted(gestureEvent.isAccepted()); - return true; - } - break; case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: diff --git a/src/gui/kernel/kernel.pri b/src/gui/kernel/kernel.pri index 2917592..e6eff6e 100644 --- a/src/gui/kernel/kernel.pri +++ b/src/gui/kernel/kernel.pri @@ -44,12 +44,8 @@ HEADERS += \ kernel/qkeymapper_p.h \ kernel/qgesture.h \ kernel/qgesture_p.h \ - kernel/qgesturemanager_p.h \ - kernel/qgesturerecognizer_p.h \ - kernel/qgesturerecognizer.h \ - kernel/qgesturestandardrecognizers_p.h \ - kernel/qdirectionrecognizer_p.h \ - kernel/qdirectionsimplificator_p.h + kernel/qstandardgestures.h \ + kernel/qstandardgestures_p.h SOURCES += \ kernel/qaction.cpp \ @@ -80,10 +76,7 @@ SOURCES += \ kernel/qwidgetaction.cpp \ kernel/qkeymapper.cpp \ kernel/qgesture.cpp \ - kernel/qgesturemanager.cpp \ - kernel/qgesturerecognizer.cpp \ - kernel/qgesturestandardrecognizers.cpp \ - kernel/qdirectionrecognizer.cpp + kernel/qstandardgestures.cpp win32 { DEFINES += QT_NO_DIRECTDRAW diff --git a/src/gui/kernel/qapplication.cpp b/src/gui/kernel/qapplication.cpp index a7b7a0a..0e8978f 100644 --- a/src/gui/kernel/qapplication.cpp +++ b/src/gui/kernel/qapplication.cpp @@ -90,8 +90,6 @@ #include "qapplication.h" -#include <private/qgesturemanager_p.h> - #ifdef Q_WS_WINCE #include "qdatetime.h" #include "qguifunctions_wince.h" @@ -137,14 +135,6 @@ int QApplicationPrivate::autoMaximizeThreshold = -1; bool QApplicationPrivate::autoSipEnabled = false; #endif -QGestureManager* QGestureManager::instance() -{ - QApplicationPrivate *d = qApp->d_func(); - if (!d->gestureManager) - d->gestureManager = new QGestureManager(qApp); - return d->gestureManager; -} - QApplicationPrivate::QApplicationPrivate(int &argc, char **argv, QApplication::Type type) : QCoreApplicationPrivate(argc, argv) { @@ -168,8 +158,6 @@ QApplicationPrivate::QApplicationPrivate(int &argc, char **argv, QApplication::T directPainters = 0; #endif - gestureManager = 0; - if (!self) self = this; } @@ -3595,14 +3583,6 @@ bool QApplication::notify(QObject *receiver, QEvent *e) #endif // !QT_NO_WHEELEVENT || !QT_NO_TABLETEVENT } - if (!d->grabbedGestures.isEmpty() && e->spontaneous() && receiver->isWidgetType()) { - const QEvent::Type t = e->type(); - if (t != QEvent::Gesture && t != QEvent::GraphicsSceneGesture) { - if (QGestureManager::instance()->filterEvent(static_cast<QWidget*>(receiver), e)) - return true; - } - } - // User input and window activation makes tooltips sleep switch (e->type()) { case QEvent::Wheel: @@ -4061,6 +4041,19 @@ bool QApplication::notify(QObject *receiver, QEvent *e) touchEvent->setAccepted(eventAccepted); break; } + case QEvent::WinGesture: + { + // only propagate the first gesture event (after the GID_BEGIN) + QWidget *w = static_cast<QWidget *>(receiver); + while (w) { + e->ignore(); + res = d->notify_helper(w, e); + if ((res && e->isAccepted()) || w->isWindow()) + break; + w = w->parentWidget(); + } + break; + } default: res = d->notify_helper(receiver, e); break; @@ -5057,57 +5050,6 @@ bool QApplicationPrivate::shouldSetFocus(QWidget *w, Qt::FocusPolicy policy) return true; } -/*! - \since 4.6 - - Adds custom gesture \a recognizer object. - - Qt takes ownership of the provided \a recognizer. - - \sa QGestureEvent -*/ -void QApplication::addGestureRecognizer(QGestureRecognizer *recognizer) -{ - QGestureManager::instance()->addRecognizer(recognizer); -} - -/*! - \since 4.6 - - Removes custom gesture \a recognizer object. - - \sa QGestureEvent -*/ -void QApplication::removeGestureRecognizer(QGestureRecognizer *recognizer) -{ - Q_D(QApplication); - if (!d->gestureManager) - return; - d->gestureManager->removeRecognizer(recognizer); -} - -/*! - \property QApplication::eventDeliveryDelayForGestures - \since 4.6 - - Specifies the \a delay before input events are delivered to the - gesture enabled widgets. - - The delay allows to postpone widget's input event handling until - gestures framework can successfully recognize a gesture. - - \sa QWidget::grabGesture() -*/ -void QApplication::setEventDeliveryDelayForGestures(int delay) -{ - QGestureManager::instance()->setEventDeliveryDelay(delay); -} - -int QApplication::eventDeliveryDelayForGestures() -{ - return QGestureManager::instance()->eventDeliveryDelay(); -} - /*! \fn QDecoration &QApplication::qwsDecoration() Return the QWSDecoration used for decorating windows. diff --git a/src/gui/kernel/qapplication.h b/src/gui/kernel/qapplication.h index 954f824..19ae085 100644 --- a/src/gui/kernel/qapplication.h +++ b/src/gui/kernel/qapplication.h @@ -71,7 +71,6 @@ class QStyle; class QEventLoop; class QIcon; class QInputContext; -class QGestureRecognizer; template <typename T> class QList; class QLocale; #if defined(Q_WS_QWS) @@ -107,7 +106,6 @@ class Q_GUI_EXPORT QApplication : public QCoreApplication Q_PROPERTY(int autoMaximizeThreshold READ autoMaximizeThreshold WRITE setAutoMaximizeThreshold) Q_PROPERTY(bool autoSipEnabled READ autoSipEnabled WRITE setAutoSipEnabled) #endif - Q_PROPERTY(int eventDeliveryDelayForGestures READ eventDeliveryDelayForGestures WRITE setEventDeliveryDelayForGestures) public: enum Type { Tty, GuiClient, GuiServer }; @@ -268,12 +266,6 @@ public: static bool keypadNavigationEnabled(); #endif - void addGestureRecognizer(QGestureRecognizer *recognizer); - void removeGestureRecognizer(QGestureRecognizer *recognizer); - - void setEventDeliveryDelayForGestures(int delay); - int eventDeliveryDelayForGestures(); - Q_SIGNALS: void lastWindowClosed(); void focusChanged(QWidget *old, QWidget *now); @@ -382,7 +374,6 @@ private: friend class QDirectPainter; friend class QDirectPainterPrivate; #endif - friend class QGestureManager; #if defined(Q_WS_WIN) friend QApplicationPrivate* getQApplicationPrivateInternal(); diff --git a/src/gui/kernel/qapplication_p.h b/src/gui/kernel/qapplication_p.h index 4b2bf15..db77b07 100644 --- a/src/gui/kernel/qapplication_p.h +++ b/src/gui/kernel/qapplication_p.h @@ -193,7 +193,109 @@ extern "C" { typedef BOOL (WINAPI *qt_RegisterTouchWindowPtr)(HWND, ULONG); typedef BOOL (WINAPI *qt_GetTouchInputInfoPtr)(HANDLE, UINT, PVOID, int); typedef BOOL (WINAPI *qt_CloseTouchInputHandlePtr)(HANDLE); -#endif + +#ifndef WM_GESTURE + +#define WM_GESTURE 0x0119 +#define WM_GESTURE_NOTIFY 0x011A + +DECLARE_HANDLE(HGESTUREINFO); + +#define GF_BEGIN 0x00000001 +#define GF_INERTIA 0x00000002 +#define GF_END 0x00000004 + +/* + * Gesture IDs + */ +#define GID_BEGIN 1 +#define GID_END 2 +#define GID_ZOOM 3 +#define GID_PAN 4 +#define GID_ROTATE 5 +#define GID_TWOFINGERTAP 6 +#define GID_ROLLOVER 7 + +typedef struct tagGESTUREINFO { + UINT cbSize; // size, in bytes, of this structure (including variable length Args field) + DWORD dwFlags; // see GF_* flags + DWORD dwID; // gesture ID, see GID_* defines + HWND hwndTarget; // handle to window targeted by this gesture + POINTS ptsLocation; // current location of this gesture + DWORD dwInstanceID; // internally used + DWORD dwSequenceID; // internally used + ULONGLONG ullArguments; // arguments for gestures whose arguments fit in 8 BYTES + UINT cbExtraArgs; // size, in bytes, of extra arguments, if any, that accompany this gesture +} GESTUREINFO, *PGESTUREINFO; +typedef GESTUREINFO const * PCGESTUREINFO; + +typedef struct tagGESTURENOTIFYSTRUCT { + UINT cbSize; // size, in bytes, of this structure + DWORD dwFlags; // unused + HWND hwndTarget; // handle to window targeted by the gesture + POINTS ptsLocation; // starting location + DWORD dwInstanceID; // internally used +} GESTURENOTIFYSTRUCT, *PGESTURENOTIFYSTRUCT; + +/* + * Gesture argument helpers + * - Angle should be a double in the range of -2pi to +2pi + * - Argument should be an unsigned 16-bit value + */ +#define GID_ROTATE_ANGLE_TO_ARGUMENT(_arg_) ((USHORT)((((_arg_) + 2.0 * 3.14159265) / (4.0 * 3.14159265)) * 65535.0)) +#define GID_ROTATE_ANGLE_FROM_ARGUMENT(_arg_) ((((double)(_arg_) / 65535.0) * 4.0 * 3.14159265) - 2.0 * 3.14159265) + +typedef struct tagGESTURECONFIG { + DWORD dwID; // gesture ID + DWORD dwWant; // settings related to gesture ID that are to be turned on + DWORD dwBlock; // settings related to gesture ID that are to be turned off +} GESTURECONFIG, *PGESTURECONFIG; + +#define GC_ALLGESTURES 0x00000001 +#define GC_ZOOM 0x00000001 +#define GC_PAN 0x00000001 +#define GC_PAN_WITH_SINGLE_FINGER_VERTICALLY 0x00000002 +#define GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY 0x00000004 +#define GC_PAN_WITH_GUTTER 0x00000008 +#define GC_PAN_WITH_INERTIA 0x00000010 +#define GC_ROTATE 0x00000001 +#define GC_TWOFINGERTAP 0x00000001 +#define GC_ROLLOVER 0x00000001 +#define GESTURECONFIGMAXCOUNT 256 // Maximum number of gestures that can be included + // in a single call to SetGestureConfig / GetGestureConfig + + + +#define GCF_INCLUDE_ANCESTORS 0x00000001 // If specified, GetGestureConfig returns consolidated configuration + // for the specified window and it's parent window chain + +typedef BOOL (*PtrGetGestureInfo)(HGESTUREINFO hGestureInfo, PGESTUREINFO pGestureInfo); +typedef BOOL (*PtrGetGestureExtraArgs)(HGESTUREINFO hGestureInfo, UINT cbExtraArgs, PBYTE pExtraArgs); +typedef BOOL (*PtrCloseGestureInfoHandle)(HGESTUREINFO hGestureInfo); +typedef BOOL (*PtrSetGestureConfig)(HWND hwnd, DWORD dwReserved, UINT cIDs, + PGESTURECONFIG pGestureConfig, + UINT cbSize); +typedef BOOL (*PtrGetGestureConfig)(HWND hwnd, DWORD dwReserved, + DWORD dwFlags, PUINT pcIDs, + PGESTURECONFIG pGestureConfig, + UINT cbSize); + +typedef BOOL (*PtrBeginPanningFeedback)(HWND hwnd); +typedef BOOL (*PtrUpdatePanningFeedback)(HWND hwnd, LONG, LONG, BOOL); +typedef BOOL (*PtrEndPanningFeedback)(HWND hwnd, BOOL); + +#endif // WM_GESTURE +#endif // Q_WS_WIN + +class QPanGesture; +class QPinchGesture; +struct StandardGestures +{ + QPanGesture *pan; + QPinchGesture *pinch; + StandardGestures() : pan(0), pinch(0) { } +}; + class QScopedLoopLevelCounter { @@ -439,10 +541,6 @@ public: void sendSyntheticEnterLeave(QWidget *widget); #endif - QGestureManager *gestureManager; - // map<gesture name -> number of widget subscribed to it> - QMap<QString, int> grabbedGestures; - QMap<int, QWidget *> widgetForTouchPointId; QMap<int, QTouchEvent::TouchPoint> appCurrentTouchPoints; static void updateTouchPointsForWidget(QWidget *widget, QTouchEvent *touchEvent); @@ -465,6 +563,19 @@ public: QHash<DWORD, int> touchInputIDToTouchPointID; QList<QTouchEvent::TouchPoint> appAllTouchPoints; bool translateTouchEvent(const MSG &msg); + + typedef QMap<QWidget*, StandardGestures> WidgetStandardGesturesMap; + WidgetStandardGesturesMap widgetGestures; + ulong lastGestureId; + + PtrGetGestureInfo GetGestureInfo; + PtrGetGestureExtraArgs GetGestureExtraArgs; + PtrCloseGestureInfoHandle CloseGestureInfoHandle; + PtrSetGestureConfig SetGestureConfig; + PtrGetGestureConfig GetGestureConfig; + PtrBeginPanningFeedback BeginPanningFeedback; + PtrUpdatePanningFeedback UpdatePanningFeedback; + PtrEndPanningFeedback EndPanningFeedback; #endif #ifdef QT_RX71_MULTITOUCH diff --git a/src/gui/kernel/qapplication_win.cpp b/src/gui/kernel/qapplication_win.cpp index e1af0f7..13f19a3 100644 --- a/src/gui/kernel/qapplication_win.cpp +++ b/src/gui/kernel/qapplication_win.cpp @@ -89,6 +89,8 @@ extern void qt_wince_hide_taskbar(HWND hwnd); //defined in qguifunctions_wince.c #include <private/qkeymapper_p.h> #include <private/qlocale_p.h> #include "qevent_p.h" +#include "qstandardgestures.h" +#include "qstandardgestures_p.h" //#define ALIEN_DEBUG @@ -451,6 +453,7 @@ public: bool translateConfigEvent(const MSG &msg); bool translateCloseEvent(const MSG &msg); bool translateTabletEvent(const MSG &msg, PACKET *localPacketBuf, int numPackets); + bool translateGestureEvent(const MSG &msg); void repolishStyle(QStyle &style); inline void showChildren(bool spontaneous) { d_func()->showChildren(spontaneous); } inline void hideChildren(bool spontaneous) { d_func()->hideChildren(spontaneous); } @@ -809,6 +812,33 @@ void qt_init(QApplicationPrivate *priv, int) QLibrary::resolve(QLatin1String("user32"), "SetProcessDPIAware")) ptrSetProcessDPIAware(); #endif + + priv->lastGestureId = 0; + + priv->GetGestureInfo = + (PtrGetGestureInfo)QLibrary::resolve(QLatin1String("user32"), + "GetGestureInfo"); + priv->GetGestureExtraArgs = + (PtrGetGestureExtraArgs)QLibrary::resolve(QLatin1String("user32"), + "GetGestureExtraArgs"); + priv->CloseGestureInfoHandle = + (PtrCloseGestureInfoHandle)QLibrary::resolve(QLatin1String("user32"), + "CloseGestureInfoHandle"); + priv->SetGestureConfig = + (PtrSetGestureConfig)QLibrary::resolve(QLatin1String("user32"), + "SetGestureConfig"); + priv->GetGestureConfig = + (PtrGetGestureConfig)QLibrary::resolve(QLatin1String("user32"), + "GetGestureConfig"); + priv->BeginPanningFeedback = + (PtrBeginPanningFeedback)QLibrary::resolve(QLatin1String("uxtheme"), + "BeginPanningFeedback"); + priv->UpdatePanningFeedback = + (PtrUpdatePanningFeedback)QLibrary::resolve(QLatin1String("uxtheme"), + "UpdatePanningFeedback"); + priv->EndPanningFeedback = + (PtrEndPanningFeedback)QLibrary::resolve(QLatin1String("uxtheme"), + "EndPanningFeedback"); } /***************************************************************************** @@ -2469,6 +2499,10 @@ LRESULT CALLBACK QtWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam } result = false; break; + case WM_GESTURE: + widget->translateGestureEvent(msg); + result = true; + break; default: result = false; // event was not processed break; @@ -3649,6 +3683,60 @@ bool QETWidget::translateCloseEvent(const MSG &) return d_func()->close_helper(QWidgetPrivate::CloseWithSpontaneousEvent); } +bool QETWidget::translateGestureEvent(const MSG &msg) +{ + GESTUREINFO gi; + gi.cbSize = sizeof(GESTUREINFO); + gi.dwFlags = 0; + gi.ptsLocation.x = 0; + gi.ptsLocation.y = 0; + gi.dwID = 0; + gi.dwInstanceID = 0; + gi.dwSequenceID = 0; + + QApplicationPrivate *qAppPriv = getQApplicationPrivateInternal(); + BOOL bResult = qAppPriv->GetGestureInfo((HGESTUREINFO)msg.lParam, &gi); + + const QPoint widgetPos = QPoint(gi.ptsLocation.x, gi.ptsLocation.y); + QWidget *alienWidget = !internalWinId() ? this : childAt(widgetPos); + if (alienWidget && alienWidget->internalWinId()) + alienWidget = 0; + QWidget *widget = alienWidget ? alienWidget : this; + + QWinGestureEvent event; + event.sequenceId = gi.dwSequenceID; + event.position = QPoint(gi.ptsLocation.x, gi.ptsLocation.y); + if (bResult) { + switch (gi.dwID) { + case GID_BEGIN: + // we are not interested in this type of event. + break; + case GID_END: + event.gestureType = QWinGestureEvent::GestureEnd; + break; + case GID_ZOOM: + event.gestureType = QWinGestureEvent::Pinch; + break; + case GID_PAN: + event.gestureType = QWinGestureEvent::Pan; + break; + case GID_ROTATE: + case GID_TWOFINGERTAP: + case GID_ROLLOVER: + default: + break; + } + if (event.gestureType != QWinGestureEvent::None) + qt_sendSpontaneousEvent(widget, &event); + } else { + DWORD dwErr = GetLastError(); + if (dwErr > 0) + qWarning() << "translateGestureEvent: error = " << dwErr; + } + qAppPriv->CloseGestureInfoHandle((HGESTUREINFO)msg.lParam); + return true; +} + void QApplication::setCursorFlashTime(int msecs) { @@ -3830,6 +3918,7 @@ void QSessionManager::cancel() #endif //QT_NO_SESSIONMANAGER + qt_RegisterTouchWindowPtr QApplicationPrivate::RegisterTouchWindow = 0; qt_GetTouchInputInfoPtr QApplicationPrivate::GetTouchInputInfo = 0; qt_CloseTouchInputHandlePtr QApplicationPrivate::CloseTouchInputHandle = 0; diff --git a/src/gui/kernel/qdirectionrecognizer.cpp b/src/gui/kernel/qdirectionrecognizer.cpp deleted file mode 100644 index a1bc5b1..0000000 --- a/src/gui/kernel/qdirectionrecognizer.cpp +++ /dev/null @@ -1,182 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 "qdirectionrecognizer_p.h" - -#include <math.h> - -#ifndef M_PI -#define M_PI 3.141592653589793238462643 -#endif - -QT_BEGIN_NAMESPACE - -enum { - DistanceDelta = 20 -}; - -QDirectionSimpleRecognizer::QDirectionSimpleRecognizer() -{ -} - -Direction QDirectionSimpleRecognizer::addPosition(const QPoint &pos) -{ - if (!directions.isEmpty()) { - const QPoint tmp = pos - directions.back().point; - if (tmp.manhattanLength() < 5) - return Direction(); - } - if (lastPoint.isNull()) { - lastPoint = pos; - return Direction(); - } - int dx = pos.x() - lastPoint.x(); - int dy = pos.y() - lastPoint.y(); - Qt::DirectionType direction = Qt::NoDirection; - if (dx < 0) { - if (-1*dx >= DistanceDelta/2) - direction = Qt::LeftDirection; - } else { - if (dx >= DistanceDelta/2) - direction = Qt::RightDirection; - } - if (dy < 0) { - if (-1*dy >= DistanceDelta/2) - direction = Qt::UpDirection; - } else { - if (dy >= DistanceDelta/2) - direction = Qt::DownDirection; - } - if (direction == Qt::NoDirection) - return Direction(); - - lastPoint = pos; - directions.push_back(Direction(direction, pos)); - return Direction(direction, pos); -} - - -DirectionList QDirectionSimpleRecognizer::getDirections() const -{ - return directions; -} - -void QDirectionSimpleRecognizer::reset() -{ - directions.clear(); - lastPoint = QPoint(); -} - - -/// QDirectionDiagonalRecognizer - -QDirectionDiagonalRecognizer::QDirectionDiagonalRecognizer() -{ -} - -Direction QDirectionDiagonalRecognizer::addPosition(const QPoint &pos) -{ - if (!directions.isEmpty()) { - const QPoint tmp = pos - directions.back().point; - if (tmp.manhattanLength() < 5) - return Direction(); - } - if (lastPoint.isNull()) { - lastPoint = pos; - return Direction(); - } - int dx = pos.x() - lastPoint.x(); - int dy = pos.y() - lastPoint.y(); - int distance = sqrt(static_cast<double>(dx*dx + dy*dy)); - if (distance < DistanceDelta/2) - return Direction(); - - Qt::DirectionType direction = Qt::NoDirection; - double angle = atan(1.0*qAbs(lastPoint.y() - pos.y())/qAbs(pos.x() - lastPoint.x())) * 180. / M_PI; - if (dx < 0 && dy <= 0) { - angle = 180 - angle; - } else if (dx <= 0 && dy > 0) { - angle += 180; - } else if (dx > 0 && dy > 0) { - angle = 360-angle; - } - if (angle < 0) - angle += 360; - if (angle <= 20) - direction = Qt::RightDirection; - else if (angle <= 65) - direction = Qt::RightUpDirection; - else if (angle <= 110) - direction = Qt::UpDirection; - else if (angle <= 155) - direction = Qt::LeftUpDirection; - else if (angle <= 200) - direction = Qt::LeftDirection; - else if (angle <= 245) - direction = Qt::LeftDownDirection; - else if (angle <= 290) - direction = Qt::DownDirection; - else if (angle <= 335) - direction = Qt::RightDownDirection; - else - direction = Qt::RightDirection; - - if (direction == Qt::NoDirection) - return Direction(); - - lastPoint = pos; - directions.push_back(Direction(direction, pos)); - return Direction(direction, pos); -} - - -DirectionList QDirectionDiagonalRecognizer::getDirections() const -{ - return directions; -} - -void QDirectionDiagonalRecognizer::reset() -{ - directions.clear(); - lastPoint = QPoint(); -} - -QT_END_NAMESPACE diff --git a/src/gui/kernel/qdirectionrecognizer_p.h b/src/gui/kernel/qdirectionrecognizer_p.h deleted file mode 100644 index 12307c6..0000000 --- a/src/gui/kernel/qdirectionrecognizer_p.h +++ /dev/null @@ -1,105 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 QDIRECTIONRECOGNIZER_P_H -#define QDIRECTIONRECOGNIZER_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qpoint.h" -#include "qlist.h" - -QT_BEGIN_NAMESPACE - -struct Direction -{ - Qt::DirectionType direction; - QPoint point; - - Direction(Qt::DirectionType dir, const QPoint &pt) - : direction(dir), point(pt) { } - Direction() - : direction(Qt::NoDirection) { } - - inline bool isEmpty() const { return direction == Qt::NoDirection; } - inline bool isNull() const { return direction == Qt::NoDirection; } -}; - -typedef QList<Direction> DirectionList; - -class QDirectionSimpleRecognizer -{ -public: - QDirectionSimpleRecognizer(); - Direction addPosition(const QPoint &pos); - DirectionList getDirections() const; - void reset(); - -private: - QPoint lastPoint; - DirectionList directions; -}; - -class QDirectionDiagonalRecognizer -{ -public: - QDirectionDiagonalRecognizer(); - Direction addPosition(const QPoint &pos); - DirectionList getDirections() const; - void reset(); - -private: - QPoint lastPoint; - DirectionList directions; -}; - -QT_END_NAMESPACE - -#endif // QDIRECTIONRECOGNIZER_P_H diff --git a/src/gui/kernel/qdirectionsimplificator_p.h b/src/gui/kernel/qdirectionsimplificator_p.h deleted file mode 100644 index d7491dc..0000000 --- a/src/gui/kernel/qdirectionsimplificator_p.h +++ /dev/null @@ -1,172 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 QDIRECTIONSIMPLIFICATOR_P_H -#define QDIRECTIONSIMPLIFICATOR_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "private/qdirectionrecognizer_p.h" - -QT_BEGIN_NAMESPACE - -class QDirectionSimplificator -{ -public: - QDirectionSimplificator(const DirectionList &dir); - - bool simplify(DirectionList *result); - -private: - DirectionList directions; - DirectionList lastResult; - enum State { - None, - Trim, // remove first and last element - AccidentalMoves, // 66866 => 6666 - ComplexAccidentalMoves, // 778788 => 777888 (swapping elements without changing direction) - ShortMoves, // (moves of length 1) - } state; - - struct SimplifyTrim - { - SimplifyTrim() : state(0) { } - bool operator()(DirectionList &directions) - { - if (state == 0) { - directions.removeFirst(); - state = 1; - } else if (state == 1) { - directions.removeLast(); - state = 2; - } else if (state == 2 && directions.size() >= 2) { - directions.removeFirst(); - directions.removeLast(); - state = 3; - } else { - return false; - } - return true; - } - int state; - }; - struct SimplifyAccidentalMoves - { - SimplifyAccidentalMoves() : state(0) { } - bool operator()(DirectionList &directions) - { - return false; - } - int state; - }; - struct SimplifyComplexAccidentalMoves - { - SimplifyComplexAccidentalMoves() : state(0) { } - bool operator()(DirectionList &directions) - { - return false; - } - int state; - }; - - SimplifyTrim trim; - SimplifyAccidentalMoves accidentalMoves; - SimplifyComplexAccidentalMoves complexAccidentalMoves; - //SimplifyShortMoves shortMoves; -}; - -QDirectionSimplificator::QDirectionSimplificator(const DirectionList &dir) - : directions(dir), state(None) -{ -} - -bool QDirectionSimplificator::simplify(DirectionList *result) -{ - if (directions.isEmpty() || !result) - return false; - *result = directions; - switch(state) { - case None: - state = Trim; - trim = SimplifyTrim(); - case Trim: - if (trim(*result)) - break; - *result = lastResult; - state = AccidentalMoves; - accidentalMoves = SimplifyAccidentalMoves(); - case AccidentalMoves: - if (accidentalMoves(*result)) - break; - *result = lastResult; - state = ComplexAccidentalMoves; - complexAccidentalMoves = SimplifyComplexAccidentalMoves(); - case ComplexAccidentalMoves: - if (complexAccidentalMoves(*result)) - break; - *result = lastResult; - // state = ShortMoves; - // shortMoves = SimplifyShortMoves(); - // case ShortMoves: - // if (shortMoves(*result)) - // break; - // state = None; - default: - return false; - } - lastResult = *result; - if (lastResult.isEmpty()) - return false; - return true; -} - -QT_END_NAMESPACE - -#endif // QDIRECTIONSIMPLIFICATOR_P_H diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index a7a7f2d..bef7ee1 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -53,8 +53,6 @@ QT_BEGIN_NAMESPACE -QString qt_getStandardGestureTypeName(Qt::GestureType type); - /*! \class QInputEvent \ingroup events @@ -3332,9 +3330,6 @@ QDebug operator<<(QDebug dbg, const QEvent *e) { case QEvent::ChildRemoved: n = n ? n : "ChildRemoved"; dbg.nospace() << "QChildEvent(" << n << ", " << (static_cast<const QChildEvent*>(e))->child(); return dbg.space(); - case QEvent::Gesture: - n = "Gesture"; - break; default: dbg.nospace() << "QEvent(" << (const void *)e << ", type = " << e->type() << ')'; return dbg.space(); @@ -3531,157 +3526,6 @@ QMenubarUpdatedEvent::QMenubarUpdatedEvent(QMenuBar * const menuBar) #endif -/*! - \class QGestureEvent - \since 4.6 - \ingroup events - - \brief The QGestureEvent class provides the parameters used for - gesture recognition. - - The QGestureEvent class contains a list of gestures that are being - executed right now (\l{QGestureEvent::}{gestureTypes()}) and a - list of gestures that are cancelled (the gesture might be - cancelled because the window lost focus, or because of timeout, - etc). - - \sa QGesture -*/ - -/*! - Creates new QGestureEvent containing a list of \a gestures that - are being executed and a list of gesture that were cancelled (\a - cancelledGestures). -*/ -QGestureEvent::QGestureEvent(const QSet<QGesture*> &gestures, - const QSet<QString> &cancelledGestures) - : QEvent(QEvent::Gesture), m_cancelledGestures(cancelledGestures) -{ - setAccepted(false); - foreach(QGesture *r, gestures) - m_gestures.insert(r->type(), r); -} - -/*! - Destroys the QGestureEvent object. -*/ -QGestureEvent::~QGestureEvent() -{ -} - -/*! - Returns true if the gesture event contains gesture of specific \a - type; returns false otherwise. -*/ -bool QGestureEvent::contains(Qt::GestureType type) const -{ - return contains(qt_getStandardGestureTypeName(type)); -} - -/*! - Returns true if the gesture event contains gesture of specific \a - type; returns false otherwise. -*/ -bool QGestureEvent::contains(const QString &type) const -{ - return gesture(type) != 0; -} - -/*! - Returns a list of gesture names that this event contains. -*/ -QList<QString> QGestureEvent::gestureTypes() const -{ - return m_gestures.keys(); -} - -/*! - Returns extended information about a gesture of specific \a type. -*/ -const QGesture* QGestureEvent::gesture(Qt::GestureType type) const -{ - return gesture(qt_getStandardGestureTypeName(type)); -} - -/*! - Returns extended information about a gesture of specific \a type. -*/ -const QGesture* QGestureEvent::gesture(const QString &type) const -{ - return m_gestures.value(type, 0); -} - -/*! - Returns extended information about all gestures in the event. -*/ -QList<QGesture*> QGestureEvent::gestures() const -{ - return m_gestures.values(); -} - -/*! - Returns a set of gesture names that used to be executed, but were - cancelled (i.e. they were not finished properly). -*/ -QSet<QString> QGestureEvent::cancelledGestures() const -{ - return m_cancelledGestures; -} - -/*! \fn void QGestureEvent::accept() - Calls QEvent::accept(). -*/ - -/*! - Sets the accept flag of the all gestures inside the event object, - the equivalent of calling \l{QEvent::accept()}{accept()} or - \l{QEvent::setAccepted()}{setAccepted(true)}. - - Setting the accept parameter indicates that the event receiver - wants the gesture. Unwanted gestures might be propagated to the parent - widget. -*/ -void QGestureEvent::acceptAll() -{ - QHash<QString, QGesture*>::iterator it = m_gestures.begin(), - e = m_gestures.end(); - for(; it != e; ++it) - it.value()->accept(); - setAccepted(true); -} - -/*! - Sets the accept flag of the gesture specified by \a type. - This is equivalent to calling - \l{QGestureEvent::gesture()}{gesture(type)}-> - \l{QGesture::accept()}{accept()} - - Setting the accept flag indicates that the event receiver wants - the gesture. Unwanted gestures might be propagated to the parent - widget. -*/ -void QGestureEvent::accept(Qt::GestureType type) -{ - if (QGesture *g = m_gestures.value(qt_getStandardGestureTypeName(type), 0)) - g->accept(); -} - -/*! - Sets the accept flag of the gesture specified by \a type. - This is equivalent to calling - \l{QGestureEvent::gesture()}{gesture(type)}-> - \l{QGesture::accept()}{accept()} - - Setting the accept flag indicates that the event receiver wants - the gesture. Unwanted gestures might be propagated to the parent - widget. -*/ -void QGestureEvent::accept(const QString &type) -{ - if (QGesture *g = m_gestures.value(type, 0)) - g->accept(); -} - /*! \class QTouchEvent \brief The QTouchEvent class contains parameters that describe a touch event . diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h index 136dd7f..11843cb 100644 --- a/src/gui/kernel/qevent.h +++ b/src/gui/kernel/qevent.h @@ -714,41 +714,6 @@ private: }; #endif -class Q_GUI_EXPORT QGestureEvent : public QEvent -{ -public: - QGestureEvent(const QSet<QGesture*> &gestures, - const QSet<QString> &cancelledGestures = QSet<QString>()); - ~QGestureEvent(); - - bool contains(Qt::GestureType type) const; - bool contains(const QString &type) const; - - QList<QString> gestureTypes() const; - - const QGesture* gesture(Qt::GestureType type) const; - const QGesture* gesture(const QString &type) const; - QList<QGesture*> gestures() const; - - QSet<QString> cancelledGestures() const; - - void acceptAll(); -#ifndef Q_NO_USING_KEYWORD - using QEvent::accept; -#else - inline void accept() { QEvent::accept(); } -#endif - void accept(Qt::GestureType type); - void accept(const QString &type); - -protected: - QHash<QString, QGesture*> m_gestures; - QSet<QString> m_cancelledGestures; - - friend class QApplication; - friend class QGestureManager; -}; - #ifndef QT_NO_DEBUG_STREAM Q_GUI_EXPORT QDebug operator<<(QDebug, const QEvent *); #endif diff --git a/src/gui/kernel/qevent_p.h b/src/gui/kernel/qevent_p.h index 8a2bb05..67441ea 100644 --- a/src/gui/kernel/qevent_p.h +++ b/src/gui/kernel/qevent_p.h @@ -119,6 +119,22 @@ public: qreal pressure; }; +class QWinGestureEvent : public QEvent +{ +public: + enum Type { + None, + GestureEnd, + Pan, + Pinch + }; + + QWinGestureEvent() : QEvent(QEvent::WinGesture), gestureType(None), sequenceId(0) { } + Type gestureType; + QPoint position; + ulong sequenceId; +}; + QT_END_NAMESPACE #endif // QEVENT_P_H diff --git a/src/gui/kernel/qgesture.cpp b/src/gui/kernel/qgesture.cpp index ff369e2..32a219c 100644 --- a/src/gui/kernel/qgesture.cpp +++ b/src/gui/kernel/qgesture.cpp @@ -41,190 +41,166 @@ #include "qgesture.h" #include <private/qgesture_p.h> +#include "qgraphicsitem.h" QT_BEGIN_NAMESPACE -QString qt_getStandardGestureTypeName(Qt::GestureType type); + +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 represents a gesture, containing all - properties that describe a 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). - - 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. However, if the gesture developer wants to add a new - property to the gesture object that describe coordinate (like a - QPoint or QRect), it is required to subclass the QGesture and - re-implement the \l{QGesture::}{translate()} function to make sure - the coordinates are translated properly when the gesture event is - propagated to parent widgets. - - \sa QGestureEvent, QGestureRecognizer -*/ + \brief The QGesture class is the base class for implementing custom + gestures. -/*! - Creates a new gesture object of type \a type in a \a state and - marks it as a child of \a parent. + 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. - Usually QGesture objects should only be contructed by the - QGestureRecognizer classes. -*/ -QGesture::QGesture(QObject *parent, const QString &type, Qt::GestureState state) - : QObject(*new QGesturePrivate, parent), m_accept(0) -{ - Q_D(QGesture); - d->type = type; - d->state = state; -} + 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). -/*! - Creates a new gesture object of type \a type in a \a state and - marks it as a child of \a parent. - - This constructor also fills some basic information about the - gesture like a \a startPos which describes the start point of the - gesture, \a lastPos - last point where the gesture happened, \a - pos - a current point, \a rect - a bounding rect of the gesture, - \a hotSpot - a center point of the gesture, \a startTime - a time - when the gesture has started, \a duration - how long the gesture - is going on. - - Usually QGesture objects should only be contructed by the - QGestureRecognizer classes. -*/ -QGesture::QGesture(QObject *parent, const QString &type, const QPoint &startPos, - const QPoint &lastPos, const QPoint &pos, const QRect &rect, - const QPoint &hotSpot, const QDateTime &startTime, - uint duration, Qt::GestureState state) - : QObject(*new QGesturePrivate, parent) -{ - Q_D(QGesture); - d->type = type; - d->state = state; - d->init(startPos, lastPos, pos, rect, hotSpot, startTime, duration); -} + 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. -/*! \internal + 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, QTapAndHoldGesture */ -QGesture::QGesture(QGesturePrivate &dd, QObject *parent, const QString &type, - Qt::GestureState state) - : QObject(dd, parent) -{ - Q_D(QGesture); - d->type = type; - d->state = state; -} -/*! - Destroys the gesture object. +/*! \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. */ -QGesture::~QGesture() -{ -} -/*! - \property QGesture::type +/*! \fn void QGesture::started() - \brief The type of the gesture. + 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. */ -QString QGesture::type() const -{ - return d_func()->type; -} +/*! \fn void QGesture::triggered() -/*! - \property QGesture::state + The signal is emitted when the gesture is detected. Extended information + about the gesture is contained in the signal sender object. +*/ - \brief The current state of the gesture. +/*! \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. */ -Qt::GestureState QGesture::state() const -{ - return d_func()->state; -} -/*! - Translates the internal gesture properties that represent - coordinates by \a offset. +/*! \fn void QGesture::cancelled() - Custom gesture recognizer developer have to re-implement this - function if they want to store custom properties that represent - coordinates. + 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. */ -void QGesture::translate(const QPoint &offset) -{ - Q_D(QGesture); - d->rect.translate(offset); - d->hotSpot += offset; - d->startPos += offset; - d->lastPos += offset; - d->pos += offset; -} + /*! - \property QGesture::rect + 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. - \brief The bounding rect of a gesture. + \sa setGraphicsItem() */ -QRect QGesture::rect() const +QGesture::QGesture(QObject *parent) + : QObject(*new QGesturePrivate, parent) { - return d_func()->rect; + if (parent) + installEventFilter(parent); } -void QGesture::setRect(const QRect &rect) +/*! \internal + */ +QGesture::QGesture(QGesturePrivate &dd, QObject *parent) + : QObject(dd, parent) { - d_func()->rect = rect; + if (parent) + installEventFilter(parent); } /*! - \property QGesture::hotSpot - - \brief The center point of a gesture. + Destroys the gesture object. */ -QPoint QGesture::hotSpot() const +QGesture::~QGesture() { - return d_func()->hotSpot; } -void QGesture::setHotSpot(const QPoint &point) +/*! \internal + */ +bool QGesture::eventFilter(QObject *receiver, QEvent *event) { - d_func()->hotSpot = point; + Q_D(QGesture); + if (d->graphicsItem && receiver == parent()) + return false; + return filterEvent(event); } /*! - \property QGesture::startTime + \property QGesture::state - \brief The time when the gesture has started. + \brief The current state of the gesture. */ -QDateTime QGesture::startTime() const +Qt::GestureState QGesture::state() const { - return d_func()->startTime; + return d_func()->state; } -/*! - \property QGesture::duration - - \brief The duration time of a gesture. -*/ -uint QGesture::duration() const +void QGesture::setState(Qt::GestureState state) { - return d_func()->duration; + d_func()->state = state; } /*! \property QGesture::startPos - \brief The start position of the pointer. + \brief The start position of the gesture (if relevant). */ QPoint QGesture::startPos() const { @@ -239,7 +215,7 @@ void QGesture::setStartPos(const QPoint &point) /*! \property QGesture::lastPos - \brief The last recorded position of the pointer. + \brief The last recorded position of the gesture (if relevant). */ QPoint QGesture::lastPos() const { @@ -254,7 +230,7 @@ void QGesture::setLastPos(const QPoint &point) /*! \property QGesture::pos - \brief The current position of the pointer. + \brief The current position of the gesture (if relevant). */ QPoint QGesture::pos() const { @@ -266,66 +242,48 @@ void QGesture::setPos(const QPoint &point) d_func()->pos = point; } -/*! \fn void QGesture::setAccepted(bool accepted) - Marks the gesture with the value of \a accepted. - */ - -/*! \fn bool QGesture::isAccepted() const - Returns true if the gesture is marked accepted. - */ - -/*! \fn void QGesture::accept() - Marks the gesture accepted. -*/ - -/*! \fn void QGesture::ignore() - Marks the gesture ignored. -*/ - /*! - \class QPanningGesture - \since 4.6 + Sets the \a graphicsItem the gesture is filtering events for. - \brief The QPanningGesture class represents a Pan gesture, - providing additional information related to panning. + The gesture will install an event filter to the \a graphicsItem and + redirect them to the filterEvent() function. - This class is provided for convenience, panning direction - information is also contained in the QGesture object in it's - properties. + \sa graphicsItem() */ - -/*! \internal -*/ -QPanningGesture::QPanningGesture(QObject *parent) - : QGesture(*new QPanningGesturePrivate, parent, - qt_getStandardGestureTypeName(Qt::PanGesture)) -{ -} - -/*! \internal -*/ -QPanningGesture::~QPanningGesture() +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); } /*! - \property QPanningGesture::lastDirection + Returns the graphics item the gesture is filtering events for. - \brief The last recorded direction of panning. + \sa setGraphicsItem() */ -Qt::DirectionType QPanningGesture::lastDirection() const +QGraphicsItem* QGesture::graphicsItem() const { - return d_func()->lastDirection; + return d_func()->graphicsItem; } -/*! - \property QPanningGesture::direction +/*! \fn void QGesture::reset() - \brief The current direction of panning. + 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 emits the cancelled() + signal if the state() of the gesture wasn't empty. */ -Qt::DirectionType QPanningGesture::direction() const +void QGesture::reset() { - return d_func()->direction; + if (state() != Qt::NoGesture) + emit cancelled(); + setState(Qt::NoGesture); } QT_END_NAMESPACE diff --git a/src/gui/kernel/qgesture.h b/src/gui/kernel/qgesture.h index f3c95cc..b5abdd2 100644 --- a/src/gui/kernel/qgesture.h +++ b/src/gui/kernel/qgesture.h @@ -55,50 +55,32 @@ QT_BEGIN_NAMESPACE QT_MODULE(Gui) +class QGraphicsItem; class QGesturePrivate; class Q_GUI_EXPORT QGesture : public QObject { Q_OBJECT Q_DECLARE_PRIVATE(QGesture) - Q_PROPERTY(QString type READ type) Q_PROPERTY(Qt::GestureState state READ state) - Q_PROPERTY(QDateTime startTime READ startTime) - Q_PROPERTY(uint duration READ duration) - - Q_PROPERTY(QRect rect READ rect WRITE setRect) - Q_PROPERTY(QPoint hotSpot READ hotSpot WRITE setHotSpot) Q_PROPERTY(QPoint startPos READ startPos WRITE setStartPos) Q_PROPERTY(QPoint lastPos READ lastPos WRITE setLastPos) Q_PROPERTY(QPoint pos READ pos WRITE setPos) public: - QGesture(QObject *parent, const QString &type, - Qt::GestureState state = Qt::GestureStarted); - QGesture(QObject *parent, - const QString &type, const QPoint &startPos, - const QPoint &lastPos, const QPoint &pos, const QRect &rect, - const QPoint &hotSpot, const QDateTime &startTime, - uint duration, Qt::GestureState state); - virtual ~QGesture(); - - inline void setAccepted(bool accepted) { m_accept = accepted; } - inline bool isAccepted() const { return m_accept; } - - inline void accept() { m_accept = true; } - inline void ignore() { m_accept = false; } - - QString type() const; - Qt::GestureState state() const; + explicit QGesture(QObject *parent = 0); + ~QGesture(); + + virtual bool filterEvent(QEvent *event) = 0; + + void setGraphicsItem(QGraphicsItem *); + QGraphicsItem *graphicsItem() const; - QDateTime startTime() const; - uint duration() const; + virtual void reset(); - QRect rect() const; - void setRect(const QRect &rect); - QPoint hotSpot() const; - void setHotSpot(const QPoint &point); + Qt::GestureState state() const; + void setState(Qt::GestureState state); QPoint startPos() const; void setStartPos(const QPoint &point); @@ -108,45 +90,19 @@ public: void setPos(const QPoint &point); protected: - QGesture(QGesturePrivate &dd, QObject *parent, const QString &type, - Qt::GestureState state = Qt::GestureStarted); - virtual void translate(const QPoint &offset); + QGesture(QGesturePrivate &dd, QObject *parent); + bool eventFilter(QObject*, QEvent*); -private: - ushort m_accept : 1; - - friend class QGestureManager; - friend class QApplication; - friend class QGraphicsScene; - friend class QGraphicsScenePrivate; - friend class QGestureRecognizerPan; - friend class QDoubleTapGestureRecognizer; - friend class QTapAndHoldGestureRecognizer; -}; - -class QPanningGesturePrivate; -class Q_GUI_EXPORT QPanningGesture : public QGesture -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QPanningGesture) - - Q_PROPERTY(Qt::DirectionType lastDirection READ lastDirection) - Q_PROPERTY(Qt::DirectionType direction READ direction) - -public: - Qt::DirectionType lastDirection() const; - Qt::DirectionType direction() const; +signals: + void started(); + void triggered(); + void finished(); + void cancelled(); private: - QPanningGesture(QObject *parent = 0); - ~QPanningGesture(); - - friend class QGestureRecognizerPan; + friend class QWidget; }; -Q_DECLARE_METATYPE(Qt::DirectionType) -Q_DECLARE_METATYPE(Qt::GestureState) - QT_END_NAMESPACE QT_END_HEADER diff --git a/src/gui/kernel/qgesture_p.h b/src/gui/kernel/qgesture_p.h index caf851e..99f572f 100644 --- a/src/gui/kernel/qgesture_p.h +++ b/src/gui/kernel/qgesture_p.h @@ -56,10 +56,12 @@ #include "qrect.h" #include "qpoint.h" #include "qdatetime.h" +#include "qgesture.h" #include "private/qobject_p.h" QT_BEGIN_NAMESPACE +class QObject; class QGraphicsItem; class QGesturePrivate : public QObjectPrivate { @@ -67,47 +69,28 @@ class QGesturePrivate : public QObjectPrivate public: QGesturePrivate() - : state(Qt::NoGesture), graphicsItem(0), singleshot(0), duration(0) { } + : graphicsItem(0), eventFilterProxyGraphicsItem(0), state(Qt::NoGesture) + { + } void init(const QPoint &startPos, const QPoint &lastPos, - const QPoint &pos, const QRect &rect, - const QPoint &hotSpot, const QDateTime &startTime, - uint duration) + const QPoint &pos) { - this->rect = rect; - this->hotSpot = hotSpot; - this->startTime = startTime; - this->duration = duration; this->startPos = startPos; this->lastPos = lastPos; this->pos = pos; } - QString type; - Qt::GestureState state; - - QPointer<QWidget> widget; QGraphicsItem *graphicsItem; - uint singleshot:1; + QGraphicsItem *eventFilterProxyGraphicsItem; + + Qt::GestureState state; - QRect rect; - QPoint hotSpot; - QDateTime startTime; - uint duration; QPoint startPos; QPoint lastPos; QPoint pos; }; -class QPanningGesturePrivate : public QGesturePrivate -{ - Q_DECLARE_PUBLIC(QPanningGesture) - -public: - Qt::DirectionType lastDirection; - Qt::DirectionType direction; -}; - QT_END_NAMESPACE #endif // QGESTURE_P_H diff --git a/src/gui/kernel/qgesturemanager.cpp b/src/gui/kernel/qgesturemanager.cpp deleted file mode 100644 index 20abda9..0000000 --- a/src/gui/kernel/qgesturemanager.cpp +++ /dev/null @@ -1,644 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 "qgesturemanager_p.h" -#include "qgesture.h" -#include "qgesture_p.h" -#include "qevent.h" - -#include "qapplication.h" -#include "qapplication_p.h" -#include "qwidget.h" -#include "qwidget_p.h" - -#include "qgesturestandardrecognizers_p.h" - -#include "qdebug.h" - -// #define GESTURE_DEBUG -#ifndef GESTURE_DEBUG -# define DEBUG if (0) qDebug -#else -# define DEBUG qDebug -#endif - -QT_BEGIN_NAMESPACE - -bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); - -static const unsigned int MaximumGestureRecognitionTimeout = 2000; - -QGestureManager::QGestureManager(QObject *parent) - : QObject(parent), eventDeliveryDelayTimeout(300), - delayedPressTimer(0), lastMousePressReceiver(0), lastMousePressEvent(QEvent::None, QPoint(), Qt::NoButton, 0, 0), - lastGestureId(0), state(NotGesture) -{ - qRegisterMetaType<Qt::DirectionType>(); - qRegisterMetaType<Qt::GestureState>(); - - recognizers << new QDoubleTapGestureRecognizer(this); - recognizers << new QTapAndHoldGestureRecognizer(this); - recognizers << new QGestureRecognizerPan(this); - - foreach(QGestureRecognizer *r, recognizers) - connect(r, SIGNAL(stateChanged(QGestureRecognizer::Result)), - this, SLOT(recognizerStateChanged(QGestureRecognizer::Result))); -} - -void QGestureManager::addRecognizer(QGestureRecognizer *recognizer) -{ - recognizer->setParent(this); - recognizers << recognizer; -} - -void QGestureManager::removeRecognizer(QGestureRecognizer *recognizer) -{ - recognizers.remove(recognizer); -} - -bool QGestureManager::filterEvent(QWidget *receiver, QEvent *event) -{ - QPoint currentPos; - switch (event->type()) { - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseButtonDblClick: - case QEvent::MouseMove: - currentPos = static_cast<QMouseEvent*>(event)->pos(); break; - default: break; - } - - const QMap<QString, int> &grabbedGestures = qApp->d_func()->grabbedGestures; - - bool ret = false; - QSet<QGestureRecognizer*> startedGestures; - QSet<QGestureRecognizer*> finishedGestures; - QSet<QGestureRecognizer*> newMaybeGestures; - QSet<QGestureRecognizer*> cancelledGestures; - QSet<QGestureRecognizer*> notGestures; - if (state == NotGesture || state == MaybeGesture) { - DEBUG() << "QGestureManager: current event processing state: " - << (state == NotGesture ? "NotGesture" : "MaybeGesture"); - - QSet<QGestureRecognizer*> stillMaybeGestures; - // try other recognizers. - foreach(QGestureRecognizer *r, recognizers) { - if (grabbedGestures.value(r->gestureType(), 0) <= 0) - continue; - QGestureRecognizer::Result result = r->filterEvent(event); - if (result == QGestureRecognizer::GestureStarted) { - DEBUG() << "QGestureManager: gesture started: " << r; - startedGestures << r; - } else if (result == QGestureRecognizer::GestureFinished) { - DEBUG() << "QGestureManager: gesture finished: " << r; - finishedGestures << r; - } else if (result == QGestureRecognizer::MaybeGesture) { - DEBUG() << "QGestureManager: maybe gesture: " << r; - newMaybeGestures << r; - } else if (result == QGestureRecognizer::NotGesture) { - // if it was maybe gesture, but isn't a gesture anymore. - DEBUG() << "QGestureManager: not gesture: " << r; - notGestures << r; - } - } - Q_ASSERT(activeGestures.isEmpty()); - activeGestures += startedGestures; - for(QMap<QGestureRecognizer*, int>::iterator it = maybeGestures.begin(); - it != maybeGestures.end();) { - QGestureRecognizer *r = it.key(); - if (startedGestures.contains(r) || finishedGestures.contains(r) || - notGestures.contains(r)) { - killTimer(it.value()); - it = maybeGestures.erase(it); - } else { - ++it; - } - } - foreach(QGestureRecognizer *r, newMaybeGestures) { - if (!maybeGestures.contains(r)) { - int timerId = startTimer(MaximumGestureRecognitionTimeout); - if (!timerId) - qWarning("QGestureManager: couldn't start timer!"); - maybeGestures.insert(r, timerId); - } - } - if (!finishedGestures.isEmpty() || !startedGestures.isEmpty()) { - // gesture found! - ret = true; - QSet<QGesture*> started; - foreach(QGestureRecognizer *r, finishedGestures) { - if (QGesture *gesture = r->getGesture()) { - started << gesture; - gesture->d_func()->singleshot = true; - } - } - foreach(QGestureRecognizer *r, startedGestures) { - if (QGesture *gesture = r->getGesture()) { - started << gesture; - gesture->d_func()->singleshot = false; - } - } - - if (!activeGestures.isEmpty()) { - DEBUG() << "QGestureManager: new state = Gesture"; - state = Gesture; - } else if (!maybeGestures.isEmpty()) { - DEBUG() << "QGestureManager: new state = Maybe"; - state = MaybeGesture; - } else { - DEBUG() << "QGestureManager: new state = NotGesture"; - state = NotGesture; - } - - Q_ASSERT(!started.isEmpty()); - ret = sendGestureEvent(receiver, started, QSet<QGesture*>()); - } else if (!maybeGestures.isEmpty()) { - if (state != MaybeGesture) { - // We got a new set of events that look like a start - // of some gesture, so we switch to state MaybeGesture - // and wait for more events. - DEBUG() << "QGestureManager: new state = Maybe. Waiting for events"; - state = MaybeGesture; - // start gesture timer - } else { - // we still not sure if it is a gesture or not. - } - } else if (state == MaybeGesture) { - // last time we thought it looks like gesture, but now we - // know for sure that it isn't. - DEBUG() << "QGestureManager: new state = NotGesture"; - state = NotGesture; - } - foreach(QGestureRecognizer *r, finishedGestures) - r->reset(); - foreach(QGestureRecognizer *r, cancelledGestures) - r->reset(); - foreach(QGestureRecognizer *r, notGestures) - r->reset(); - } else if (state == Gesture) { - DEBUG() << "QGestureManager: current event processing state: Gesture"; - Q_ASSERT(!activeGestures.isEmpty()); - - foreach(QGestureRecognizer *r, recognizers) { - if (grabbedGestures.value(r->gestureType(), 0) <= 0) - continue; - QGestureRecognizer::Result result = r->filterEvent(event); - if (result == QGestureRecognizer::GestureStarted) { - DEBUG() << "QGestureManager: gesture started: " << r; - startedGestures << r; - } else if (result == QGestureRecognizer::GestureFinished) { - DEBUG() << "QGestureManager: gesture finished: " << r; - finishedGestures << r; - } else if (result == QGestureRecognizer::MaybeGesture) { - DEBUG() << "QGestureManager: maybe gesture: " << r; - newMaybeGestures << r; - } else if (result == QGestureRecognizer::NotGesture) { - // if it was an active gesture, but isn't a gesture anymore. - if (activeGestures.contains(r)) { - DEBUG() << "QGestureManager: cancelled gesture: " << r; - cancelledGestures << r; - } else { - DEBUG() << "QGestureManager: not gesture: " << r; - notGestures << r; - } - } - } - - for(QMap<QGestureRecognizer*, int>::iterator it = maybeGestures.begin(); - it != maybeGestures.end();) { - QGestureRecognizer *r = it.key(); - if (startedGestures.contains(r) || finishedGestures.contains(r) || - notGestures.contains(r)) { - killTimer(it.value()); - it = maybeGestures.erase(it); - } else { - ++it; - } - } - foreach(QGestureRecognizer *r, newMaybeGestures) { - if (!maybeGestures.contains(r)) { - int timerId = startTimer(MaximumGestureRecognitionTimeout); - if (!timerId) - qWarning("QGestureManager: couldn't start timer!"); - maybeGestures.insert(r, timerId); - } - } - QSet<QGesture*> started, updated; - if (!finishedGestures.isEmpty() || !startedGestures.isEmpty()) { - // another gesture found! - ret = true; - foreach(QGestureRecognizer *r, finishedGestures) { - if (QGesture *gesture = r->getGesture()) { - gesture->d_func()->singleshot = !activeGestures.contains(r); - if (gesture->d_func()->singleshot) - started << gesture; - else - updated << gesture; - } - } - foreach(QGestureRecognizer *r, startedGestures) { - if (QGesture *gesture = r->getGesture()) { - gesture->d_func()->singleshot = !activeGestures.contains(r); - if (gesture->d_func()->singleshot) - started << gesture; - else - updated << gesture; - } - } - } - activeGestures -= newMaybeGestures; - activeGestures -= cancelledGestures; - activeGestures += startedGestures; - activeGestures -= finishedGestures; - QSet<QString> cancelledGestureNames; - foreach(QGestureRecognizer *r, cancelledGestures) - cancelledGestureNames << r->gestureType(); - ret = sendGestureEvent(receiver, started, updated, cancelledGestureNames); - - foreach(QGestureRecognizer *r, finishedGestures) - r->reset(); - foreach(QGestureRecognizer *r, cancelledGestures) - r->reset(); - foreach(QGestureRecognizer *r, notGestures) - r->reset(); - if (!activeGestures.isEmpty()) { - // nothing changed, we are still handling a gesture - } else if (!maybeGestures.isEmpty()) { - DEBUG() << "QGestureManager: new state = Maybe. Waiting for events: " << maybeGestures; - state = MaybeGesture; - } else { - DEBUG() << "QGestureManager: new state = NotGesture"; - state = NotGesture; - } - } - - if (delayedPressTimer && state == Gesture) { - DEBUG() << "QGestureManager: gesture started. Forgetting about postponed mouse press event"; - killTimer(delayedPressTimer); - delayedPressTimer = 0; - lastMousePressReceiver = 0; - } else if (delayedPressTimer && (state == NotGesture || - event->type() == QEvent::MouseButtonRelease)) { - // not a gesture or released button too fast, so replay press - // event back. - DEBUG() << "QGestureManager: replaying mouse press event"; - QMap<QGestureRecognizer*, int>::const_iterator it = maybeGestures.begin(), - e = maybeGestures.end();; - for (; it != e; ++it) { - it.key()->reset(); - killTimer(it.value()); - } - maybeGestures.clear(); - state = NotGesture; - - if (lastMousePressReceiver) { - QApplication::sendEvent(lastMousePressReceiver, &lastMousePressEvent); - if (event->type() == QEvent::MouseButtonRelease) { - QMouseEvent *me = static_cast<QMouseEvent*>(event); - QMouseEvent move(QEvent::MouseMove, me->pos(), me->globalPos(), me->button(), - me->buttons(), me->modifiers()); - QApplication::sendEvent(lastMousePressReceiver, &move); - ret = false; - } - lastMousePressReceiver = 0; - } - lastMousePressReceiver = 0; - killTimer(delayedPressTimer); - delayedPressTimer = 0; - } else if (state == MaybeGesture && event->type() == QEvent::MouseButtonPress - && eventDeliveryDelayTimeout) { - // postpone the press event delivery until we know for - // sure whether it is a gesture. - DEBUG() << "QGestureManager: postponing mouse press event"; - QMouseEvent *me = static_cast<QMouseEvent*>(event); - lastMousePressReceiver = receiver; - lastMousePressEvent = QMouseEvent(QEvent::MouseButtonPress, me->pos(), - me->globalPos(), me->button(), - me->buttons(), me->modifiers()); - Q_ASSERT(delayedPressTimer == 0); - delayedPressTimer = startTimer(eventDeliveryDelayTimeout); - if (!delayedPressTimer) - qWarning("QGestureManager: couldn't start delayed press timer!"); - ret = true; - } - if (delayedPressTimer && event->type() == QEvent::MouseMove) { - // if we have postponed a mouse press event, postpone all - // subsequent mouse move events as well. - ret = true; - } - - lastPos = currentPos; - return ret; -} - -void QGestureManager::timerEvent(QTimerEvent *event) -{ - if (event->timerId() == delayedPressTimer) { - DEBUG() << "QGestureManager: replaying mouse press event due to timeout"; - // sanity checks - Q_ASSERT(state != Gesture); - - QMap<QGestureRecognizer*, int>::const_iterator it = maybeGestures.begin(), - e = maybeGestures.end();; - for (; it != e; ++it) { - it.key()->reset(); - killTimer(it.value()); - } - maybeGestures.clear(); - state = NotGesture; - - if (lastMousePressReceiver) { - // we neither received a mouse release event nor gesture - // started, so we replay stored mouse press event. - QApplication::sendEvent(lastMousePressReceiver, &lastMousePressEvent); - lastMousePressReceiver = 0; - } - - lastMousePressReceiver = 0; - killTimer(delayedPressTimer); - delayedPressTimer = 0; - } else { - // sanity checks, remove later - Q_ASSERT((state == Gesture && !activeGestures.isEmpty()) || (state != Gesture && activeGestures.isEmpty())); - - typedef QMap<QGestureRecognizer*, int> MaybeGestureMap; - for (MaybeGestureMap::iterator it = maybeGestures.begin(), e = maybeGestures.end(); - it != e; ++it) { - if (it.value() == event->timerId()) { - DEBUG() << "QGestureManager: gesture timeout."; - QGestureRecognizer *r = it.key(); - r->reset(); - maybeGestures.erase(it); - killTimer(event->timerId()); - break; - } - } - - if (state == MaybeGesture && maybeGestures.isEmpty()) { - DEBUG() << "QGestureManager: new state = NotGesture because of timeout"; - state = NotGesture; - } - } -} - -bool QGestureManager::inGestureMode() -{ - return state == Gesture; -} - -void QGestureManager::recognizerStateChanged(QGestureRecognizer::Result result) -{ - QGestureRecognizer *recognizer = qobject_cast<QGestureRecognizer*>(sender()); - if (!recognizer) - return; - if (qApp->d_func()->grabbedGestures.value(recognizer->gestureType(), 0) <= 0) { - recognizer->reset(); - return; - } - - switch (result) { - case QGestureRecognizer::GestureStarted: - case QGestureRecognizer::GestureFinished: { - if (result == QGestureRecognizer::GestureStarted) { - DEBUG() << "QGestureManager: gesture started: " << recognizer; - activeGestures << recognizer; - DEBUG() << "QGestureManager: new state = Gesture"; - state = Gesture; - } else { - DEBUG() << "QGestureManager: gesture finished: " << recognizer; - } - if (maybeGestures.contains(recognizer)) { - killTimer(maybeGestures.value(recognizer)); - maybeGestures.remove(recognizer); - } - QSet<QGesture*> gestures; - if (QGesture *gesture = recognizer->getGesture()) - gestures << gesture; - if(!gestures.isEmpty()) { - //FIXME: sendGestureEvent(targetWidget, gestures); - } - if (result == QGestureRecognizer::GestureFinished) - recognizer->reset(); - } - break; - case QGestureRecognizer::MaybeGesture: { - DEBUG() << "QGestureManager: maybe gesture: " << recognizer; - if (activeGestures.contains(recognizer)) { - //FIXME: sendGestureEvent(targetWidget, QSet<QGesture*>(), QSet<QString>() << recognizer->gestureType()); - } - if (!maybeGestures.contains(recognizer)) { - int timerId = startTimer(MaximumGestureRecognitionTimeout); - if (!timerId) - qWarning("QGestureManager: couldn't start timer!"); - maybeGestures.insert(recognizer, timerId); - } - } - break; - case QGestureRecognizer::NotGesture: - DEBUG() << "QGestureManager: not gesture: " << recognizer; - if (maybeGestures.contains(recognizer)) { - killTimer(maybeGestures.value(recognizer)); - maybeGestures.remove(recognizer); - } - recognizer->reset(); - break; - default: - Q_ASSERT(false); - } - - if (delayedPressTimer && state == Gesture) { - killTimer(delayedPressTimer); - delayedPressTimer = 0; - } -} - -bool QGestureManager::sendGestureEvent(QWidget *receiver, - const QSet<QGesture*> &startedGestures, - const QSet<QGesture*> &updatedGestures, - const QSet<QString> &cancelled) -{ - DEBUG() << "QGestureManager::sendGestureEvent: sending to" << receiver - << "gestures:" << startedGestures << "," << updatedGestures - << "cancelled:" << cancelled; - // grouping gesture objects by receiver widgets. - typedef QMap<QWidget*, QSet<QGesture*> > WidgetGesturesMap; - WidgetGesturesMap widgetGestures; - for(QSet<QGesture*>::const_iterator it = startedGestures.begin(), e = startedGestures.end(); - it != e; ++it) { - QGesture *g = *it; - QGesturePrivate *gd = g->d_func(); - if (receiver) { - // find the target widget - gd->widget = 0; - gd->graphicsItem = 0; - QWidget *w = receiver; - QPoint offset; - const QString gestureType = g->type(); - while (w) { - if (w->d_func()->hasGesture(gestureType)) - break; - if (w->isWindow()) { - w = 0; - break; - } - offset += w->pos(); - w = w->parentWidget(); - } - if (w && w != gd->widget) { - DEBUG() << "QGestureManager::sendGestureEvent:" << g << "propagating to widget" << w << "offset" << offset; - g->translate(offset); - } - gd->widget = w; - } - if (!gd->widget) { - DEBUG() << "QGestureManager: didn't find a widget to send gesture event (" - << g->type() << ") for tree:" << receiver; - // TODO: maybe we should reset gesture recognizers when nobody interested in its gestures. - continue; - } - widgetGestures[gd->widget].insert(g); - } - - QSet<QGesture*> ignoredGestures; - bool ret = false; - for(WidgetGesturesMap::const_iterator it = widgetGestures.begin(), e = widgetGestures.end(); - it != e; ++it) { - QWidget *receiver = it.key(); - Q_ASSERT(receiver != 0 /*should be taken care above*/); - QSet<QGesture*> gestures = it.value(); - // mark all gestures as ignored by default - for(QSet<QGesture*>::iterator it = gestures.begin(), e = gestures.end(); it != e; ++it) - (*it)->ignore(); - // TODO: send cancelled gesture event to the widget that received the original gesture! - QGestureEvent event(gestures, cancelled); - DEBUG() << "QGestureManager::sendGestureEvent: sending now to" << receiver - << "gestures" << gestures; - bool processed = qt_sendSpontaneousEvent(receiver, &event); - QSet<QGesture*> started = startedGestures & gestures; - DEBUG() << "QGestureManager::sendGestureEvent:" << - (event.isAccepted() ? "" : "not") << "all gestures were accepted"; - if (!started.isEmpty() && !(processed && event.isAccepted())) { - // there are started gestures events that weren't - // accepted, so propagating each gesture independently. - if (event.isAccepted()) { - foreach(QGesture *g, started) - g->accept(); - } - QSet<QGesture*>::const_iterator it = started.begin(), - e = started.end(); - for(; it != e; ++it) { - QGesture *g = *it; - if (processed && g->isAccepted()) { - ret = true; - continue; - } - // if it wasn't accepted, find the first parent widget - // that is subscribed to the gesture. - QGesturePrivate *gd = g->d_func(); - QWidget *w = gd->widget; - gd->widget = 0; - - if (w && !w->isWindow()) { - g->translate(w->pos()); - w = w->parentWidget(); - QPoint offset; - const QString gestureType = g->type(); - while (w) { - if (w->d_func()->hasGesture(gestureType)) { - DEBUG() << "QGestureManager::sendGestureEvent:" << receiver - << "didn't accept gesture" << g << "propagating to" << w; - ignoredGestures.insert(g); - gd->widget = w; - break; - } - if (w->isWindow()) { - w = 0; - break; - } - offset += w->pos(); - w = w->parentWidget(); - } - if (w) { - g->translate(offset); - } else { - DEBUG() << "QGestureManager::sendGestureEvent:" << receiver - << "didn't accept gesture" << g << "and nobody wants it"; - } - } - } - } - } - if (ignoredGestures.isEmpty()) - return ret; - // try to send all gestures that were ignored to the next parent - return sendGestureEvent(0, ignoredGestures, QSet<QGesture*>(), cancelled) || ret; -} - -int QGestureManager::eventDeliveryDelay() const -{ - return eventDeliveryDelayTimeout; -} - -void QGestureManager::setEventDeliveryDelay(int ms) -{ - eventDeliveryDelayTimeout = ms; -} - -int QGestureManager::makeGestureId(const QString &name) -{ - gestureIdMap[++lastGestureId] = name; - return lastGestureId; -} - -void QGestureManager::releaseGestureId(int gestureId) -{ - gestureIdMap.remove(gestureId); -} - -QString QGestureManager::gestureNameFromId(int gestureId) const -{ - return gestureIdMap.value(gestureId); -} - -QT_END_NAMESPACE - -#include "moc_qgesturemanager_p.cpp" - diff --git a/src/gui/kernel/qgesturemanager_p.h b/src/gui/kernel/qgesturemanager_p.h deleted file mode 100644 index 8656590..0000000 --- a/src/gui/kernel/qgesturemanager_p.h +++ /dev/null @@ -1,126 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 QGESTUREMANAGER_P_H -#define QGESTUREMANAGER_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists for the convenience -// of other Qt classes. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qlist.h" -#include "qset.h" -#include "qevent.h" -#include "qbasictimer.h" -#include "qpointer.h" - -#include "qgesturerecognizer.h" - -QT_BEGIN_NAMESPACE - -class QWidget; -class Q_AUTOTEST_EXPORT QGestureManager : public QObject -{ - Q_OBJECT -public: - QGestureManager(QObject *parent); - - int eventDeliveryDelay() const; - void setEventDeliveryDelay(int ms); - - void addRecognizer(QGestureRecognizer *recognizer); - void removeRecognizer(QGestureRecognizer *recognizer); - - bool filterEvent(QWidget *receiver, QEvent *event); - bool inGestureMode(); - - int makeGestureId(const QString &name); - void releaseGestureId(int gestureId); - QString gestureNameFromId(int gestureId) const; - - // declared in qapplication.cpp - static QGestureManager* instance(); - - bool sendGestureEvent(QWidget *receiver, - const QSet<QGesture*> &startedGestures, - const QSet<QGesture*> &updatedGestures, - const QSet<QString> &cancelled = QSet<QString>()); - -protected: - void timerEvent(QTimerEvent *event); - -private slots: - void recognizerStateChanged(QGestureRecognizer::Result); - -private: - QSet<QGestureRecognizer*> activeGestures; - QMap<QGestureRecognizer*, int> maybeGestures; - QSet<QGestureRecognizer*> recognizers; - - QPoint lastPos; - - int eventDeliveryDelayTimeout; - int delayedPressTimer; - QPointer<QWidget> lastMousePressReceiver; - QMouseEvent lastMousePressEvent; - - QMap<int, QString> gestureIdMap; - int lastGestureId; - - enum State { - Gesture, - NotGesture, - MaybeGesture // that mean timers are up and waiting for some - // more events, and input events are handled by - // gesture recognizer explicitely - } state; -}; - -QT_END_NAMESPACE - -#endif // QGESTUREMANAGER_P_H diff --git a/src/gui/kernel/qgesturerecognizer.cpp b/src/gui/kernel/qgesturerecognizer.cpp deleted file mode 100644 index 30889d7..0000000 --- a/src/gui/kernel/qgesturerecognizer.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 "qgesturerecognizer.h" -#include "qgesture.h" - -#include <private/qobject_p.h> -#include <private/qgesturerecognizer_p.h> - -QT_BEGIN_NAMESPACE - -QString qt_getStandardGestureTypeName(Qt::GestureType gestureType); - -/*! - \class QGestureRecognizer - \since 4.6 - - \brief The QGestureRecognizer class is the base class for - implementing custom gestures. - - This is a base class, to create a custom gesture type, you should - subclass it and implement its pure virtual functions. - - Usually gesture recognizer implements state machine, storing its - state internally in the recognizer object. The recognizer receives - input events through the \l{QGestureRecognizer::}{filterEvent()} - virtual function and decides whether the parsed event should - change the state of the recognizer - i.e. if the event starts or - ends a gesture or if it isn't related to gesture at all. -*/ - -/*! - \enum QGestureRecognizer::Result - \since 4.6 - - This enum type defines the state of the gesture recognizer. - - \value Ignore Gesture recognizer ignores the event. - - \value NotGesture Not a gesture. - - \value GestureStarted The continuous gesture has started. When the - recognizer is in this state, a \l{QGestureEvent}{gesture event} - containing QGesture objects returned by the - \l{QGestureRecognizer::}{getGesture()} will be sent to a widget. - - \value GestureFinished The gesture has ended. A - \l{QGestureEvent}{gesture event} will be sent to a widget. - - \value MaybeGesture Gesture recognizer hasn't decided yet if a - gesture has started, but it might start soon after the following - events are received by the recognizer. This means that gesture - manager shouldn't reset() the internal state of the gesture - recognizer. -*/ - -/*! \fn QGestureRecognizer::Result QGestureRecognizer::filterEvent(const QEvent *event) - - This is a pure virtual function that needs to be implemented in - subclasses. - - Parses input \a event and returns the result, which specifies - whether the event sequence is a gesture or not. -*/ - -/*! \fn QGesture* QGestureRecognizer::getGesture() - - Returns a gesture object that will be send to the widget. This - function is called when the gesture recognizer changed its state - to QGestureRecognizer::GestureStarted or - QGestureRecognizer::GestureFinished. - - The returned QGesture object must point to the same object in a - single gesture sequence. - - The gesture object is owned by the recognizer itself. -*/ - -/*! \fn void QGestureRecognizer::reset() - - Resets the internal state of the gesture recognizer. -*/ - -/*! \fn void QGestureRecognizer::stateChanged(QGestureRecognizer::Result result) - - The gesture recognizer might emit the stateChanged() signal when - the gesture state changes asynchronously, i.e. without any event - being filtered through filterEvent(). \a result specifies whether - the event sequence is a gesture or not. -*/ - -QGestureRecognizerPrivate::QGestureRecognizerPrivate() - : gestureType(Qt::UnknownGesture) -{ -} - -/*! - Creates a new gesture recognizer object that handles gestures of - the specific \a gestureType as a child of \a parent. - - \sa QApplication::addGestureRecognizer(), - QApplication::removeGestureRecognizer(), -*/ -QGestureRecognizer::QGestureRecognizer(const QString &gestureType, QObject *parent) - : QObject(*new QGestureRecognizerPrivate, parent) -{ - Q_D(QGestureRecognizer); - d->customGestureType = gestureType; -} - -/*! - Returns the name of the gesture that is handled by the recognizer. -*/ -QString QGestureRecognizer::gestureType() const -{ - Q_D(const QGestureRecognizer); - if (d->gestureType == Qt::UnknownGesture) - return d->customGestureType; - return qt_getStandardGestureTypeName(d->gestureType); -} - -QT_END_NAMESPACE diff --git a/src/gui/kernel/qgesturestandardrecognizers.cpp b/src/gui/kernel/qgesturestandardrecognizers.cpp deleted file mode 100644 index b108994..0000000 --- a/src/gui/kernel/qgesturestandardrecognizers.cpp +++ /dev/null @@ -1,306 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 "qgesturestandardrecognizers_p.h" -#include "qgesture_p.h" -#include "qgesturerecognizer_p.h" - -// #define GESTURE_RECOGNIZER_DEBUG -#ifndef GESTURE_RECOGNIZER_DEBUG -# define DEBUG if (0) qDebug -#else -# define DEBUG qDebug -#endif - -QT_BEGIN_NAMESPACE - -QString qt_getStandardGestureTypeName(Qt::GestureType gestureType) -{ - switch (gestureType) { - case Qt::TapGesture: - return QLatin1String("__QTapGesture"); - case Qt::DoubleTapGesture: - return QLatin1String("__QDoubleTapGesture"); - case Qt::TrippleTapGesture: - return QLatin1String("__QTrippleTapGesture"); - case Qt::TapAndHoldGesture: - return QLatin1String("__QTapAndHoldGesture"); - case Qt::PanGesture: - return QLatin1String("__QPanGesture"); - case Qt::PinchGesture: - return QLatin1String("__QPinchGesture"); - case Qt::UnknownGesture: - break; - } - qFatal("QGestureRecognizer::gestureType: got an unhandled gesture type."); - return QLatin1String("__unknown_gesture"); -} - -// -// QGestureRecognizerPan -// - -QGestureRecognizerPan::QGestureRecognizerPan(QObject *parent) - : QGestureRecognizer(QString(), parent), - mousePressed(false), gestureState(Qt::NoGesture), - lastDirection(Qt::NoDirection), currentDirection(Qt::NoDirection) -{ - Q_D(QGestureRecognizer); - d->gestureType = Qt::PanGesture; -} - -QGestureRecognizer::Result QGestureRecognizerPan::filterEvent(const QEvent *event) -{ - if (event->type() == QEvent::MouseButtonPress) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if (currentDirection != Qt::NoDirection) { - DEBUG() << "Pan: MouseButtonPress: fail. another press during pan"; - reset(); - return QGestureRecognizer::NotGesture; - } - if (ev->button() != Qt::LeftButton) { - return QGestureRecognizer::NotGesture; - } - DEBUG() << "Pan: MouseButtonPress: maybe gesture started"; - mousePressed = true; - pressedPos = lastPos = currentPos = ev->pos(); - return QGestureRecognizer::MaybeGesture; - } else if (event->type() == QEvent::MouseButtonRelease) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if (mousePressed && currentDirection != Qt::NoDirection - && ev->button() == Qt::LeftButton) { - DEBUG() << "Pan: MouseButtonRelease: pan detected"; - gestureState = Qt::GestureFinished; - currentPos = ev->pos(); - internalReset(); - return QGestureRecognizer::GestureFinished; - } - DEBUG() << "Pan: MouseButtonRelease: some weird release detected, ignoring"; - reset(); - return QGestureRecognizer::NotGesture; - } else if (event->type() == QEvent::MouseMove) { - if (!mousePressed) - return QGestureRecognizer::NotGesture; - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - lastPos = currentPos; - currentPos = ev->pos(); - Qt::DirectionType newDirection = - simpleRecognizer.addPosition(ev->pos()).direction; - DEBUG() << "Pan: MouseMove: simplerecognizer result = " << newDirection; - QGestureRecognizer::Result result = QGestureRecognizer::NotGesture; - if (currentDirection == Qt::NoDirection) { - if (newDirection == Qt::NoDirection) { - result = QGestureRecognizer::MaybeGesture; - } else { - result = QGestureRecognizer::GestureStarted; - gestureState = Qt::GestureStarted; - } - } else { - result = QGestureRecognizer::GestureStarted; - gestureState = Qt::GestureUpdated; - } - if (newDirection != Qt::NoDirection) { - if (currentDirection != newDirection) - lastDirection = currentDirection; - currentDirection = newDirection; - } - return result; - } - return QGestureRecognizer::Ignore; -} - -QGesture* QGestureRecognizerPan::getGesture() -{ - if (currentDirection == Qt::NoDirection) - return 0; - QPanningGesturePrivate *d = gesture.d_func(); - d->startPos = pressedPos; - d->lastPos = lastPos; - d->pos = currentPos; - d->hotSpot = pressedPos; - d->state = gestureState; - d->lastDirection = lastDirection; - d->direction = currentDirection; - - return &gesture; -} - -void QGestureRecognizerPan::reset() -{ - mousePressed = false; - lastDirection = Qt::NoDirection; - currentDirection = Qt::NoDirection; - gestureState = Qt::NoGesture; - diagonalRecognizer.reset(); - simpleRecognizer.reset(); -} - -void QGestureRecognizerPan::internalReset() -{ - mousePressed = false; - diagonalRecognizer.reset(); - simpleRecognizer.reset(); -} - - -// -// QDoubleTapGestureRecognizer -// -QDoubleTapGestureRecognizer::QDoubleTapGestureRecognizer(QObject *parent) - : QGestureRecognizer(QString(), parent), - gesture(0, qt_getStandardGestureTypeName(Qt::DoubleTapGesture)) -{ - Q_D(QGestureRecognizer); - d->gestureType = Qt::DoubleTapGesture; -} - -QGestureRecognizer::Result QDoubleTapGestureRecognizer::filterEvent(const QEvent *event) -{ - if (event->type() == QEvent::MouseButtonPress) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if (pressedPosition.isNull()) { - pressedPosition = ev->pos(); - return QGestureRecognizer::MaybeGesture; - } else if ((pressedPosition - ev->pos()).manhattanLength() < 10) { - return QGestureRecognizer::GestureFinished; - } - return QGestureRecognizer::NotGesture; - } else if (event->type() == QEvent::MouseButtonRelease) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if (!pressedPosition.isNull() && (pressedPosition - ev->pos()).manhattanLength() < 10) - return QGestureRecognizer::MaybeGesture; - return QGestureRecognizer::NotGesture; - } else if (event->type() == QEvent::MouseButtonDblClick) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - pressedPosition = ev->pos(); - return QGestureRecognizer::GestureFinished; - } - return QGestureRecognizer::NotGesture; -} - -QGesture* QDoubleTapGestureRecognizer::getGesture() -{ - QGesturePrivate *d = gesture.d_func(); - d->startPos = pressedPosition; - d->lastPos = pressedPosition; - d->pos = pressedPosition; - d->hotSpot = pressedPosition; - d->state = Qt::GestureFinished; - return &gesture; -} - -void QDoubleTapGestureRecognizer::reset() -{ - pressedPosition = QPoint(); -} - -// -// QTapAndHoldGestureRecognizer -// -const int QTapAndHoldGestureRecognizer::iterationCount = 40; -const int QTapAndHoldGestureRecognizer::iterationTimeout = 50; - -QTapAndHoldGestureRecognizer::QTapAndHoldGestureRecognizer(QObject *parent) - : QGestureRecognizer(QString(), parent), - gesture(0, qt_getStandardGestureTypeName(Qt::TapAndHoldGesture)), - iteration(0) -{ - Q_D(QGestureRecognizer); - d->gestureType = Qt::TapAndHoldGesture; -} - -QGestureRecognizer::Result QTapAndHoldGestureRecognizer::filterEvent(const QEvent *event) -{ - if (event->type() == QEvent::MouseButtonPress) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if (timer.isActive()) - timer.stop(); - timer.start(QTapAndHoldGestureRecognizer::iterationTimeout, this); - pressedPosition = ev->pos(); - return QGestureRecognizer::MaybeGesture; - } else if (event->type() == QEvent::MouseMove) { - const QMouseEvent *ev = static_cast<const QMouseEvent*>(event); - if ((pressedPosition - ev->pos()).manhattanLength() < 15) - return QGestureRecognizer::GestureStarted; - else - return QGestureRecognizer::NotGesture; - } else if (event->type() == QEvent::MouseButtonRelease) { - timer.stop(); - return QGestureRecognizer::NotGesture; - } - return QGestureRecognizer::Ignore; -} - -void QTapAndHoldGestureRecognizer::timerEvent(QTimerEvent *event) -{ - if (event->timerId() != timer.timerId()) - return; - if (iteration == QTapAndHoldGestureRecognizer::iterationCount) { - timer.stop(); - emit stateChanged(QGestureRecognizer::GestureFinished); - } else { - emit stateChanged(QGestureRecognizer::GestureStarted); - } - ++iteration; -} - -QGesture* QTapAndHoldGestureRecognizer::getGesture() -{ - QGesturePrivate *d = gesture.d_func(); - d->startPos = pressedPosition; - d->lastPos = pressedPosition; - d->pos = pressedPosition; - d->hotSpot = pressedPosition; - if (iteration >= QTapAndHoldGestureRecognizer::iterationCount) - d->state = Qt::GestureFinished; - else - d->state = iteration == 0 ? Qt::GestureStarted : Qt::GestureUpdated; - return &gesture; -} - -void QTapAndHoldGestureRecognizer::reset() -{ - pressedPosition = QPoint(); - timer.stop(); - iteration = 0; -} - -QT_END_NAMESPACE diff --git a/src/gui/kernel/qgesturestandardrecognizers_p.h b/src/gui/kernel/qgesturestandardrecognizers_p.h deleted file mode 100644 index 8abc1fb..0000000 --- a/src/gui/kernel/qgesturestandardrecognizers_p.h +++ /dev/null @@ -1,131 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** 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 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 QGESTURESTANDARDRECOGNIZERS_P_H -#define QGESTURESTANDARDRECOGNIZERS_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists for the convenience -// of other Qt classes. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qevent.h" -#include "qbasictimer.h" -#include "qdebug.h" - -#include "qgesture.h" -#include "qgesturerecognizer.h" -#include "private/qdirectionrecognizer_p.h" - -QT_BEGIN_NAMESPACE - -class QGestureRecognizerPan : public QGestureRecognizer -{ - Q_OBJECT -public: - QGestureRecognizerPan(QObject *parent); - - QGestureRecognizer::Result filterEvent(const QEvent *event); - QGesture* getGesture(); - void reset(); - -private: - void internalReset(); - - QPanningGesture gesture; - - QPoint pressedPos; - QPoint lastPos; - QPoint currentPos; - bool mousePressed; - Qt::GestureState gestureState; - Qt::DirectionType lastDirection; - Qt::DirectionType currentDirection; - QDirectionDiagonalRecognizer diagonalRecognizer; - QDirectionSimpleRecognizer simpleRecognizer; -}; - -class QDoubleTapGestureRecognizer : public QGestureRecognizer -{ - Q_OBJECT -public: - QDoubleTapGestureRecognizer(QObject *parent); - - QGestureRecognizer::Result filterEvent(const QEvent *event); - QGesture* getGesture(); - void reset(); - -private: - QGesture gesture; - QPoint pressedPosition; -}; - -class QTapAndHoldGestureRecognizer : public QGestureRecognizer -{ - Q_OBJECT -public: - QTapAndHoldGestureRecognizer(QObject *parent); - - QGestureRecognizer::Result filterEvent(const QEvent *event); - QGesture* getGesture(); - void reset(); - -protected: - void timerEvent(QTimerEvent *event); - -private: - QGesture gesture; - QPoint pressedPosition; - QBasicTimer timer; - int iteration; - static const int iterationCount; - static const int iterationTimeout; -}; - -QT_END_NAMESPACE - -#endif // QGESTURESTANDARDRECOGNIZERS_P_H diff --git a/src/gui/kernel/qstandardgestures.cpp b/src/gui/kernel/qstandardgestures.cpp new file mode 100644 index 0000000..c8b11c5 --- /dev/null +++ b/src/gui/kernel/qstandardgestures.cpp @@ -0,0 +1,254 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** 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 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 "qstandardgestures.h" +#include "qstandardgestures_p.h" + +#include <qabstractscrollarea.h> +#include <qscrollbar.h> +#include <private/qapplication_p.h> + +QT_BEGIN_NAMESPACE + +/*! + \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) +{ +#ifdef Q_WS_WIN + if (parent) { + QApplicationPrivate* getQApplicationPrivateInternal(); + QApplicationPrivate *qAppPriv = getQApplicationPrivateInternal(); + qAppPriv->widgetGestures[parent].pan = this; + } +#endif +} + +/*! \internal */ +bool QPanGesture::event(QEvent *event) +{ +#ifdef Q_WS_WIN + QApplicationPrivate* getQApplicationPrivateInternal(); + switch (event->type()) { + case QEvent::ParentAboutToChange: + if (QWidget *w = qobject_cast<QWidget*>(parent())) + getQApplicationPrivateInternal()->widgetGestures[w].pan = 0; + break; + case QEvent::ParentChange: + if (QWidget *w = qobject_cast<QWidget*>(parent())) + getQApplicationPrivateInternal()->widgetGestures[w].pan = this; + break; + default: + break; + } +#endif + return QObject::event(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) { + d->touchPoints = ev->touchPoints(); + const QPoint p = ev->touchPoints().at(0).pos().toPoint(); + setStartPos(p); + setLastPos(p); + setPos(p); + return false; + } else if (event->type() == QEvent::TouchEnd) { + if (state() != Qt::NoGesture) { + setState(Qt::GestureFinished); + setLastPos(pos()); + setPos(ev->touchPoints().at(0).pos().toPoint()); + emit triggered(); + emit finished(); + } + setState(Qt::NoGesture); + reset(); + } else if (event->type() == QEvent::TouchUpdate) { + d->touchPoints = ev->touchPoints(); + QPointF pt = d->touchPoints.at(0).pos() - d->touchPoints.at(0).startPos(); + setLastPos(pos()); + setPos(ev->touchPoints().at(0).pos().toPoint()); + if (pt.x() > 10 || pt.y() > 10 || pt.x() < -10 || pt.y() < -10) { + if (state() == Qt::NoGesture) + setState(Qt::GestureStarted); + else + setState(Qt::GestureUpdated); + emit triggered(); + } + } + return false; +} + +/*! \internal */ +void QPanGesture::reset() +{ + Q_D(QPanGesture); + d->touchPoints.clear(); +} + +/*! + \property QPanGesture::totalOffset + + Specifies a total pan offset since the start of the gesture. +*/ +QSize QPanGesture::totalOffset() const +{ + QPoint pt = pos() - startPos(); + return QSize(pt.x(), pt.y()); +} + +/*! + \property QPanGesture::lastOffset + + Specifies a pan offset since the last time the gesture was + triggered. +*/ +QSize QPanGesture::lastOffset() const +{ + QPoint pt = pos() - lastPos(); + return QSize(pt.x(), pt.y()); +} + +/*! + \class QTapAndHoldGesture + \since 4.6 + + \brief The QTapAndHoldGesture class represents a Tap-and-Hold gesture, + providing additional information. +*/ + +const int QTapAndHoldGesturePrivate::iterationCount = 40; +const int QTapAndHoldGesturePrivate::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. +*/ +QTapAndHoldGesture::QTapAndHoldGesture(QWidget *parent) + : QGesture(*new QTapAndHoldGesturePrivate, parent) +{ +} + +/*! \internal */ +bool QTapAndHoldGesture::filterEvent(QEvent *event) +{ + Q_D(QTapAndHoldGesture); + if (!event->spontaneous()) + return false; + const QTouchEvent *ev = static_cast<const QTouchEvent*>(event); + switch (event->type()) { + case QEvent::TouchBegin: { + if (d->timer.isActive()) + d->timer.stop(); + d->timer.start(QTapAndHoldGesturePrivate::iterationTimeout, this); + const QPoint p = ev->touchPoints().at(0).pos().toPoint(); + setStartPos(p); + setLastPos(p); + setPos(p); + break; + } + case QEvent::TouchUpdate: + if (ev->touchPoints().size() != 1) + reset(); + else if ((startPos() - ev->touchPoints().at(0).pos().toPoint()).manhattanLength() > 15) + reset(); + break; + case QEvent::TouchEnd: + reset(); + break; + default: + break; + } + return false; +} + +/*! \internal */ +void QTapAndHoldGesture::timerEvent(QTimerEvent *event) +{ + Q_D(QTapAndHoldGesture); + if (event->timerId() != d->timer.timerId()) + return; + if (d->iteration == QTapAndHoldGesturePrivate::iterationCount) { + d->timer.stop(); + setState(Qt::GestureFinished); + emit triggered(); + } else { + setState(Qt::GestureStarted); + emit triggered(); + } + ++d->iteration; +} + +/*! \internal */ +void QTapAndHoldGesture::reset() +{ + Q_D(QTapAndHoldGesture); + if (state() != Qt::NoGesture) + emit cancelled(); + setState(Qt::NoGesture); + d->timer.stop(); + d->iteration = 0; +} + +QT_END_NAMESPACE diff --git a/src/gui/kernel/qgesturerecognizer.h b/src/gui/kernel/qstandardgestures.h index 2c1c61b..db96ef6 100644 --- a/src/gui/kernel/qgesturerecognizer.h +++ b/src/gui/kernel/qstandardgestures.h @@ -39,49 +39,64 @@ ** ****************************************************************************/ -#ifndef QGESTURERECOGNIZER_H -#define QGESTURERECOGNIZER_H +#ifndef QSTANDARDGESTURES_H +#define QSTANDARDGESTURES_H #include "qevent.h" -#include "qlist.h" -#include "qset.h" +#include "qbasictimer.h" +#include "qdebug.h" + +#include "qgesture.h" + +QT_BEGIN_HEADER QT_BEGIN_NAMESPACE -class QGesture; -class QGestureRecognizerPrivate; -class Q_GUI_EXPORT QGestureRecognizer : public QObject +QT_MODULE(Gui) + +class QPanGesturePrivate; +class Q_GUI_EXPORT QPanGesture : public QGesture { Q_OBJECT - Q_DECLARE_PRIVATE(QGestureRecognizer) + Q_DECLARE_PRIVATE(QPanGesture) -public: - enum Result - { - Ignore, - NotGesture, - GestureStarted, //TODO: rename to just Gesture? - GestureFinished, - MaybeGesture - }; + Q_PROPERTY(QSize totalOffset READ totalOffset) + Q_PROPERTY(QSize lastOffset READ lastOffset) - explicit QGestureRecognizer(const QString &gestureType, QObject *parent = 0); +public: + QPanGesture(QWidget *parent); - QString gestureType() const; + bool filterEvent(QEvent *event); + void reset(); - virtual QGestureRecognizer::Result filterEvent(const QEvent* event) = 0; - virtual QGesture* getGesture() = 0; - virtual void reset() = 0; + QSize totalOffset() const; + QSize lastOffset() const; -signals: - void stateChanged(QGestureRecognizer::Result result); +protected: + bool event(QEvent *event); private: - friend class QDoubleTapGestureRecognizer; - friend class QTapAndHoldGestureRecognizer; - friend class QGestureRecognizerPan; + friend class QWidget; +}; + +class QTapAndHoldGesturePrivate; +class Q_GUI_EXPORT QTapAndHoldGesture : public QGesture +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QTapAndHoldGesture) + +public: + QTapAndHoldGesture(QWidget *parent); + + bool filterEvent(QEvent *event); + void reset(); + +protected: + void timerEvent(QTimerEvent *event); }; QT_END_NAMESPACE -#endif // QGESTURERECOGNIZER_P_H +QT_END_HEADER + +#endif // QSTANDARDGESTURES_H diff --git a/src/gui/kernel/qgesturerecognizer_p.h b/src/gui/kernel/qstandardgestures_p.h index e250201..bb11c9f 100644 --- a/src/gui/kernel/qgesturerecognizer_p.h +++ b/src/gui/kernel/qstandardgestures_p.h @@ -39,8 +39,8 @@ ** ****************************************************************************/ -#ifndef QGESTURERECOGNIZER_P_H -#define QGESTURERECOGNIZER_P_H +#ifndef QSTANDARDGESTURES_P_H +#define QSTANDARDGESTURES_P_H // // W A R N I N G @@ -53,20 +53,39 @@ // We mean it. // -#include <private/qobject_p.h> +#include "qevent.h" +#include "qbasictimer.h" +#include "qdebug.h" + +#include "qgesture.h" +#include "qgesture_p.h" QT_BEGIN_NAMESPACE -class QGestureRecognizerPrivate : public QObjectPrivate +class QPanGesturePrivate : public QGesturePrivate { + Q_DECLARE_PUBLIC(QPanGesture) + public: - QGestureRecognizerPrivate(); + QPanGesturePrivate() { } + + QList<QTouchEvent::TouchPoint> touchPoints; +}; + +class QTapAndHoldGesturePrivate : public QGesturePrivate +{ + Q_DECLARE_PUBLIC(QTapAndHoldGesture) public: - Qt::GestureType gestureType; - QString customGestureType; + QTapAndHoldGesturePrivate() + : iteration(0) { } + + QBasicTimer timer; + int iteration; + static const int iterationCount; + static const int iterationTimeout; }; QT_END_NAMESPACE -#endif // QGESTURERECOGNIZER_P_H +#endif // QSTANDARDGESTURES_P_H diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp index 9a834aa..611cb44 100644 --- a/src/gui/kernel/qwidget.cpp +++ b/src/gui/kernel/qwidget.cpp @@ -82,6 +82,8 @@ #include "private/qstyle_p.h" #include "private/qinputcontext_p.h" #include "qfileinfo.h" +#include "qstandardgestures.h" +#include "qstandardgestures_p.h" #if defined (Q_WS_WIN) # include <private/qwininputcontext_p.h> @@ -107,9 +109,9 @@ #include "private/qgraphicsproxywidget_p.h" #include "QtGui/qabstractscrollarea.h" #include "private/qabstractscrollarea_p.h" +#include "private/qevent_p.h" #include "private/qgraphicssystem_p.h" -#include "private/qgesturemanager_p.h" // widget/widget data creation count //#define QWIDGET_EXTRA_DEBUG @@ -128,8 +130,6 @@ Q_GUI_EXPORT void qt_x11_set_global_double_buffer(bool enable) } #endif -QString qt_getStandardGestureTypeName(Qt::GestureType); - static inline bool qRectIntersects(const QRect &r1, const QRect &r2) { return (qMax(r1.left(), r2.left()) <= qMin(r1.right(), r2.right()) && @@ -7484,8 +7484,6 @@ bool QWidget::event(QEvent *event) #ifndef QT_NO_WHEELEVENT case QEvent::Wheel: #endif - case QEvent::Gesture: - return false; default: break; } @@ -7880,9 +7878,6 @@ bool QWidget::event(QEvent *event) d->needWindowChange = false; break; #endif - case QEvent::Gesture: - event->ignore(); - break; case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: @@ -7920,6 +7915,59 @@ bool QWidget::event(QEvent *event) (void) QApplication::sendEvent(this, &mouseEvent); break; } +#ifdef Q_WS_WIN + case QEvent::WinGesture: { + QWinGestureEvent *ev = static_cast<QWinGestureEvent*>(event); + QApplicationPrivate *qAppPriv = qApp->d_func(); + QApplicationPrivate::WidgetStandardGesturesMap::iterator it; + it = qAppPriv->widgetGestures.find(this); + if (it != qAppPriv->widgetGestures.end()) { + Qt::GestureState state = Qt::GestureUpdated; + if (qAppPriv->lastGestureId == 0) + state = Qt::GestureStarted; + QWinGestureEvent::Type type = ev->gestureType; + if (ev->gestureType == QWinGestureEvent::GestureEnd) { + type = (QWinGestureEvent::Type)qAppPriv->lastGestureId; + state = Qt::GestureFinished; + } + + QGesture *gesture = 0; + switch (type) { + case QWinGestureEvent::Pan: { + QPanGesture *pan = it.value().pan; + gesture = pan; + if (state == Qt::GestureStarted) { + gesture->setStartPos(ev->position); + gesture->setLastPos(ev->position); + } else { + gesture->setLastPos(gesture->pos()); + } + gesture->setPos(ev->position); + break; + } + case QWinGestureEvent::Pinch: + break; + default: + break; + } + if (gesture) { + gesture->setState(state); + if (state == Qt::GestureStarted) + emit gesture->started(); + emit gesture->triggered(); + if (state == Qt::GestureFinished) + emit gesture->finished(); + event->accept(); + } + if (ev->gestureType == QWinGestureEvent::GestureEnd) { + qAppPriv->lastGestureId = 0; + } else { + qAppPriv->lastGestureId = type; + } + } + break; + } +#endif #ifndef QT_NO_PROPERTIES case QEvent::DynamicPropertyChange: { const QByteArray &propName = static_cast<QDynamicPropertyChangeEvent *>(event)->propertyName(); @@ -11016,105 +11064,6 @@ QWindowSurface *QWidget::windowSurface() const return bs ? bs->windowSurface : 0; } -/*! - \since 4.6 - - Subscribes the widget to the specified \a gesture type. - - Returns the id of the gesture. - - \sa releaseGesture(), setGestureEnabled() -*/ -int QWidget::grabGesture(const QString &gesture) -{ - Q_D(QWidget); - int id = d->grabGesture(QGestureManager::instance()->makeGestureId(gesture)); - if (d->extra && d->extra->proxyWidget) - d->extra->proxyWidget->QGraphicsItem::d_ptr->grabGesture(id); - return id; -} - -int QWidgetPrivate::grabGesture(int gestureId) -{ - gestures << gestureId; - ++qApp->d_func()->grabbedGestures[QGestureManager::instance()->gestureNameFromId(gestureId)]; - return gestureId; -} - -bool QWidgetPrivate::releaseGesture(int gestureId) -{ - QApplicationPrivate *qAppPriv = qApp->d_func(); - if (gestures.contains(gestureId)) { - QString name = QGestureManager::instance()->gestureNameFromId(gestureId); - Q_ASSERT(qAppPriv->grabbedGestures[name] > 0); - --qAppPriv->grabbedGestures[name]; - gestures.remove(gestureId); - return true; - } - return false; -} - -bool QWidgetPrivate::hasGesture(const QString &name) const -{ - QGestureManager *gm = QGestureManager::instance(); - QSet<int>::const_iterator it = gestures.begin(), - e = gestures.end(); - for (; it != e; ++it) { - if (gm->gestureNameFromId(*it) == name) - return true; - } - return false; -} - -/*! - \since 4.6 - - Subscribes the widget to the specified \a gesture type. - - Returns the id of the gesture. - - \sa releaseGesture(), setGestureEnabled() -*/ -int QWidget::grabGesture(Qt::GestureType gesture) -{ - return grabGesture(qt_getStandardGestureTypeName(gesture)); -} - -/*! - \since 4.6 - - Unsubscribes the widget from a gesture, which is specified by the - \a gestureId. - - \sa grabGesture(), setGestureEnabled() -*/ -void QWidget::releaseGesture(int gestureId) -{ - Q_D(QWidget); - if (d->releaseGesture(gestureId)) { - if (d->extra && d->extra->proxyWidget) - d->extra->proxyWidget->QGraphicsItem::d_ptr->releaseGesture(gestureId); - QGestureManager::instance()->releaseGestureId(gestureId); - } -} - -/*! - \since 4.6 - - If \a enable is true, the gesture with the given \a gestureId is - enabled; otherwise the gesture is disabled. - - The id of the gesture is returned by the grabGesture(). - - \sa grabGesture(), releaseGesture() -*/ -void QWidget::setGestureEnabled(int gestureId, bool enable) -{ - Q_UNUSED(gestureId); - Q_UNUSED(enable); - //### -} - void QWidgetPrivate::getLayoutItemMargins(int *left, int *top, int *right, int *bottom) const { if (left) diff --git a/src/gui/kernel/qwidget.h b/src/gui/kernel/qwidget.h index 1667275..bc9952c 100644 --- a/src/gui/kernel/qwidget.h +++ b/src/gui/kernel/qwidget.h @@ -90,7 +90,6 @@ class QDragLeaveEvent; class QDropEvent; class QShowEvent; class QHideEvent; -class QGestureEvent; class QInputContext; class QIcon; class QWindowSurface; @@ -612,11 +611,6 @@ public: void setWindowSurface(QWindowSurface *surface); QWindowSurface *windowSurface() const; - int grabGesture(const QString &gesture); - int grabGesture(Qt::GestureType gesture); - void releaseGesture(int gestureId); - void setGestureEnabled(int gestureId, bool enable = true); - Q_SIGNALS: void customContextMenuRequested(const QPoint &pos); @@ -752,7 +746,6 @@ private: friend bool isWidgetOpaque(const QWidget *); friend class QGLWidgetPrivate; #endif - friend class QGestureManager; #ifdef Q_WS_X11 friend void qt_net_update_user_time(QWidget *tlw, unsigned long timestamp); friend void qt_net_remove_user_time(QWidget *tlw); diff --git a/src/gui/kernel/qwidget_p.h b/src/gui/kernel/qwidget_p.h index 7385e83..626950e 100644 --- a/src/gui/kernel/qwidget_p.h +++ b/src/gui/kernel/qwidget_p.h @@ -523,11 +523,6 @@ public: QList<QAction*> actions; #endif - QSet<int> gestures; - int grabGesture(int gestureId); - bool releaseGesture(int gestureId); - bool hasGesture(const QString &type) const; - // Bit fields. uint high_attributes[3]; // the low ones are in QWidget::widget_attributes QPalette::ColorRole fg_role : 8; diff --git a/src/gui/widgets/qabstractscrollarea.cpp b/src/gui/widgets/qabstractscrollarea.cpp index e1beb98..66572b8 100644 --- a/src/gui/widgets/qabstractscrollarea.cpp +++ b/src/gui/widgets/qabstractscrollarea.cpp @@ -51,10 +51,13 @@ #include "qdebug.h" #include "qboxlayout.h" #include "qpainter.h" +#include "qstandardgestures.h" #include "qabstractscrollarea_p.h" #include <qwidget.h> +#include <private/qapplication_p.h> + #ifdef Q_WS_MAC #include <private/qt_mac_p.h> #include <private/qt_cocoa_helpers_mac_p.h> @@ -157,6 +160,9 @@ QAbstractScrollAreaPrivate::QAbstractScrollAreaPrivate() :hbar(0), vbar(0), vbarpolicy(Qt::ScrollBarAsNeeded), hbarpolicy(Qt::ScrollBarAsNeeded), viewport(0), cornerWidget(0), left(0), top(0), right(0), bottom(0), xoffset(0), yoffset(0), viewportFilter(0) +#ifdef Q_WS_WIN + , singleFingerPanEnabled(false) +#endif { } @@ -290,6 +296,31 @@ void QAbstractScrollAreaPrivate::init() layoutChildren(); } +void QAbstractScrollAreaPrivate::setupGestures() +{ +#ifdef Q_OS_WIN + if (!viewport) + return; + QApplicationPrivate *qAppPriv = getQApplicationPrivateInternal(); + bool needh = (hbarpolicy == Qt::ScrollBarAlwaysOn + || (hbarpolicy == Qt::ScrollBarAsNeeded && hbar->minimum() < hbar->maximum())); + + bool needv = (vbarpolicy == Qt::ScrollBarAlwaysOn + || (vbarpolicy == Qt::ScrollBarAsNeeded && vbar->minimum() < vbar->maximum())); + if (qAppPriv->SetGestureConfig && (needh || needv)) { + GESTURECONFIG gc[1]; + gc[0].dwID = GID_PAN; + gc[0].dwWant = GC_PAN; + gc[0].dwBlock = 0; + if (needv && singleFingerPanEnabled) + gc[0].dwWant |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY; + if (needh && singleFingerPanEnabled) + gc[0].dwWant |= GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY; + qAppPriv->SetGestureConfig(viewport->winId(), 0, 1, gc, sizeof(gc)); + } +#endif // Q_OS_WIN +} + void QAbstractScrollAreaPrivate::layoutChildren() { Q_Q(QAbstractScrollArea); @@ -909,8 +940,6 @@ bool QAbstractScrollArea::event(QEvent *e) case QEvent::DragMove: case QEvent::DragLeave: #endif - case QEvent::Gesture: - return false; case QEvent::StyleChange: case QEvent::LayoutDirectionChange: case QEvent::ApplicationLayoutDirectionChange: @@ -1239,6 +1268,7 @@ void QAbstractScrollAreaPrivate::_q_vslide(int y) void QAbstractScrollAreaPrivate::_q_showOrHideScrollBars() { layoutChildren(); + setupGestures(); } QPoint QAbstractScrollAreaPrivate::contentsOffset() const diff --git a/src/gui/widgets/qabstractscrollarea_p.h b/src/gui/widgets/qabstractscrollarea_p.h index 71a83cc..5d3494b 100644 --- a/src/gui/widgets/qabstractscrollarea_p.h +++ b/src/gui/widgets/qabstractscrollarea_p.h @@ -99,6 +99,11 @@ public: inline bool viewportEvent(QEvent *event) { return q_func()->viewportEvent(event); } QObject *viewportFilter; + +#ifdef Q_WS_WIN + bool singleFingerPanEnabled; +#endif + void setupGestures(); }; class QAbstractScrollAreaFilter : public QObject diff --git a/src/gui/widgets/qplaintextedit.cpp b/src/gui/widgets/qplaintextedit.cpp index bab6b89..c9d1d6e 100644 --- a/src/gui/widgets/qplaintextedit.cpp +++ b/src/gui/widgets/qplaintextedit.cpp @@ -66,6 +66,8 @@ #include <qtexttable.h> #include <qvariant.h> +#include <qstandardgestures.h> + #include <qinputcontext.h> #ifndef QT_NO_TEXTEDIT @@ -726,6 +728,9 @@ QPlainTextEditPrivate::QPlainTextEditPrivate() backgroundVisible = false; centerOnScroll = false; inDrag = false; +#ifdef Q_WS_WIN + singleFingerPanEnabled = true; +#endif } @@ -781,6 +786,9 @@ void QPlainTextEditPrivate::init(const QString &txt) #ifndef QT_NO_CURSOR viewport->setCursor(Qt::IBeamCursor); #endif + originalOffsetY = 0; + panGesture = new QPanGesture(q); + QObject::connect(panGesture, SIGNAL(triggered()), q, SLOT(_q_gestureTriggered())); } void QPlainTextEditPrivate::_q_repaintContents(const QRectF &contentsRect) @@ -2899,6 +2907,30 @@ QAbstractTextDocumentLayout::PaintContext QPlainTextEdit::getPaintContext() cons (\a available is true) or unavailable (\a available is false). */ +void QPlainTextEditPrivate::_q_gestureTriggered() +{ + Q_Q(QPlainTextEdit); + QPanGesture *g = qobject_cast<QPanGesture*>(q->sender()); + if (!g) + return; + QScrollBar *hBar = q->horizontalScrollBar(); + QScrollBar *vBar = q->verticalScrollBar(); + if (g->state() == Qt::GestureStarted) + originalOffsetY = vBar->value(); + QSize totalOffset = g->totalOffset(); + if (!totalOffset.isNull()) { + if (QApplication::isRightToLeft()) + totalOffset.rwidth() *= -1; + // QPlainTextEdit scrolls by lines only in vertical direction + QFontMetrics fm(q->document()->defaultFont()); + int lineHeight = fm.height(); + int newX = hBar->value() - g->lastOffset().width(); + int newY = originalOffsetY - totalOffset.height()/lineHeight; + hbar->setValue(newX); + vbar->setValue(newY); + } +} + QT_END_NAMESPACE #include "moc_qplaintextedit.cpp" diff --git a/src/gui/widgets/qplaintextedit.h b/src/gui/widgets/qplaintextedit.h index dc0851b..35bbc37 100644 --- a/src/gui/widgets/qplaintextedit.h +++ b/src/gui/widgets/qplaintextedit.h @@ -269,6 +269,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars()) Q_PRIVATE_SLOT(d_func(), void _q_verticalScrollbarActionTriggered(int)) Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged()) + Q_PRIVATE_SLOT(d_func(), void _q_gestureTriggered()) friend class QPlainTextEditControl; }; diff --git a/src/gui/widgets/qplaintextedit_p.h b/src/gui/widgets/qplaintextedit_p.h index 5075fc4..ae584e0 100644 --- a/src/gui/widgets/qplaintextedit_p.h +++ b/src/gui/widgets/qplaintextedit_p.h @@ -72,6 +72,7 @@ class QMimeData; class QPlainTextEdit; class ExtraArea; +class QPanGesture; class QPlainTextEditControl : public QTextControl { @@ -173,6 +174,10 @@ public: void _q_cursorPositionChanged(); void _q_modificationChanged(bool); + + void _q_gestureTriggered(); + int originalOffsetY; + QPanGesture *panGesture; }; QT_END_NAMESPACE diff --git a/src/gui/widgets/qtextedit.cpp b/src/gui/widgets/qtextedit.cpp index c8d8d04..e80df92 100644 --- a/src/gui/widgets/qtextedit.cpp +++ b/src/gui/widgets/qtextedit.cpp @@ -67,6 +67,8 @@ #include <qtexttable.h> #include <qvariant.h> +#include <qstandardgestures.h> + #include <qinputcontext.h> #endif @@ -111,6 +113,9 @@ QTextEditPrivate::QTextEditPrivate() preferRichText = false; showCursorOnInitialShow = true; inDrag = false; +#ifdef Q_WS_WIN + singleFingerPanEnabled = true; +#endif } void QTextEditPrivate::createAutoBulletList() @@ -178,6 +183,8 @@ void QTextEditPrivate::init(const QString &html) #ifndef QT_NO_CURSOR viewport->setCursor(Qt::IBeamCursor); #endif + panGesture = new QPanGesture(q); + QObject::connect(panGesture, SIGNAL(triggered()), q, SLOT(_q_gestureTriggered())); } void QTextEditPrivate::_q_repaintContents(const QRectF &contentsRect) @@ -2610,6 +2617,25 @@ void QTextEdit::ensureCursorVisible() d->control->ensureCursorVisible(); } +void QTextEditPrivate::_q_gestureTriggered() +{ + Q_Q(QTextEdit); + QPanGesture *g = qobject_cast<QPanGesture*>(q->sender()); + if (!g) + return; + QScrollBar *hBar = q->horizontalScrollBar(); + QScrollBar *vBar = q->verticalScrollBar(); + QPoint delta = g->pos() - (g->lastPos().isNull() ? g->pos() : g->lastPos()); + if (!delta.isNull()) { + if (QApplication::isRightToLeft()) + delta.rx() *= -1; + int newX = hBar->value() - delta.x(); + int newY = vBar->value() - delta.y(); + hbar->setValue(newX); + vbar->setValue(newY); + } +} + /*! \enum QTextEdit::KeyboardAction diff --git a/src/gui/widgets/qtextedit.h b/src/gui/widgets/qtextedit.h index 617822a..9e10e07 100644 --- a/src/gui/widgets/qtextedit.h +++ b/src/gui/widgets/qtextedit.h @@ -414,6 +414,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_currentCharFormatChanged(const QTextCharFormat &)) Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars()) Q_PRIVATE_SLOT(d_func(), void _q_ensureVisible(const QRectF &)) + Q_PRIVATE_SLOT(d_func(), void _q_gestureTriggered()) friend class QTextEditControl; friend class QTextDocument; friend class QTextControl; diff --git a/src/gui/widgets/qtextedit_p.h b/src/gui/widgets/qtextedit_p.h index e7609d6..249331e 100644 --- a/src/gui/widgets/qtextedit_p.h +++ b/src/gui/widgets/qtextedit_p.h @@ -70,7 +70,7 @@ QT_BEGIN_NAMESPACE #ifndef QT_NO_TEXTEDIT class QMimeData; - +class QPanGesture; class QTextEditPrivate : public QAbstractScrollAreaPrivate { Q_DECLARE_PUBLIC(QTextEdit) @@ -129,6 +129,9 @@ public: QString anchorToScrollToWhenVisible; + void _q_gestureTriggered(); + QPanGesture *panGesture; + #ifdef QT_KEYPAD_NAVIGATION QBasicTimer deleteAllTimer; #endif |