diff options
author | Wolfgang Beck <wolfgang.beck@nokia.com> | 2011-11-01 16:56:55 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-10 06:21:04 +0100 |
commit | 1f20d5ef3e27981e2af2c6533656694ea3335678 (patch) | |
tree | 4e99a1106937cddb8d46ed33d8fdb29bd17658c2 /src | |
parent | db2df16d1aa7ece4ee27b16122142c002b01d65d (diff) | |
download | qtsensors-1f20d5ef3e27981e2af2c6533656694ea3335678.tar.gz |
Add QtSensorGesture QML API
Change-Id: Iaa0a875941316c341d69f473eeba3be4a87ec736
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lincoln Ramsay <lincoln.ramsay@nokia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/sensors2/qsensor2gesture.cpp | 266 | ||||
-rw-r--r-- | src/imports/sensors2/qsensor2gesture.h | 106 | ||||
-rw-r--r-- | src/imports/sensors2/sensors2.cpp | 2 | ||||
-rw-r--r-- | src/imports/sensors2/sensors2.pro | 8 |
4 files changed, 380 insertions, 2 deletions
diff --git a/src/imports/sensors2/qsensor2gesture.cpp b/src/imports/sensors2/qsensor2gesture.cpp new file mode 100644 index 0000000..33d9723 --- /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 <qsensorgesture.h> +#include <qsensorgesturemanager.h> + +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 0000000..009428d --- /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 <QtDeclarative/QDeclarativeParserStatus> +#include <QtDeclarative/qdeclarative.h> +#include <QtCore/QStringList> +#include <QtCore/QMap> + +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 d91ba4d..b1320e7 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 <QtCore/QDebug> QT_BEGIN_NAMESPACE @@ -60,6 +61,7 @@ public: qmlRegisterType<QSensor2Tilt>(uri, 5, 0, "TiltSensor"); qmlRegisterType<QSensor2AmbientLight>(uri, 5, 0, "AmbientLightSensor"); qmlRegisterType<QSensor2Proximity>(uri, 5, 0, "ProximitySensor"); + qmlRegisterType<QSensor2Gesture>(uri, 5, 0, "SensorGesture"); } }; diff --git a/src/imports/sensors2/sensors2.pro b/src/imports/sensors2/sensors2.pro index d7133b2..ec0bca9 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 } + + |