File indexing completed on 2024-04-28 05:27:37
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 } 0035 Q_EMIT availableChanged(available()); 0036 } 0037 0038 QOrientationReading::Orientation OrientationSensor::value() const 0039 { 0040 return m_value; 0041 } 0042 0043 bool OrientationSensor::available() const 0044 { 0045 return m_sensor->connectToBackend() && m_sensor->reading() != nullptr && m_sensor->reading()->orientation() != QOrientationReading::Undefined; 0046 } 0047 0048 bool OrientationSensor::enabled() const 0049 { 0050 return m_sensor->isActive(); 0051 } 0052 0053 void OrientationSensor::setEnabled(bool enable) 0054 { 0055 if (m_enabled == enable) { 0056 return; 0057 } 0058 m_enabled = enable; 0059 0060 if (enable) { 0061 connect(m_sensor, &QOrientationSensor::readingChanged, this, &OrientationSensor::updateState); 0062 m_sensor->start(); 0063 } else { 0064 disconnect(m_sensor, &QOrientationSensor::readingChanged, this, &OrientationSensor::updateState); 0065 m_value = QOrientationReading::Undefined; 0066 } 0067 Q_EMIT enabledChanged(enable); 0068 } 0069 0070 #include "moc_orientation_sensor.cpp"