| 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 <QtSensors/QSensor> |
| 26 | |
| 27 | #include "qmlsensorrange_p.h" |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QSensor; |
| 32 | class QSensorReading; |
| 33 | |
| 34 | class QmlSensorReading; |
| 35 | |
| 36 | class QmlSensorPrivate; |
| 37 | class Q_SENSORSQUICK_EXPORT QmlSensor : public QObject, public QQmlParserStatus |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | Q_DECLARE_PRIVATE(QmlSensor) |
| 41 | Q_INTERFACES(QQmlParserStatus) |
| 42 | Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged) |
| 43 | Q_PROPERTY(QByteArray type READ type CONSTANT) |
| 44 | Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend NOTIFY connectedToBackendChanged) |
| 45 | Q_PROPERTY(QQmlListProperty<QmlSensorRange> availableDataRates READ availableDataRates NOTIFY availableDataRatesChanged) |
| 46 | Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged) |
| 47 | Q_PROPERTY(QmlSensorReading* reading READ reading NOTIFY readingChanged BINDABLE bindableReading) |
| 48 | Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged) |
| 49 | Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged) |
| 50 | Q_PROPERTY(QQmlListProperty<QmlSensorOutputRange> outputRanges READ outputRanges NOTIFY outputRangesChanged) |
| 51 | Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange NOTIFY outputRangeChanged) |
| 52 | Q_PROPERTY(QString description READ description NOTIFY descriptionChanged) |
| 53 | Q_PROPERTY(int error READ error NOTIFY errorChanged) |
| 54 | Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged) |
| 55 | Q_PROPERTY(bool skipDuplicates READ skipDuplicates WRITE setSkipDuplicates NOTIFY skipDuplicatesChanged REVISION 1) |
| 56 | Q_PROPERTY(AxesOrientationMode axesOrientationMode READ axesOrientationMode WRITE setAxesOrientationMode NOTIFY axesOrientationModeChanged REVISION 1) |
| 57 | Q_PROPERTY(int currentOrientation READ currentOrientation NOTIFY currentOrientationChanged REVISION 1) |
| 58 | Q_PROPERTY(int userOrientation READ userOrientation WRITE setUserOrientation NOTIFY userOrientationChanged REVISION 1) |
| 59 | Q_PROPERTY(int maxBufferSize READ maxBufferSize NOTIFY maxBufferSizeChanged REVISION 1) |
| 60 | Q_PROPERTY(int efficientBufferSize READ efficientBufferSize NOTIFY efficientBufferSizeChanged REVISION 1) |
| 61 | Q_PROPERTY(int bufferSize READ bufferSize WRITE setBufferSize NOTIFY bufferSizeChanged REVISION 1) |
| 62 | |
| 63 | QML_NAMED_ELEMENT(Sensor) |
| 64 | QML_UNCREATABLE("Cannot create Sensor" ) |
| 65 | QML_ADDED_IN_VERSION(5,0) |
| 66 | public: |
| 67 | // Keep in sync with QSensor::Feature |
| 68 | enum Feature : int { |
| 69 | Buffering = QSensor::Buffering, |
| 70 | AlwaysOn = QSensor::AlwaysOn, |
| 71 | GeoValues = QSensor::GeoValues, |
| 72 | FieldOfView = QSensor::FieldOfView, |
| 73 | AccelerationMode = QSensor::AccelerationMode, |
| 74 | SkipDuplicates = QSensor::SkipDuplicates, |
| 75 | AxesOrientation = QSensor::AxesOrientation, |
| 76 | PressureSensorTemperature = QSensor::PressureSensorTemperature |
| 77 | }; |
| 78 | Q_ENUM(Feature) |
| 79 | |
| 80 | // Keep in sync with QSensor::AxesOrientationMode |
| 81 | enum AxesOrientationMode { |
| 82 | FixedOrientation, |
| 83 | AutomaticOrientation, |
| 84 | UserOrientation |
| 85 | }; |
| 86 | Q_ENUM(AxesOrientationMode) |
| 87 | |
| 88 | explicit QmlSensor(QObject *parent = 0); |
| 89 | ~QmlSensor(); |
| 90 | |
| 91 | QByteArray identifier() const; |
| 92 | void setIdentifier(const QByteArray &identifier); |
| 93 | |
| 94 | QByteArray 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 | QBindable<QmlSensorReading*> bindableReading() const; |
| 122 | |
| 123 | Q_INVOKABLE Q_REVISION(6, 7) bool isFeatureSupported(Feature feature) const; |
| 124 | |
| 125 | AxesOrientationMode axesOrientationMode() const; |
| 126 | void setAxesOrientationMode(AxesOrientationMode axesOrientationMode); |
| 127 | |
| 128 | int currentOrientation() const; |
| 129 | |
| 130 | int userOrientation() const; |
| 131 | void setUserOrientation(int userOrientation); |
| 132 | |
| 133 | int maxBufferSize() const; |
| 134 | |
| 135 | int efficientBufferSize() const; |
| 136 | |
| 137 | int bufferSize() const; |
| 138 | void setBufferSize(int bufferSize); |
| 139 | |
| 140 | virtual QSensor *sensor() const = 0; |
| 141 | |
| 142 | void componentComplete() override; |
| 143 | |
| 144 | public Q_SLOTS: |
| 145 | bool start(); |
| 146 | void stop(); |
| 147 | |
| 148 | Q_SIGNALS: |
| 149 | void identifierChanged(); |
| 150 | void connectedToBackendChanged(); |
| 151 | void availableDataRatesChanged(); |
| 152 | void dataRateChanged(); |
| 153 | void readingChanged(); |
| 154 | void activeChanged(); |
| 155 | void outputRangesChanged(); |
| 156 | void outputRangeChanged(); |
| 157 | void descriptionChanged(); |
| 158 | void errorChanged(); |
| 159 | void alwaysOnChanged(); |
| 160 | void busyChanged(); |
| 161 | Q_REVISION(1) void skipDuplicatesChanged(bool skipDuplicates); |
| 162 | Q_REVISION(1) void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode); |
| 163 | Q_REVISION(1) void currentOrientationChanged(int currentOrientation); |
| 164 | Q_REVISION(1) void userOrientationChanged(int userOrientation); |
| 165 | Q_REVISION(1) void maxBufferSizeChanged(int maxBufferSize); |
| 166 | Q_REVISION(1) void efficientBufferSizeChanged(int efficientBufferSize); |
| 167 | Q_REVISION(1) void bufferSizeChanged(int bufferSize); |
| 168 | |
| 169 | protected: |
| 170 | virtual QmlSensorReading *createReading() const = 0; |
| 171 | |
| 172 | private Q_SLOTS: |
| 173 | void updateReading(); |
| 174 | |
| 175 | private: |
| 176 | void classBegin() override; |
| 177 | bool m_componentComplete = false; |
| 178 | bool m_activateOnComplete = false; |
| 179 | Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QmlSensor, QmlSensorReading*, |
| 180 | m_reading, nullptr) |
| 181 | }; |
| 182 | |
| 183 | class Q_SENSORSQUICK_EXPORT QmlSensorReading : public QObject |
| 184 | { |
| 185 | Q_OBJECT |
| 186 | Q_PROPERTY(quint64 timestamp READ timestamp NOTIFY timestampChanged BINDABLE bindableTimestamp) |
| 187 | QML_NAMED_ELEMENT(SensorReading) |
| 188 | QML_UNCREATABLE("Cannot create SensorReading" ) |
| 189 | QML_ADDED_IN_VERSION(5,0) |
| 190 | public: |
| 191 | explicit QmlSensorReading() = default; |
| 192 | ~QmlSensorReading() = default; |
| 193 | |
| 194 | quint64 timestamp() const; |
| 195 | QBindable<quint64> bindableTimestamp() const; |
| 196 | |
| 197 | void update(); |
| 198 | |
| 199 | Q_SIGNALS: |
| 200 | void timestampChanged(); |
| 201 | |
| 202 | private: |
| 203 | virtual QSensorReading *reading() const = 0; |
| 204 | virtual void readingUpdate() = 0; |
| 205 | Q_OBJECT_BINDABLE_PROPERTY(QmlSensorReading, quint64, |
| 206 | m_timestamp, &QmlSensorReading::timestampChanged) |
| 207 | }; |
| 208 | |
| 209 | QT_END_NAMESPACE |
| 210 | |
| 211 | #endif |
| 212 | |