1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSensors module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QMLSENSOR_H
41#define QMLSENSOR_H
42
43#include <QQmlParserStatus>
44#include <QQmlListProperty>
45#include "qmlsensorrange.h"
46
47QT_BEGIN_NAMESPACE
48
49class QSensor;
50class QSensorReading;
51
52class QmlSensorReading;
53
54class QmlSensor : public QObject, public QQmlParserStatus
55{
56 Q_OBJECT
57 Q_ENUMS(AxesOrientationMode)
58 Q_INTERFACES(QQmlParserStatus)
59 Q_PROPERTY(QString identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged)
60 Q_PROPERTY(QString type READ type NOTIFY typeChanged)
61 Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend NOTIFY connectedToBackendChanged)
62 Q_PROPERTY(QQmlListProperty<QmlSensorRange> availableDataRates READ availableDataRates NOTIFY availableDataRatesChanged)
63 Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged)
64 Q_PROPERTY(QmlSensorReading* reading READ reading NOTIFY readingChanged)
65 Q_PROPERTY(bool busy READ isBusy)
66 Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
67 Q_PROPERTY(QQmlListProperty<QmlSensorOutputRange> outputRanges READ outputRanges NOTIFY outputRangesChanged)
68 Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange NOTIFY outputRangeChanged)
69 Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
70 Q_PROPERTY(int error READ error NOTIFY errorChanged)
71 Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged)
72 Q_PROPERTY(bool skipDuplicates READ skipDuplicates WRITE setSkipDuplicates NOTIFY skipDuplicatesChanged REVISION 1)
73 Q_PROPERTY(AxesOrientationMode axesOrientationMode READ axesOrientationMode WRITE setAxesOrientationMode NOTIFY axesOrientationModeChanged REVISION 1)
74 Q_PROPERTY(int currentOrientation READ currentOrientation NOTIFY currentOrientationChanged REVISION 1)
75 Q_PROPERTY(int userOrientation READ userOrientation WRITE setUserOrientation NOTIFY userOrientationChanged REVISION 1)
76 Q_PROPERTY(int maxBufferSize READ maxBufferSize NOTIFY maxBufferSizeChanged REVISION 1)
77 Q_PROPERTY(int efficientBufferSize READ efficientBufferSize NOTIFY efficientBufferSizeChanged REVISION 1)
78 Q_PROPERTY(int bufferSize READ bufferSize WRITE setBufferSize NOTIFY bufferSizeChanged REVISION 1)
79
80public:
81 // Keep in sync with QSensor::AxesOrientationMode
82 enum AxesOrientationMode {
83 FixedOrientation,
84 AutomaticOrientation,
85 UserOrientation
86 };
87
88 explicit QmlSensor(QObject *parent = 0);
89 ~QmlSensor();
90
91 QString identifier() const;
92 void setIdentifier(const QString &identifier);
93
94 QString type() const;
95
96 bool isConnectedToBackend() const;
97
98 bool isBusy() const;
99
100 void setActive(bool active);
101 bool isActive() const;
102
103 bool isAlwaysOn() const;
104 void setAlwaysOn(bool alwaysOn);
105
106 bool skipDuplicates() const;
107 void setSkipDuplicates(bool skipDuplicates);
108
109 QQmlListProperty<QmlSensorRange> availableDataRates() const;
110 int dataRate() const;
111 void setDataRate(int rate);
112
113 QQmlListProperty<QmlSensorOutputRange> outputRanges() const;
114 int outputRange() const;
115 void setOutputRange(int index);
116
117 QString description() const;
118 int error() const;
119
120 QmlSensorReading *reading() const;
121
122 AxesOrientationMode axesOrientationMode() const;
123 void setAxesOrientationMode(AxesOrientationMode axesOrientationMode);
124
125 int currentOrientation() const;
126
127 int userOrientation() const;
128 void setUserOrientation(int userOrientation);
129
130 int maxBufferSize() const;
131
132 int efficientBufferSize() const;
133
134 int bufferSize() const;
135 void setBufferSize(int bufferSize);
136
137public Q_SLOTS:
138 bool start();
139 void stop();
140
141Q_SIGNALS:
142 void identifierChanged();
143 void typeChanged();
144 void connectedToBackendChanged();
145 void availableDataRatesChanged();
146 void dataRateChanged();
147 void readingChanged();
148 void activeChanged();
149 void outputRangesChanged();
150 void outputRangeChanged();
151 void descriptionChanged();
152 void errorChanged();
153 void alwaysOnChanged();
154 Q_REVISION(1) void skipDuplicatesChanged(bool skipDuplicates);
155 Q_REVISION(1) void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode);
156 Q_REVISION(1) void currentOrientationChanged(int currentOrientation);
157 Q_REVISION(1) void userOrientationChanged(int userOrientation);
158 Q_REVISION(1) void maxBufferSizeChanged(int maxBufferSize);
159 Q_REVISION(1) void efficientBufferSizeChanged(int efficientBufferSize);
160 Q_REVISION(1) void bufferSizeChanged(int bufferSize);
161
162protected:
163 virtual QSensor *sensor() const = 0;
164 virtual QmlSensorReading *createReading() const = 0;
165
166private Q_SLOTS:
167 void updateReading();
168
169protected Q_SLOTS:
170 void componentComplete() override;
171
172private:
173 void classBegin() override;
174 virtual void _update();
175 bool m_parsed;
176 bool m_active;
177 QString m_identifier;
178 QmlSensorReading *m_reading;
179};
180
181class QmlSensorReading : public QObject
182{
183 Q_OBJECT
184 Q_PROPERTY(quint64 timestamp READ timestamp NOTIFY timestampChanged)
185public:
186 explicit QmlSensorReading(QSensor *sensor);
187 ~QmlSensorReading();
188
189 quint64 timestamp() const;
190 void update();
191
192Q_SIGNALS:
193 void timestampChanged();
194
195private:
196 virtual QSensorReading *reading() const = 0;
197 virtual void readingUpdate() = 0;
198 quint64 m_timestamp;
199};
200
201QT_END_NAMESPACE
202
203#endif
204