File indexing completed on 2024-05-05 17:34:27

0001 /*
0002     SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "orientation_sensor.h"
0007 
0008 #include <QOrientationSensor>
0009 
0010 OrientationSensor::OrientationSensor(QObject *parent)
0011     : QObject(parent)
0012     , m_sensor(new QOrientationSensor(this))
0013 {
0014     connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh);
0015 }
0016 
0017 OrientationSensor::~OrientationSensor() = default;
0018 
0019 void OrientationSensor::updateState()
0020 {
0021     const auto orientation = m_sensor->reading()->orientation();
0022     if (m_value != orientation) {
0023         m_value = orientation;
0024         Q_EMIT valueChanged(orientation);
0025     }
0026 }
0027 
0028 void OrientationSensor::refresh()
0029 {
0030     if (m_sensor->isActive()) {
0031         if (m_enabled) {
0032             updateState();
0033         }
0034         Q_EMIT availableChanged(true);
0035     } else {
0036         Q_EMIT availableChanged(false);
0037     }
0038 }
0039 
0040 QOrientationReading::Orientation OrientationSensor::value() const
0041 {
0042     return m_value;
0043 }
0044 
0045 bool OrientationSensor::available() const
0046 {
0047     return m_sensor->connectToBackend();
0048 }
0049 
0050 bool OrientationSensor::enabled() const
0051 {
0052     return m_sensor->isActive();
0053 }
0054 
0055 void OrientationSensor::setEnabled(bool enable)
0056 {
0057     if (m_enabled == enable) {
0058         return;
0059     }
0060     m_enabled = enable;
0061 
0062     if (enable) {
0063         connect(m_sensor, &QOrientationSensor::readingChanged, this, &OrientationSensor::updateState);
0064         m_sensor->start();
0065     } else {
0066         disconnect(m_sensor, &QOrientationSensor::readingChanged, this, &OrientationSensor::updateState);
0067         m_value = QOrientationReading::Undefined;
0068     }
0069     Q_EMIT enabledChanged(enable);
0070 }