diff options
author | Wolfgang Beck <wolfgang.beck@nokia.com> | 2011-12-15 14:11:46 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-12-20 04:38:54 +0100 |
commit | 52a414bd2291dbf9b99606019ab50a914280ecb0 (patch) | |
tree | e9c25309ea7aa6c121b688459ab491436406e9e8 /examples/sensors/grue | |
parent | 72a3d3b60b20abbc7e6af2ec1e33275bfa7841b1 (diff) | |
download | qtsensors-52a414bd2291dbf9b99606019ab50a914280ecb0.tar.gz |
Grue example doesn't work in simulator
Change-Id: I486e24befd3f31345ec7f8dffd9a183f9f2ec4cb
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lincoln Ramsay <lincoln.ramsay@nokia.com>
Diffstat (limited to 'examples/sensors/grue')
-rw-r--r-- | examples/sensors/grue/lib/gruesensor.cpp | 8 | ||||
-rw-r--r-- | examples/sensors/grue/lib/gruesensor.h | 6 | ||||
-rw-r--r-- | examples/sensors/grue/lib/gruesensor_p.h | 4 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensorimpl.cpp | 17 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensorimpl.h | 1 | ||||
-rw-r--r-- | examples/sensors/grue/qml_app/main.qml | 16 |
6 files changed, 28 insertions, 24 deletions
diff --git a/examples/sensors/grue/lib/gruesensor.cpp b/examples/sensors/grue/lib/gruesensor.cpp index 549eb98..12be650 100644 --- a/examples/sensors/grue/lib/gruesensor.cpp +++ b/examples/sensors/grue/lib/gruesensor.cpp @@ -59,19 +59,19 @@ IMPLEMENT_READING(GrueSensorReading) \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 + The value is the probability (from 0 to 100) that a Grue will eat you. + A probability of 100 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 +int GrueSensorReading::chanceOfBeingEaten() const { return d->chanceOfBeingEaten; } -void GrueSensorReading::setChanceOfBeingEaten(qreal chanceOfBeingEaten) +void GrueSensorReading::setChanceOfBeingEaten(int chanceOfBeingEaten) { d->chanceOfBeingEaten = chanceOfBeingEaten; } diff --git a/examples/sensors/grue/lib/gruesensor.h b/examples/sensors/grue/lib/gruesensor.h index 1465aaa..e6696a2 100644 --- a/examples/sensors/grue/lib/gruesensor.h +++ b/examples/sensors/grue/lib/gruesensor.h @@ -54,11 +54,11 @@ class GrueSensorReadingPrivate; class Q_GRUE_EXPORT GrueSensorReading : public QSensorReading { Q_OBJECT - Q_PROPERTY(qreal chanceOfBeingEaten READ chanceOfBeingEaten WRITE setChanceOfBeingEaten) + Q_PROPERTY(int chanceOfBeingEaten READ chanceOfBeingEaten WRITE setChanceOfBeingEaten) DECLARE_READING(GrueSensorReading) public: - qreal chanceOfBeingEaten() const; - void setChanceOfBeingEaten(qreal chanceOfBeingEaten); + int chanceOfBeingEaten() const; + void setChanceOfBeingEaten(int chanceOfBeingEaten); }; // begin generated code diff --git a/examples/sensors/grue/lib/gruesensor_p.h b/examples/sensors/grue/lib/gruesensor_p.h index 7d789a5..bf0996e 100644 --- a/examples/sensors/grue/lib/gruesensor_p.h +++ b/examples/sensors/grue/lib/gruesensor_p.h @@ -56,11 +56,11 @@ class GrueSensorReadingPrivate { public: GrueSensorReadingPrivate() - : chanceOfBeingEaten(-1.0) + : chanceOfBeingEaten(-1) { } - qreal chanceOfBeingEaten; + int chanceOfBeingEaten; }; #endif diff --git a/examples/sensors/grue/plugin/gruesensorimpl.cpp b/examples/sensors/grue/plugin/gruesensorimpl.cpp index d06ab8e..7ea4aa0 100644 --- a/examples/sensors/grue/plugin/gruesensorimpl.cpp +++ b/examples/sensors/grue/plugin/gruesensorimpl.cpp @@ -46,6 +46,7 @@ char const * const gruesensorimpl::id("grue.gruesensor"); gruesensorimpl::gruesensorimpl(QSensor *sensor) : QSensorBackend(sensor) + , lightLevel(QAmbientLightReading::Undefined) { // We need a light sensor lightSensor = new QAmbientLightSensor(this); @@ -95,13 +96,18 @@ void gruesensorimpl::stop() void gruesensorimpl::lightChanged() { - qreal chance = 0.0; + if (lightLevel == lightSensor->reading()->lightLevel()) + return; + + lightLevel = lightSensor->reading()->lightLevel(); + + int chance = 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; + chance = 10; darkTimer->start(); break; default: @@ -112,7 +118,6 @@ void gruesensorimpl::lightChanged() if (chance != m_reading.chanceOfBeingEaten() || m_reading.timestamp() == 0) { m_reading.setTimestamp(timer.elapsed()); m_reading.setChanceOfBeingEaten(chance); - newReadingAvailable(); } } @@ -120,16 +125,16 @@ void gruesensorimpl::lightChanged() void gruesensorimpl::increaseChance() { // The longer you stay in the dark, the higher your chance of being eaten - qreal chance = m_reading.chanceOfBeingEaten() + 0.1; + int chance = m_reading.chanceOfBeingEaten() + 10; 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 + // No point in using the timer anymore if we've hit 100... you can't get more // likely to be eaten than 100% - if (chance >= 1.0) + if (chance >= 100) darkTimer->stop(); } diff --git a/examples/sensors/grue/plugin/gruesensorimpl.h b/examples/sensors/grue/plugin/gruesensorimpl.h index fa4938e..55d567b 100644 --- a/examples/sensors/grue/plugin/gruesensorimpl.h +++ b/examples/sensors/grue/plugin/gruesensorimpl.h @@ -70,6 +70,7 @@ private: QAmbientLightSensor *lightSensor; QTimer *darkTimer; QTime timer; + QAmbientLightReading::LightLevel lightLevel; }; #endif diff --git a/examples/sensors/grue/qml_app/main.qml b/examples/sensors/grue/qml_app/main.qml index f3ae6ed..5177b04 100644 --- a/examples/sensors/grue/qml_app/main.qml +++ b/examples/sensors/grue/qml_app/main.qml @@ -50,24 +50,22 @@ Item { GrueSensor { id: sensor active: true - property int lastPercent: 0 onReadingChanged: { - var percent = Math.floor(reading.chanceOfBeingEaten * 100); + var percent = reading.chanceOfBeingEaten; var thetext = ""; - if (percent == 0) { + if (percent === 0) { thetext = "It is light. You are safe from Grues."; - } else if (lastPercent == 0) { - thetext = "It is dark. You are likely to be eaten by a Grue."; } - if (percent == 100) { - thetext += " You have been eaten by a Grue!"; + else if (percent === 100) { + thetext = "You have been eaten by a Grue!"; sensor.active = false; - } else if (percent) { + } + else if (percent > 0) { + thetext = "It is dark. You are likely to be eaten by a Grue."; thetext += " Your chance of being eaten by a Grue: "+percent+" percent."; } text.font.pixelSize = 30; text.text = "<p>" + thetext + "</p>"; - lastPercent = percent; } } |