From 51381e1454070960bc3c1d6acdfa8e742c37620c Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Tue, 6 Apr 2010 15:56:38 +1000 Subject: Labs (experimental) Gesture support. By popular demand. --- doc/src/declarative/elements.qdoc | 3 +- .../declarative/gestures/experimental-gestures.qml | 27 +++ src/declarative/qml/qdeclarativeparser_p.h | 2 +- src/imports/gestures/gestures.pro | 27 +++ src/imports/gestures/plugin.cpp | 66 +++++ src/imports/gestures/qdeclarativegesturearea.cpp | 265 +++++++++++++++++++++ src/imports/gestures/qdeclarativegesturearea_p.h | 102 ++++++++ src/imports/gestures/qmldir | 1 + 8 files changed, 491 insertions(+), 2 deletions(-) create mode 100644 examples/declarative/gestures/experimental-gestures.qml create mode 100644 src/imports/gestures/gestures.pro create mode 100644 src/imports/gestures/plugin.cpp create mode 100644 src/imports/gestures/qdeclarativegesturearea.cpp create mode 100644 src/imports/gestures/qdeclarativegesturearea_p.h create mode 100644 src/imports/gestures/qmldir diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc index 5eaaf7e..1bb739d 100644 --- a/doc/src/declarative/elements.qdoc +++ b/doc/src/declarative/elements.qdoc @@ -132,6 +132,7 @@ The following table lists the QML elements provided by the Qt Declarative module \o \l FocusScope \o \l Flickable \o \l Flipable +\o \l GestureArea (experimental) \endlist \o @@ -194,7 +195,7 @@ The following table lists the QML elements provided by the Qt Declarative module \o \list -\o \l Particles +\o \l Particles (experimental) \list \o \l ParticleMotionLinear \o \l ParticleMotionGravity diff --git a/examples/declarative/gestures/experimental-gestures.qml b/examples/declarative/gestures/experimental-gestures.qml new file mode 100644 index 0000000..914d403 --- /dev/null +++ b/examples/declarative/gestures/experimental-gestures.qml @@ -0,0 +1,27 @@ +import Qt 4.7 +import Qt.labs.gestures 1.0 + +// Only works on platforms with Touch support. + +Rectangle { + id: rect + + GestureArea { + anchors.fill: parent + + // Only some of the many gesture properties are shown. See Gesture documentation. + + onTap: + console.log("tap pos = (",gesture.position.x,",",gesture.position.y,")") + onTapAndHold: + console.log("tap and hold pos = (",gesture.position.x,",",gesture.position.y,")") + onPan: + console.log("pan delta = (",gesture.delta.x,",",gesture.delta.y,") acceleration = ",gesture.acceleration) + onPinch: + console.log("pinch center = (",gesture.centerPoint.x,",",gesture.centerPoint.y,") rotation =",gesture.rotationAngle," scale =",gesture.scaleFactor) + onSwipe: + console.log("swipe angle=",gesture.swipeAngle) + onGesture: + console.log("gesture hot spot = (",gesture.hotSpot.x,",",gesture.hotSpot.y,")") + } +} diff --git a/src/declarative/qml/qdeclarativeparser_p.h b/src/declarative/qml/qdeclarativeparser_p.h index f432024..be8f906 100644 --- a/src/declarative/qml/qdeclarativeparser_p.h +++ b/src/declarative/qml/qdeclarativeparser_p.h @@ -237,7 +237,7 @@ namespace QDeclarativeParser QList dynamicSlots; }; - class Variant + class Q_DECLARATIVE_EXPORT Variant { public: enum Type { diff --git a/src/imports/gestures/gestures.pro b/src/imports/gestures/gestures.pro new file mode 100644 index 0000000..b9c5b4e --- /dev/null +++ b/src/imports/gestures/gestures.pro @@ -0,0 +1,27 @@ +TARGET = gesturesqmlplugin +TARGETPATH = Qt/labs/gestures +include(../qimportbase.pri) + +QT += declarative + +SOURCES += qdeclarativegesturearea.cpp plugin.cpp +HEADERS += qdeclarativegesturearea_p.h + +QTDIR_build:DESTDIR = $$QT_BUILD_TREE/imports/$$TARGETPATH +target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +qmldir.files += $$PWD/qmldir +qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +symbian:{ + load(data_caging_paths) + include($$QT_SOURCE_TREE/demos/symbianpkgrules.pri) + + importFiles.sources = gesturesqmlplugin.dll \ + qmldir + importFiles.path = $$QT_IMPORTS_BASE_DIR/$$TARGETPATH + + DEPLOYMENT = importFiles +} + +INSTALLS += target qmldir diff --git a/src/imports/gestures/plugin.cpp b/src/imports/gestures/plugin.cpp new file mode 100644 index 0000000..9f5d923 --- /dev/null +++ b/src/imports/gestures/plugin.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "qdeclarativegesturearea_p.h" + +QT_BEGIN_NAMESPACE + +class GestureAreaQmlPlugin : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + virtual void registerTypes(const char *uri) + { + Q_ASSERT(QLatin1String(uri) == QLatin1String("Qt.labs.gestures")); + qmlRegisterCustomType(uri,1,0, "GestureArea", "QDeclarativeGestureArea", + new QDeclarativeGestureAreaParser); + } +}; + +QT_END_NAMESPACE + +#include "plugin.moc" + +Q_EXPORT_PLUGIN2(gesturesqmlplugin, QT_PREPEND_NAMESPACE(GestureAreaQmlPlugin)); + diff --git a/src/imports/gestures/qdeclarativegesturearea.cpp b/src/imports/gestures/qdeclarativegesturearea.cpp new file mode 100644 index 0000000..9b4af03 --- /dev/null +++ b/src/imports/gestures/qdeclarativegesturearea.cpp @@ -0,0 +1,265 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdeclarativegesturearea_p.h" + +#include +#include +#include + +#include +#include + +#include +#include + +#include + +#include + +QT_BEGIN_NAMESPACE + +class QDeclarativeGestureAreaPrivate : public QDeclarativeItemPrivate +{ + Q_DECLARE_PUBLIC(QDeclarativeGestureArea) +public: + QDeclarativeGestureAreaPrivate() : componentcomplete(false), gesture(0) {} + + typedef QMap Bindings; + Bindings bindings; + + bool componentcomplete; + + QByteArray data; + + QGesture *gesture; + + bool gestureEvent(QGestureEvent *event); +}; + +/*! + \qmlclass GestureArea QDeclarativeGestureArea + \brief The GestureArea item enables simple gesture handling. + \inherits Item + + A GestureArea is like a MouseArea, but it has signals for gesture events. + + \e {This element is only functional on devices with touch input.} + + \qml + import Qt.labs.gestures 0.1 + + GestureArea { + anchors.fill: parent + onPan: ... gesture.acceleration ... + onPinch: ... gesture.rotationAngle ... + onSwipe: ... + onTapAndHold: ... + onTap: ... + onGesture: ... + } + \endqml + + Each signal has a \e gesture parameter that has the + properties of the gesture. + + \table + \header \o Signal \o Type \o Property \o Description + \row \o onTap \o point \o position \o the position of the tap + \row \o onTapAndHold \o point \o position \o the position of the tap + \row \o onPan \o real \o acceleration \o the acceleration of the pan + \row \o onPan \o point \o delta \o the offset from the previous input position to the current input + \row \o onPan \o point \o offset \o the total offset from the first input position to the current input position + \row \o onPan \o point \o lastOffset \o the previous value of offset + \row \o onPinch \o point \o centerPoint \o the midpoint between the two input points + \row \o onPinch \o point \o lastCenterPoint \o the previous value of centerPoint + \row \o onPinch \o point \o startCenterPoint \o the first value of centerPoint + \row \o onPinch \o real \o rotationAngle \o the angle covered by the gesture motion + \row \o onPinch \o real \o lastRotationAngle \o the previous value of rotationAngle + \row \o onPinch \o real \o totalRotationAngle \o the complete angle covered by the gesture + \row \o onPinch \o real \o scaleFactor \o the change in distance between the two input points + \row \o onPinch \o real \o lastScaleFactor \o the previous value of scaleFactor + \row \o onPinch \o real \o totalScaleFactor \o the complete scale factor of the gesture + \row \o onSwipe \o real \o swipeAngle \o the angle of the swipe + \endtable + + Custom gestures, handled by onGesture, will have custom properties. + + GestureArea is an invisible item: it is never painted. + + \sa Gesture, MouseArea +*/ + +/*! + \internal + \class QDeclarativeGestureArea + \brief The QDeclarativeGestureArea class provides simple gesture handling. + +*/ +QDeclarativeGestureArea::QDeclarativeGestureArea(QDeclarativeItem *parent) : + QDeclarativeItem(*(new QDeclarativeGestureAreaPrivate), parent) +{ + setAcceptedMouseButtons(Qt::LeftButton); + setAcceptTouchEvents(true); +} + +QDeclarativeGestureArea::~QDeclarativeGestureArea() +{ +} + +QByteArray +QDeclarativeGestureAreaParser::compile(const QList &props) +{ + QByteArray rv; + QDataStream ds(&rv, QIODevice::WriteOnly); + + for(int ii = 0; ii < props.count(); ++ii) + { + QString propName = QString::fromUtf8(props.at(ii).name()); + Qt::GestureType type; + + if (propName == QLatin1String("onTap")) { + type = Qt::TapGesture; + } else if (propName == QLatin1String("onTapAndHold")) { + type = Qt::TapAndHoldGesture; + } else if (propName == QLatin1String("onPan")) { + type = Qt::PanGesture; + } else if (propName == QLatin1String("onPinch")) { + type = Qt::PinchGesture; + } else if (propName == QLatin1String("onSwipe")) { + type = Qt::SwipeGesture; + } else if (propName == QLatin1String("onGesture")) { + type = Qt::CustomGesture; + } else { + error(props.at(ii), QDeclarativeGestureArea::tr("Cannot assign to non-existent property \"%1\"").arg(propName)); + return QByteArray(); + } + + QList values = props.at(ii).assignedValues(); + + for (int i = 0; i < values.count(); ++i) { + const QVariant &value = values.at(i); + + if (value.userType() == qMetaTypeId()) { + error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: nested objects not allowed")); + return QByteArray(); + } else if (value.userType() == qMetaTypeId()) { + error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: syntax error")); + return QByteArray(); + } else { + QDeclarativeParser::Variant v = qvariant_cast(value); + if (v.isScript()) { + ds << propName; + ds << int(type); + ds << v.asScript(); + } else { + error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: script expected")); + return QByteArray(); + } + } + } + } + + return rv; +} + +void QDeclarativeGestureAreaParser::setCustomData(QObject *object, + const QByteArray &data) +{ + QDeclarativeGestureArea *ga = static_cast(object); + ga->d_func()->data = data; +} + + +void QDeclarativeGestureArea::connectSignals() +{ + Q_D(QDeclarativeGestureArea); + if (!d->componentcomplete) + return; + + QDataStream ds(d->data); + while (!ds.atEnd()) { + QString propName; + ds >> propName; + int gesturetype; + ds >> gesturetype; + QString script; + ds >> script; + QDeclarativeExpression *exp = new QDeclarativeExpression(qmlContext(this), script, 0); + d->bindings.insert(Qt::GestureType(gesturetype),exp); + grabGesture(Qt::GestureType(gesturetype)); + } +} + +void QDeclarativeGestureArea::componentComplete() +{ + Q_D(QDeclarativeGestureArea); + d->componentcomplete=true; + connectSignals(); +} + +QGesture *QDeclarativeGestureArea::gesture() const +{ + Q_D(const QDeclarativeGestureArea); + return d->gesture; +} + +bool QDeclarativeGestureArea::sceneEvent(QEvent *event) +{ + Q_D(QDeclarativeGestureArea); + if (event->type() == QEvent::Gesture) + return d->gestureEvent(static_cast(event)); + return QDeclarativeItem::sceneEvent(event); +} + +bool QDeclarativeGestureAreaPrivate::gestureEvent(QGestureEvent *event) +{ + bool accept = true; + for (Bindings::Iterator it = bindings.begin(); it != bindings.end(); ++it) { + if ((gesture = event->gesture(it.key()))) { + it.value()->value(); + event->setAccepted(true); // XXX only if value returns true? + } + } + return accept; +} + +QT_END_NAMESPACE diff --git a/src/imports/gestures/qdeclarativegesturearea_p.h b/src/imports/gestures/qdeclarativegesturearea_p.h new file mode 100644 index 0000000..8d7fdfe --- /dev/null +++ b/src/imports/gestures/qdeclarativegesturearea_p.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVEGESTUREAREA_H +#define QDECLARATIVEGESTUREAREA_H + +#include +#include +#include + +#include +#include +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QDeclarativeBoundSignal; +class QDeclarativeContext; +class QDeclarativeGestureAreaPrivate; +class Q_DECLARATIVE_EXPORT QDeclarativeGestureArea : public QDeclarativeItem +{ + Q_OBJECT + + Q_PROPERTY(QGesture *gesture READ gesture) + + Q_INTERFACES(QDeclarativeParserStatus) + +public: + QDeclarativeGestureArea(QDeclarativeItem *parent=0); + ~QDeclarativeGestureArea(); + + QGesture *gesture() const; + +protected: + bool sceneEvent(QEvent *event); + +private: + void connectSignals(); + void componentComplete(); + friend class QDeclarativeGestureAreaParser; + + Q_DISABLE_COPY(QDeclarativeGestureArea) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativeGestureArea) +}; + +class QDeclarativeGestureAreaParser : public QDeclarativeCustomParser +{ +public: + virtual QByteArray compile(const QList &); + virtual void setCustomData(QObject *, const QByteArray &); +}; + + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativeGestureArea) + +QT_END_HEADER + +#endif diff --git a/src/imports/gestures/qmldir b/src/imports/gestures/qmldir new file mode 100644 index 0000000..9d9d587 --- /dev/null +++ b/src/imports/gestures/qmldir @@ -0,0 +1 @@ +plugin gesturesqmlplugin -- cgit v0.12