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#ifndef QMLSENSOR_P_H
5#define QMLSENSOR_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qsensorsquickglobal_p.h"
19
20#include <QObject>
21#include <QProperty>
22#include <QQmlParserStatus>
23#include <QtQml/qqml.h>
24#include <QQmlListProperty>
25#include "qmlsensorrange_p.h"
26
27QT_BEGIN_NAMESPACE
28
29class QSensor;
30class QSensorReading;
31
32class QmlSensorReading;
33
34class QmlSensorPrivate;
35class Q_SENSORSQUICK_PRIVATE_EXPORT QmlSensor : public QObject, public QQmlParserStatus
36{
37 Q_OBJECT
38 Q_DECLARE_PRIVATE(QmlSensor)
39 Q_INTERFACES(QQmlParserStatus)
40 Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged)
41 Q_PROPERTY(QByteArray type READ type CONSTANT)
42 Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend NOTIFY connectedToBackendChanged)
43 Q_PROPERTY(QQmlListProperty<QmlSensorRange> availableDataRates READ availableDataRates NOTIFY availableDataRatesChanged)
44 Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged)
45 Q_PROPERTY(QmlSensorReading* reading READ reading NOTIFY readingChanged BINDABLE bindableReading)
46 Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged)
47 Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
48 Q_PROPERTY(QQmlListProperty<QmlSensorOutputRange> outputRanges READ outputRanges NOTIFY outputRangesChanged)
49 Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange NOTIFY outputRangeChanged)
50 Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
51 Q_PROPERTY(int error READ error NOTIFY errorChanged)
52 Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged)
53 Q_PROPERTY(bool skipDuplicates READ skipDuplicates WRITE setSkipDuplicates NOTIFY skipDuplicatesChanged REVISION 1)
54 Q_PROPERTY(AxesOrientationMode axesOrientationMode READ axesOrientationMode WRITE setAxesOrientationMode NOTIFY axesOrientationModeChanged REVISION 1)
55 Q_PROPERTY(int currentOrientation READ currentOrientation NOTIFY currentOrientationChanged REVISION 1)
56 Q_PROPERTY(int userOrientation READ userOrientation WRITE setUserOrientation NOTIFY userOrientationChanged REVISION 1)
57 Q_PROPERTY(int maxBufferSize READ maxBufferSize NOTIFY maxBufferSizeChanged REVISION 1)
58 Q_PROPERTY(int efficientBufferSize READ efficientBufferSize NOTIFY efficientBufferSizeChanged REVISION 1)
59 Q_PROPERTY(int bufferSize READ bufferSize WRITE setBufferSize NOTIFY bufferSizeChanged REVISION 1)
60
61 QML_NAMED_ELEMENT(Sensor)
62 QML_UNCREATABLE("Cannot create Sensor")
63 QML_ADDED_IN_VERSION(5,0)
64public:
65 // Keep in sync with QSensor::AxesOrientationMode
66 enum AxesOrientationMode {
67 FixedOrientation,
68 AutomaticOrientation,
69 UserOrientation
70 };
71 Q_ENUM(AxesOrientationMode)
72
73 explicit QmlSensor(QObject *parent = 0);
74 ~QmlSensor();
75
76 QByteArray identifier() const;
77 void setIdentifier(const QByteArray &identifier);
78
79 QByteArray type() const;
80
81 bool isConnectedToBackend() const;
82
83 bool isBusy() const;
84
85 void setActive(bool active);
86 bool isActive() const;
87
88 bool isAlwaysOn() const;
89 void setAlwaysOn(bool alwaysOn);
90
91 bool skipDuplicates() const;
92 void setSkipDuplicates(bool skipDuplicates);
93
94 QQmlListProperty<QmlSensorRange> availableDataRates() const;
95 int dataRate() const;
96 void setDataRate(int rate);
97
98 QQmlListProperty<QmlSensorOutputRange> outputRanges() const;
99 int outputRange() const;
100 void setOutputRange(int index);
101
102 QString description() const;
103 int error() const;
104
105 QmlSensorReading *reading() const;
106 QBindable<QmlSensorReading*> bindableReading() const;
107
108 AxesOrientationMode axesOrientationMode() const;
109 void setAxesOrientationMode(AxesOrientationMode axesOrientationMode);
110
111 int currentOrientation() const;
112
113 int userOrientation() const;
114 void setUserOrientation(int userOrientation);
115
116 int maxBufferSize() const;
117
118 int efficientBufferSize() const;
119
120 int bufferSize() const;
121 void setBufferSize(int bufferSize);
122
123 virtual QSensor *sensor() const = 0;
124
125 void componentComplete() override;
126
127public Q_SLOTS:
128 bool start();
129 void stop();
130
131Q_SIGNALS:
132 void identifierChanged();
133 void connectedToBackendChanged();
134 void availableDataRatesChanged();
135 void dataRateChanged();
136 void readingChanged();
137 void activeChanged();
138 void outputRangesChanged();
139 void outputRangeChanged();
140 void descriptionChanged();
141 void errorChanged();
142 void alwaysOnChanged();
143 void busyChanged();
144 Q_REVISION(1) void skipDuplicatesChanged(bool skipDuplicates);
145 Q_REVISION(1) void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode);
146 Q_REVISION(1) void currentOrientationChanged(int currentOrientation);
147 Q_REVISION(1) void userOrientationChanged(int userOrientation);
148 Q_REVISION(1) void maxBufferSizeChanged(int maxBufferSize);
149 Q_REVISION(1) void efficientBufferSizeChanged(int efficientBufferSize);
150 Q_REVISION(1) void bufferSizeChanged(int bufferSize);
151
152protected:
153 virtual QmlSensorReading *createReading() const = 0;
154
155private Q_SLOTS:
156 void updateReading();
157
158private:
159 void classBegin() override;
160 bool m_componentComplete = false;
161 bool m_activateOnComplete = false;
162 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QmlSensor, QmlSensorReading*,
163 m_reading, nullptr)
164};
165
166class Q_SENSORSQUICK_PRIVATE_EXPORT QmlSensorReading : public QObject
167{
168 Q_OBJECT
169 Q_PROPERTY(quint64 timestamp READ timestamp NOTIFY timestampChanged BINDABLE bindableTimestamp)
170 QML_NAMED_ELEMENT(SensorReading)
171 QML_UNCREATABLE("Cannot create SensorReading")
172 QML_ADDED_IN_VERSION(5,0)
173public:
174 explicit QmlSensorReading() = default;
175 ~QmlSensorReading() = default;
176
177 quint64 timestamp() const;
178 QBindable<quint64> bindableTimestamp() const;
179
180 void update();
181
182Q_SIGNALS:
183 void timestampChanged();
184
185private:
186 virtual QSensorReading *reading() const = 0;
187 virtual void readingUpdate() = 0;
188 Q_OBJECT_BINDABLE_PROPERTY(QmlSensorReading, quint64,
189 m_timestamp, &QmlSensorReading::timestampChanged)
190};
191
192QT_END_NAMESPACE
193
194#endif
195

source code of qtsensors/src/sensorsquick/qmlsensor_p.h