From 3c599b87f33673932a55f3f5621073885dea5bc1 Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Fri, 17 Aug 2012 11:29:22 +1000 Subject: Add SensorGesture QtMobility.sensors 1.3 This has been imported from QtSensors 5.0 without changes. Change-Id: Ie6a919f9b806bf19287a693fb7cdb563f8697e13 Reviewed-by: Lorn Potter --- src/imports/sensors/plugins.qmltypes | 20 +++ src/imports/sensors/qmlsensorgesture.cpp | 262 +++++++++++++++++++++++++++++++ src/imports/sensors/qmlsensorgesture.h | 102 ++++++++++++ src/imports/sensors/sensors.cpp | 3 + src/imports/sensors/sensors.pro | 6 +- src/imports/sensors2/qsensor2gesture.cpp | 103 ++++++------ src/imports/sensors2/qsensor2gesture.h | 21 ++- 7 files changed, 445 insertions(+), 72 deletions(-) create mode 100644 src/imports/sensors/qmlsensorgesture.cpp create mode 100644 src/imports/sensors/qmlsensorgesture.h (limited to 'src') diff --git a/src/imports/sensors/plugins.qmltypes b/src/imports/sensors/plugins.qmltypes index eecc3a93..d1b0bba3 100644 --- a/src/imports/sensors/plugins.qmltypes +++ b/src/imports/sensors/plugins.qmltypes @@ -423,6 +423,26 @@ Module { Method { name: "start"; type: "bool" } Method { name: "stop"; type: "void" } } + Component { + name: "QmlSensorGesture" + prototype: "QObject" + exports: ["SensorGesture 1.3"] + Property { name: "availableGestures"; type: "QStringList"; isReadonly: true } + Property { name: "gestures"; type: "QStringList" } + Property { name: "validGestures"; type: "QStringList"; isReadonly: true } + Property { name: "invalidGestures"; type: "QStringList"; isReadonly: true } + Property { name: "enabled"; type: "bool" } + Signal { + name: "detected" + type: "void" + Parameter { name: "gesture"; type: "string" } + } + Signal { name: "availableGesturesChanged"; type: "void" } + Signal { name: "gesturesChanged"; type: "void" } + Signal { name: "validGesturesChanged"; type: "void" } + Signal { name: "invalidGesturesChanged"; type: "void" } + Signal { name: "enabledChanged"; type: "void" } + } Component { name: "QmlSensorGlobal" prototype: "QObject" diff --git a/src/imports/sensors/qmlsensorgesture.cpp b/src/imports/sensors/qmlsensorgesture.cpp new file mode 100644 index 00000000..4f1b8367 --- /dev/null +++ b/src/imports/sensors/qmlsensorgesture.cpp @@ -0,0 +1,262 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 "qmlsensorgesture.h" +#include +#include + +QT_BEGIN_NAMESPACE + +/*! + \qmltype SensorGesture + \instantiates QmlSensorGesture + \inqmlmodule QtMobility.sensors 1.3 + \since QtMobility.sensors 1.3 + \brief Provides notifications when sensor-based gestures are detected. + + This type provides notification when sensor gestures are triggered. + + The following QML code creates a "shake" and "SecondCounter" SensorGesture QML type, and + displays the detected gesture in a text type. + + QtSensors.shake gesture is available with the Qt Sensors API, but the QtSensors.SecondCounter + sensor gesture is provided as example code for the \l {Qt Sensors - SensorGesture QML Type example} + + \qml + Item { + SensorGesture { + id: sensorGesture + enabled: false + gestures : ["QtSensors.shake", "QtSensors.SecondCounter"] + onDetected:{ + detectedText.text = gesture + } + } + Text { + id: detectedText + x:5 + y:160 + } + } + \endqml + + \l {Qt Sensor Gestures} contains a list of currently supported sensor gestures and their + descriptions. + + +*/ +QmlSensorGesture::QmlSensorGesture(QObject* parent) + : QObject(parent) + , isEnabled(false) + , initDone(false) + , sensorGesture(0) + , sensorGestureManager(new QSensorGestureManager(this)) +{ + connect(sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged())); +} + +QmlSensorGesture::~QmlSensorGesture() +{ +} + +/* + QQmlParserStatus interface implementation +*/ +void QmlSensorGesture::classBegin() +{ +} + +void QmlSensorGesture::componentComplete() +{ + /* + this is needed in the case the customer defines the type(s) and set it enabled = true + */ + initDone = true; + setEnabled(isEnabled); +} +/* + End of QQmlParserStatus interface implementation +*/ + +/*! + \qmlproperty stringlist QtMobility.sensors1::SensorGesture::availableGestures + This property can be used to determine all available gestures on the system. +*/ +QStringList QmlSensorGesture::availableGestures() +{ + return sensorGestureManager->gestureIds(); +} + +/*! + \qmlproperty stringlist QtMobility.sensors1::SensorGesture::gestures + Set this property to a list of the gestures that the application is interested in detecting. + This property cannot be changed while the type is enabled. + + The properties validGestures and invalidGestures will be set as appropriate immediately. + To determine all available getures on the system please use the + \l {QtMobility.sensors1::SensorGesture::availableGestures} {availableGestures} property. + + \sa {QtSensorGestures Plugins} +*/ +QStringList QmlSensorGesture::gestures() const +{ + return gestureList; +} + +void QmlSensorGesture::setGestures(const QStringList& value) +{ + if (gestureList == value) + return; + + if (initDone && enabled()) { + qWarning() << "Cannot change gestures while running."; + return; + } + gestureList = value; + createGesture(); + Q_EMIT gesturesChanged(); +} + + +/*! + \qmlproperty stringlist QtMobility.sensors1::SensorGesture::validGestures + This property holds the requested gestures that were found on the system. +*/ +QStringList QmlSensorGesture::validGestures() const +{ + if (sensorGesture) + return sensorGesture->validIds(); + return QStringList(); +} + +/*! + \qmlproperty stringlist QtMobility.sensors1::SensorGesture::invalidGestures + This property holds the requested gestures that were not found on the system. +*/ +QStringList QmlSensorGesture::invalidGestures() const +{ + if (sensorGesture) + return sensorGesture->invalidIds(); + return QStringList(); +} + +/*! + \qmlproperty bool QtMobility.sensors1::SensorGesture::enabled + This property can be used to activate or deactivate the sensor gesture. + Default value is false; + \sa {QtMobility.sensors1::SensorGesture::detected}, {detected} +*/ +bool QmlSensorGesture::enabled() const +{ + return isEnabled; +} + +void QmlSensorGesture::setEnabled(bool value) +{ + bool hasChanged = false; + if (isEnabled != value) { + isEnabled = value; + hasChanged = true; + } + if (!initDone) + return; + + if (sensorGesture) { + if (value) { + sensorGesture->startDetection(); + } else { + sensorGesture->stopDetection(); + } + } + if (hasChanged) + Q_EMIT enabledChanged(); +} + +/*! + \qmlsignal QtMobility.sensors1::SensorGesture::detected(string gesture) + This signal is emitted whenever a gesture is detected. + The gesture parameter contains the gesture that was detected. +*/ + +/* + private funtion implementation +*/ +void QmlSensorGesture::deleteGesture() +{ + if (sensorGesture) { + bool emitInvalidChange = !invalidGestures().isEmpty(); + bool emitValidChange = !validGestures().isEmpty(); + + if (sensorGesture->isActive()) { + sensorGesture->stopDetection(); + } + delete sensorGesture; + sensorGesture = 0; + + if (emitInvalidChange) { + Q_EMIT invalidGesturesChanged(); + } + if (emitValidChange) { + Q_EMIT validGesturesChanged(); + } + } +} + +void QmlSensorGesture::createGesture() +{ + deleteGesture(); + sensorGesture = new QSensorGesture(gestureList, this); + if (!validGestures().isEmpty()) { + QObject::connect(sensorGesture + , SIGNAL(detected(QString)) + , this + , SIGNAL(detected(QString))); + Q_EMIT validGesturesChanged(); + } + if (!invalidGestures().isEmpty()) + Q_EMIT invalidGesturesChanged(); +} + +/* + End of private funtion implementation +*/ + +QT_END_NAMESPACE diff --git a/src/imports/sensors/qmlsensorgesture.h b/src/imports/sensors/qmlsensorgesture.h new file mode 100644 index 00000000..808ef769 --- /dev/null +++ b/src/imports/sensors/qmlsensorgesture.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $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 QMLSENSORGESTURE_H +#define QMLSENSORGESTURE_H + +#include +#include + +QT_BEGIN_NAMESPACE + +class QSensorGesture; +class QSensorGestureManager; + +class QmlSensorGesture : public QObject, public QQmlParserStatus +{ + Q_OBJECT + Q_PROPERTY(QStringList availableGestures READ availableGestures NOTIFY availableGesturesChanged) + Q_PROPERTY(QStringList gestures READ gestures WRITE setGestures NOTIFY gesturesChanged) + Q_PROPERTY(QStringList validGestures READ validGestures NOTIFY validGesturesChanged) + Q_PROPERTY(QStringList invalidGestures READ invalidGestures NOTIFY invalidGesturesChanged) + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) + Q_INTERFACES(QQmlParserStatus) + +public: + explicit QmlSensorGesture(QObject* parent = 0); + ~QmlSensorGesture(); + void classBegin() Q_DECL_OVERRIDE; + void componentComplete() Q_DECL_OVERRIDE; + +Q_SIGNALS: + void detected(const QString &gesture); + void availableGesturesChanged(); + void gesturesChanged(); + void validGesturesChanged(); + void invalidGesturesChanged(); + void enabledChanged(); + +public: + QStringList availableGestures(); + QStringList gestures() const; + void setGestures(const QStringList& value); + bool enabled() const; + void setEnabled(bool value); + QStringList validGestures() const; + QStringList invalidGestures() const; + +private: + void deleteGesture(); + void createGesture(); + +private: + QStringList gestureIds; + bool isEnabled; + bool initDone; + QStringList gestureList; + + QSensorGesture* sensorGesture; + QSensorGestureManager* sensorGestureManager; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/imports/sensors/sensors.cpp b/src/imports/sensors/sensors.cpp index 9e425297..9c0c864b 100644 --- a/src/imports/sensors/sensors.cpp +++ b/src/imports/sensors/sensors.cpp @@ -70,6 +70,7 @@ #include "qmlrotationsensor.h" #include "qmltapsensor.h" #include "qmltiltsensor.h" +#include "qmlsensorgesture.h" QT_BEGIN_NAMESPACE @@ -169,6 +170,8 @@ public: qmlRegisterUncreatableType(package, major, minor, "TapReading", QLatin1String("Cannot create TapReading")); qmlRegisterType (package, major, minor, "TiltSensor"); qmlRegisterUncreatableType(package, major, minor, "TiltReading", QLatin1String("Cannot create TiltReading")); + + qmlRegisterType (package, major, minor, "SensorGesture"); } }; diff --git a/src/imports/sensors/sensors.pro b/src/imports/sensors/sensors.pro index 89af5ead..0d08bf3c 100644 --- a/src/imports/sensors/sensors.pro +++ b/src/imports/sensors/sensors.pro @@ -22,7 +22,8 @@ HEADERS += \ qmltapsensor.h \ qmlrotationsensor.h \ qmlsensorglobal.h \ - qmltiltsensor.h + qmltiltsensor.h \ + qmlsensorgesture.h SOURCES += sensors.cpp \ qmlsensor.cpp \ @@ -39,7 +40,8 @@ SOURCES += sensors.cpp \ qmltapsensor.cpp \ qmlrotationsensor.cpp \ qmlsensorglobal.cpp \ - qmltiltsensor.cpp + qmltiltsensor.cpp \ + qmlsensorgesture.cpp DESTDIR = $$QT.sensors.imports/$$TARGETPATH diff --git a/src/imports/sensors2/qsensor2gesture.cpp b/src/imports/sensors2/qsensor2gesture.cpp index d68966cd..8341c521 100644 --- a/src/imports/sensors2/qsensor2gesture.cpp +++ b/src/imports/sensors2/qsensor2gesture.cpp @@ -45,8 +45,6 @@ QT_BEGIN_NAMESPACE -//#define LOGGESTURQMLAPI - /*! \qmltype SensorGesture \instantiates QSensor2Gesture @@ -58,7 +56,7 @@ QT_BEGIN_NAMESPACE This type is part of the \b{QtSensors 5} module. - The following QML code creates a "shake" and "template" SensorGesture QML type, and + The following QML code creates a "shake" and "SecondCounter" SensorGesture QML type, and displays the detected gesture in a text type. QtSensors.shake gesture is available with the Qt Sensors API, but the QtSensors.SecondCounter @@ -90,14 +88,12 @@ QT_BEGIN_NAMESPACE */ QSensor2Gesture::QSensor2Gesture(QObject* parent) : QObject(parent) - , _enabled(false) - , _oldEnabled(false) - , _init(true) - , _sensorGesture(0) - , _sensorGestureManager(0) + , isEnabled(false) + , initDone(false) + , sensorGesture(0) + , sensorGestureManager(new QSensorGestureManager(this)) { - _sensorGestureManager = new QSensorGestureManager(this); - connect(_sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged())); + connect(sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged())); } QSensor2Gesture::~QSensor2Gesture() @@ -116,8 +112,8 @@ void QSensor2Gesture::componentComplete() /* this is needed in the case the customer defines the type(s) and set it enabled = true */ - _init = false; - setEnabled(_enabled); + initDone = true; + setEnabled(isEnabled); } /* End of QQmlParserStatus interface implementation @@ -129,7 +125,7 @@ void QSensor2Gesture::componentComplete() */ QStringList QSensor2Gesture::availableGestures() { - return _sensorGestureManager->gestureIds(); + return sensorGestureManager->gestureIds(); } /*! @@ -145,20 +141,20 @@ QStringList QSensor2Gesture::availableGestures() */ QStringList QSensor2Gesture::gestures() const { - return _gestures; + return gestureList; } void QSensor2Gesture::setGestures(const QStringList& value) { - if (_gestures == value) + if (gestureList == value) return; - if (!_init && enabled()){ + if (initDone && enabled()) { qWarning() << "Cannot change gestures while running."; return; } - _gestures.clear(); - _gestures = value; + gestureList.clear(); + gestureList = value; createGesture(); Q_EMIT gesturesChanged(); } @@ -170,8 +166,8 @@ void QSensor2Gesture::setGestures(const QStringList& value) */ QStringList QSensor2Gesture::validGestures() const { - if (_sensorGesture) - return _sensorGesture->validIds(); + if (sensorGesture) + return sensorGesture->validIds(); return QStringList(); } @@ -181,8 +177,8 @@ QStringList QSensor2Gesture::validGestures() const */ QStringList QSensor2Gesture::invalidGestures() const { - if (_sensorGesture) - return _sensorGesture->invalidIds(); + if (sensorGesture) + return sensorGesture->invalidIds(); return QStringList(); } @@ -194,33 +190,28 @@ QStringList QSensor2Gesture::invalidGestures() const */ bool QSensor2Gesture::enabled() const { - return _enabled; + return isEnabled; } void QSensor2Gesture::setEnabled(bool value) { - _enabled = value; - if (_init) + bool hasChanged = false; + if (isEnabled != value) { + isEnabled = value; + hasChanged = true; + } + if (!initDone) return; - if (_enabled != _oldEnabled){ - _oldEnabled = _enabled; - if (_sensorGesture){ - if (_enabled){ -#ifdef LOGGESTUREQMLAPI - qDebug() << "start detection" << _gestureIds; -#endif - _sensorGesture->startDetection(); - } - else { -#ifdef LOGGESTUREQMLAPI - qDebug() << "stop detection" << _gestureIds; -#endif - _sensorGesture->stopDetection(); - } + if (sensorGesture) { + if (value) { + sensorGesture->startDetection(); + } else { + sensorGesture->stopDetection(); } - Q_EMIT enabledChanged(); } + if (hasChanged) + Q_EMIT enabledChanged(); } /*! @@ -234,20 +225,20 @@ void QSensor2Gesture::setEnabled(bool value) */ void QSensor2Gesture::deleteGesture() { - if (_sensorGesture){ - bool emitinvalidchange = !invalidGestures().isEmpty(); - bool emitvalidchange = !validGestures().isEmpty(); + if (sensorGesture) { + bool emitInvalidChange = !invalidGestures().isEmpty(); + bool emitValidChange = !validGestures().isEmpty(); - if (_sensorGesture->isActive()) { - _sensorGesture->stopDetection(); + if (sensorGesture->isActive()) { + sensorGesture->stopDetection(); } - delete _sensorGesture; - _sensorGesture = 0; + delete sensorGesture; + sensorGesture = 0; - if (emitinvalidchange){ + if (emitInvalidChange) { Q_EMIT invalidGesturesChanged(); } - if (emitvalidchange){ + if (emitValidChange) { Q_EMIT validGesturesChanged(); } } @@ -256,15 +247,9 @@ void QSensor2Gesture::deleteGesture() void QSensor2Gesture::createGesture() { deleteGesture(); -#ifdef LOGGESTURQMLAPI - qDebug() << "Create Gestrues:"; - for (int i = 0; i < _gestures.count(); i++){ - qDebug() << " -" << _gestures[i]; - } -#endif - _sensorGesture = new QSensorGesture(_gestures, this); - if (!validGestures().isEmpty()){ - QObject::connect(_sensorGesture + sensorGesture = new QSensorGesture(gestureList, this); + if (!validGestures().isEmpty()) { + QObject::connect(sensorGesture , SIGNAL(detected(QString)) , this , SIGNAL(detected(QString))); diff --git a/src/imports/sensors2/qsensor2gesture.h b/src/imports/sensors2/qsensor2gesture.h index 0c9b6fb0..2a27b055 100644 --- a/src/imports/sensors2/qsensor2gesture.h +++ b/src/imports/sensors2/qsensor2gesture.h @@ -62,10 +62,10 @@ class QSensor2Gesture : public QObject, public QQmlParserStatus Q_INTERFACES(QQmlParserStatus) public: - QSensor2Gesture(QObject* parent = 0); - virtual ~QSensor2Gesture(); - void classBegin(); - void componentComplete(); + explicit QSensor2Gesture(QObject* parent = 0); + ~QSensor2Gesture(); + void classBegin() Q_DECL_OVERRIDE; + void componentComplete() Q_DECL_OVERRIDE; Q_SIGNALS: void detected(const QString &gesture); @@ -89,14 +89,13 @@ private: void createGesture(); private: - QStringList _gestureIds; - bool _enabled; - bool _oldEnabled; - bool _init; - QStringList _gestures; + QStringList gestureIds; + bool isEnabled; + bool initDone; + QStringList gestureList; - QSensorGesture* _sensorGesture; - QSensorGestureManager* _sensorGestureManager; + QSensorGesture* sensorGesture; + QSensorGestureManager* sensorGestureManager; }; QT_END_NAMESPACE -- cgit v1.2.3