| 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 | #include "qmlaccelerometer.h" | 
| 41 | #include <QtSensors/QAccelerometer> | 
| 42 |  | 
| 43 | /*! | 
| 44 |     \qmltype Accelerometer | 
| 45 |     \instantiates QmlAccelerometer | 
| 46 |     \ingroup qml-sensors_type | 
| 47 |     \inqmlmodule QtSensors | 
| 48 |     \since QtSensors 5.0 | 
| 49 |     \inherits Sensor | 
| 50 |     \brief The Accelerometer element reports on linear acceleration | 
| 51 |            along the X, Y and Z axes. | 
| 52 |  | 
| 53 |     The Accelerometer element reports on linear acceleration | 
| 54 |     along the X, Y and Z axes. | 
| 55 |  | 
| 56 |     This element wraps the QAccelerometer class. Please see the documentation for | 
| 57 |     QAccelerometer for details. | 
| 58 |  | 
| 59 |     \sa AccelerometerReading | 
| 60 | */ | 
| 61 |  | 
| 62 | QmlAccelerometer::QmlAccelerometer(QObject *parent) | 
| 63 |     : QmlSensor(parent) | 
| 64 |     , m_sensor(new QAccelerometer(this)) | 
| 65 | { | 
| 66 |     connect(sender: m_sensor, SIGNAL(accelerationModeChanged(AccelerationMode)), | 
| 67 |             receiver: this, SIGNAL(accelerationModeChanged(AccelerationMode))); | 
| 68 | } | 
| 69 |  | 
| 70 | QmlAccelerometer::~QmlAccelerometer() | 
| 71 | { | 
| 72 | } | 
| 73 |  | 
| 74 | /*! | 
| 75 |     \qmlproperty AccelerationMode Accelerometer::accelerationMode | 
| 76 |     \since QtSensors 5.1 | 
| 77 |  | 
| 78 |     This property holds the current acceleration mode. | 
| 79 |  | 
| 80 |     Please see QAccelerometer::accelerationMode for information about this property. | 
| 81 | */ | 
| 82 |  | 
| 83 | QmlAccelerometer::AccelerationMode QmlAccelerometer::accelerationMode() const | 
| 84 | { | 
| 85 |     return static_cast<QmlAccelerometer::AccelerationMode>(m_sensor->accelerationMode()); | 
| 86 | } | 
| 87 |  | 
| 88 | void QmlAccelerometer::setAccelerationMode(QmlAccelerometer::AccelerationMode accelerationMode) | 
| 89 | { | 
| 90 |     m_sensor->setAccelerationMode(static_cast<QAccelerometer::AccelerationMode>(accelerationMode)); | 
| 91 | } | 
| 92 |  | 
| 93 | QmlSensorReading *QmlAccelerometer::createReading() const | 
| 94 | { | 
| 95 |     return new QmlAccelerometerReading(m_sensor); | 
| 96 | } | 
| 97 |  | 
| 98 | QSensor *QmlAccelerometer::sensor() const | 
| 99 | { | 
| 100 |     return m_sensor; | 
| 101 | } | 
| 102 |  | 
| 103 | /*! | 
| 104 |     \qmltype AccelerometerReading | 
| 105 |     \instantiates QmlAccelerometerReading | 
| 106 |     \ingroup qml-sensors_reading | 
| 107 |     \inqmlmodule QtSensors | 
| 108 |     \since QtSensors 5.0 | 
| 109 |     \inherits SensorReading | 
| 110 |     \brief The AccelerometerReading element holds the most recent Accelerometer reading. | 
| 111 |  | 
| 112 |     The AccelerometerReading element holds the most recent Accelerometer reading. | 
| 113 |  | 
| 114 |     This element wraps the QAccelerometerReading class. Please see the documentation for | 
| 115 |     QAccelerometerReading for details. | 
| 116 |  | 
| 117 |     This element cannot be directly created. | 
| 118 | */ | 
| 119 |  | 
| 120 | QmlAccelerometerReading::QmlAccelerometerReading(QAccelerometer *sensor) | 
| 121 |     : QmlSensorReading(sensor) | 
| 122 |     , m_sensor(sensor) | 
| 123 | { | 
| 124 | } | 
| 125 |  | 
| 126 | QmlAccelerometerReading::~QmlAccelerometerReading() | 
| 127 | { | 
| 128 | } | 
| 129 |  | 
| 130 | /*! | 
| 131 |     \qmlproperty qreal AccelerometerReading::x | 
| 132 |     This property holds the acceleration on the X axis. | 
| 133 |  | 
| 134 |     Please see QAccelerometerReading::x for information about this property. | 
| 135 | */ | 
| 136 |  | 
| 137 | qreal QmlAccelerometerReading::x() const | 
| 138 | { | 
| 139 |     return m_x; | 
| 140 | } | 
| 141 |  | 
| 142 | /*! | 
| 143 |     \qmlproperty qreal AccelerometerReading::y | 
| 144 |     This property holds the acceleration on the Y axis. | 
| 145 |  | 
| 146 |     Please see QAccelerometerReading::y for information about this property. | 
| 147 | */ | 
| 148 |  | 
| 149 | qreal QmlAccelerometerReading::y() const | 
| 150 | { | 
| 151 |     return m_y; | 
| 152 | } | 
| 153 |  | 
| 154 | /*! | 
| 155 |     \qmlproperty qreal AccelerometerReading::z | 
| 156 |     This property holds the acceleration on the Z axis. | 
| 157 |  | 
| 158 |     Please see QAccelerometerReading::z for information about this property. | 
| 159 | */ | 
| 160 |  | 
| 161 | qreal QmlAccelerometerReading::z() const | 
| 162 | { | 
| 163 |     return m_z; | 
| 164 | } | 
| 165 |  | 
| 166 | QSensorReading *QmlAccelerometerReading::reading() const | 
| 167 | { | 
| 168 |     return m_sensor->reading(); | 
| 169 | } | 
| 170 |  | 
| 171 | void QmlAccelerometerReading::readingUpdate() | 
| 172 | { | 
| 173 |     qreal aX = m_sensor->reading()->x(); | 
| 174 |     if (m_x != aX) { | 
| 175 |         m_x = aX; | 
| 176 |         Q_EMIT xChanged(); | 
| 177 |     } | 
| 178 |     qreal aY = m_sensor->reading()->y(); | 
| 179 |     if (m_y != aY) { | 
| 180 |         m_y = aY; | 
| 181 |         Q_EMIT yChanged(); | 
| 182 |     } | 
| 183 |     qreal aZ = m_sensor->reading()->z(); | 
| 184 |     if (m_z != aZ) { | 
| 185 |         m_z = aZ; | 
| 186 |         Q_EMIT zChanged(); | 
| 187 |     } | 
| 188 | } | 
| 189 |  |