1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qmlsensorglobal_p.h"
5#include <QtSensors/QSensor>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype QmlSensors
11//! \instantiates QmlSensorGlobal
12 \inqmlmodule QtSensors
13 \since QtSensors 5.0
14 \brief The QmlSensors singleton provides the module API.
15
16 The QmlSensors singleton provides the module API.
17
18 This element cannot be directly created, but its functionality
19 can be accessed as a QML singleton as illustrated below:
20
21 \code
22 import QtSensors
23 import QtSensors as Sensors
24 ...
25 Component.onCompleted: {
26 var types = Sensors.QmlSensors.sensorTypes();
27 console.log(types.join(", "));
28 }
29 \endcode
30*/
31
32QmlSensorGlobal::QmlSensorGlobal(QObject *parent)
33 : QObject(parent)
34 , m_sensor(new QSensor(QByteArray(), this))
35{
36 connect(sender: m_sensor, SIGNAL(availableSensorsChanged()), receiver: this, SIGNAL(availableSensorsChanged()));
37}
38
39QmlSensorGlobal::~QmlSensorGlobal()
40{
41}
42
43/*!
44 \qmlmethod list<string> QmlSensors::sensorTypes()
45 Returns a list of the sensor types that have been registered.
46
47 Please see QSensor::sensorTypes() for information.
48*/
49QStringList QmlSensorGlobal::sensorTypes() const
50{
51 QStringList ret;
52 const QList<QByteArray> sensorTypes = QSensor::sensorTypes();
53 ret.reserve(asize: sensorTypes.size());
54 for (const QByteArray &type : sensorTypes)
55 ret << QString::fromLocal8Bit(ba: type);
56 return ret;
57}
58
59/*!
60 \qmlmethod list<string> QmlSensors::sensorsForType(type)
61 Returns a list of the sensor identifiers that have been registered for \a type.
62
63 Please see QSensor::sensorsForType() for information.
64*/
65QStringList QmlSensorGlobal::sensorsForType(const QString &type) const
66{
67 QStringList ret;
68 const QList<QByteArray> sensors = QSensor::sensorsForType(type: type.toLocal8Bit());
69 ret.reserve(asize: sensors.size());
70 for (const QByteArray &identifier : sensors)
71 ret << QString::fromLocal8Bit(ba: identifier);
72 return ret;
73}
74
75/*!
76 \qmlmethod string QmlSensors::defaultSensorForType(type)
77 Returns the default sensor identifier that has been registered for \a type.
78
79 Please see QSensor::defaultSensorForType() for information.
80*/
81QString QmlSensorGlobal::defaultSensorForType(const QString &type) const
82{
83 return QString::fromLocal8Bit(ba: QSensor::defaultSensorForType(type: type.toLocal8Bit()));
84}
85
86QT_END_NAMESPACE
87

source code of qtsensors/src/sensorsquick/qmlsensorglobal.cpp