From 44150b8815fd133bd86b033abd19a41b9ad81fb8 Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Tue, 20 Dec 2011 19:03:08 +1000 Subject: alwaysOn for QtSensors 5.0 sensors refactor the enabled and alwaysOn properties into a common base class use appropriate namespacing to avoid collisions with the QtMobility.sensors elements Change-Id: I6ac9f040d226bb648ede5459df2c26b82feff587 Sanity-Review: Qt Sanity Bot Reviewed-by: Wolfgang Beck --- src/imports/sensors2/qsensor2ambientlight.cpp | 27 +------ src/imports/sensors2/qsensor2ambientlight.h | 9 +-- src/imports/sensors2/qsensor2common.cpp | 110 ++++++++++++++++++++++++++ src/imports/sensors2/qsensor2common.h | 76 ++++++++++++++++++ src/imports/sensors2/qsensor2proximity.cpp | 27 +------ src/imports/sensors2/qsensor2proximity.h | 8 +- src/imports/sensors2/qsensor2tilt.cpp | 12 +-- src/imports/sensors2/qsensor2tilt.h | 9 +-- src/imports/sensors2/sensors2.cpp | 9 ++- src/imports/sensors2/sensors2.pro | 9 +-- 10 files changed, 211 insertions(+), 85 deletions(-) create mode 100644 src/imports/sensors2/qsensor2common.cpp create mode 100644 src/imports/sensors2/qsensor2common.h (limited to 'src/imports/sensors2') diff --git a/src/imports/sensors2/qsensor2ambientlight.cpp b/src/imports/sensors2/qsensor2ambientlight.cpp index ffe7b632..227076bf 100644 --- a/src/imports/sensors2/qsensor2ambientlight.cpp +++ b/src/imports/sensors2/qsensor2ambientlight.cpp @@ -46,6 +46,7 @@ QT_BEGIN_NAMESPACE /*! \qmlclass AmbientLightSensor QSensor2AmbientLight + \inherits QtSensors5::Sensor \inqmlmodule QtSensors 5 \ingroup qml-QtSensors5 \since QtSensors 5.0 @@ -54,7 +55,7 @@ QT_BEGIN_NAMESPACE This element is part of the \bold{QtSensors 5} module. */ QSensor2AmbientLight::QSensor2AmbientLight(QObject* parent) - : QObject(parent) + : qsensor2common(parent) , _lightLevel(QSensor2AmbientLight::Unknown) { _ambientLight = new QAmbientLightSensor(this); @@ -66,30 +67,6 @@ QSensor2AmbientLight::~QSensor2AmbientLight() { } -/*! - \qmlproperty bool QtSensors5::QSensor2AmbientLight::enabled - This property can be used to activate or deactivate the sensor. -*/ -bool QSensor2AmbientLight::enabled() -{ - return _ambientLight->isActive(); -} - -void QSensor2AmbientLight::setEnabled(bool val) -{ - bool active = enabled(); - if (active != val){ - if (val){ - bool ret = _ambientLight->start(); - if (!ret) - qWarning() << "couldn't start the sensor."; - } - else - _ambientLight->stop(); - emit enabledChanged(); - } -} - /*! \qmlproperty enumeration QtSensors5::AmbientLightSensor::lightLevel Holds the ambient light level in the form of the LightLevel enum: diff --git a/src/imports/sensors2/qsensor2ambientlight.h b/src/imports/sensors2/qsensor2ambientlight.h index 664f4577..d50058bc 100644 --- a/src/imports/sensors2/qsensor2ambientlight.h +++ b/src/imports/sensors2/qsensor2ambientlight.h @@ -45,16 +45,15 @@ #include #include #include +#include "qsensor2common.h" QT_BEGIN_NAMESPACE -class QSensor2AmbientLight : public QObject, public QAmbientLightFilter +class QSensor2AmbientLight : public qsensor2common, public QAmbientLightFilter { Q_OBJECT Q_ENUMS(LightLevel) Q_PROPERTY(LightLevel lightLevel READ lightLevel NOTIFY lightLevelChanged) - Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) - public: QSensor2AmbientLight(QObject* parent = 0); virtual ~QSensor2AmbientLight(); @@ -70,15 +69,13 @@ public: Q_SIGNALS: void lightLevelChanged(); - void enabledChanged(); private: // Override of QAmbientLightFilter::filter(QAmbientLightReading*) bool filter(QAmbientLightReading *reading); LightLevel lightLevel(); - bool enabled(); - void setEnabled(bool val); + QSensor *sensor() { return _ambientLight; } QAmbientLightSensor* _ambientLight; LightLevel _lightLevel; }; diff --git a/src/imports/sensors2/qsensor2common.cpp b/src/imports/sensors2/qsensor2common.cpp new file mode 100644 index 00000000..ed158cd5 --- /dev/null +++ b/src/imports/sensors2/qsensor2common.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2012 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 "qsensor2common.h" +#include +#include + +QT_BEGIN_NAMESPACE + +/*! + \qmlclass Sensor qsensor2common + \inqmlmodule QtSensors 5 + \brief The Sensor element serves as a base type for sensors. + + The Sensor element serves as a base type for sensors. + + This element cannot be directly created. Please use one of the sub-classes instead. +*/ + +qsensor2common::qsensor2common(QObject *parent) + : QObject(parent) +{ +} + +qsensor2common::~qsensor2common() +{ +} + +/*! + \qmlproperty bool QtSensors5::Sensor::enabled + Starts or stops the sensor. +*/ + +bool qsensor2common::enabled() +{ + return sensor()->isActive(); +} + +void qsensor2common::setEnabled(bool val) +{ + bool active = enabled(); + if (active != val){ + if (val){ + bool ret = sensor()->start(); + if (!ret) + qWarning() << "couldn't start the sensor."; + } + else + sensor()->stop(); + emit enabledChanged(); + } +} + +/*! + \qmlproperty bool QtSensors5::Sensor::alwaysOn + Keeps the sensor running when the screen turns off. +*/ + +bool qsensor2common::alwaysOn() +{ + return sensor()->isAlwaysOn(); +} + +void qsensor2common::setAlwaysOn(bool alwaysOn) +{ + if (sensor()->isAlwaysOn() == alwaysOn) return; + sensor()->setAlwaysOn(alwaysOn); + emit alwaysOnChanged(); +} + +QT_END_NAMESPACE + diff --git a/src/imports/sensors2/qsensor2common.h b/src/imports/sensors2/qsensor2common.h new file mode 100644 index 00000000..0d87975d --- /dev/null +++ b/src/imports/sensors2/qsensor2common.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 QSENSOR2COMMON_H +#define QSENSOR2COMMON_H + +#include + +QT_BEGIN_NAMESPACE + +class QSensor; + +class qsensor2common : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(bool alwaysOn READ alwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged) +public: + explicit qsensor2common(QObject *parent = 0); + virtual ~qsensor2common(); + + bool enabled(); + virtual void setEnabled(bool val); + + bool alwaysOn(); + void setAlwaysOn(bool alwaysOn); + +signals: + void enabledChanged(); + void alwaysOnChanged(); + +protected: + virtual QSensor *sensor() = 0; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/imports/sensors2/qsensor2proximity.cpp b/src/imports/sensors2/qsensor2proximity.cpp index 4322ad7c..14b0e599 100644 --- a/src/imports/sensors2/qsensor2proximity.cpp +++ b/src/imports/sensors2/qsensor2proximity.cpp @@ -46,6 +46,7 @@ QT_BEGIN_NAMESPACE /*! \qmlclass ProximitySensor QSensor2Proximity + \inherits QtSensors5::Sensor \inqmlmodule QtSensors 5 \ingroup qml-QtSensors5 \since QtSensors 5.0 @@ -55,7 +56,7 @@ QT_BEGIN_NAMESPACE */ QSensor2Proximity::QSensor2Proximity(QObject* parent) - : QObject(parent) + : qsensor2common(parent) , _near(false) { _proximity = new QProximitySensor(this); @@ -66,30 +67,6 @@ QSensor2Proximity::~QSensor2Proximity() { } -/*! - \qmlproperty bool QtSensors5::ProximitySensor::enabled - This property can be used to activate or deactivate the sensor. -*/ -bool QSensor2Proximity::enabled() -{ - return _proximity->isActive(); -} - -void QSensor2Proximity::setEnabled(bool val) -{ - bool active = enabled(); - if (active != val){ - if (val){ - bool ret = _proximity->start(); - if (!ret) - qWarning() << "couldn't start the sensor."; - } - else - _proximity->stop(); - emit enabledChanged(); - } -} - /*! \qmlproperty bool QtSensors5::ProximitySensor::near This property holds whether the sensor has detected something in close proximity. diff --git a/src/imports/sensors2/qsensor2proximity.h b/src/imports/sensors2/qsensor2proximity.h index 2caa6733..c6f8cdc1 100644 --- a/src/imports/sensors2/qsensor2proximity.h +++ b/src/imports/sensors2/qsensor2proximity.h @@ -45,14 +45,14 @@ #include #include #include +#include "qsensor2common.h" QT_BEGIN_NAMESPACE -class QSensor2Proximity : public QObject, public QProximityFilter +class QSensor2Proximity : public qsensor2common, public QProximityFilter { Q_OBJECT Q_PROPERTY(bool near READ near NOTIFY nearChanged) - Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) public: QSensor2Proximity(QObject* parent = 0); @@ -60,16 +60,14 @@ public: Q_SIGNALS: void nearChanged(); - void enabledChanged(); private: // Override of QProximityFilter::filter(QProximityReading*) bool filter(QProximityReading *reading); bool near(); - bool enabled(); - void setEnabled(bool val); private: + QSensor *sensor() { return _proximity; } QProximitySensor* _proximity; bool _near; }; diff --git a/src/imports/sensors2/qsensor2tilt.cpp b/src/imports/sensors2/qsensor2tilt.cpp index ebf51177..8a6f26d2 100644 --- a/src/imports/sensors2/qsensor2tilt.cpp +++ b/src/imports/sensors2/qsensor2tilt.cpp @@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE /*! \qmlclass TiltSensor QSensor2Tilt + \inherits QtSensors5::Sensor \inqmlmodule QtSensors 5 \ingroup qml-QtSensors5 \since QtSensors 5.0 @@ -59,7 +60,7 @@ QT_BEGIN_NAMESPACE This element is part of the \bold{QtSensors 5} module. */ QSensor2Tilt::QSensor2Tilt(QObject* parent) - : QObject(parent) + : qsensor2common(parent) , _yRotation(0) , _xRotation(0) , _radAccuracy(M_PI / 180) @@ -166,15 +167,6 @@ void QSensor2Tilt::setSpeed(const QSensor2Tilt::Speed val) _speed = val; } -/*! - \qmlproperty bool QtSensors5::QSensor2Tilt::enabled - This property can be used to activate or deactivate the sensor. -*/ -bool QSensor2Tilt::enabled() -{ - return _accel->isActive(); -} - void QSensor2Tilt::setEnabled(const bool val) { bool active = enabled(); diff --git a/src/imports/sensors2/qsensor2tilt.h b/src/imports/sensors2/qsensor2tilt.h index c9c90717..79a953f6 100644 --- a/src/imports/sensors2/qsensor2tilt.h +++ b/src/imports/sensors2/qsensor2tilt.h @@ -45,10 +45,11 @@ #include #include #include +#include "qsensor2common.h" QT_BEGIN_NAMESPACE -class QSensor2Tilt : public QObject, public QAccelerometerFilter +class QSensor2Tilt : public qsensor2common, public QAccelerometerFilter { Q_OBJECT Q_ENUMS(Unit Speed) @@ -56,7 +57,6 @@ class QSensor2Tilt : public QObject, public QAccelerometerFilter Q_PROPERTY(qreal xRotation READ xRotation NOTIFY xRotationChanged) Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged) Q_PROPERTY(Speed speed READ speed WRITE setSpeed NOTIFY speedChanged) - Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) Q_PROPERTY(qreal accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged) Q_PROPERTY(QByteArray settings READ settings WRITE setSettings) @@ -82,7 +82,6 @@ Q_SIGNALS: void yRotationChanged(); void xRotationChanged(); void speedChanged(); - void enabledChanged(); void tiltChanged(qreal deltaX, qreal deltaY); void accuracyChanged(); @@ -95,14 +94,14 @@ private: void setUnit(const Unit val); Speed speed(); void setSpeed(const Speed val); - bool enabled(); - void setEnabled(const bool val); + void setEnabled(bool val); qreal accuracy(); void setAccuracy(const qreal val); QByteArray settings() const; void setSettings(const QByteArray val); void createRunModeDataRateMap(); + QSensor *sensor() { return _accel; } QAccelerometer* _accel; qreal _yRotation; qreal _xRotation; diff --git a/src/imports/sensors2/sensors2.cpp b/src/imports/sensors2/sensors2.cpp index cd44a58b..2b30c377 100644 --- a/src/imports/sensors2/sensors2.cpp +++ b/src/imports/sensors2/sensors2.cpp @@ -58,10 +58,11 @@ public: qDebug() << "QSensors2DeclarativeModule::registerTypes(const char *uri)"; Q_ASSERT(QLatin1String(uri) == QLatin1String("QtSensors")); - qmlRegisterType(uri, 5, 0, "TiltSensor"); - qmlRegisterType(uri, 5, 0, "AmbientLightSensor"); - qmlRegisterType(uri, 5, 0, "ProximitySensor"); - qmlRegisterType(uri, 5, 0, "SensorGesture"); + qmlRegisterUncreatableType(uri, 5, 0, "Sensor", QLatin1String("Cannot create Sensor")); + 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 ec0bca97..884c3369 100644 --- a/src/imports/sensors2/sensors2.pro +++ b/src/imports/sensors2/sensors2.pro @@ -9,12 +9,14 @@ SOURCES += sensors2.cpp \ qsensor2ambientlight.cpp \ qsensor2proximity.cpp \ qsensor2tilt.cpp \ - qsensor2gesture.cpp + qsensor2gesture.cpp \ + qsensor2common.cpp HEADERS += qsensor2ambientlight.h \ qsensor2proximity.h \ qsensor2tilt.h \ - qsensor2gesture.h + qsensor2gesture.h \ + qsensor2common.h DESTDIR = $$QT.sensors.imports/$$TARGETPATH target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH @@ -36,6 +38,3 @@ symbian { importFiles.path = $$QT_IMPORTS_BASE_DIR/$$TARGETPATH DEPLOYMENT = importFiles } - - - -- cgit v1.2.3