diff options
author | Lincoln Ramsay <lincoln.ramsay@nokia.com> | 2011-10-04 15:14:01 +1000 |
---|---|---|
committer | Lincoln Ramsay <lincoln.ramsay@nokia.com> | 2011-10-10 09:25:31 +1000 |
commit | 4e04cda0e600f4e3bb148956ba631647a5c1fa4c (patch) | |
tree | 5dd8a0f2e2a0b2cdc8075f05a66cb1f6689e51c4 /examples/sensors/grue | |
parent | 93be0e5c0fd1694b0868187ed3daa006dfa4bd79 (diff) | |
download | qtsensors-4e04cda0e600f4e3bb148956ba631647a5c1fa4c.tar.gz |
Grue Sensor example
Updated, documented as an example.
Now with a QML import and a QML app.
Fixes: MTMW-85
Change-Id: I06772cf4a07ea0d2ec7bfccda6e8f51b5d866c20
Reviewed-on: http://codereview.qt-project.org/6016
Reviewed-by: Lincoln Ramsay <lincoln.ramsay@nokia.com>
Diffstat (limited to 'examples/sensors/grue')
-rw-r--r-- | examples/sensors/grue/console_app/console_app.pro | 5 | ||||
-rw-r--r-- | examples/sensors/grue/console_app/main.cpp | 89 | ||||
-rw-r--r-- | examples/sensors/grue/grue.pro | 8 | ||||
-rw-r--r-- | examples/sensors/grue/import/import.pro | 19 | ||||
-rw-r--r-- | examples/sensors/grue/import/main.cpp | 117 | ||||
-rw-r--r-- | examples/sensors/grue/import/qmldir | 1 | ||||
-rw-r--r-- | examples/sensors/grue/import/qsensorsimport.pri | 38 | ||||
-rw-r--r-- | examples/sensors/grue/lib/gruesensor.cpp | 137 | ||||
-rw-r--r-- | examples/sensors/grue/lib/gruesensor.h | 79 | ||||
-rw-r--r-- | examples/sensors/grue/lib/gruesensor_p.h | 66 | ||||
-rw-r--r-- | examples/sensors/grue/lib/lib.pro | 17 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensorimpl.cpp | 135 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensorimpl.h | 74 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/main.cpp | 82 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/plugin.pro | 20 | ||||
-rw-r--r-- | examples/sensors/grue/qml_app/main.qml | 75 | ||||
-rw-r--r-- | examples/sensors/grue/qml_app/qml_app.qmlproject | 20 |
17 files changed, 982 insertions, 0 deletions
diff --git a/examples/sensors/grue/console_app/console_app.pro b/examples/sensors/grue/console_app/console_app.pro new file mode 100644 index 0000000..fefc015 --- /dev/null +++ b/examples/sensors/grue/console_app/console_app.pro @@ -0,0 +1,5 @@ +TEMPLATE=app +TARGET=detect_grue +QT=core sensors +SOURCES=main.cpp + diff --git a/examples/sensors/grue/console_app/main.cpp b/examples/sensors/grue/console_app/main.cpp new file mode 100644 index 0000000..789e645 --- /dev/null +++ b/examples/sensors/grue/console_app/main.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore> +#include <qsensor.h> + +class Filter : public QSensorFilter +{ + int lastPercent; +public: + Filter() + : QSensorFilter() + , lastPercent(0) + { + } + + bool filter(QSensorReading *reading) + { + int percent = reading->property("chanceOfBeingEaten").value<qreal>() * 100; + if (percent == 0) { + qDebug() << "It is light. You are safe from Grues."; + } else if (lastPercent == 0) { + qDebug() << "It is dark. You are likely to be eaten by a Grue."; + } + if (percent == 100) { + qDebug() << "You have been eaten by a Grue!"; + QCoreApplication::instance()->quit(); + } else if (percent) + qDebug() << "Your chance of being eaten by a Grue:" << percent << "percent."; + lastPercent = percent; + return false; + } +}; + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QSensor sensor("GrueSensor"); + + Filter filter; + sensor.addFilter(&filter); + sensor.start(); + + if (!sensor.isActive()) { + qWarning("The Grue sensor didn't start. You're on your own!"); + return 1; + } + + return app.exec(); +} + diff --git a/examples/sensors/grue/grue.pro b/examples/sensors/grue/grue.pro new file mode 100644 index 0000000..5af352b --- /dev/null +++ b/examples/sensors/grue/grue.pro @@ -0,0 +1,8 @@ +TEMPLATE = subdirs + +SUBDIRS += lib plugin console_app +SUBDIRS += import #qml_app + +plugin.depends = lib +import.depends = lib + diff --git a/examples/sensors/grue/import/import.pro b/examples/sensors/grue/import/import.pro new file mode 100644 index 0000000..fc163b6 --- /dev/null +++ b/examples/sensors/grue/import/import.pro @@ -0,0 +1,19 @@ +TARGET = declarative_grue +TARGETPATH = Grue + +include(qsensorsimport.pri) + +QT = declarative sensors + +DESTDIR = $$QT.sensors.imports/$$TARGETPATH + +INCLUDEPATH += $$PWD/../lib +LIBS += -L$$OUT_PWD/../lib -lgruesensor + +SOURCES = main.cpp + +symbian { + TARGET.EPOCALLOWDLLDATA = 1 + TARGET.CAPABILITY = ALL -TCB +} + diff --git a/examples/sensors/grue/import/main.cpp b/examples/sensors/grue/import/main.cpp new file mode 100644 index 0000000..ec1d470 --- /dev/null +++ b/examples/sensors/grue/import/main.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** 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 <QtDeclarative/qdeclarativeextensionplugin.h> +#include <QtDeclarative/qdeclarative.h> + +#include <gruesensor.h> + +QT_BEGIN_NAMESPACE + +class GrueSensorQmlImport : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + virtual void registerTypes(const char *uri) + { + char const * const package = "Grue"; + if (QLatin1String(uri) != QLatin1String(package)) return; + int major; + int minor; + + // Register the 1.0 interfaces + major = 1; + minor = 0; + qmlRegisterType <GrueSensor >(package, major, minor, "GrueSensor"); + qmlRegisterUncreatableType<GrueSensorReading>(package, major, minor, "GrueSensorReading", QLatin1String("Cannot create GrueSensorReading")); + } +}; + +QT_END_NAMESPACE + +#include "main.moc" + +Q_EXPORT_PLUGIN2(gruesensorqmlimport, QT_PREPEND_NAMESPACE(GrueSensorQmlImport)); + +/*! + \qmlclass GrueSensor GrueSensor + \inqmlmodule Grue 1 + \brief The GrueSensor element reports on your chance of being eaten by a Grue. + + The GrueSensor element reports on your chance of being eaten by a Grue. + + This element wraps the GrueSensor class. Please see the documentation for + GrueSensor for details. + + \sa {QtMobility.sensors QML Limitations} +*/ + +/*! + \qmlproperty GrueSensorReading Grue1::GrueSensor::reading + Holds the most recent Grue sensor reading. + Please see QSensor::reading for information about this property. +*/ + +/*! + \qmlsignal Grue1::GrueSensor::onReadingChanged() + Called when the reading object changes. + Please see QSensor::readingChanged() for information about this signal. +*/ + +/*! + \qmlclass GrueSensorReading GrueSensorReading + \inqmlmodule Grue 1 + \inherits QtMobility.sensors1::SensorReading + \brief The GrueSensorReading element holds the most recent GrueSensor reading. + + The GrueSensorReading element holds the most recent GrueSensor reading. + + This element wraps the GrueSensorReading class. Please see the documentation for + GrueSensorReading for details. + + This element cannot be directly created. +*/ + +/*! + \qmlproperty qreal Grue1::GrueSensorReading::chanceOfBeingEaten + Please see GrueSensorReading::chanceOfBeingEaten for information about this property. +*/ + diff --git a/examples/sensors/grue/import/qmldir b/examples/sensors/grue/import/qmldir new file mode 100644 index 0000000..266b7c1 --- /dev/null +++ b/examples/sensors/grue/import/qmldir @@ -0,0 +1 @@ +plugin declarative_grue diff --git a/examples/sensors/grue/import/qsensorsimport.pri b/examples/sensors/grue/import/qsensorsimport.pri new file mode 100644 index 0000000..d5cfeff --- /dev/null +++ b/examples/sensors/grue/import/qsensorsimport.pri @@ -0,0 +1,38 @@ +load(qt_module) + +symbian:load(qt_plugin) +TEMPLATE = lib +CONFIG += qt plugin + +win32|mac:!wince*:!win32-msvc:!macx-xcode:CONFIG += debug_and_release + +isEmpty(TARGETPATH) { + error("qimportbase.pri: You must provide a TARGETPATH!") +} +isEmpty(TARGET) { + error("qimportbase.pri: You must provide a TARGET!") +} + +QMLDIRFILE = $${_PRO_FILE_PWD_}/qmldir +copy2build.input = QMLDIRFILE +copy2build.output = $$QT.sensors.imports/$$TARGETPATH/qmldir +!contains(TEMPLATE_PREFIX, vc):copy2build.variable_out = PRE_TARGETDEPS +copy2build.commands = $$QMAKE_COPY ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT} +copy2build.name = COPY ${QMAKE_FILE_IN} +copy2build.CONFIG += no_link +# `clean' should leave the build in a runnable state, which means it shouldn't delete qmldir +copy2build.CONFIG += no_clean +QMAKE_EXTRA_COMPILERS += copy2build + +TARGET = $$qtLibraryTarget($$TARGET) +contains(QT_CONFIG, reduce_exports):CONFIG += hide_symbols + +load(qt_targets) + +wince*:LIBS += $$QMAKE_LIBS_GUI + +symbian: { + TARGET.EPOCALLOWDLLDATA=1 + TARGET.CAPABILITY = All -Tcb + load(armcc_warnings) +} diff --git a/examples/sensors/grue/lib/gruesensor.cpp b/examples/sensors/grue/lib/gruesensor.cpp new file mode 100644 index 0000000..da322cb --- /dev/null +++ b/examples/sensors/grue/lib/gruesensor.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "gruesensor.h" +#include "gruesensor_p.h" + +IMPLEMENT_READING(GrueSensorReading) + +/*! + \class GrueSensorReading + + \brief The GrueSensorReading class holds readings from the Grue sensor. + + The Grue Sensor informs you of your chance of being eaten by a Grue. + + Grues love the dark so as long as your surroundings are relatively light + you are safe. However the more time you spend in the dark, the higher + your chances are of being eaten by a Grue. +*/ + +/*! + \property GrueSensorReading::chanceOfBeingEaten + \brief holds your chance of being eaten. + + The value is the probability (from 0 to 1) that a Grue will eat you. + A probability of 1 means you are currently being eaten. The darker + it is, the more likely you are to be eaten by a Grue. The longer you + stay in a dark area, the more likely you are to be eaten by a Grue. + If you are in a lit room, the probability will be 0 as Grues fear light. +*/ + +qreal GrueSensorReading::chanceOfBeingEaten() const +{ + return d->chanceOfBeingEaten; +} + +void GrueSensorReading::setChanceOfBeingEaten(qreal chanceOfBeingEaten) +{ + d->chanceOfBeingEaten = chanceOfBeingEaten; +} + +// ===================================================================== + +// begin generated code + +/*! + \class GrueFilter + + \brief The GrueFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to GrueSensorReading + instead of QSensorReading. +*/ + +/*! + \fn GrueFilter::filter(GrueSensorReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const GrueSensor::type("GrueSensor"); + +/*! + \class GrueSensor + + \brief The GrueSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a GrueSensorReading instead of a QSensorReading. + + For details about how the sensor works, see \l GrueSensorReading. + + \sa GrueSensorReading +*/ + +/*! + \fn GrueSensor::GrueSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn GrueSensor::~GrueSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn GrueSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ +// end generated code + +#include "moc_gruesensor.cpp" diff --git a/examples/sensors/grue/lib/gruesensor.h b/examples/sensors/grue/lib/gruesensor.h new file mode 100644 index 0000000..52b08e9 --- /dev/null +++ b/examples/sensors/grue/lib/gruesensor.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GRUESENSOR_H +#define GRUESENSOR_H + +#include <qsensor.h> + +class GrueSensorReadingPrivate; + +class GrueSensorReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal chanceOfBeingEaten READ chanceOfBeingEaten WRITE setChanceOfBeingEaten) + DECLARE_READING(GrueSensorReading) +public: + qreal chanceOfBeingEaten() const; + void setChanceOfBeingEaten(qreal chanceOfBeingEaten); +}; + +// begin generated code + +class GrueFilter : public QSensorFilter +{ +public: + virtual bool filter(GrueSensorReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast<GrueSensorReading*>(reading)); } +}; + +class GrueSensor : public QSensor +{ + Q_OBJECT +public: + explicit GrueSensor(QObject *parent = 0) : QSensor(GrueSensor::type, parent) {} + virtual ~GrueSensor() {} + GrueSensorReading *reading() const { return static_cast<GrueSensorReading*>(QSensor::reading()); } + static char const * const type; +}; +// end generated code + +#endif diff --git a/examples/sensors/grue/lib/gruesensor_p.h b/examples/sensors/grue/lib/gruesensor_p.h new file mode 100644 index 0000000..5eff57e --- /dev/null +++ b/examples/sensors/grue/lib/gruesensor_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GRUESENSOR_P_H +#define GRUESENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +class GrueSensorReadingPrivate +{ +public: + GrueSensorReadingPrivate() + : chanceOfBeingEaten(-1.0) + { + } + + qreal chanceOfBeingEaten; +}; + +#endif diff --git a/examples/sensors/grue/lib/lib.pro b/examples/sensors/grue/lib/lib.pro new file mode 100644 index 0000000..80bf136 --- /dev/null +++ b/examples/sensors/grue/lib/lib.pro @@ -0,0 +1,17 @@ +TEMPLATE = lib +TARGET = gruesensor + +QT = core sensors + +HEADERS += gruesensor.h\ + gruesensor_p.h\ + +SOURCES += gruesensor.cpp\ + +DESTDIR=$$QT.sensors.libs + +symbian { + TARGET.EPOCALLOWDLLDATA = 1 + TARGET.CAPABILITY = ALL -TCB +} + diff --git a/examples/sensors/grue/plugin/gruesensorimpl.cpp b/examples/sensors/grue/plugin/gruesensorimpl.cpp new file mode 100644 index 0000000..4a277c9 --- /dev/null +++ b/examples/sensors/grue/plugin/gruesensorimpl.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "gruesensorimpl.h" +#include <QDebug> +#include <QTimer> + +char const * const gruesensorimpl::id("grue.gruesensor"); + +gruesensorimpl::gruesensorimpl(QSensor *sensor) + : QSensorBackend(sensor) +{ + // We need a light sensor + lightSensor = new QAmbientLightSensor(this); + connect(lightSensor, SIGNAL(readingChanged()), this, SLOT(lightChanged())); + lightSensor->connectToBackend(); + + // We need a timer + darkTimer = new QTimer(this); + darkTimer->setInterval(1000); + connect(darkTimer, SIGNAL(timeout()), this, SLOT(increaseChance())); + + // We use this as our timestamp source + timer.start(); + +//! [setReading] + // Register our reading instance + setReading<GrueSensorReading>(&m_reading); +//! [setReading] + +//! [metadata] + // Supply metadata + // We can run as fast as the light sensor does + setDataRates(lightSensor); + // Only one output range, 0 to 1 in .1 increments + addOutputRange(0, 1, 0.1); + setDescription(QLatin1String("Grue Sensor")); +//! [metadata] +} + +void gruesensorimpl::start() +{ +//! [start] + lightSensor->setDataRate(sensor()->dataRate()); + lightSensor->start(); + // If the light sensor doesn't work we don't work either + if (!lightSensor->isActive()) + sensorStopped(); + if (lightSensor->isBusy()) + sensorBusy(); +//! [start] +} + +void gruesensorimpl::stop() +{ + lightSensor->stop(); +} + +void gruesensorimpl::lightChanged() +{ + qreal chance = 0.0; + darkTimer->stop(); + + switch (lightSensor->reading()->lightLevel()) { + case QAmbientLightReading::Dark: + // It is dark. You are likely to be eaten by a grue. + chance = 0.1; + darkTimer->start(); + break; + default: + break; + } + + // Only send an update if the value has changed. + if (chance != m_reading.chanceOfBeingEaten() || m_reading.timestamp() == 0) { + m_reading.setTimestamp(timer.elapsed()); + m_reading.setChanceOfBeingEaten(chance); + + newReadingAvailable(); + } +} + +void gruesensorimpl::increaseChance() +{ + // The longer you stay in the dark, the higher your chance of being eaten + qreal chance = m_reading.chanceOfBeingEaten() + 0.1; + + m_reading.setTimestamp(timer.elapsed()); + m_reading.setChanceOfBeingEaten(chance); + + newReadingAvailable(); + + // No point in using the timer anymore if we've hit 1... you can't get more + // likely to be eaten than 100% + if (chance == 1.0) + darkTimer->stop(); +} + diff --git a/examples/sensors/grue/plugin/gruesensorimpl.h b/examples/sensors/grue/plugin/gruesensorimpl.h new file mode 100644 index 0000000..bddca84 --- /dev/null +++ b/examples/sensors/grue/plugin/gruesensorimpl.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GRUESENSORIMPL_H +#define GRUESENSORIMPL_H + +#include <qsensorbackend.h> +#include "gruesensor.h" +#include <qambientlightsensor.h> +#include <QTime> + +class QTimer; + +class gruesensorimpl : public QSensorBackend +{ + Q_OBJECT +public: + static char const * const id; + + gruesensorimpl(QSensor *sensor); + + void start(); + void stop(); + +private Q_SLOTS: + void lightChanged(); + void increaseChance(); + +private: + GrueSensorReading m_reading; + QAmbientLightSensor *lightSensor; + QTimer *darkTimer; + QTime timer; +}; + +#endif + diff --git a/examples/sensors/grue/plugin/main.cpp b/examples/sensors/grue/plugin/main.cpp new file mode 100644 index 0000000..d471ece --- /dev/null +++ b/examples/sensors/grue/plugin/main.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "gruesensorimpl.h" +#include <qsensorplugin.h> +#include <qsensorbackend.h> +#include <qsensormanager.h> +#include <QFile> +#include <QDebug> + +class GrueSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorChangesInterface, public QSensorBackendFactory +{ + Q_OBJECT + Q_INTERFACES(QSensorPluginInterface QSensorChangesInterface) +public: + void registerSensors() + { + qDebug() << "loaded the grue plugin"; + } + + void sensorsChanged() + { + if (!QSensor::defaultSensorForType(QAmbientLightSensor::type).isEmpty()) { + // There is a light sensor available. Register the backend + if (!QSensorManager::isBackendRegistered(GrueSensor::type, gruesensorimpl::id)) + QSensorManager::registerBackend(GrueSensor::type, gruesensorimpl::id, this); + } else { + if (QSensorManager::isBackendRegistered(GrueSensor::type, gruesensorimpl::id)) + QSensorManager::unregisterBackend(GrueSensor::type, gruesensorimpl::id); + } + } + + QSensorBackend *createBackend(QSensor *sensor) + { + if (sensor->identifier() == gruesensorimpl::id) + return new gruesensorimpl(sensor); + + return 0; + } +}; + +Q_EXPORT_PLUGIN2(libsensors_grueplugin, GrueSensorPlugin); + +#include "main.moc" + diff --git a/examples/sensors/grue/plugin/plugin.pro b/examples/sensors/grue/plugin/plugin.pro new file mode 100644 index 0000000..fed8460 --- /dev/null +++ b/examples/sensors/grue/plugin/plugin.pro @@ -0,0 +1,20 @@ +TEMPLATE = lib +CONFIG += plugin +TARGET = $$qtLibraryTarget(qtsensors_grue) +PLUGIN_TYPE = sensors + +QT = core sensors + +INCLUDEPATH += $$PWD/../lib +LIBS += -L$$QT.sensors.libs -lgruesensor + +HEADERS += gruesensorimpl.h\ + +SOURCES += gruesensorimpl.cpp\ + main.cpp\ + +symbian { + TARGET.EPOCALLOWDLLDATA = 1 + TARGET.CAPABILITY = ALL -TCB +} + diff --git a/examples/sensors/grue/qml_app/main.qml b/examples/sensors/grue/qml_app/main.qml new file mode 100644 index 0000000..278bf72 --- /dev/null +++ b/examples/sensors/grue/qml_app/main.qml @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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 examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import Grue 1.0 + +Item { + width: 200 + height: 300 + + GrueSensor { + id: sensor + active: true + property int lastPercent: 0 + onReadingChanged: { + var percent = Math.floor(reading.chanceOfBeingEaten * 100); + if (percent == 0) { + text.text = "It is light. You are safe from Grues."; + } else if (lastPercent == 0) { + text.text = "It is dark. You are likely to be eaten by a Grue."; + } + if (percent == 100) { + text.text += "You have been eaten by a Grue!"; + sensor.active = false; + } else if (percent) { + text.text += "Your chance of being eaten by a Grue: "+percent+" percent."; + } + lastPercent = percent; + } + } + + Text { + id: text + anchors.fill: parent + text: "I can't tell if you're going to be eaten by a Grue or not. You're on your own!" + wrapMode: Text.WordWrap + } +} diff --git a/examples/sensors/grue/qml_app/qml_app.qmlproject b/examples/sensors/grue/qml_app/qml_app.qmlproject new file mode 100644 index 0000000..2218c43 --- /dev/null +++ b/examples/sensors/grue/qml_app/qml_app.qmlproject @@ -0,0 +1,20 @@ +/* File generated by Qt Creator, version 2.2.0 */ + +import QmlProject 1.1 + +Project { + mainFile: "main.qml" + + /* Include .qml, .js, and image files from current directory and subdirectories */ + QmlFiles { + directory: "." + } + JavaScriptFiles { + directory: "." + } + ImageFiles { + directory: "." + } + /* List of plugin directories passed to QML runtime */ + // importPaths: [ "../exampleplugin" ] +} |