summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/sensors/grue/lib/gruesensor.cpp8
-rw-r--r--examples/sensors/grue/lib/gruesensor.h6
-rw-r--r--examples/sensors/grue/lib/gruesensor_p.h4
-rw-r--r--examples/sensors/grue/plugin/gruesensorimpl.cpp17
-rw-r--r--examples/sensors/grue/plugin/gruesensorimpl.h1
-rw-r--r--examples/sensors/grue/qml_app/main.qml16
6 files changed, 28 insertions, 24 deletions
diff --git a/examples/sensors/grue/lib/gruesensor.cpp b/examples/sensors/grue/lib/gruesensor.cpp
index 549eb98b..12be6502 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 1465aaaa..e6696a2e 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 7d789a5b..bf0996e9 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 d06ab8ee..7ea4aa00 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 fa4938e7..55d567be 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 f3ae6ed1..5177b04f 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;
}
}