From 4187d0baa96c4731abfcddfe2c24cf4ef4e72ac5 Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Tue, 20 Dec 2011 19:01:11 +1000 Subject: alwaysOn for legacy QML elements You must use QtMobility.sensors 1.3 to get alwaysOn Use apprpriate namespacing to avoid collisions with the QtSensors 5 elements. Change-Id: If0f6dfff116d7de8a6de403b2d7c75ffd48b03b8 Sanity-Review: Qt Sanity Bot Reviewed-by: Zsolt Simon --- src/imports/sensors/sensors.cpp | 91 +++++++++++++++++------- src/sensors/qsensor.cpp | 2 + src/sensors/qsensor.h | 3 +- tests/auto/legacy_sensors/legacy_sensors.pro | 2 +- tests/auto/legacy_sensors/tst_legacy_sensors.cpp | 64 +++++++++++++++++ 5 files changed, 135 insertions(+), 27 deletions(-) diff --git a/src/imports/sensors/sensors.cpp b/src/imports/sensors/sensors.cpp index f1855aca..272d9bab 100644 --- a/src/imports/sensors/sensors.cpp +++ b/src/imports/sensors/sensors.cpp @@ -56,6 +56,43 @@ QT_BEGIN_NAMESPACE +// QtDeclarative doesn't have this for some reason. It has qmlRegisterRevision +// and qmlRegisterUncreatableType but they both only do half the job. This one +// registers an uncreatable type and sets the revision so that derived classes will +// pick up new properties. +template +int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason) +{ + QByteArray name(T::staticMetaObject.className()); + + QByteArray pointerName(name + '*'); + QByteArray listName("QDeclarativeListProperty<" + name + ">"); + + QDeclarativePrivate::RegisterType type = { + 1, + + qRegisterMetaType(pointerName.constData()), + qRegisterMetaType >(listName.constData()), + 0, 0, + reason, + + uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, + + QDeclarativePrivate::attachedPropertiesFunc(), + QDeclarativePrivate::attachedPropertiesMetaObject(), + + QDeclarativePrivate::StaticCastSelector::cast(), + QDeclarativePrivate::StaticCastSelector::cast(), + QDeclarativePrivate::StaticCastSelector::cast(), + + 0, 0, + + 0, + metaObjectRevision + }; + + return QDeclarativePrivate::qmlregister(QDeclarativePrivate::TypeRegistration, &type); +} class QSensorsDeclarativeModule : public QDeclarativeExtensionPlugin { @@ -118,7 +155,7 @@ public: // Register the 1.3 interfaces major = 1; minor = 3; - qmlRegisterUncreatableType(package, major, minor, "Sensor", QLatin1String("Cannot create Sensor")); + qmlRegisterUncreatableType(package, major, minor, "Sensor", QLatin1String("Cannot create Sensor")); qmlRegisterUncreatableType(package, major, minor, "SensorReading", QLatin1String("Cannot create SensorReading")); qmlRegisterType (package, major, minor, "Accelerometer"); qmlRegisterUncreatableType(package, major, minor, "AccelerometerReading", QLatin1String("Cannot create AccelerometerReading")); @@ -155,7 +192,6 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara /*! \qmlclass Sensor QSensor - \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 \brief The Sensor element serves as a base type for sensors. @@ -187,6 +223,12 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara Please see QSensor::reading for information about this property. */ +/*! + \qmlproperty bool QtMobility.sensors1::Sensor::alwaysOn + Keeps the sensor running when the display turns off. + Please see QSensor::alwaysOn for information about this property. +*/ + /*! \qmlsignal QtMobility.sensors1::Sensor::onReadingChanged() Called when the reading object changes. @@ -195,7 +237,6 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara /*! \qmlclass SensorReading QSensorReading - \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 \brief The SensorReading element serves as a base type for sensor readings. @@ -217,7 +258,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass Accelerometer QAccelerometer \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The Accelerometer element reports on linear acceleration along the X, Y and Z axes. @@ -246,7 +287,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass AccelerometerReading QAccelerometerReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The AccelerometerReading element holds the most recent Accelerometer reading. The AccelerometerReading element holds the most recent Accelerometer reading. @@ -276,7 +317,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass AmbientLightSensor QAmbientLightSensor \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The AmbientLightSensor element repors on ambient lighting conditions. The AmbientLightSensor element repors on ambient lighting conditions. @@ -303,7 +344,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass AmbientLightReading QAmbientLightReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The AmbientLightReading element holds the most AmbientLightSensor reading. The AmbientLightReading element holds the most AmbientLightSensor reading. @@ -333,7 +374,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass Compass QCompass \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The Compass element reports on heading using magnetic north as a reference. The Compass element reports on heading using magnetic north as a reference. @@ -360,7 +401,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass CompassReading QCompassReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The CompassReading element holds the most recent Compass reading. The CompassReading element holds the most recent Compass reading. @@ -385,7 +426,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass Magnetometer QMagnetometer \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The Magnetometer element reports on magnetic field strength along the Z, Y and Z axes. @@ -414,7 +455,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass MagnetometerReading QMagnetometerReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The MagnetometerReading element holds the most recent Magnetometer reading. The MagnetometerReading element holds the most recent Magnetometer reading. @@ -449,7 +490,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass OrientationSensor QOrientationSensor \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The OrientationSensor element reports device orientation. The OrientationSensor element reports device orientation. @@ -476,7 +517,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass OrientationReading QOrientationReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The OrientationReading element holds the most recent OrientationSensor reading. The OrientationReading element holds the most recent OrientationSensor reading. @@ -506,7 +547,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass ProximitySensor QProximitySensor \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The ProximitySensor element reports on object proximity. The ProximitySensor element reports on object proximity. @@ -533,7 +574,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass ProximityReading QProximityReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The ProximityReading element holds the most recent ProximitySensor reading. The ProximityReading element holds the most recent ProximitySensor reading. @@ -553,7 +594,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass RotationSensor QRotationSensor \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The RotationSensor element reports on device rotation around the X, Y and Z axes. @@ -582,7 +623,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass RotationReading QRotationReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The RotationReading element holds the most recent RotationSensor reading. The RotationReading element holds the most recent RotationSensor reading. @@ -612,7 +653,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass TapSensor QTapSensor \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The TapSensor element reports tap and double tap events along the X, Y and Z axes. @@ -641,7 +682,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \qmlclass TapReading QTapReading \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The TapReading element holds the most recent TapSensor reading. The TapReading element holds the most recent TapSensor reading. @@ -677,7 +718,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.2 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The LightSensor element reports on light levels using LUX. The LightSensor element reports on light levels using LUX. @@ -705,7 +746,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.2 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The LightReading element holds the most recent LightSensor reading. The LightReading element holds the most recent LightSensor reading. @@ -726,7 +767,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.2 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The Gyroscope element reports on rotational acceleration around the X, Y and Z axes. @@ -753,7 +794,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.2 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The GyroscopeReading element holds the most recent Gyroscope reading. The GyroscopeReading element holds the most recent Gyroscope reading. @@ -784,7 +825,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_type \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.3 - \inherits Sensor + \inherits QtMobility.sensors1::Sensor \brief The IRProximitySensor element reports on infra-red reflectance values. This element wraps the QIRProximitySensor class. Please see the documentation for @@ -810,7 +851,7 @@ Q_EXPORT_PLUGIN2(qsensorsdeclarativemodule, QT_PREPEND_NAMESPACE(QSensorsDeclara \ingroup qml-sensors_reading \inqmlmodule QtMobility.sensors 1 \since QtMobility.sensors 1.3 - \inherits SensorReading + \inherits QtMobility.sensors1::SensorReading \brief The IRProximityReading element holds the most recent IR proximity reading. The IRProximityReading element holds the most recent IR proximity reading. diff --git a/src/sensors/qsensor.cpp b/src/sensors/qsensor.cpp index 9dc11d19..b077353a 100644 --- a/src/sensors/qsensor.cpp +++ b/src/sensors/qsensor.cpp @@ -349,7 +349,9 @@ bool QSensor::isActive() const */ void QSensor::setAlwaysOn(bool alwaysOn) { + if (d->alwaysOn == alwaysOn) return; d->alwaysOn = alwaysOn; + emit alwaysOnChanged(); } bool QSensor::isAlwaysOn() const diff --git a/src/sensors/qsensor.h b/src/sensors/qsensor.h index 08bb0a7e..09b5f7e4 100644 --- a/src/sensors/qsensor.h +++ b/src/sensors/qsensor.h @@ -89,7 +89,7 @@ class Q_SENSORS_EXPORT QSensor : public QObject Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange) Q_PROPERTY(QString description READ description) Q_PROPERTY(int error READ error NOTIFY sensorError) - Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn) + Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged REVISION 1) #ifdef Q_QDOC Q_PROPERTY(int maxBufferSize) Q_PROPERTY(int efficientBufferSize) @@ -153,6 +153,7 @@ Q_SIGNALS: void readingChanged(); void sensorError(int error); void availableSensorsChanged(); + void alwaysOnChanged(); protected: // called by the back end diff --git a/tests/auto/legacy_sensors/legacy_sensors.pro b/tests/auto/legacy_sensors/legacy_sensors.pro index 0597a7f2..a9b1531b 100644 --- a/tests/auto/legacy_sensors/legacy_sensors.pro +++ b/tests/auto/legacy_sensors/legacy_sensors.pro @@ -1,6 +1,6 @@ TEMPLATE=app TARGET=tst_legacy_sensors !no_system_tests:CONFIG += testcase -QT = core testlib gui declarative +QT = core testlib gui declarative sensors SOURCES += tst_legacy_sensors.cpp diff --git a/tests/auto/legacy_sensors/tst_legacy_sensors.cpp b/tests/auto/legacy_sensors/tst_legacy_sensors.cpp index 2a1b5c76..dd7ae297 100644 --- a/tests/auto/legacy_sensors/tst_legacy_sensors.cpp +++ b/tests/auto/legacy_sensors/tst_legacy_sensors.cpp @@ -42,6 +42,7 @@ #include #include #include +#include class tst_legacy_sensors : public QObject { @@ -193,6 +194,69 @@ private slots: QCOMPARE(expected, actual); } } + + void alwaysOn_data() + { + QTest::addColumn("version"); + QTest::addColumn("element"); + QTest::addColumn("validSyntax"); + + QTest::newRow("") << "1.1" << "Accelerometer" << false; + QTest::newRow("") << "1.1" << "AmbientLightSensor" << false; + QTest::newRow("") << "1.1" << "Compass" << false; + QTest::newRow("") << "1.1" << "Magnetometer" << false; + QTest::newRow("") << "1.1" << "OrientationSensor" << false; + QTest::newRow("") << "1.1" << "ProximitySensor" << false; + QTest::newRow("") << "1.1" << "RotationSensor" << false; + QTest::newRow("") << "1.1" << "TapSensor" << false; + + QTest::newRow("") << "1.2" << "Accelerometer" << false; + QTest::newRow("") << "1.2" << "AmbientLightSensor" << false; + QTest::newRow("") << "1.2" << "Compass" << false; + QTest::newRow("") << "1.2" << "Magnetometer" << false; + QTest::newRow("") << "1.2" << "OrientationSensor" << false; + QTest::newRow("") << "1.2" << "ProximitySensor" << false; + QTest::newRow("") << "1.2" << "RotationSensor" << false; + QTest::newRow("") << "1.2" << "TapSensor" << false; + QTest::newRow("") << "1.2" << "LightSensor" << false; + QTest::newRow("") << "1.2" << "Gyroscope" << false; + + QTest::newRow("") << "1.3" << "Accelerometer" << true; + QTest::newRow("") << "1.3" << "AmbientLightSensor" << true; + QTest::newRow("") << "1.3" << "Compass" << true; + QTest::newRow("") << "1.3" << "Magnetometer" << true; + QTest::newRow("") << "1.3" << "OrientationSensor" << true; + QTest::newRow("") << "1.3" << "ProximitySensor" << true; + QTest::newRow("") << "1.3" << "RotationSensor" << true; + QTest::newRow("") << "1.3" << "TapSensor" << true; + QTest::newRow("") << "1.3" << "LightSensor" << true; + QTest::newRow("") << "1.3" << "Gyroscope" << true; + QTest::newRow("") << "1.3" << "IRProximitySensor" << true; + } + + void alwaysOn() + { + QFETCH(QString, version); + QFETCH(QString, element); + QFETCH(bool, validSyntax); + + QDeclarativeEngine engine; + QString qml = QString("import QtQuick 2.0\nimport QtMobility.sensors %1\n%2 {\nalwaysOn: true\n}").arg(version).arg(element); + QDeclarativeComponent c(&engine); + if (!validSyntax) + QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready"); + c.setData(qml.toLocal8Bit(), QUrl::fromLocalFile(QDir::currentPath())); + QObject *obj = c.create(); + if (validSyntax) { + QVERIFY(obj); + QSensor *sensor = qobject_cast(obj); + QVERIFY(sensor); + QCOMPARE(sensor->isAlwaysOn(), true); + delete obj; + } else { + QCOMPARE(obj, static_cast(0)); + } + } }; QTEST_MAIN(tst_legacy_sensors) -- cgit v1.2.3