diff options
author | Lorn Potter <lorn.potter@gmail.com> | 2012-09-12 13:51:23 +1000 |
---|---|---|
committer | Lorn Potter <lorn.potter@gmail.com> | 2012-11-07 22:15:49 +0100 |
commit | 182969371548eb3c17f7fe605898cdb2588b9226 (patch) | |
tree | 5fd304b6bdbba5c9402abe86d51976bdf7aa16ee | |
parent | cfe9f4be6f2c5529d42686fa67650bae43395e38 (diff) |
backport sensor gestures from qt 5
Change-Id: I0ce06c6859d471b4cada6e7ea908f62d32879ca2
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@digia.com>
54 files changed, 5647 insertions, 1 deletions
@@ -718,6 +718,7 @@ for module in $MOBILITY_MODULES; do ;; sensors) $relpath/bin/syncheaders $shadowpath/include/QtSensors $relpath/src/sensors + $relpath/bin/syncheaders $shadowpath/include/QtSensors $relpath/src/sensors/gestures ;; feedback) $relpath/bin/syncheaders $shadowpath/include/QtFeedback $relpath/src/feedback diff --git a/examples/sensors/shakeit/audio/loopy2a_mono.wav b/examples/sensors/shakeit/audio/loopy2a_mono.wav Binary files differnew file mode 100644 index 0000000000..7d76cb6560 --- /dev/null +++ b/examples/sensors/shakeit/audio/loopy2a_mono.wav diff --git a/examples/sensors/shakeit/audio/phone.wav b/examples/sensors/shakeit/audio/phone.wav Binary files differnew file mode 100644 index 0000000000..884e9ac25e --- /dev/null +++ b/examples/sensors/shakeit/audio/phone.wav diff --git a/examples/sensors/shakeit/content/triangle.png b/examples/sensors/shakeit/content/triangle.png Binary files differnew file mode 100644 index 0000000000..f366537eae --- /dev/null +++ b/examples/sensors/shakeit/content/triangle.png diff --git a/examples/sensors/shakeit/content/triangle2.png b/examples/sensors/shakeit/content/triangle2.png Binary files differnew file mode 100644 index 0000000000..9318d5a370 --- /dev/null +++ b/examples/sensors/shakeit/content/triangle2.png diff --git a/examples/sensors/shakeit/content/triangle3.png b/examples/sensors/shakeit/content/triangle3.png Binary files differnew file mode 100644 index 0000000000..4cc5ccdaf8 --- /dev/null +++ b/examples/sensors/shakeit/content/triangle3.png diff --git a/examples/sensors/shakeit/icon.png b/examples/sensors/shakeit/icon.png Binary files differnew file mode 100644 index 0000000000..d8f21546fd --- /dev/null +++ b/examples/sensors/shakeit/icon.png diff --git a/examples/sensors/shakeit/main.cpp b/examples/sensors/shakeit/main.cpp new file mode 100644 index 0000000000..5f06aede32 --- /dev/null +++ b/examples/sensors/shakeit/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtGui/QApplication> +#include <QtDeclarative/QDeclarativeView> + + +int main(int argc, char **argv) +{ + QApplication app(argc,argv); + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("shakeit.qml")); + view.show(); + return app.exec(); +} + diff --git a/examples/sensors/shakeit/shakeit.pro b/examples/sensors/shakeit/shakeit.pro new file mode 100644 index 0000000000..a5b97ba69e --- /dev/null +++ b/examples/sensors/shakeit/shakeit.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET = shakeit +QT += declarative +SOURCES = main.cpp + +app.files = \ + $$files(*.qml) \ + icon.png \ + audio \ + content + +target.path = $$[QT_INSTALL_EXAMPLES]/qtsensors/shakeit +app.path = $$[QT_INSTALL_EXAMPLES]/qtsensors/shakeit +INSTALLS += target app diff --git a/examples/sensors/shakeit/shakeit.qml b/examples/sensors/shakeit/shakeit.qml new file mode 100644 index 0000000000..019b35b825 --- /dev/null +++ b/examples/sensors/shakeit/shakeit.qml @@ -0,0 +1,321 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt 4.7 +import QtMobility.sensors 1.1 +import QtMultimediaKit 1.1 + +Rectangle { + id: window + width: 320 + height: 480 + + state: "default" + + Audio { + id :phone + source: "audio/phone.wav" //mono + } + Audio { + id :loopy2a_mono + source: "audio/loopy2a_mono.wav" //mono + } + + Text { + id: label + text: qsTr("Shake to rotate triangles") + y: parent.height / 4 + anchors.horizontalCenter: parent.horizontalCenter + } + Image { + id: triangle1 + smooth: true + source: "content/triangle.png" + x: parent.width / 2 - (triangle1.width / 2) + y: parent.height / 2 - (triangle1.height); + Behavior on x { SmoothedAnimation { velocity: 200 } } + Behavior on y { SmoothedAnimation { velocity: 200 } } + transform: Rotation { + id: myRot + } + } + Image { + id: triangle2 + smooth: true + source: "content/triangle2.png" + x: parent.width / 2 - (triangle1.width + triangle2.width / 2) + y: parent.height / 2 + (triangle2.height / 2); + Behavior on x { SmoothedAnimation { velocity: 200 } } + Behavior on y { SmoothedAnimation { velocity: 200 } } + } + Image { + id: triangle3 + smooth: true + source: "content/triangle3.png" + x: parent.width / 2 + (triangle1.width / 2) + y: parent.height / 2 + (triangle3.height / 2); + + Behavior on x { SmoothedAnimation { velocity: 200 } } + Behavior on y { SmoothedAnimation { velocity: 200 } } + } + + states: [ + State { + name: "rotated" + PropertyChanges { target: triangle1; rotation: 180 } + PropertyChanges { target: triangle2; rotation: 90 } + PropertyChanges { target: triangle3; rotation: 270 } + }, + State { + name: "default" + PropertyChanges { target: triangle1; rotation: 0; + x: parent.width / 2 - (triangle1.width / 2) + y: parent.height / 2 - (triangle1.height); + } + PropertyChanges { target: triangle2; rotation: 0; + x: parent.width / 2 - (triangle1.width + triangle2.width / 2) + y: parent.height / 2 + (triangle2.height / 2); + } + PropertyChanges { target: triangle3; rotation: 0; + x: parent.width / 2 + (triangle1.width / 2) + y: parent.height / 2 + (triangle3.height / 2); + } + }, + State { + name: "whipped" + PropertyChanges { target: triangle1; rotation: 0; x:0; } + PropertyChanges { target: triangle2; rotation: 0; x:0; y:triangle1.x + triangle1.height; } + PropertyChanges { target: triangle3; rotation: 0; x:0; + y: triangle2.y + triangle2.height; } + }, + State { + name: "twistedR" + PropertyChanges { target: triangle1; rotation: 270; + x:window.width - triangle1.width; + } + PropertyChanges { target: triangle2; rotation: 180; + x:window.width - triangle2.width; + } + PropertyChanges { target: triangle3; rotation: 90; + x:window.width - triangle3.width; + y:triangle2.y + triangle2.height; + } + }, + State { + name: "twistedL" + PropertyChanges { target: triangle1; rotation: 270; + x:0; + } + PropertyChanges { target: triangle2; rotation: 180; + x:0; + } + PropertyChanges { target: triangle3; rotation: 90; + x:0; + y:triangle2.y + triangle2.height; + } + }, + State { + name: "covered" + PropertyChanges { target: triangle1; rotation: 0; + x: window.width / 3 - triangle1.width / 2; + y: window.height - triangle1.height; + } + PropertyChanges { target: triangle2; rotation: 0; + x: window.width / 2 - triangle2.width / 2; // middle + y: window.height - triangle2.height; + } + PropertyChanges { target: triangle3; rotation: 0; + x: (window.width / 3 + window.width / 3) - triangle3.width / 2; + y: window.height - triangle3.height; + } + }, + State { + name: "hovered" + PropertyChanges { target: triangle1; rotation: 90; + x: window.width / 3 - triangle1.width / 2; + y: triangle1.height; + } + PropertyChanges { target: triangle2; rotation: 270; + x: window.width / 2 - triangle2.width / 2; // middle + y: triangle2.height; + } + PropertyChanges { target: triangle3; rotation: 195; + x: (window.width / 3 + window.width / 3) - triangle3.width / 2; + y: triangle3.height + } + }, + State { + name :"slammed" + PropertyChanges { target: triangle1; rotation: 0; + x: 0; + y: 0 + 30 + } + PropertyChanges { target: triangle2; rotation: 0; + x: window.width - triangle2.width; + y: 0 + 30 + ;} + PropertyChanges { target: triangle3; rotation: 0; + x: window.width / 2 - triangle3.width / 2; + y:window.height - triangle3.height; + } + }, + State { + name: "doubletapped" + PropertyChanges { target: triangle1; rotation: 114; + transformOrigin: Item.BottomLeft + } + PropertyChanges { target: triangle2; rotation: 120; + transformOrigin: Item.BottomLeft + } + } + ] + + + transitions: [ + Transition { + + ParallelAnimation { + NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce;duration: 2000; } + RotationAnimation { id: t1Rotation; target: triangle1; duration: 1000; + direction: RotationAnimation.Clockwise } + RotationAnimation { id: t2Rotation; target: triangle2; duration: 2000; + direction: RotationAnimation.Counterclockwise } + RotationAnimation { id: t3Rotation; target: triangle3; duration: 2000; + direction: RotationAnimation.Clockwise } + } + + }, Transition { + to: "slammed" + SequentialAnimation { + NumberAnimation { properties: "x"; easing.type: Easing.OutBounce;duration: 500; } + + } + }, Transition { + to: "doubletapped" + SequentialAnimation { + PropertyAction { target: triangle1; property: "transformOrigin" } + PropertyAction { target: triangle2; property: "transformOrigin" } + NumberAnimation { target: triangle1; properties: "rotation"; easing.type: Easing.OutBounce;duration: 500; } + NumberAnimation { target: triangle2; properties: "rotation"; easing.type: Easing.OutBounce;duration: 1500; } + } + }, Transition { + from: "doubletapped" + SequentialAnimation { + NumberAnimation { properties: "rotation"; easing.type: Easing.OutBounce;duration: 1500; } + PropertyAction { target: triangle1; property: "transformOrigin" } + PropertyAction { target: triangle2; property: "transformOrigin" } + } + } + ] + +//! [1] + SensorGesture { +//! [1] + id: sensorGesture +//! [3] + enabled: true +//! [3] +//! [2] + gestures : ["QtSensors.shake", "QtSensors.whip", "QtSensors.twist", "QtSensors.cover", + "QtSensors.hover", "QtSensors.turnover", "QtSensors.pickup", "QtSensors.slam" , "QtSensors.doubletap"] +//! [2] +//! [4] + onDetected:{ + console.debug(gesture) + label.text = gesture + + if (gesture == "shake") { + window.state == "rotated" ? window.state = "default" : window.state = "rotated" + timer.start() + } + if (gesture == "whip") { + window.state == "whipped" ? window.state = "default" : window.state = "whipped" + timer.start() + } + if (gesture == "twistRight") { + window.state == "twistedR" ? window.state = "default" : window.state = "twistedR" + timer.start() + } + if (gesture == "twistLeft") { + window.state == "twistedL" ? window.state = "default" : window.state = "twistedL" + timer.start() + } + if (gesture == "cover") { + window.state == "covered" ? window.state = "default" : window.state = "covered" + timer.start() + } + if (gesture == "hover") { + window.state == "hovered" ? window.state = "default" : window.state = "hovered" + timer.start() + } + if (gesture == "turnover") { + window.state = "default" + loopy2a_mono.play(); + timer.start() + } + if (gesture == "pickup") { + window.state = "default" + phone.play() + timer.start() + } + if (gesture == "slam") { + window.state == "slammed" ? window.state = "default" : window.state = "slammed" + timer.start() + } + if (gesture == "doubletap") { + window.state == "doubletapped" ? window.state = "default" : window.state = "doubletapped" + timer.start() + } + } +//! [4] + } + Timer { + id: timer + running: false + repeat: false + interval: 3000 + onTriggered: { + console.log("timer triggered") + window.state = "default" + label.text = "Try another gesture" + } + } +} diff --git a/plugins/plugins.pro b/plugins/plugins.pro index d3bcf667b9..9f21ec4cf1 100644 --- a/plugins/plugins.pro +++ b/plugins/plugins.pro @@ -4,7 +4,7 @@ TEMPLATE = subdirs contains(mobility_modules,contacts): SUBDIRS += contacts contains(mobility_modules,multimedia): SUBDIRS += multimedia -contains(mobility_modules,sensors): SUBDIRS += sensors +contains(mobility_modules,sensors): SUBDIRS += sensors sensorgestures contains(mobility_modules,feedback): SUBDIRS += feedback contains(mobility_modules,organizer): SUBDIRS += organizer contains(mobility_modules,location): SUBDIRS += landmarks diff --git a/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.cpp new file mode 100644 index 0000000000..a092f35979 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.cpp @@ -0,0 +1,145 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qcoversensorgesturerecognizer.h" +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +QCoverSensorGestureRecognizer::QCoverSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent), + orientationReading(0), proximityReading(0),active(0), detecting(0) +{ +} + +QCoverSensorGestureRecognizer::~QCoverSensorGestureRecognizer() +{ +} + +void QCoverSensorGestureRecognizer::create() +{ + timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(750); +} + +QString QCoverSensorGestureRecognizer::id() const +{ + return QString("QtSensors.cover"); +} + +bool QCoverSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Proximity)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(proximityReadingChanged(QProximityReading *)), + this,SLOT(proximityChanged(QProximityReading *))); + + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Proximity); + active = false; + } + } else { + active = false; + } + return active; +} + +bool QCoverSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Proximity); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(proximityReadingChanged(QProximityReading *)), + this,SLOT(proximityChanged(QProximityReading *))); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + active = false; + timer->stop(); + return active; +} + +bool QCoverSensorGestureRecognizer::isActive() +{ + return active; +} + +void QCoverSensorGestureRecognizer::proximityChanged(QProximityReading *reading) +{ + if (orientationReading == 0) + return; + + proximityReading = reading->close(); + + // look at case of face up->face down->face up. + if (orientationReading->orientation() == QOrientationReading::FaceUp + && proximityReading) { + if (!timer->isActive()) { + timer->start(); + detecting = true; + } + } + lastTs = reading->timestamp(); +} + +void QCoverSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + orientationReading = reading; +} + +void QCoverSensorGestureRecognizer::timeout() +{ + if ((orientationReading->orientation() == QOrientationReading::FaceUp) + && proximityReading) { + Q_EMIT cover(); + Q_EMIT detected("cover"); + detecting = false; + } +} + + +#include "moc_qcoversensorgesturerecognizer.cpp" +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.h new file mode 100644 index 0000000000..34969027ef --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qcoversensorgesturerecognizer.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QCOVERSENSORGESTURERECOGNIZER_H +#define QCOVERSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <QTimer> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +class QCoverSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QCoverSensorGestureRecognizer(QObject *parent = 0); + ~QCoverSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void cover(); + +private slots: + void proximityChanged(QProximityReading *reading); + void orientationReadingChanged(QOrientationReading *reading); + void timeout(); + +private: + + QOrientationReading *orientationReading; + bool proximityReading; + + QTimer *timer; + QtSensorGestureSensorHandler *handler; + bool active; + bool detecting; + qreal lastTs; +}; + +QTM_END_NAMESPACE +#endif // QCOVERSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.cpp new file mode 100644 index 0000000000..a8855f77ac --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QTapSensor> +#include "qdoubletapsensorgesturerecognizer.h" +QTM_BEGIN_NAMESPACE + +QDoubleTapSensorGestureRecognizer::QDoubleTapSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent) +{ +} + +QDoubleTapSensorGestureRecognizer::~QDoubleTapSensorGestureRecognizer() +{ +} + +void QDoubleTapSensorGestureRecognizer::create() +{ +} + + +QString QDoubleTapSensorGestureRecognizer::id() const +{ + return QString("QtSensors.doubletap"); +} + +bool QDoubleTapSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Tap)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(dTabReadingChanged(QTapReading *)), + this,SLOT(tapChanged(QTapReading *))); + } else { + active = false; + } + return active; +} + +bool QDoubleTapSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Tap); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(dTabReadingChanged(QTapReading *)), + this,SLOT(tapChanged(QTapReading *))); + active = false; + return active; +} + +bool QDoubleTapSensorGestureRecognizer::isActive() +{ + return active; +} + +void QDoubleTapSensorGestureRecognizer::tapChanged(QTapReading *reading) +{ + if (reading->isDoubleTap()) { + Q_EMIT doubletap(); + Q_EMIT detected("doubletap"); + } +} + + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.h new file mode 100644 index 0000000000..a99a0f4b25 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qdoubletapsensorgesturerecognizer.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDOUBLETAPSENSORGESTURERECOGNIZER_H +#define QDOUBLETAPSENSORGESTURERECOGNIZER_H + +#include <qsensors.h> + +#include <qsensorgesturerecognizer.h> +//#include <QTapSensor> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +class QDoubleTapSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QDoubleTapSensorGestureRecognizer(QObject *parent = 0); + ~QDoubleTapSensorGestureRecognizer(); + + void create(); + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void doubletap(); + +private slots: + void tapChanged(QTapReading *reading); + +private: + QTapSensor *tapSensor; + bool active; + +}; +QTM_END_NAMESPACE +#endif // QDOUBLETAPSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.cpp new file mode 100644 index 0000000000..65f232a0aa --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.cpp @@ -0,0 +1,212 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qhoversensorgesturerecognizer.h" +#include <math.h> + +QTM_BEGIN_NAMESPACE + +QHoverSensorGestureRecognizer::QHoverSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent), + orientationReading(0), + hoverOk(0), detecting(0), active(0), initialReflectance(0), useHack(0) +{ +} + +QHoverSensorGestureRecognizer::~QHoverSensorGestureRecognizer() +{ +} + +void QHoverSensorGestureRecognizer::create() +{ + timer2 = new QTimer(this); + connect(timer2,SIGNAL(timeout()),this,SLOT(timeout2())); + timer2->setSingleShot(true); + timer2->setInterval(3000); +} + +QString QHoverSensorGestureRecognizer::id() const +{ + return QString("QtSensors.hover"); +} + +bool QHoverSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::IrProximity)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QIRProximityReading *)), + this,SLOT(irProximityReadingChanged(QIRProximityReading *))); + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::IrProximity); + active = false; + } + } else { + active = false; + } + + detecting = false; + detectedHigh = 0; + initialReflectance = 0; + useHack = false; + return active; +} + +bool QHoverSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::IrProximity); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QProximityReading *)), + this,SLOT(irProximityReadingChanged(QProximityReading *))); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + active = false; + return active; +} + +bool QHoverSensorGestureRecognizer::isActive() +{ + return active; +} + + +void QHoverSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + orientationReading = reading; +} + +void QHoverSensorGestureRecognizer::irProximityReadingChanged(QProximityReading *reading) +{ +#warning FIXME +// reflectance = reading->reflectance(); +// if (reflectance == 0) +// return; + +// if (initialReflectance == 0) { +// initialReflectance = reflectance; +// } + +// if (initialReflectance > .2) { +// useHack = true; +// initialReflectance -= .1; +// } +// if (useHack) +// reflectance -= .1; + +// if (detecting && !hoverOk) { +// detectedHigh = qMax(detectedHigh, reflectance); +// } + +// if (reflectance > 0.4) { +// // if close stop detecting +// hoverOk = false; +// detecting = false; +// detectedHigh = 0; +// } + + +// qreal detectedPercent = 100 - (detectedHigh / reflectance * 100); + +// qint16 percentCheck; +// if (useHack) +// percentCheck = -60; +// else +// percentCheck = -101; + +// if (!detecting +// && checkForHovering()) { +// detecting = true; +// detectedHigh = reflectance; +// } else if (detecting +// && detectedPercent < percentCheck +// && !checkForHovering()) { +// // went light again after 1 seconds +// Q_EMIT hover(); +// Q_EMIT detected("hover"); +// hoverOk = false; +// detecting = false; +// detectedHigh = 0; +// timer2->stop(); +// } +// if (detecting && reflectance < 0.2) { +// timeout(); +// } +} + +bool QHoverSensorGestureRecognizer::checkForHovering() +{ + if (orientationReading == 0) { + return false; + } + if (orientationReading->orientation() != QOrientationReading::FaceUp) + return false; + if ( (reflectance > 0.2 && reflectance < 0.4) + && (initialReflectance - reflectance) < -0.1) + return true; + + return false; +} + + +void QHoverSensorGestureRecognizer::timeout() +{ + if (checkForHovering()) { + hoverOk = true; + timer2->start(); + } else { + detecting = false; + detectedHigh = 0; + } +} + +void QHoverSensorGestureRecognizer::timeout2() +{ + detecting = false; + hoverOk = false; + detectedHigh = 0; +} + +#include "moc_qhoversensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.h new file mode 100644 index 0000000000..71b549b8ba --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QHOVERSENSORGESTURERECOGNIZER_H +#define QHOVERSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <QTimer> + +#include "qtsensorgesturesensorhandler.h" + + +QTM_BEGIN_NAMESPACE + +class QHoverSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QHoverSensorGestureRecognizer(QObject *parent = 0); + ~QHoverSensorGestureRecognizer(); + + void create(); + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void hover(); + +private slots: + void orientationReadingChanged(QOrientationReading *reading); + void irProximityReadingChanged(QProximityReading *reading); + void timeout(); + void timeout2(); +private: + QOrientationReading *orientationReading; + qreal reflectance; + + QTimer *timer2; + bool hoverOk; + bool detecting; + + qreal detectedHigh; + bool active; + qreal initialReflectance; + bool checkForHovering(); + bool useHack; + +}; +QTM_END_NAMESPACE +#endif // QHOVERSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp new file mode 100644 index 0000000000..1686099a99 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp @@ -0,0 +1,208 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qpickupsensorgesturerecognizer.h" + +#include <QtCore/qmath.h> + +#define RADIANS_TO_DEGREES 57.2957795 + +QTM_BEGIN_NAMESPACE + +QPickupSensorGestureRecognizer::QPickupSensorGestureRecognizer(QObject *parent) + : QSensorGestureRecognizer(parent) + , active(0) + , atRest(1) + , okToSignal(1) + , lastpitch(0) + , detecting(0) +{ +} + +QPickupSensorGestureRecognizer::~QPickupSensorGestureRecognizer() +{ +} + +void QPickupSensorGestureRecognizer::create() +{ + timer = new QTimer(this); + + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(750); + +} + +QString QPickupSensorGestureRecognizer::id() const +{ + return QString("QtSensors.pickup"); +} + +bool QPickupSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Accel)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + } else { + active = false; + } + return active; + +} + +bool QPickupSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), + this,SLOT(accelChanged(QAccelerometerReading *))); + active = false; + timer->stop(); + + return active; +} + +bool QPickupSensorGestureRecognizer::isActive() +{ + return active; +} + +#define PICKUP_BOTTOM_THRESHOLD 20 +#define PICKUP_TOP_THRESHOLD 87 + +void QPickupSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) +{ + accelReading = reading; + const qreal x = reading->x(); + const qreal xdiff = pXaxis - x; + const qreal y = reading->y(); + const qreal ydiff = pYaxis - y; + const qreal z = reading->z(); + const qreal zdiff = pZaxis - z; + + pitch = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; + + if (zList.count() > 5) + zList.removeLast(); + + if (qAbs(xdiff) < 0.7 && qAbs(ydiff) < .7 && qAbs(zdiff) < .7) { + atRest = true; + } else { + atRest = false; + } + + if (detectingNegativeList.count() > 5) + detectingNegativeList.removeLast(); + + if (!detecting) { + zList.insert(0,z); + } + if (detecting && z < 0) { + okToSignal = false; + detecting = false; + detectingNegativeList.insert(0,true); + atRest = true; + } + + if (!atRest && !detecting && (lastpitch - pitch < -PICKUP_BOTTOM_THRESHOLD)) { + detecting = true; + if (!timer->isActive()) { + timer->start(); + } + detectedPitchDifference = lastpitch - pitch; + lastpitch = pitch; + okToSignal = true; + } + + pXaxis = x; + pYaxis = y; + pZaxis = z; + if (atRest && !detecting) + lastpitch = pitch; +} + +void QPickupSensorGestureRecognizer::timeout() +{ + const qreal x = accelReading->x(); + const qreal y = accelReading->y(); + const qreal z = accelReading->z(); + + const qreal roll = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + + bool ok = true; + for (int i = 0; i < zList.count() - 1; i++) { + if (zList.at(i) < 0) { + ok = false; + } + } + + if (ok) { + for (int i = 0; i < detectingNegativeList.count() - 1; i++) { + if (detectingNegativeList.at(i) == true) { + ok = false; + } + } + } + + if (ok && detecting + && okToSignal + && qAbs(roll) < 10 + && (pitch < PICKUP_TOP_THRESHOLD + && pitch > PICKUP_BOTTOM_THRESHOLD) + && (y > 4.0 && y < 10) + && (z > 4.0 && z < 10)) { + Q_EMIT pickup(); + Q_EMIT detected("pickup"); + } + clear(); +} + + +void QPickupSensorGestureRecognizer::clear() +{ + okToSignal = false; + detecting = false; + detectingNegativeList.clear(); +} +#include "moc_qpickupsensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE + diff --git a/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h new file mode 100644 index 0000000000..c25a64da36 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QPICKUPSENSORGESTURERECOGNIZER_H +#define QPICKUPSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <QTimer> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +class QPickupSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QPickupSensorGestureRecognizer(QObject *parent = 0); + ~QPickupSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void pickup(); + +private slots: + void accelChanged(QAccelerometerReading *reading); + void timeout(); +private: + QAccelerometerReading *accelReading; + + QTimer *timer; + bool active; + bool atRest; + bool okToSignal; + + qreal pXaxis; + qreal pYaxis; + qreal pZaxis; + + qreal pitch; + qreal lastpitch; + qreal detectedPitchDifference; + bool detecting; + + QList <bool> detectingNegativeList; + QList <qreal> zList; + + void clear(); +}; +QTM_END_NAMESPACE +#endif // QPICKUPSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qshake2recognizer.cpp b/plugins/sensorgestures/qtsensors/qshake2recognizer.cpp new file mode 100644 index 0000000000..6eb5e14230 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qshake2recognizer.cpp @@ -0,0 +1,223 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDebug> +#include <QTimer> + +#include "qshake2recognizer.h" +#include <math.h> + + +QTM_BEGIN_NAMESPACE + +QShake2SensorGestureRecognizer::QShake2SensorGestureRecognizer(QObject *parent) + : QSensorGestureRecognizer(parent) + , active(0) + , shakeDirection(QShake2SensorGestureRecognizer::ShakeUndefined) + , shaking(0) + , shakeCount(0) +{ + timerTimeout = 250; +} + +QShake2SensorGestureRecognizer::~QShake2SensorGestureRecognizer() +{ +} + +void QShake2SensorGestureRecognizer::create() +{ + timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(timerTimeout); +} + +bool QShake2SensorGestureRecognizer::start() +{ + + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Accel)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + } else { + active = false; + } + prevData.x = 0; + prevData.y = 0; + prevData.z = 0; + shakeCount = 0; + shaking = false; + shakeDirection = QShake2SensorGestureRecognizer::ShakeUndefined; + return active; +} + +bool QShake2SensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + active = false; + timer->stop(); + return active; +} + +bool QShake2SensorGestureRecognizer::isActive() +{ + return active; +} + +QString QShake2SensorGestureRecognizer::id() const +{ + return QString("QtSensors.shake2"); +} + +#define NUMBER_SHAKES 3 +#define THRESHOLD 25 + +void QShake2SensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) +{ + const qreal x = reading->x(); + const qreal y = reading->y(); + const qreal z = reading->z(); + + currentData.x = x; + currentData.y = y; + currentData.z = z; + + if (qAbs(prevData.x - currentData.x) < 1 + && qAbs(prevData.y - currentData.y) < 1 + && qAbs(prevData.z - currentData.z) < 1) { + + prevData.x = currentData.x; + prevData.y = currentData.y; + prevData.z = currentData.z; + return; + } + + bool wasShake = checkForShake(prevData, currentData, THRESHOLD); + + if (!shaking && wasShake && + shakeCount == NUMBER_SHAKES) { + shaking = true; + shakeCount = 0; + + switch (shakeDirection) { + case QShake2SensorGestureRecognizer::ShakeLeft: + Q_EMIT shakeLeft(); + Q_EMIT detected("shakeLeft"); + break; + case QShake2SensorGestureRecognizer::ShakeRight: + Q_EMIT shakeRight(); + Q_EMIT detected("shakeRight"); + break; + case QShake2SensorGestureRecognizer::ShakeUp: + Q_EMIT shakeUp(); + Q_EMIT detected("shakeUp"); + break; + case QShake2SensorGestureRecognizer::ShakeDown: + Q_EMIT shakeDown(); + Q_EMIT detected("shakeDown"); + break; + default: + break; + }; + } else if (wasShake) { + + if (shakeCount == 0 && shakeDirection == QShake2SensorGestureRecognizer::ShakeUndefined) { + + const int xdiff = prevData.x - currentData.x; + const int ydiff = prevData.x - currentData.y; + + const int max = qMax(qAbs(ydiff), qAbs(xdiff)); + + if (max == qAbs(xdiff)) { + if (isNegative(xdiff)) + shakeDirection = QShake2SensorGestureRecognizer::ShakeLeft; + else + shakeDirection = QShake2SensorGestureRecognizer::ShakeRight; + + } else if (max == qAbs(ydiff)) { + if (isNegative(ydiff)) + shakeDirection = QShake2SensorGestureRecognizer::ShakeDown; + else + shakeDirection = QShake2SensorGestureRecognizer::ShakeUp; + } + } + shakeCount++; + if (shakeCount == 3) { + timer->start(); + } + } + + prevData.x = currentData.x; + prevData.y = currentData.y; + prevData.z = currentData.z; +} + +void QShake2SensorGestureRecognizer::timeout() +{ + shakeCount = 0; + shaking = false; + shakeDirection = QShake2SensorGestureRecognizer::ShakeUndefined; +} + +bool QShake2SensorGestureRecognizer::checkForShake(ShakeData prevSensorData, ShakeData currentSensorData, qreal threshold) +{ + const double deltaX = qAbs(prevSensorData.x - currentSensorData.x); + const double deltaY = qAbs(prevSensorData.y - currentSensorData.y); + const double deltaZ = qAbs(prevSensorData.z - currentSensorData.z); + + return (deltaX > threshold + || deltaY > threshold + || deltaZ > threshold); +} + +bool QShake2SensorGestureRecognizer::isNegative(qreal num) +{ + if (num < 0) + return true; + return false; +} + +#include "moc_qshake2recognizer.cpp" + + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qshake2recognizer.h b/plugins/sensorgestures/qtsensors/qshake2recognizer.h new file mode 100644 index 0000000000..121e5861a5 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qshake2recognizer.h @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSHAKERECOGNIZER_H +#define QSHAKERECOGNIZER_H + +#include <QDebug> +#include <QTimer> + +#include <qsensorgesturerecognizer.h> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +struct ShakeData { + qreal x; + qreal y; + qreal z; +}; + +class QShake2SensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT + +public: + + enum ShakeDirection { + ShakeUndefined = 0, + ShakeLeft, + ShakeRight, + ShakeUp, + ShakeDown + }; + + QShake2SensorGestureRecognizer(QObject *parent = 0); + ~QShake2SensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + + QTimer *timer; + int timerTimeout; + + +Q_SIGNALS: + void shakeLeft(); + void shakeRight(); + void shakeUp(); + void shakeDown(); + +private slots: + void accelChanged(QAccelerometerReading *reading); + void timeout(); + + +private: + QAccelerometerReading *accelReading; + + bool active; + + ShakeDirection shakeDirection; + + ShakeData prevData; + ShakeData currentData; + + bool checkForShake(ShakeData prevSensorData, ShakeData currentSensorData, qreal threshold); + bool shaking; + int shakeCount; + int threshold; + + bool isNegative(qreal num); +}; +QTM_END_NAMESPACE +#endif // QSHAKERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp new file mode 100644 index 0000000000..266d608713 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qslamgesturerecognizer.h" +#include "qtsensorgesturesensorhandler.h" + +#include <QtCore/qmath.h> + +QTM_BEGIN_NAMESPACE + +QSlamSensorGestureRecognizer::QSlamSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent), + orientationReading(0), + accelRange(0), + active(0), + lastX(0), + lastY(0), + lastZ(0), + detectedX(0), + detecting(0), + accelX(0), + roll(0), + resting(0) +{ +} + +QSlamSensorGestureRecognizer::~QSlamSensorGestureRecognizer() +{ +} + +void QSlamSensorGestureRecognizer::create() +{ +} + + +QString QSlamSensorGestureRecognizer::id() const +{ + return QString("QtSensors.slam"); +} + +bool QSlamSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Accel)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + active = true; + accelRange = QtSensorGestureSensorHandler::instance()->accelRange; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + active = false; + } + } else { + active = false; + } + return active; +} + +bool QSlamSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + detecting = false; + restingList.clear(); + active = false; + return active; +} + +bool QSlamSensorGestureRecognizer::isActive() +{ + return active; +} + +void QSlamSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + orientationReading = reading; +} + +#define SLAM_DETECTION_FACTOR 0.3 // 11.7 +#define SLAM_RESTING_FACTOR 2.5 +#define SLAM_RESTING_COUNT 5 +#define SLAM_ZERO_FACTOR .02 + +void QSlamSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) +{ + const qreal x = reading->x(); + const qreal y = reading->y(); + const qreal z = reading->z(); + + + if (qAbs(lastX - x) < SLAM_RESTING_FACTOR + && qAbs(lastY - y) < SLAM_RESTING_FACTOR + && qAbs(lastZ - z) < SLAM_RESTING_FACTOR) { + resting = true; + } else { + resting = false; + } + + if (restingList.count() > SLAM_RESTING_COUNT) + restingList.removeLast(); + restingList.insert(0, resting); + + if (orientationReading == 0) { + return; + } + + const qreal difference = lastX - x; + + if (!detecting + && orientationReading->orientation() == QOrientationReading::TopUp + && resting + && hasBeenResting()) { + detectedX = x; + // start of gesture + detecting = true; + if (difference > 0) + wasNegative = false; + else + wasNegative = true; + restingList.clear(); + } + if (detecting + && qAbs(difference) > (accelRange * SLAM_DETECTION_FACTOR)) { + QTimer::singleShot(225,this,SLOT(doSlam())); + } + if (detecting && + (qAbs(difference) < SLAM_ZERO_FACTOR && qAbs(difference) > 0)) { + detecting = false; + } + lastX = x; + lastY = y; + lastZ = z; +} + +bool QSlamSensorGestureRecognizer::hasBeenResting() +{ + for (int i = 0; i < restingList.count() - 1; i++) { + if (!restingList.at(i)) { + return false; + } + } + return true; +} + +void QSlamSensorGestureRecognizer::doSlam() +{ + if (detecting && (orientationReading->orientation() == QOrientationReading::RightUp + || orientationReading->orientation() == QOrientationReading::LeftUp)) { + Q_EMIT slam(); + Q_EMIT detected("slam"); + restingList.clear(); + detecting = false; + } +} + +#include "moc_qslamgesturerecognizer.cpp" + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h new file mode 100644 index 0000000000..0529ca87ce --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QSLAMSENSORGESTURERECOGNIZER_H +#define QSLAMSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <qorientationsensor.h> +#include <qaccelerometer.h> + +QTM_BEGIN_NAMESPACE + +class QSlamSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QSlamSensorGestureRecognizer(QObject *parent = 0); + ~QSlamSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void slam(); + +private slots: + void accelChanged(QAccelerometerReading *reading); + void orientationReadingChanged(QOrientationReading *reading); + void doSlam(); + +private: + + QAccelerometer *accel; + QOrientationReading *orientationReading; + int accelRange; + bool active; + + bool wasNegative; + qreal lastX; + qreal lastY; + qreal lastZ; + qreal detectedX; + + bool detecting; + + qreal accelX; + qreal roll; + QList<qreal> restingList; + bool resting; + + bool hasBeenResting(); +}; + +QTM_END_NAMESPACE +#endif // QSLAMSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp b/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp new file mode 100644 index 0000000000..cb57a53290 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtPlugin> +#include <QStringList> +#include <QObject> + +#include "qtsensorgestureplugin.h" + +#include <qsensorgestureplugininterface.h> + +#include "qcoversensorgesturerecognizer.h" +#include "qtwistsensorgesturerecognizer.h" +//#include "qdoubletapsensorgesturerecognizer.h" +#include "qhoversensorgesturerecognizer.h" +#include "qpickupsensorgesturerecognizer.h" +#include "qshake2recognizer.h" +#include "qslamgesturerecognizer.h" +#include "qturnoversensorgesturerecognizer.h" +#include "qwhipsensorgesturerecognizer.h" + +QT_USE_NAMESPACE + +QTM_BEGIN_NAMESPACE + +QtSensorGesturePlugin::QtSensorGesturePlugin() +{ +} + +QtSensorGesturePlugin::~QtSensorGesturePlugin() +{ +} + +QStringList QtSensorGesturePlugin::supportedIds() const +{ + QStringList list; + list << "QtSensors.cover"; +// list << "QtSensors.doubletap"; + list << "QtSensors.hover"; + list << "QtSensors.pickup"; + list << "QtSensors.shake2"; + list << "QtSensors.slam"; + list << "QtSensors.turnover"; + list << "QtSensors.twist"; + list << "QtSensors.whip"; + return list; +} + +QList <QSensorGestureRecognizer *> QtSensorGesturePlugin::createRecognizers() +{ + QList <QSensorGestureRecognizer *> recognizers; + + recognizers.append(new QCoverSensorGestureRecognizer(this)); + +// recognizers.append(new QDoubleTapSensorGestureRecognizer(this)); + + recognizers.append(new QHoverSensorGestureRecognizer(this)); + + recognizers.append(new QPickupSensorGestureRecognizer(this)); + + recognizers.append(new QShake2SensorGestureRecognizer(this)); + + recognizers.append(new QSlamSensorGestureRecognizer(this)); + + recognizers.append(new QTurnoverSensorGestureRecognizer(this)); + + recognizers.append(new QWhipSensorGestureRecognizer(this)); + + recognizers.append(new QTwistSensorGestureRecognizer(this)); + + return recognizers; +} + +#include "moc_qtsensorgestureplugin.cpp" + +QTM_END_NAMESPACE + +Q_EXPORT_PLUGIN2(qtsensorgestures_qtsensors, QtMobility::QtSensorGesturePlugin) diff --git a/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.h b/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.h new file mode 100644 index 0000000000..a1e5010fda --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtsensorgestureplugin.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTSENSORGESTURESPLUGIN_H +#define QTSENSORGESTURESPLUGIN_H + +#include <qmobilityglobal.h> +#include <QObject> +#include <QStringList> + +#include <qsensorgestureplugininterface.h> +#include <qsensorgesturerecognizer.h> + +QTM_BEGIN_NAMESPACE + +class QtSensorGesturePlugin : public QObject, public QSensorGesturePluginInterface +{ + Q_OBJECT + Q_INTERFACES(QtMobility::QSensorGesturePluginInterface) +public: + explicit QtSensorGesturePlugin(); + ~QtSensorGesturePlugin(); + QList <QtMobility::QSensorGestureRecognizer *> createRecognizers(); + + QStringList gestureSignals() const; + QStringList supportedIds() const; + QString name() const { return "QtSensorGestures"; } +}; + +QTM_END_NAMESPACE +#endif // QTSENSORGESTURESPLUGIN_H diff --git a/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.cpp b/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.cpp new file mode 100644 index 0000000000..e12636da50 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDebug> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +QtSensorGestureSensorHandler::QtSensorGestureSensorHandler(QObject *parent) : + QObject(parent), + accel(0), orientation(0), proximity(0)/*, irProx(0)*//*,tapSensor(0)*/ +{ +} + +QtSensorGestureSensorHandler* QtSensorGestureSensorHandler::instance() +{ + static QtSensorGestureSensorHandler *instance = 0; + if (!instance) { + instance = new QtSensorGestureSensorHandler; + } + return instance; +} + +void QtSensorGestureSensorHandler::accelChanged() +{ + Q_EMIT accelReadingChanged(accel->reading()); +} + +void QtSensorGestureSensorHandler::orientationChanged() +{ + Q_EMIT orientationReadingChanged(orientation->reading()); +} + +void QtSensorGestureSensorHandler::proximityChanged() +{ + Q_EMIT proximityReadingChanged(proximity->reading()); +} + +//void QtSensorGestureSensorHandler::irProximityChanged() +//{ +// Q_EMIT irProximityReadingChanged(irProx->reading()); +//} + +void QtSensorGestureSensorHandler::doubletap() +{ + // Q_EMIT dTabReadingChanged(tapSensor->reading()); +} + +bool QtSensorGestureSensorHandler::startSensor(SensorGestureSensors sensor) +{ + bool ok = true; + switch (sensor) { + case Accel: + //accel + if (accel == 0x0) { + accel = new QAccelerometer(this); + ok = accel->connectToBackend(); + accel->setDataRate(50); + qoutputrangelist outputranges = accel->outputRanges(); + + if (outputranges.count() > 0) + accelRange = (int)(outputranges.at(0).maximum);//39 + else + accelRange = 39; //this should never happen + connect(accel,SIGNAL(readingChanged()),this,SLOT(accelChanged())); + } + if (ok && !accel->isActive()) + accel->start(); + break; + case Orientation: + //orientation + if (orientation == 0x0) { + orientation = new QOrientationSensor(this); + ok = orientation->connectToBackend(); + orientation->setDataRate(50); + connect(orientation,SIGNAL(readingChanged()),this,SLOT(orientationChanged())); + } + if (ok && !orientation->isActive()) + orientation->start(); + break; + case Proximity: + //proximity + if (proximity == 0x0) { + proximity = new QProximitySensor(this); + ok = proximity->connectToBackend(); + connect(proximity,SIGNAL(readingChanged()),this,SLOT(proximityChanged())); + } + if (ok && !proximity->isActive()) + proximity->start(); + break; + case IrProximity: +// //irproximity +// if (irProx == 0x0) { +// irProx = new QIRProximitySensor(this); +// ok = irProx->connectToBackend(); +// connect(irProx,SIGNAL(readingChanged()),this,SLOT(irProximityChanged())); +// } +// if (ok && !irProx->isActive()) +// irProx->start(); +// break; + case Tap: +// //dtap +// if (tapSensor == 0x0) { +// tapSensor = new QTapSensor(this); +// ok = tapSensor->connectToBackend(); +// connect(tapSensor,SIGNAL(readingChanged()),this,SLOT(doubletap())); +// } +// if (ok && !tapSensor->isActive()) +// tapSensor->start(); + break; + }; + int val = usedSensorsMap.value(sensor); + usedSensorsMap.insert(sensor,++val); + + return ok; +} + +void QtSensorGestureSensorHandler::stopSensor(SensorGestureSensors sensor) +{ + // qDebug() << __FUNCTION__ << sensor; + if (usedSensorsMap.value(sensor) == 0) + return; + int val = usedSensorsMap.value(sensor); + usedSensorsMap.insert(sensor,--val); + switch (sensor) { + case Accel: + //accel + if (usedSensorsMap.value(sensor) == 0) { + accel->stop(); + } + break; + case Orientation: + if (usedSensorsMap.value(sensor) == 0) { + orientation->stop(); + } + //orientation + break; + case Proximity: + if (usedSensorsMap.value(sensor) == 0) { + proximity->stop(); + } + //proximity + break; + case IrProximity: +// if (usedSensorsMap.value(sensor) == 0) { +// irProx->stop(); +// } +// //irproximity +// break; + case Tap: +// if (usedSensorsMap.value(sensor) == 0) { +// tapSensor->stop(); +// } +// //dtap + break; + }; +} + +#include "moc_qtsensorgesturesensorhandler.cpp" + + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.h b/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.h new file mode 100644 index 0000000000..5a8f68591f --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtsensorgesturesensorhandler.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTSENSORGESTURESENSORHANDLER_H +#define QTSENSORGESTURESENSORHANDLER_H + +#include <QObject> + +#include <qaccelerometer.h> +#include <qsensor.h> +#include <qorientationsensor.h> +#include <qproximitysensor.h> +//#include <qirproximitysensor.h> +//#include <QTapSensor> + +QTM_BEGIN_NAMESPACE + +class QtSensorGestureSensorHandler : public QObject +{ + Q_OBJECT + Q_ENUMS(SensorGestureSensors) +public: + explicit QtSensorGestureSensorHandler(QObject *parent = 0); + + enum SensorGestureSensors { + Accel = 0, + Orientation, + Proximity, + IrProximity, + Tap + }; + static QtSensorGestureSensorHandler *instance(); + qreal accelRange; + +public slots: + void accelChanged(); + void orientationChanged(); + void proximityChanged(); +// void irProximityChanged(); + void doubletap(); + + bool startSensor(SensorGestureSensors sensor); + void stopSensor(SensorGestureSensors sensor); + +Q_SIGNALS: + void accelReadingChanged(QAccelerometerReading *reading); + void orientationReadingChanged(QOrientationReading *reading); + void proximityReadingChanged(QProximityReading *reading); +// void irProximityReadingChanged(QIRProximityReading *reading); +// void dTabReadingChanged(QTapReading *reading); + +private: + QAccelerometer *accel; + QOrientationSensor *orientation; + QProximitySensor *proximity; +// QIRProximitySensor *irProx; +// QTapSensor *tapSensor; + + QMap<SensorGestureSensors, int> usedSensorsMap; + +}; + +QTM_END_NAMESPACE + +#endif // QTSENSORGESTURESENSORHANDLER_H diff --git a/plugins/sensorgestures/qtsensors/qtsensors.pro b/plugins/sensorgestures/qtsensors/qtsensors.pro new file mode 100644 index 0000000000..a34c18cc18 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtsensors.pro @@ -0,0 +1,46 @@ +PLUGIN_TYPE = sensorgestures + +CONFIG += plugin +TEMPLATE = lib + +TARGET = $$qtLibraryTarget(qtsensorgestures_qtsensors) + +include(../../../common.pri) +QT = core +CONFIG += mobility +MOBILITY += sensors + +INCLUDEPATH += $$QT_MOBILITY_SOURCE_TREE/src/sensors/gestures +INCLUDEPATH += $$QT_MOBILITY_SOURCE_TREE/src/sensors +# Input + +HEADERS +=\ + qcoversensorgesturerecognizer.h \ +# qdoubletapsensorgesturerecognizer.h \ + qhoversensorgesturerecognizer.h \ +# qfreefallsensorgesturerecognizer.h \ + qpickupsensorgesturerecognizer.h \ + qshake2recognizer.h \ + qslamgesturerecognizer.h \ + qturnoversensorgesturerecognizer.h \ + qtwistsensorgesturerecognizer.h \ + qwhipsensorgesturerecognizer.h \ + qtsensorgesturesensorhandler.h \ + qtsensorgestureplugin.h + +SOURCES += \ + qcoversensorgesturerecognizer.cpp \ + # qdoubletapsensorgesturerecognizer.cpp \ + qhoversensorgesturerecognizer.cpp \ + # qfreefallsensorgesturerecognizer.cpp \ + qpickupsensorgesturerecognizer.cpp \ + qshake2recognizer.cpp \ + qslamgesturerecognizer.cpp \ + qturnoversensorgesturerecognizer.cpp \ + qtwistsensorgesturerecognizer.cpp \ + qwhipsensorgesturerecognizer.cpp \ + qtsensorgesturesensorhandler.cpp \ + qtsensorgestureplugin.cpp + + +CONFIG+=strict_flags diff --git a/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.cpp new file mode 100644 index 0000000000..e1b9c6df7d --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qturnoversensorgesturerecognizer.h" +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +// turnover and put down i.e. facedown + +QTurnoverSensorGestureRecognizer::QTurnoverSensorGestureRecognizer(QObject *parent) : + QSensorGestureRecognizer(parent), + isClose(0) + , isFaceDown(0), active(0) +{ +} + +QTurnoverSensorGestureRecognizer::~QTurnoverSensorGestureRecognizer() +{ +} + +void QTurnoverSensorGestureRecognizer::create() +{ +} + +bool QTurnoverSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Proximity)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(proximityReadingChanged(QProximityReading *)), + this,SLOT(proximityChanged(QProximityReading *))); + + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Proximity); + active = false; + } + } else { + active = false; + } + return active; +} + +bool QTurnoverSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Proximity); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(proximityReadingChanged(QProximityReading *)), + this,SLOT(proximityChanged(QProximityReading *))); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + active = false; + + return active; +} + +bool QTurnoverSensorGestureRecognizer::isActive() +{ + return active; +} + +QString QTurnoverSensorGestureRecognizer::id() const +{ + return QString("QtSensors.turnover"); +} + +void QTurnoverSensorGestureRecognizer::proximityChanged(QProximityReading *reading) +{ + isClose = reading->close(); + isRecognized(); +} + +void QTurnoverSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + switch (reading->orientation()) { + case QOrientationReading::FaceDown: + { + isFaceDown = true; + isRecognized(); + } + break; + default: + isFaceDown = false; + break; + }; +} + +void QTurnoverSensorGestureRecognizer::isRecognized() +{ + if (isClose && isFaceDown) { + Q_EMIT turnover(); + Q_EMIT detected("turnover"); + } +} + +#include "moc_qturnoversensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE + diff --git a/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.h new file mode 100644 index 0000000000..d6f71eeec0 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qturnoversensorgesturerecognizer.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTURNOVERSENSORGESTURERECOGNIZER_H +#define QTURNOVERSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +class QTurnoverSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QTurnoverSensorGestureRecognizer(QObject *parent = 0); + ~QTurnoverSensorGestureRecognizer(); + void create(); + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void turnover(); + +private slots: + void orientationReadingChanged(QOrientationReading *reading); + void proximityChanged(QProximityReading *reading); + +private: + + bool isClose; + bool isFaceDown; + bool active; + + void isRecognized(); + QList<qreal> zList; + +}; +QTM_END_NAMESPACE +#endif // QTURNOVERSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp new file mode 100644 index 0000000000..d7edfb2d83 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp @@ -0,0 +1,294 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qtwistsensorgesturerecognizer.h" + +#include <QtCore/qmath.h> + +QTM_BEGIN_NAMESPACE + +#define RADIANS_TO_DEGREES 57.2957795 + +QTwistSensorGestureRecognizer::QTwistSensorGestureRecognizer(QObject *parent) + : QSensorGestureRecognizer(parent) + , orientationReading(0) + , active(0) + , detecting(0) + , checking(0) + , increaseCount(0) + , decreaseCount(0) + , lastAngle(0) + , detectedAngle(0) +{ +} + +QTwistSensorGestureRecognizer::~QTwistSensorGestureRecognizer() +{ +} + +void QTwistSensorGestureRecognizer::create() +{ +} + +QString QTwistSensorGestureRecognizer::id() const +{ + return QString("QtSensors.twist"); +} + +bool QTwistSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Accel)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + active = false; + } + } else { + + active = false; + } + return active; +} + +bool QTwistSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + + reset(); + orientationList.clear(); + active = false; + return active; +} + +bool QTwistSensorGestureRecognizer::isActive() +{ + return active; +} + +void QTwistSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + orientationReading = reading; + if (orientationList.count() == 3) + orientationList.removeFirst(); + + orientationList.append(reading->orientation()); + + if (orientationList.count() == 3 + && orientationList.at(2) == QOrientationReading::FaceUp + && (orientationList.at(1) == QOrientationReading::RightUp + || orientationList.at(1) == QOrientationReading::LeftUp)) { + checkTwist(); + } + + checkOrientation(); +} + +bool QTwistSensorGestureRecognizer::checkOrientation() +{ + if (orientationReading->orientation() == QOrientationReading::TopDown + || orientationReading->orientation() == QOrientationReading::FaceDown) { + reset(); + return false; + } + return true; +} + + +void QTwistSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) +{ + if (orientationReading == 0) + return; + + const qreal x = reading->x(); + const qreal y = reading->y(); + const qreal z = reading->z(); + + if (!detecting && !checking&& dataList.count() > 21) + dataList.removeFirst(); + + qreal angle = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + + if (qAbs(angle) > 2) { + if (detecting){ + if ((angle > 0 && angle < lastAngle) + || (angle < 0 && angle > lastAngle)) { + decreaseCount++; + } else { + if (decreaseCount > 0) + decreaseCount--; + } + } + + if (!detecting && ((angle > 0 && angle > lastAngle) + || (angle < 0 && angle < lastAngle)) + && ((angle > 0 && lastAngle > 0) + || (angle < 0 && lastAngle < 0))) { + increaseCount++; + } else + if (!detecting && increaseCount > 3 && qAbs(angle) > 30) { + decreaseCount = 0; + detecting = true; + detectedAngle = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; + } + } else { + increaseCount = 0; + increaseCount = 0; + } + + lastAngle = angle; + if (detecting && decreaseCount >= 4 && qAbs(angle) < 25) { + checkTwist(); + } + + twistAccelData data; + data.x = x; + data.y = y; + data.z = z; + + if (qAbs(x) > 1) + dataList.append(data); + + if (qAbs(z) > 15.0) { + reset(); + } + +} + +void QTwistSensorGestureRecognizer::checkTwist() +{ + checking = true; + int lastx = 0; + bool ok = false; + bool spinpoint = false; + + if (detectedAngle < 0) { + reset(); + return; + } + + //// check for orientation changes first + if (orientationList.count() < 2) + return; + + if (orientationList.count() > 2) + if (orientationList.at(0) == orientationList.at(2) + && (orientationList.at(1) == QOrientationReading::LeftUp + || orientationList.at(1) == QOrientationReading::RightUp)) { + ok = true; + if (orientationList.at(1) == QOrientationReading::RightUp) + detectedAngle = 1; + else + detectedAngle = -1; + } + + // now the manual increase/decrease count + if (!ok) { + if (increaseCount < 1 || decreaseCount < 3) + return; + + if (increaseCount > 6 && decreaseCount > 4) { + ok = true; + if (orientationList.at(1) == QOrientationReading::RightUp) + detectedAngle = 1; + else + detectedAngle = -1; + } + } + // now we're really grasping for anything + if (!ok) + for (int i = 0; i < dataList.count(); i++) { + twistAccelData curData = dataList.at(i); + if (!spinpoint && qAbs(curData.x) < 1) + continue; + if (curData.z >= 0 ) { + if (!spinpoint && (curData.x > lastx || curData.x < lastx) && curData.x - lastx > 1) { + ok = true; + } else if (spinpoint && (curData.x < lastx || curData.x > lastx)&& lastx - curData.x > 1) { + ok = true; + } else { + ok = false; + } + } else if (!spinpoint && curData.z < 0) { + spinpoint = true; + } else if (spinpoint && curData.z > 9) { + break; + } + + lastx = curData.x; + } + if (ok) { + if (detectedAngle > 0) { + Q_EMIT twistLeft(); + Q_EMIT detected("twistLeft"); + } else { + Q_EMIT twistRight(); + Q_EMIT detected("twistRight"); + } + } + reset(); +} + +void QTwistSensorGestureRecognizer::reset() +{ + detecting = false; + checking = false; + dataList.clear(); + increaseCount = 0; + decreaseCount = 0; + lastAngle = 0; +} + +#include "moc_qtwistsensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h new file mode 100644 index 0000000000..47cf4f6dfc --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QWFLICKSENSORGESTURERECOGNIZER_H +#define QWFLICKSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> +#include <qaccelerometer.h> +#include <qorientationsensor.h> +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +struct twistAccelData { + qreal x; + qreal y; + qreal z; +}; + +class QTwistSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QTwistSensorGestureRecognizer(QObject *parent = 0); + ~QTwistSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void twistLeft(); + void twistRight(); + +private slots: + void accelChanged(QAccelerometerReading *reading); + void orientationReadingChanged(QOrientationReading *reading); + void checkTwist(); + +private: + + QOrientationReading *orientationReading; + bool active; + bool detecting; + QList <twistAccelData> dataList; + bool checking; + void reset(); + bool checkOrientation(); + int increaseCount; + int decreaseCount; + qreal lastAngle; + QList <QOrientationReading::Orientation> orientationList; + + qreal detectedAngle; + +}; +QTM_END_NAMESPACE +#endif // QWFLICKSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp b/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp new file mode 100644 index 0000000000..37f29097a7 --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp @@ -0,0 +1,233 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qwhipsensorgesturerecognizer.h" +#include "qtsensorgesturesensorhandler.h" + +#include <QtCore/qmath.h> + +QTM_BEGIN_NAMESPACE + +QWhipSensorGestureRecognizer::QWhipSensorGestureRecognizer(QObject *parent) + : QSensorGestureRecognizer(parent), + orientationReading(0), + accelRange(0), + active(0), + lastX(0), + lastY(0), + lastZ(0), + detecting(0), + whipOk(0){ +} + +QWhipSensorGestureRecognizer::~QWhipSensorGestureRecognizer() +{ +} + +void QWhipSensorGestureRecognizer::create() +{ + timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(850); +} + +QString QWhipSensorGestureRecognizer::id() const +{ + return QString("QtSensors.whip"); +} + +bool QWhipSensorGestureRecognizer::start() +{ + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Accel)) { + if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) { + accelRange = QtSensorGestureSensorHandler::instance()->accelRange; + active = true; + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + connect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + } else { + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + active = false; + } + } else { + active = false; + } + return active; +} + +bool QWhipSensorGestureRecognizer::stop() +{ + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Accel); + QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation); + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading *)), + this,SLOT(orientationReadingChanged(QOrientationReading *))); + + disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading *)), + this,SLOT(accelChanged(QAccelerometerReading *))); + active = false; + timer->stop(); + + return active; +} + +bool QWhipSensorGestureRecognizer::isActive() +{ + return active; +} + +void QWhipSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) +{ + orientationReading = reading; +} + +#define WHIP_FACTOR -11.0 +#define WHIP_WIGGLE_FACTOR 0.35 + +void QWhipSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) +{ + const qreal x = reading->x(); + const qreal y = reading->y(); + qreal z = reading->z(); + + if (zList.count() > 4) + zList.removeLast(); + + qreal averageZ = 0; + Q_FOREACH (qreal az, zList) { + averageZ += az; + } +// averageZ += z; + averageZ /= zList.count(); + + zList.insert(0,z); + + if (orientationReading == 0) + return; + //// very hacky + if (orientationReading->orientation() == QOrientationReading::FaceUp) { + z = z - 9.8; + } + + const qreal diffX = lastX - x; + const qreal diffY = lastY - y; + + if (detecting && whipMap.count() > 5 && whipMap.at(5) == true) { + checkForWhip(); + } + + if (whipMap.count() > 5) + whipMap.removeLast(); + + if (negativeList.count() > 5) + negativeList.removeLast(); + + + if (z < WHIP_FACTOR + && qAbs(diffX) > -(accelRange * .1285)//-5.0115 + && qAbs(lastX) < 7 + && qAbs(x) < 7) { + whipMap.insert(0,true); + if (!detecting && !timer->isActive()) { + timer->start(); + detecting = true; + } + } else { + whipMap.insert(0,false); + } + + // check if shaking + if ((((x < 0 && lastX > 0) || (x > 0 && lastX < 0)) + && qAbs(diffX) > (accelRange * 0.7)) //27.3 + || (((y < 0 && lastY > 0) || (y > 0 && lastY < 0)) + && qAbs(diffY) > (accelRange * 0.7))) { + negativeList.insert(0,true); + } else { + negativeList.insert(0,false); + } + + lastX = x; + lastY = y; + lastZ = z; +} + +void QWhipSensorGestureRecognizer::timeout() +{ + detecting = false; +} + + +void QWhipSensorGestureRecognizer::checkForWhip() +{ + whipOk = false; + + int check = 0; + Q_FOREACH (qreal az, zList) { + if (az < -10) + check++; + } + if (check >= 4) + whipOk = true; + else + return; + if (whipOk) { + bool ok = true; + for (int i = 0; i < negativeList.count() - 1; i++) { + if (negativeList.at(i)) { + ok = false; + } + } + if (ok) { + Q_EMIT whip(); + Q_EMIT detected("whip"); + } + detecting = false; + whipMap.clear(); + timer->stop(); + } +} + +#include "moc_qwhipsensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE diff --git a/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h b/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h new file mode 100644 index 0000000000..476c16628d --- /dev/null +++ b/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef QWHIPSENSORGESTURERECOGNIZER_H +#define QWHIPSENSORGESTURERECOGNIZER_H + +#include <qsensorgesturerecognizer.h> + +#include "qtsensorgesturesensorhandler.h" + +QTM_BEGIN_NAMESPACE + +class QWhipSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT +public: + explicit QWhipSensorGestureRecognizer(QObject *parent = 0); + ~QWhipSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + +Q_SIGNALS: + void whip(); + +private slots: + void accelChanged(QAccelerometerReading *reading); + void orientationReadingChanged(QOrientationReading *reading); + void timeout(); + +private: + QOrientationReading *orientationReading; + QTimer *timer; + qreal accelRange; + bool active; + + qreal lastX; + qreal lastY; + qreal lastZ; + + bool detecting; + bool whipOk; + + QList<bool> whipMap; + + void checkForWhip(); + + QList <bool> negativeList; + + QList<qreal> zList; +}; + +QTM_END_NAMESPACE +#endif // QWHIPSENSORGESTURERECOGNIZER_H diff --git a/plugins/sensorgestures/sensorgestures.pro b/plugins/sensorgestures/sensorgestures.pro new file mode 100644 index 0000000000..89255fbf6e --- /dev/null +++ b/plugins/sensorgestures/sensorgestures.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS += qtsensors +include(../../common.pri) diff --git a/plugins/sensorgestures/shake/qshakerecognizer.cpp b/plugins/sensorgestures/shake/qshakerecognizer.cpp new file mode 100644 index 0000000000..fde0812a94 --- /dev/null +++ b/plugins/sensorgestures/shake/qshakerecognizer.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDebug> +#include <QTimer> + +#include "qshakerecognizer.h" + +QShakeSensorGestureRecognizer::QShakeSensorGestureRecognizer(QObject *parent) + : QSensorGestureRecognizer(parent) + , timerTimeout(450) + , active(0) + , shaking(0) + , shakeCount(0) +{ +} + +QShakeSensorGestureRecognizer::~QShakeSensorGestureRecognizer() +{ +} + +void QShakeSensorGestureRecognizer::create() +{ + accel = new QAccelerometer(this); + accel->connectToBackend(); + accel->setDataRate(50); + + qoutputrangelist outputranges = accel->outputRanges(); + + if (outputranges.count() > 0) + accelRange = (int)(outputranges.at(0).maximum *2) / 9.8; //approx range in g's + else + accelRange = 4; //this should never happen + + connect(accel,SIGNAL(readingChanged()),this,SLOT(accelChanged())); + timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this,SLOT(timeout())); + timer->setSingleShot(true); + timer->setInterval(timerTimeout); +} + +bool QShakeSensorGestureRecognizer::start() +{ + active = accel->start(); + return active; +} + +bool QShakeSensorGestureRecognizer::stop() +{ + accel->stop(); + active = accel->isActive(); + return !active; +} + +bool QShakeSensorGestureRecognizer::isActive() +{ + return active; +} + +QString QShakeSensorGestureRecognizer::id() const +{ + return QString("QtSensors.shake"); +} + +#define NUMBER_SHAKES 3 +#define THRESHOLD 25 + +void QShakeSensorGestureRecognizer::accelChanged() +{ + qreal x = accel->reading()->x(); + qreal y = accel->reading()->y(); + qreal z = accel->reading()->z(); + + currentData.x = x; + currentData.y = y; + currentData.z = z; + + if (qAbs(prevData.x - currentData.x) < 1 + && qAbs(prevData.y - currentData.y) < 1 + && qAbs(prevData.z - currentData.z) < 1) { + prevData.x = currentData.x; + prevData.y = currentData.y; + prevData.z = currentData.z; + return; + } + + bool wasShake = checkForShake(prevData, currentData, THRESHOLD); + if (!shaking && wasShake && + shakeCount >= NUMBER_SHAKES) { + shaking = true; + shakeCount = 0; + Q_EMIT shake(); + Q_EMIT detected("shake"); + + } else if (wasShake) { + + shakeCount++; + if (shakeCount > NUMBER_SHAKES) { + timer->start(); + } + } + + prevData.x = currentData.x; + prevData.y = currentData.y; + prevData.z = currentData.z; +} + +void QShakeSensorGestureRecognizer::timeout() +{ + shakeCount = 0; + shaking = false; +} + +bool QShakeSensorGestureRecognizer::checkForShake(AccelData prevSensorData, AccelData currentSensorData, qreal threshold) +{ + double deltaX = qAbs(prevSensorData.x - currentSensorData.x); + double deltaY = qAbs(prevSensorData.y - currentSensorData.y); + double deltaZ = qAbs(prevSensorData.z - currentSensorData.z); + + return (deltaX > threshold + || deltaY > threshold + || deltaZ > threshold); + (deltaY > threshold && deltaZ > threshold); +} + diff --git a/plugins/sensorgestures/shake/qshakerecognizer.h b/plugins/sensorgestures/shake/qshakerecognizer.h new file mode 100644 index 0000000000..2ebb79b963 --- /dev/null +++ b/plugins/sensorgestures/shake/qshakerecognizer.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSHAKERECOGNIZER_H +#define QSHAKERECOGNIZER_H + + +#include <QtSensors/QSensor> +#include <QtSensors/QAccelerometer> +#include <QtSensors/QAccelerometerFilter> +#include <QDebug> +#include <QTimer> + +#include <qsensorgesturerecognizer.h> + + struct AccelData { + qreal x; + qreal y; + qreal z; +}; + +class QShakeSensorGestureRecognizer : public QSensorGestureRecognizer +{ + Q_OBJECT + +public: + + QShakeSensorGestureRecognizer(QObject *parent = 0); + ~QShakeSensorGestureRecognizer(); + + void create(); + + QString id() const; + bool start(); + bool stop(); + bool isActive(); + + QTimer *timer; + int timerTimeout; + +Q_SIGNALS: + void shake(); + +private slots: + void accelChanged(); + void timeout(); +private: + QAccelerometer *accel; + bool active; + int accelRange; + + AccelData prevData; + AccelData currentData; + + bool checkForShake(AccelData prevSensorData, AccelData currentSensorData, qreal threshold); + bool shaking; + int shakeCount; + int threshold; + +}; + +#endif // QSHAKERECOGNIZER_H diff --git a/plugins/sensorgestures/shake/qshakesensorgestureplugin.cpp b/plugins/sensorgestures/shake/qshakesensorgestureplugin.cpp new file mode 100644 index 0000000000..6646b62bdf --- /dev/null +++ b/plugins/sensorgestures/shake/qshakesensorgestureplugin.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtPlugin> +#include <QStringList> +#include <QObject> + +#include "qshakesensorgestureplugin.h" + +#include <qsensorgestureplugininterface.h> + +#include "qshakerecognizer.h" + + +QShakeSensorGesturePlugin::QShakeSensorGesturePlugin() +{ +} + +QShakeSensorGesturePlugin::~QShakeSensorGesturePlugin() +{ +} + +QStringList QShakeSensorGesturePlugin::supportedIds() const +{ + QStringList list; + list << "QtSensors.shake"; + return list; +} + +QList <QSensorGestureRecognizer *> QShakeSensorGesturePlugin::createRecognizers() +{ + QList <QSensorGestureRecognizer *> recognizers; + + QSensorGestureRecognizer *sRec = new QShakeSensorGestureRecognizer(this); + recognizers.append(sRec); + + return recognizers; +} diff --git a/plugins/sensorgestures/shake/qshakesensorgestureplugin.h b/plugins/sensorgestures/shake/qshakesensorgestureplugin.h new file mode 100644 index 0000000000..8189e47553 --- /dev/null +++ b/plugins/sensorgestures/shake/qshakesensorgestureplugin.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSHAKESENSORGESTUREPLUGIN_H +#define QSHAKESENSORGESTUREPLUGIN_H + +#include <QObject> +#include <QStringList> + +#include <qsensorgestureplugininterface.h> + +class QShakeSensorGesturePlugin : public QObject, public QSensorGesturePluginInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "com.Nokia.QSensorGesturePluginInterface" FILE "plugin.json") + Q_INTERFACES(QSensorGesturePluginInterface) + +public: + explicit QShakeSensorGesturePlugin(); + ~QShakeSensorGesturePlugin(); + + QList <QSensorGestureRecognizer *> createRecognizers(); + + QStringList gestureSignals() const; + QStringList supportedIds() const; + QString name() const { return "ShakeGestures"; } +}; + +#endif // QSHAKESENSORGESTUREPLUGIN_H diff --git a/plugins/sensorgestures/shake/shake.pro b/plugins/sensorgestures/shake/shake.pro new file mode 100644 index 0000000000..685e1ba0dd --- /dev/null +++ b/plugins/sensorgestures/shake/shake.pro @@ -0,0 +1,21 @@ +load(qt_build_config) + +TARGET = qtsensorgestures_shakeplugin +QT = core sensors + +load(qt_plugin) + +DESTDIR = $$QT.sensors.plugins/sensorgestures + +# Input +HEADERS += qshakesensorgestureplugin.h \ + qshakerecognizer.h +SOURCES += qshakesensorgestureplugin.cpp \ + qshakerecognizer.cpp + +target.path += $$[QT_INSTALL_PLUGINS]/sensorgestures +INSTALLS += target + +OTHER_FILES += \ + plugin.json + diff --git a/src/sensors/gestures/qsensorgesture.cpp b/src/sensors/gestures/qsensorgesture.cpp new file mode 100644 index 0000000000..467b3a5daf --- /dev/null +++ b/src/sensors/gestures/qsensorgesture.cpp @@ -0,0 +1,226 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDir> +#include <QPluginLoader> +#include <QDebug> + +#include "qsensorgesture.h" +#include "qsensorgesture_p.h" +#include "qsensorgesturemanager.h" + +QTM_BEGIN_NAMESPACE + +/*! + \class QSensorGesture + \ingroup sensorgestures_main + \inmodule QtSensors + + \brief The QSensorGesture class represents one or more sensor gesture recognizers. + + + \sa QSensorGestureRecognizer + + You may use QSensorGestureManager to obtain the systems known sensor gesture ids. + + \sa QSensorGestureManager + */ + +/*! + \fn QSensorGesture::detected(QString gestureId) + Signals when the \a gestureId gesture has been recognized. + */ + +/*! + Constructs the sensor gesture, and initializes the \a ids list of recognizers, + with parent \a parent + */ +QSensorGesture::QSensorGesture(const QStringList &ids, QObject *parent) : + QObject(parent) +{ + d_ptr = new QSensorGesturePrivate(); + Q_FOREACH (const QString &id, ids) { + QSensorGestureRecognizer * rec = QSensorGestureManager::sensorGestureRecognizer(id); + if (rec != 0) { + d_ptr->m_sensorRecognizers.append(rec); + d_ptr->availableIds.append(id); + } else { + d_ptr->invalidIds.append(id); + //add to not available things + } + } + +// d_ptr->meta = 0; + +// QMetaObjectBuilder builder; +// builder.setSuperClass(&QObject::staticMetaObject); +// builder.setClassName("QSensorGesture"); + +// Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { +// Q_FOREACH (const QString &gesture, recognizer->gestureSignals()) { +// QMetaMethodBuilder b = builder.addSignal(gesture.toLatin1()); +// if (!d_ptr->localGestureSignals.contains(QLatin1String(b.signature()))) +// d_ptr->localGestureSignals.append(QLatin1String(b.signature())); +// } +// recognizer->createBackend(); +// } +// d_ptr->meta = builder.toMetaObject(); + + if (d_ptr->m_sensorRecognizers.count() > 0) { + d_ptr->valid = true; + } +} + +/*! + Destroy the QSensorGesture + */ +QSensorGesture::~QSensorGesture() +{ + stopDetection(); +// if (d_ptr->meta) +// free(d_ptr->meta); +// delete d_ptr; +} + +/*! + Returns the gesture recognizer ids that were found. + */ +QStringList QSensorGesture::validIds() const +{ + return d_ptr->availableIds; +} + +/*! + Returns the gesture recognizer ids that were not found. + */ +QStringList QSensorGesture::invalidIds() const +{ + return d_ptr->invalidIds; +} + +/*! + Starts the gesture detection routines in the recognizer. + */ +void QSensorGesture::startDetection() +{ + if (d_ptr->m_sensorRecognizers.count() < 1) + return; + if (d_ptr->isActive) + return; + + Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { + + Q_ASSERT(recognizer !=0); + + connect(recognizer,SIGNAL(detected(QString)), + this,SIGNAL(detected(QString)),Qt::UniqueConnection); + + //connect recognizer signals +// Q_FOREACH (QString method, recognizer->gestureSignals()) { +// method.prepend(QLatin1String("2")); +// connect(recognizer, method.toLatin1(), +// this, method.toLatin1(), Qt::UniqueConnection); +// } + + recognizer->startBackend(); + } + d_ptr->isActive = true; +} + +/*! + Stops the gesture detection routines. + */ +void QSensorGesture::stopDetection() +{ + if (d_ptr->m_sensorRecognizers.count() < 1) + return; + + if (!d_ptr->isActive) + return; + + Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { + disconnect(recognizer,SIGNAL(detected(QString)), + this,SIGNAL(detected(QString))); + //disconnect recognizer signals +// Q_FOREACH (QString method,recognizer->gestureSignals()) { +// method.prepend(QLatin1String("2")); +// disconnect(recognizer, method.toLatin1(), +// this, method.toLatin1()); +// } + + recognizer->stopBackend(); + } + d_ptr->isActive = false; +} + +/*! + Returns all the possible gestures signals that may be emitted. + */ +QStringList QSensorGesture::gestureSignals() const +{ + if (d_ptr->m_sensorRecognizers.count() > 0) { + return d_ptr->localGestureSignals; + } + return QStringList(); +} + +/*! + Returns whether this gesture is active or not. + */ + +bool QSensorGesture::isActive() +{ + return d_ptr->isActive; +} + +QSensorGesturePrivate::QSensorGesturePrivate(QObject *parent) + : QObject(parent),isActive(0), valid(0) +{ +} + +QSensorGesturePrivate::~QSensorGesturePrivate() +{ + +} + +#include "moc_qsensorgesture.cpp" + +QTM_END_NAMESPACE diff --git a/src/sensors/gestures/qsensorgesture.h b/src/sensors/gestures/qsensorgesture.h new file mode 100644 index 0000000000..0ee93c3ee7 --- /dev/null +++ b/src/sensors/gestures/qsensorgesture.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTURE_H +#define QSENSORGESTURE_H + +#include <qmobilityglobal.h> +#include <QtCore/QObject> +#include <QStringList> +#include <QtCore/QMetaType> + +#include <QList> +#include <QMap> +#include <QVector> + +QTM_BEGIN_NAMESPACE + +class QSensorGesturePrivate; + +class Q_SENSORS_EXPORT QSensorGesture : public QObject +{ + Q_OBJECT +public: + QSensorGesture(const QStringList &ids, QObject *parent = 0); + ~QSensorGesture(); + + bool isActive(); + + QStringList validIds() const; + QStringList invalidIds() const; + + QStringList gestureSignals() const; + + void startDetection(); + void stopDetection(); + +private: + QSensorGesturePrivate * d_ptr; + +private: + +Q_SIGNALS: + void detected(QString); +}; + +QTM_END_NAMESPACE + + +#endif // QSENSORGESTURE_H diff --git a/src/sensors/gestures/qsensorgesture_p.h b/src/sensors/gestures/qsensorgesture_p.h new file mode 100644 index 0000000000..ed846e4768 --- /dev/null +++ b/src/sensors/gestures/qsensorgesture_p.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTURE_P_H +#define QSENSORGESTURE_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 <qsensor.h> +#include <QTimer> + +#include "qsensorgesture.h" +#include "qsensorgesturemanager.h" + +QTM_BEGIN_NAMESPACE + +class QSensorGesturePrivate : public QObject +{ + +public: + QSensorGesturePrivate(QObject *parent = 0); + ~QSensorGesturePrivate(); + + QList<QSensorGestureRecognizer *> m_sensorRecognizers; + + QByteArray metadata; + bool isActive; + QStringList localGestureSignals; + QStringList availableIds; + QStringList invalidIds; + bool valid; +}; + +QTM_END_NAMESPACE + +#endif // QSENSORGESTURE_P_H diff --git a/src/sensors/gestures/qsensorgesturemanager.cpp b/src/sensors/gestures/qsensorgesturemanager.cpp new file mode 100644 index 0000000000..77c6e15882 --- /dev/null +++ b/src/sensors/gestures/qsensorgesturemanager.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensorgesturemanager.h" +#include "qsensorgesturemanagerprivate_p.h" + +QTM_BEGIN_NAMESPACE + +/*! + \class QSensorGestureManager + \ingroup sensorgestures_main + \inmodule QtSensors + + \brief The QSensorGestureManager class manages sensor gestures, registers and creates sensor gesture plugins. + + Sensor Gesture plugins register their recognizers using the registerSensorGestureRecognizer() function. + + \snippet snippets/sensorgestures/creating.cpp Receiving sensor gesture signals + + */ + +/*! + \fn QSensorGestureManager::newSensorGestureAvailable() + Signals when a new sensor gesture becomes available for use. + */ + +/*! + Constructs the QSensorGestureManager as a child of \a parent + */ +QSensorGestureManager::QSensorGestureManager(QObject *parent) + : QObject(parent) +{ + QSensorGestureManagerPrivate *d = QSensorGestureManagerPrivate::instance(); + if (!d) return; // hardly likely but just in case... + connect(d,SIGNAL(newSensorGestureAvailable()), + this,SIGNAL(newSensorGestureAvailable())); +} + +/*! + Destroy the QSensorGestureManager +*/ +QSensorGestureManager::~QSensorGestureManager() +{ +} + +/*! + Registers the sensor recognizer \a recognizer for use. + QSensorGestureManager retains ownership of the recognizer object. + Returns true unless the gesture has already been registered, in + which case the object is deleted. + + */ + + bool QSensorGestureManager::registerSensorGestureRecognizer(QSensorGestureRecognizer *recognizer) + { + QSensorGestureManagerPrivate *d = QSensorGestureManagerPrivate::instance(); + if (!d) { // hardly likely but just in case... + delete recognizer; + return false; + } + bool ok = d->registerSensorGestureRecognizer(recognizer); + if (!ok) + delete recognizer; + + return ok; + } + + + /*! + Returns the list of the currently registered gestures. + Includes all the standard built-ins as well as available plugins. + */ + QStringList QSensorGestureManager::gestureIds() const + { + QSensorGestureManagerPrivate *d = QSensorGestureManagerPrivate::instance(); + if (!d) return QStringList(); // hardly likely but just in case... + return d->gestureIds(); + } + + /*! + Returns the list of all the gesture signals for the registered \a gestureId gesture recognizer id. + */ + QStringList QSensorGestureManager::recognizerSignals(const QString &gestureId) const + { + QSensorGestureRecognizer *recognizer = sensorGestureRecognizer(gestureId); + if (recognizer != 0) + return recognizer->gestureSignals(); + else + return QStringList(); + } + +/*! + Returns the sensor gesture object for the recognizer \a id. + */ +QSensorGestureRecognizer *QSensorGestureManager::sensorGestureRecognizer(const QString &id) +{ + QSensorGestureManagerPrivate *d = QSensorGestureManagerPrivate::instance(); + if (!d) return 0; // hardly likely but just in case... + return d->sensorGestureRecognizer(id); +} + +#include "moc_qsensorgesturemanager.cpp" + +QTM_END_NAMESPACE diff --git a/src/sensors/gestures/qsensorgesturemanager.h b/src/sensors/gestures/qsensorgesturemanager.h new file mode 100644 index 0000000000..0b24c8458e --- /dev/null +++ b/src/sensors/gestures/qsensorgesturemanager.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTUREMANAGER_P_H +#define QSENSORGESTUREMANAGER_P_H + +#include <QObject> +#include <QStringList> + +#include "qsensorgesturerecognizer.h" +#include "qsensorgesture.h" + +QTM_BEGIN_NAMESPACE + +class QSensorGestureManagerPrivate; +class Q_SENSORS_EXPORT QSensorGestureManager : public QObject +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QSensorGestureManager) + +public: + explicit QSensorGestureManager(QObject *parent = 0); + + ~QSensorGestureManager(); + + bool registerSensorGestureRecognizer(QSensorGestureRecognizer *recognizer); + + QStringList gestureIds() const; + QStringList recognizerSignals(const QString &recognizerId) const; + + static QSensorGestureRecognizer *sensorGestureRecognizer(const QString &id); + +Q_SIGNALS: + void newSensorGestureAvailable(); + +}; + + + +QTM_END_NAMESPACE + +#endif // QSENSORGESTUREMANAGER_P_H diff --git a/src/sensors/gestures/qsensorgesturemanagerprivate.cpp b/src/sensors/gestures/qsensorgesturemanagerprivate.cpp new file mode 100644 index 0000000000..6e4782850c --- /dev/null +++ b/src/sensors/gestures/qsensorgesturemanagerprivate.cpp @@ -0,0 +1,252 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDir> +#include <QLibraryInfo> + +#include "qsensorgesturemanagerprivate_p.h" +#include "qsensorgesturerecognizer.h" +#include "qsensorgestureplugininterface.h" + +#include <qmobilitypluginsearch.h> + +#ifdef SIMULATOR_BUILD +#include "simulatorgesturescommon_p.h" +#endif + + +QTM_BEGIN_NAMESPACE +Q_GLOBAL_STATIC(QSensorGestureManagerPrivate, sensorGestureManagerPrivate) + +QSensorGestureManagerPrivate::QSensorGestureManagerPrivate(QObject *parent) : + QObject(parent) +{ +#ifdef SIMULATOR_BUILD + SensorGesturesConnection *connection = new SensorGesturesConnection(this); + connect(connection,SIGNAL(sensorGestureDetected()), + this,SLOT(sensorGestureDetected())); + + connect(this,SIGNAL(newSensorGestures(QStringList)), + connection,SLOT(newSensorGestures(QStringList))); + + connect(this,SIGNAL(removeSensorGestures(QStringList)), + connection,SLOT(removeSensorGestures(QStringList))); +#endif + +// loader = new QFactoryLoader("com.Nokia.QSensorGesturePluginInterface", QLatin1String("/sensorgestures")); + loadPlugins(); +} + +QSensorGestureManagerPrivate::~QSensorGestureManagerPrivate() +{ +// qDeleteAll(registeredSensorGestures); +// delete loader; +} + + + void QSensorGestureManagerPrivate::initPlugin(QObject *plugin) +{ + if (QSensorGesturePluginInterface *pInterface + = qobject_cast<QSensorGesturePluginInterface *>(plugin)) { + + Q_FOREACH (const QString &id, pInterface->supportedIds()) { + + if (!knownIds.contains(id)) + knownIds.append(id); + else + qWarning() << id <<"from the plugin" + << pInterface->name() + << "is already known."; + + } + plugins << plugin; + } else { + qWarning() << "Could not load "<< plugin; + } +} + + +/*! + Internal + Loads the sensorgesture plugins. + */ +void QSensorGestureManagerPrivate::loadPlugins() +{ + // Qt-style static plugins + Q_FOREACH (QObject *plugin, QPluginLoader::staticInstances()) { + initPlugin(plugin); + } + + QStringList gestureplugins = mobilityPlugins(QLatin1String("sensorgestures")); + for (int i = 0; i < gestureplugins.count(); i++) { + + QPluginLoader *loader = new QPluginLoader(gestureplugins.at(i), this); + QObject *plugin = loader->instance(); + if (plugin) { + initPlugin(plugin); + } + } +} + +/*! + Internal + creates the requested recognizer. + */ + +bool QSensorGestureManagerPrivate::loadRecognizer(const QString &recognizerId) +{ + //if no plugin is used return true if this is a registered recognizer + + if (registeredSensorGestures.contains(recognizerId)) + return true; + + for (int i= 0; i < plugins.count(); i++) { + + if (QSensorGesturePluginInterface *pInterface + = qobject_cast<QSensorGesturePluginInterface *>(plugins.at(i))) { + + if (pInterface->supportedIds().contains(recognizerId)) { + + if (!registeredSensorGestures.contains(recognizerId)) { + //create these recognizers + QList <QSensorGestureRecognizer *> recognizers = pInterface->createRecognizers(); + + Q_FOREACH (QSensorGestureRecognizer *recognizer, recognizers) { + + if (registeredSensorGestures.contains(recognizer->id())) { + qWarning() << "Ignoring recognizer " << recognizer->id() << "from plugin" << pInterface->name() << "because it is already registered"; + delete recognizer; + } else { + registeredSensorGestures.insert(recognizer->id(),recognizer); + } + } + } + return true; + } + } + } + return false; +} + +bool QSensorGestureManagerPrivate::registerSensorGestureRecognizer(QSensorGestureRecognizer *recognizer) +{ + if (!knownIds.contains(recognizer->id())) { + knownIds.append(recognizer->id()); + Q_ASSERT (!registeredSensorGestures.contains(recognizer->id())); + recognizer->setParent(0); + registeredSensorGestures.insert(recognizer->id(),recognizer); + Q_EMIT newSensorGestureAvailable(); + + return true; + } + return false; +} + +QSensorGestureRecognizer *QSensorGestureManagerPrivate::sensorGestureRecognizer(const QString &id) +{ + QSensorGestureRecognizer *recognizer = 0; + + if (loadRecognizer(id)) { + recognizer= registeredSensorGestures.value(id); + } + + return recognizer; +} + +QStringList QSensorGestureManagerPrivate::gestureIds() +{ + return knownIds; +} + +#ifdef SIMULATOR_BUILD +void QSensorGestureManagerPrivate::sensorGestureDetected() +{ + QString str = get_qtSensorGestureData(); + + Q_FOREACH (const QString &id, gestureIds()) { + QSensorGestureRecognizer *recognizer = sensorGestureRecognizer(id); + if (recognizer != 0) { + Q_FOREACH (const QString &sig, recognizer->gestureSignals()) { + if (!sig.contains(QLatin1String("detected"))) { //weed out detected signals + QString tmp; + tmp = sig.left(sig.length() - 2); + if (str == tmp) { + // named signal for c++ + QMetaObject::invokeMethod(recognizer, str.toLocal8Bit(), Qt::DirectConnection); + // detected signal for qml and c++ + QMetaObject::invokeMethod(recognizer, "detected", Qt::DirectConnection, + Q_ARG(QString, str)); + break; + + } + } + } + } + } +} + +void QSensorGestureManagerPrivate::recognizerStarted(const QSensorGestureRecognizer *recognizer) +{ + QStringList list = recognizer->gestureSignals(); + list.removeOne(QLatin1String("detected(QString)")); + Q_EMIT newSensorGestures(list); +} + +void QSensorGestureManagerPrivate::recognizerStopped(const QSensorGestureRecognizer *recognizer) +{ + QStringList list = recognizer->gestureSignals(); + list.removeOne(QLatin1String("detected(QString)")); + Q_EMIT removeSensorGestures(list); +} + +#endif + +QSensorGestureManagerPrivate * QSensorGestureManagerPrivate::instance() +{ + QSensorGestureManagerPrivate *priv = sensorGestureManagerPrivate(); + // It's safe to return 0 because this is checked when used + //if (!priv) qFatal("Cannot return from QSensorGestureManagerPrivate::instance() because sensorGestureManagerPrivate() returned 0."); + return priv; +} + +#include "moc_qsensorgesturemanagerprivate_p.cpp" + +QTM_END_NAMESPACE diff --git a/src/sensors/gestures/qsensorgesturemanagerprivate_p.h b/src/sensors/gestures/qsensorgesturemanagerprivate_p.h new file mode 100644 index 0000000000..80c4639863 --- /dev/null +++ b/src/sensors/gestures/qsensorgesturemanagerprivate_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTUREMANAGERPRIVATE_P_H +#define QSENSORGESTUREMANAGERPRIVATE_P_H + +#include <QObject> +#include <QMap> +#include <QStringList> +#include <QDebug> +#include <QSharedPointer> +#include <QPluginLoader> +#include <qmobilityglobal.h> + +#include "qsensorgesture.h" +#include "qsensorgesturerecognizer.h" + +QTM_BEGIN_NAMESPACE + +class QFactoryLoader; + +class QSensorGestureManagerPrivate : public QObject +{ + Q_OBJECT +public: + explicit QSensorGestureManagerPrivate(QObject *parent = 0); + ~QSensorGestureManagerPrivate(); + + QMap<QString, QSensorGestureRecognizer *> registeredSensorGestures; + + QList <QObject *> plugins; + + QFactoryLoader *loader; + void loadPlugins(); + bool loadRecognizer(const QString &id); + + QSensorGestureRecognizer *sensorGestureRecognizer(const QString &id); + + bool registerSensorGestureRecognizer(QSensorGestureRecognizer *recognizer); + QStringList gestureIds(); + QStringList knownIds; + void initPlugin(QObject *o); +#ifdef SIMULATOR_BUILD + void recognizerStarted(const QSensorGestureRecognizer *); + void recognizerStopped(const QSensorGestureRecognizer *); +#endif + + static QSensorGestureManagerPrivate * instance(); +Q_SIGNALS: + void newSensorGestureAvailable(); + +#ifdef SIMULATOR_BUILD +Q_SIGNALS: + void newSensorGestures(QStringList); + void removeSensorGestures(QStringList); + +private slots: + void sensorGestureDetected(); + +#endif +}; + +QTM_END_NAMESPACE + +#endif // QSENSORGESTUREMANAGERPRIVATE_P_H diff --git a/src/sensors/gestures/qsensorgestureplugininterface.cpp b/src/sensors/gestures/qsensorgestureplugininterface.cpp new file mode 100644 index 0000000000..95d4af6187 --- /dev/null +++ b/src/sensors/gestures/qsensorgestureplugininterface.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensorgestureplugininterface.h" + +QTM_BEGIN_NAMESPACE + +/*! + \class QSensorGesturePluginInterface + \ingroup sensorgestures_recognizer + \inmodule QtSensors + + \brief The QSensorGesturePluginInterface class is the pure virtual interface to sensor gesture + plugins. + + \since QtSensors 5.0 + + The QSensorGesturePluginInterface class is implemented in sensor gesture plugins to register + sensor gesture recognizers with QSensorGestureManager. + + \sa {QtSensorGestures Plugins} +*/ + +/*! + \fn QSensorGesturePluginInterface::createRecognizers() + + Called by the manager to create the recognizers. + Plugins should initialize and register their recognizers using + QSensorGestureManager::registerSensorGestureRecognizer() here. + + \sa QSensorGestureManager +*/ + +/*! + \fn QSensorGesturePluginInterface::supportedIds() const + + Returns a list of the recognizer Id's that this plugin supports. + */ + + +/*! + \fn QSensorGesturePluginInterface::name() const + + Returns this plugins name. + */ + +/*! + Construct the QSensorGesturePluginInterface. +*/ +QSensorGesturePluginInterface::QSensorGesturePluginInterface() +{ +} + +/*! + Destroy the QSensorGesturePluginInterface. +*/ +QSensorGesturePluginInterface::~QSensorGesturePluginInterface() +{ +} + +QTM_END_NAMESPACE diff --git a/src/sensors/gestures/qsensorgestureplugininterface.h b/src/sensors/gestures/qsensorgestureplugininterface.h new file mode 100644 index 0000000000..d13c1dea6b --- /dev/null +++ b/src/sensors/gestures/qsensorgestureplugininterface.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTUREPLUGININTERFACE_H +#define QSENSORGESTUREPLUGININTERFACE_H + +#include <QObject> +#include <QtGlobal> +#include <QtCore/qplugin.h> +#include "qsensorgesture.h" +#include "qsensorgesturerecognizer.h" + + +QTM_BEGIN_NAMESPACE + +class QSensorGestureRecognizer; + +class Q_SENSORS_EXPORT QSensorGesturePluginInterface +{ +public: + QSensorGesturePluginInterface(); + virtual ~QSensorGesturePluginInterface(); + virtual QList <QSensorGestureRecognizer *> createRecognizers() = 0; + virtual QStringList supportedIds() const = 0; + virtual QString name() const = 0; +}; + +QTM_END_NAMESPACE + +QT_BEGIN_NAMESPACE +Q_DECLARE_INTERFACE(QtMobility::QSensorGesturePluginInterface, "com.Nokia.Qt.QSensorGesturePluginInterface/1.0"); +QT_END_NAMESPACE + + +#endif // QSENSORGESTUREPLUGININTERFACE_H diff --git a/src/sensors/gestures/qsensorgesturerecognizer.cpp b/src/sensors/gestures/qsensorgesturerecognizer.cpp new file mode 100644 index 0000000000..9ec9edeac7 --- /dev/null +++ b/src/sensors/gestures/qsensorgesturerecognizer.cpp @@ -0,0 +1,221 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensorgesture_p.h" +#include "qsensorgesturerecognizer.h" + +//#include "qsensorgesturemanager.h" +#ifdef SIMULATOR_BUILD +#include "qsensorgesturemanagerprivate_p.h" +#endif + +QTM_BEGIN_NAMESPACE + + +/*! + \class QSensorGestureRecognizer + \ingroup sensorgestures_recognizer + \inmodule QtSensors + + \brief The QSensorGestureRecognizer class is the base class for a sensor gesture + recognizer. + + QSensorGesture recognizer developers should sub-class this to implement their own recognizer. + + All sensor gesture recognizers have a detected(QString) signal. Implementors can use this + and send recognizer specific gestures, such as detected("shake_left") or implement custom signals + such as shakeLeft(). + + These custom signals will be available in the QSensorGesture object at runtime. + + \sa QSensorGestureRecognizer::gestureSignals() + + */ + +/*! + \fn void QSensorGestureRecognizer::create() + + Called by QSensorGesture object constructor to create the recognizers backend. + + Implementors would use this to instantiate QSensors and connect signals. + + */ + +/*! + \fn QString QSensorGestureRecognizer::id() const + Returns the identifier for this recognizer. + */ +/*! + \fn bool QSensorGestureRecognizer::start() + + Called by QSensorGesture::startDetection() to start this recognizer. + Implementors should start the sensors. + Returns true if the operation is successful. + + */ +/*! + \fn bool QSensorGestureRecognizer::stop() + + Called by QSensorGesture::stopDetection() to stop this recognizer. + Returns true if the call succeeds, otherwise false. + + Implementors should stop the sensors. + + */ +/*! + \fn bool QSensorGestureRecognizer::isActive() + + Returns true if this recognizer is active, otherwise false. + */ + +/*! + \fn QSensorGestureRecognizer::detected(const QString &gestureId) + Signals when the \a gestureId gesture has been recognized. + */ + +class QSensorGestureRecognizerPrivate +{ +public: + bool initialized; + int count; +}; + + +/*! + Constructs the QSensorGestureRecognizer with \a parent as parent. + */ +QSensorGestureRecognizer::QSensorGestureRecognizer(QObject *parent) + :QObject(parent), + d_ptr(new QSensorGestureRecognizerPrivate()) +{ +} + +/*! + Destroy the QSensorGestureRecognizer +*/ +QSensorGestureRecognizer::~QSensorGestureRecognizer() +{ + delete d_ptr; +} + +/*! + Returns a list of signals that this recognizer supports. + + Note that all signals declared will be exported to the QSensorGesture + object. If you need to use signals that are not exported, you should use a private class + to do so. + + */ +QStringList QSensorGestureRecognizer::gestureSignals() const +{ + QStringList list; + //bool ok = false; + list.append(QString::fromLatin1("detected(QString)")) + ; + // for (int i = 0; i < this->metaObject()->methodCount(); i++) { +// //weed out objectsignals and slots +// const QByteArray sig(this->metaObject()->method(i).methodSignature()); +// if (this->metaObject()->indexOfSignal(sig) != -1) { +// if (sig.contains("detected")) +// ok = true; +// if (ok) +// list.append(QString::fromLatin1(sig)); +// } +// } + return list; +} + +/*! + Calls QSensorGestureRecognizer::create() if the recognizer is valid. +*/ +void QSensorGestureRecognizer::createBackend() +{ + if (d_ptr->initialized) { + return; + } + d_ptr->initialized = true; + create(); +} + +/*! + Calls QSensorGestureRecognizer::start() if the recognizer isn't already initialized. + This is called by the QSensorGesture object, so please use that instead. + +\sa QSensorGesture::startDetection() + +*/ +void QSensorGestureRecognizer::startBackend() +{ + if (!d_ptr->initialized) { + qWarning() << "Not starting. Gesture Recognizer not initialized"; + return; + } + if (d_ptr->count++ == 0) { + start(); +#ifdef SIMULATOR_BUILD + QSensorGestureManagerPrivate::instance()->recognizerStarted(this); +#endif + } +} + +/*! + Calls QSensorGestureRecognizer::stop() if no other clients are using it. + This is called by the QSensorGesture object, so please use that instead. + +\sa QSensorGesture::stopDetection() +*/ +void QSensorGestureRecognizer::stopBackend() +{ + if (!d_ptr->initialized) { + qWarning() << "Not stopping. Gesture Recognizer not initialized"; + return; + } + if (--d_ptr->count == 0) { + stop(); +#ifdef SIMULATOR_BUILD + QSensorGestureManagerPrivate::instance()->recognizerStopped(this); +#endif + } +} + +#include "moc_qsensorgesturerecognizer.cpp" + +QTM_END_NAMESPACE diff --git a/src/sensors/gestures/qsensorgesturerecognizer.h b/src/sensors/gestures/qsensorgesturerecognizer.h new file mode 100644 index 0000000000..d2391f447a --- /dev/null +++ b/src/sensors/gestures/qsensorgesturerecognizer.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSORGESTURERECOGNIZER_H +#define QSENSORGESTURERECOGNIZER_H + +#include <qmobilityglobal.h> +#include <QDebug> +#include <QTimer> +#include <QStringList> + + +QTM_BEGIN_NAMESPACE + +class QSensorGestureRecognizerPrivate; +class Q_SENSORS_EXPORT QSensorGestureRecognizer : public QObject +{ + Q_OBJECT +public: + QSensorGestureRecognizer(QObject *parent = 0); + virtual ~QSensorGestureRecognizer(); + + virtual QString id() const = 0; + + virtual bool isActive() = 0; + + void startBackend(); + void stopBackend(); + void createBackend(); + + QStringList gestureSignals() const; + +Q_SIGNALS: + void detected(const QString &); + +protected: + virtual void create() = 0; + virtual bool start() = 0; + virtual bool stop() = 0; + +private: + QSensorGestureRecognizerPrivate * d_ptr; +}; + +QTM_END_NAMESPACE +#endif // QSENSORGESTURERECOGNIZER_H diff --git a/src/sensors/gestures/simulatorgesturescommon.cpp b/src/sensors/gestures/simulatorgesturescommon.cpp new file mode 100644 index 0000000000..02e62e7e4f --- /dev/null +++ b/src/sensors/gestures/simulatorgesturescommon.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "simulatorgesturescommon_p.h" + +#include <QtSimulator/version.h> +#include <QtSimulator/QtSimulator> + +#include <QDebug> +#include <QStringList> + +using namespace Simulator; + + +Q_GLOBAL_STATIC(QString, qtSensorGestureData) + +SensorGesturesConnection::SensorGesturesConnection(QObject *parent) + : QObject(parent) +{ + mConnection = new Connection(Connection::Client, QLatin1String("QtSimulator_Mobility_ServerName1.3.0.0"), + 0xbeef+1, Version(1,0,0,0), this); + mWorker = mConnection->connectToServer(Connection::simulatorHostName(true), 0xbeef+1); + + if (!mWorker) { + qWarning() << "Could not connect to server"; + return; + } + + mWorker->addReceiver(this); + mWorker->call("setRequestsSensorGestures"); +} + +SensorGesturesConnection::~SensorGesturesConnection() +{ + mWorker->call("setSensorGestures", QStringList()); + delete mWorker; +} + +void SensorGesturesConnection::setSensorGestureData(const QString &data) +{ + QString gesture = data; + if (data.contains(QLatin1String("detected"))) { + gesture.remove(QLatin1String("detected(")); + gesture.remove(QLatin1String(")")); + } + *qtSensorGestureData() = gesture; +} + +void SensorGesturesConnection::newSensorGestureDetected() +{ + emit sensorGestureDetected(); +} + +void SensorGesturesConnection::newSensorGestures(const QStringList &gestures) +{ + if (!mWorker) return; + + Q_FOREACH (const QString &gest, gestures) { + if (!gest.contains(QLatin1String("detected"))) { + QString tmp = gest.left(gest.length()-2); + if (!allGestures.contains(tmp)) { + allGestures.append(tmp); + } + } + } + + mWorker->call("setSensorGestures", allGestures); +} + +void SensorGesturesConnection::removeSensorGestures(const QStringList &gestures) +{ + Q_FOREACH (const QString &gest, gestures) { + QString tmp = gest.left(gest.length()-2); + if (allGestures.contains(tmp)) { + allGestures.removeOne(tmp); + } + } + mWorker->call("setSensorGestures", allGestures); +} + +QString get_qtSensorGestureData() +{ + return *qtSensorGestureData(); +} + diff --git a/src/sensors/gestures/simulatorgesturescommon_p.h b/src/sensors/gestures/simulatorgesturescommon_p.h new file mode 100644 index 0000000000..20c26b39eb --- /dev/null +++ b/src/sensors/gestures/simulatorgesturescommon_p.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIMULATORGESTURESCOMMON_H +#define SIMULATORGESTURESCOMMON_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 <QtSimulator/connection.h> +#include <QtSimulator/connectionworker.h> +#include <QStringList> + +class QTimer; + +namespace Simulator +{ + class Connection; + class ConnectionWorker; +} + +class SensorGesturesConnection : public QObject +{ + Q_OBJECT +public: + explicit SensorGesturesConnection(QObject *parent = 0); + virtual ~SensorGesturesConnection(); + +Q_SIGNALS: + void sensorGestureDetected(); + +public slots: + void setSensorGestureData(const QString &); + void newSensorGestureDetected(); + void newSensorGestures(const QStringList &gestures); + void removeSensorGestures(const QStringList &gestures); + +private: + Simulator::Connection *mConnection; + Simulator::ConnectionWorker *mWorker; + QStringList allGestures; + +}; + +QString get_qtSensorGestureData(); + +#endif //SIMULATORGESTURESCOMMON_H + diff --git a/src/sensors/sensors.pro b/src/sensors/sensors.pro index 0c111b45b0..fe5b59fc64 100644 --- a/src/sensors/sensors.pro +++ b/src/sensors/sensors.pro @@ -67,6 +67,23 @@ for(s,SENSORS) { PRIVATE_HEADERS += $${s}_p.h } +SOURCES += \ + gestures/qsensorgesture.cpp \ + gestures/qsensorgesturerecognizer.cpp \ + gestures/qsensorgesturemanager.cpp \ + gestures/qsensorgesturemanagerprivate.cpp \ + gestures/qsensorgestureplugininterface.cpp + +PUBLIC_HEADERS += \ + gestures/qsensorgesture.h\ + gestures/qsensorgesturerecognizer.h \ + gestures/qsensorgesturemanager.h \ + gestures/qsensorgestureplugininterface.h + +PRIVATE_HEADERS += \ + gestures/qsensorgesturemanagerprivate_p.h \ + gestures/qsensorgesture_p.h + HEADERS = $$PUBLIC_HEADERS $$PRIVATE_HEADERS include(../../features/deploy.pri) |