summaryrefslogtreecommitdiffstats
path: root/src/sensorsquick/qmlsensorglobal.cpp
blob: 73f865c452ca3dab6fb62fb77f15b8cc3bd51cec (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qmlsensorglobal_p.h"
#include <QtSensors/QSensor>

QT_BEGIN_NAMESPACE

/*!
    \qmltype QmlSensors
//!    \instantiates QmlSensorGlobal
    \inqmlmodule QtSensors
    \since QtSensors 5.0
    \brief The QmlSensors singleton provides the module API.

    The QmlSensors singleton provides the module API.

    This element cannot be directly created, but its functionality
    can be accessed as a QML singleton as illustrated below:

    \code
    import QtSensors
    import QtSensors as Sensors
    ...
        Component.onCompleted: {
            var types = Sensors.QmlSensors.sensorTypes();
            console.log(types.join(", "));
        }
    \endcode
*/

QmlSensorGlobal::QmlSensorGlobal(QObject *parent)
    : QObject(parent)
    , m_sensor(new QSensor(QByteArray(), this))
{
    connect(m_sensor, SIGNAL(availableSensorsChanged()), this, SIGNAL(availableSensorsChanged()));
}

QmlSensorGlobal::~QmlSensorGlobal()
{
}

/*!
    \qmlmethod list<string> QmlSensors::sensorTypes()
    Returns a list of the sensor types that have been registered.

    Please see QSensor::sensorTypes() for information.
*/
QStringList QmlSensorGlobal::sensorTypes() const
{
    QStringList ret;
    const QList<QByteArray> sensorTypes = QSensor::sensorTypes();
    ret.reserve(sensorTypes.size());
    for (const QByteArray &type : sensorTypes)
        ret << QString::fromLocal8Bit(type);
    return ret;
}

/*!
    \qmlmethod list<string> QmlSensors::sensorsForType(type)
    Returns a list of the sensor identifiers that have been registered for \a type.

    Please see QSensor::sensorsForType() for information.
*/
QStringList QmlSensorGlobal::sensorsForType(const QString &type) const
{
    QStringList ret;
    const QList<QByteArray> sensors = QSensor::sensorsForType(type.toLocal8Bit());
    ret.reserve(sensors.size());
    for (const QByteArray &identifier : sensors)
        ret << QString::fromLocal8Bit(identifier);
    return ret;
}

/*!
    \qmlmethod string QmlSensors::defaultSensorForType(type)
    Returns the default sensor identifier that has been registered for \a type.

    Please see QSensor::defaultSensorForType() for information.
*/
QString QmlSensorGlobal::defaultSensorForType(const QString &type) const
{
    return QString::fromLocal8Bit(QSensor::defaultSensorForType(type.toLocal8Bit()));
}

QT_END_NAMESPACE