summaryrefslogtreecommitdiffstats
path: root/examples/sensors/grue/console_app/main.cpp
blob: 7de663344fe6103ae83c2b46d6ec23d213ca2676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtCore>
#include <qsensor.h>

class Filter : public QSensorFilter
{
    int lastPercent;
public:
    Filter()
        : QSensorFilter()
        , lastPercent(0)
    {
    }

    bool filter(QSensorReading *reading) override
    {
        int percent = reading->property("chanceOfBeingEaten").value<int>();
        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();
}