From 1f20d5ef3e27981e2af2c6533656694ea3335678 Mon Sep 17 00:00:00 2001 From: Wolfgang Beck Date: Tue, 1 Nov 2011 16:56:55 +1000 Subject: Add QtSensorGesture QML API Change-Id: Iaa0a875941316c341d69f473eeba3be4a87ec736 Sanity-Review: Qt Sanity Bot Reviewed-by: Lincoln Ramsay --- src/imports/sensors2/qsensor2gesture.cpp | 266 +++++++++++++++++++++++++++++++ src/imports/sensors2/qsensor2gesture.h | 106 ++++++++++++ src/imports/sensors2/sensors2.cpp | 2 + src/imports/sensors2/sensors2.pro | 8 +- 4 files changed, 380 insertions(+), 2 deletions(-) create mode 100644 src/imports/sensors2/qsensor2gesture.cpp create mode 100644 src/imports/sensors2/qsensor2gesture.h (limited to 'src/imports/sensors2') diff --git a/src/imports/sensors2/qsensor2gesture.cpp b/src/imports/sensors2/qsensor2gesture.cpp new file mode 100644 index 00000000..33d97231 --- /dev/null +++ b/src/imports/sensors2/qsensor2gesture.cpp @@ -0,0 +1,266 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensor2gesture.h" +#include +#include + +QT_BEGIN_NAMESPACE + +//#define LOGGESTURQMLAPI + +/*! + \qmlclass SensorGesture QSensor2Gesture + \inqmlmodule QtSensors 5 + \since QtSensors 5.0 + \brief The SensorGesture element provide notification when sensor gestures are triggered. + + This element is part of the \bold{QtSensors 5} module. + + The following QML code creates a "shake" and "template" SensorGesture QML element. + + \qml + Item { + SensorGesture { + id: sensorGesture + enabled: false + gestures : ["QtSensors.shake", "QtSensors.SecondCounter"] + onDetected:{ + detectedText.text = gesture + } + } + Text { + id: detectedText + x:5 + y:160 + text: "" + } + } + \endqml +*/ +QSensor2Gesture::QSensor2Gesture(QObject* parent) + : QObject(parent) + , _enabled(false) + , _oldEnabled(false) + , _init(true) + , _sensorGesture(0) + , _sensorGestureManager(0) +{ + _sensorGestureManager = new QSensorGestureManager(this); + connect(_sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged())); +} + +QSensor2Gesture::~QSensor2Gesture() +{ +} + +/* + QDeclarativeParserStatus interface implementation +*/ +void QSensor2Gesture::classBegin() +{ +} + +void QSensor2Gesture::componentComplete() +{ + /* + this is needed in the case the customer defines the type(s) and set it enabled = true + */ + _init = false; + setEnabled(_enabled); +} +/* + End of QDeclarativeParserStatus interface implementation +*/ + +/*! + \qmlproperty QStringList QtSensors5::SensorGesture::availableGestures + This property can be used to determine all available gestures on the system. +*/ +QStringList QSensor2Gesture::availableGestures() +{ + return _sensorGestureManager->gestureIds(); +} + +/*! + \qmlproperty QString QtSensors5::SensorGesture::gestures + Set this property to the gestures the application is interested in detecting. + The properties validGestures and invalidGestures will be set as appropriate immediately. + The list of available gestures can be found in the availableGestures property. + This property cannot be changed while the element is enabled. + To determine all available getures on the system please use the + \l {QtSensors5::SensorGesture::availableGestures} {availableGestures} property. +*/ +QStringList QSensor2Gesture::gestures() const +{ + return _gestures; +} + +void QSensor2Gesture::setGestures(const QStringList& value) +{ + if (_gestures == value) + return; + + if (!_init && enabled()){ + qWarning() << "Cannot change gestures while running."; + return; + } + _gestures.clear(); + _gestures = value; + createGesture(); + emit gesturesChanged(); +} + + +/*! + \qmlproperty QStringList QtSensors5::SensorGesture::validGestures + This property holds the requested gestures that were found on the system. +*/ +QStringList QSensor2Gesture::validGestures() const +{ + if (_sensorGesture) + return _sensorGesture->validIds(); + return QStringList(); +} + +/*! + \qmlproperty QStringList QtSensors5::SensorGesture::invalidGestures + This property holds the requested gestures that were not found on the system. +*/ +QStringList QSensor2Gesture::invalidGestures() const +{ + if (_sensorGesture) + return _sensorGesture->invalidIds(); + return QStringList(); +} + +/*! + \qmlproperty bool QtSensors5::SensorGesture::enabled + This property can be used to activate or deactivate the sensor gesture. + \sa {QtSensors5::SensorGesture::detected} {detected} +*/ +bool QSensor2Gesture::enabled() const +{ + return _enabled; +} + +void QSensor2Gesture::setEnabled(bool value) +{ + _enabled = value; + if (_init) + return; + + if (_enabled != _oldEnabled){ + _oldEnabled = _enabled; + if (_sensorGesture){ + if (_enabled){ +#ifdef LOGGESTURQMLAPI + qDebug() << "start detection" << _gestureIds; +#endif + _sensorGesture->startDetection(); + } + else { +#ifdef LOGGESTURQMLAPI + qDebug() << "stop detection" << _gestureIds; +#endif + _sensorGesture->stopDetection(); + } + } + emit enabledChanged(); + } +} + +/*! + \qmlsignal QtSensors5::SensorGesture::detected(QString gesture) + This signal is emitted whenever a gesture is detected. + The gesture parameter contains the gesture that was detected. +*/ + +/* + private funtion implementation +*/ +void QSensor2Gesture::deleteGesture() +{ + if (_sensorGesture){ + bool emitinvalidchange = !invalidGestures().isEmpty(); + bool emitvalidchange = !validGestures().isEmpty(); + + if (_sensorGesture->isActive()) { + _sensorGesture->stopDetection(); + } + delete _sensorGesture; + _sensorGesture = 0; + + if (emitinvalidchange){ + emit invalidGesturesChanged(); + } + if (emitvalidchange){ + emit validGesturesChanged(); + } + } +} + +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 + , SIGNAL(detected(QString)) + , this + , SIGNAL(detected(QString))); + emit validGesturesChanged(); + } + if (!invalidGestures().isEmpty()) + emit invalidGesturesChanged(); +} + +/* + End of private funtion implementation +*/ + +QT_END_NAMESPACE diff --git a/src/imports/sensors2/qsensor2gesture.h b/src/imports/sensors2/qsensor2gesture.h new file mode 100644 index 00000000..009428d2 --- /dev/null +++ b/src/imports/sensors2/qsensor2gesture.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSENSOR2GESTURE_H +#define QSENSOR2GESTURE_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QSensorGesture; +class QSensorGestureManager; +class QSensor2Gesture : public QObject, public QDeclarativeParserStatus +{ + 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(QDeclarativeParserStatus) + +public: + QSensor2Gesture(QObject* parent = 0); + virtual ~QSensor2Gesture(); + void classBegin(); + void componentComplete(); + +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 _enabled; + bool _oldEnabled; + bool _init; + QStringList _gestures; + + QSensorGesture* _sensorGesture; + QSensorGestureManager* _sensorGestureManager; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QSensor2Gesture) + +#endif // QSENSOR2GESTURE_H diff --git a/src/imports/sensors2/sensors2.cpp b/src/imports/sensors2/sensors2.cpp index d91ba4d5..b1320e73 100644 --- a/src/imports/sensors2/sensors2.cpp +++ b/src/imports/sensors2/sensors2.cpp @@ -44,6 +44,7 @@ #include "qsensor2ambientlight.h" #include "qsensor2proximity.h" #include "qsensor2tilt.h" +#include "qsensor2gesture.h" #include QT_BEGIN_NAMESPACE @@ -60,6 +61,7 @@ public: qmlRegisterType(uri, 5, 0, "TiltSensor"); qmlRegisterType(uri, 5, 0, "AmbientLightSensor"); qmlRegisterType(uri, 5, 0, "ProximitySensor"); + qmlRegisterType(uri, 5, 0, "SensorGesture"); } }; diff --git a/src/imports/sensors2/sensors2.pro b/src/imports/sensors2/sensors2.pro index d7133b27..ec0bca97 100644 --- a/src/imports/sensors2/sensors2.pro +++ b/src/imports/sensors2/sensors2.pro @@ -8,11 +8,13 @@ QT += declarative sensors SOURCES += sensors2.cpp \ qsensor2ambientlight.cpp \ qsensor2proximity.cpp \ - qsensor2tilt.cpp + qsensor2tilt.cpp \ + qsensor2gesture.cpp HEADERS += qsensor2ambientlight.h \ qsensor2proximity.h \ - qsensor2tilt.h + qsensor2tilt.h \ + qsensor2gesture.h DESTDIR = $$QT.sensors.imports/$$TARGETPATH target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH @@ -35,3 +37,5 @@ symbian { DEPLOYMENT = importFiles } + + -- cgit v1.2.3