File indexing completed on 2024-06-09 05:46:33
0001 /* 0002 * Copyright 2019 Patrick José Pereira <patrickjp@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) version 3, or any 0008 * later version accepted by the membership of KDE e.V. (or its 0009 * successor approved by the membership of KDE e.V.), which shall 0010 * act as a proxy defined in Section 6 of version 3 of the license. 0011 * 0012 * This library is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 * Lesser General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU Lesser General Public 0018 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0019 */ 0020 0021 #include "positionsource.h" 0022 0023 #include "debug.h" 0024 0025 #include <QQmlEngine> 0026 0027 PositionSource::PositionSource() 0028 { 0029 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); 0030 0031 createPositionSource(); 0032 } 0033 0034 void PositionSource::createPositionSource() 0035 { 0036 // The smartpointer will take care of the pointer, that's why we are using nullptr as parent. 0037 m_geoPositionSource.reset(QGeoPositionInfoSource::createDefaultSource(nullptr)); 0038 if (m_geoPositionSource.isNull()) { 0039 qCWarning(POSITIONSOURCE) << "Position source is not available for this device."; 0040 return; 0041 } 0042 0043 qCDebug(POSITIONSOURCE) << "New position source:" << m_geoPositionSource->sourceName(); 0044 0045 // Set the update interval to 3s. 0046 m_geoPositionSource->setUpdateInterval(3000); 0047 m_geoPositionSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods); 0048 m_geoPositionSource->startUpdates(); 0049 0050 connect(m_geoPositionSource.get(), 0051 QOverload<QGeoPositionInfoSource::Error>::of(&QGeoPositionInfoSource::error), 0052 this, 0053 &PositionSource::setPositionSourceError); 0054 connect(m_geoPositionSource.get(), &QGeoPositionInfoSource::positionUpdated, this, &PositionSource::setPositionInfo); 0055 } 0056 0057 bool PositionSource::enabled() const 0058 { 0059 return m_enabled; 0060 } 0061 0062 void PositionSource::setEnabled(bool enabled) 0063 { 0064 if (m_enabled == enabled) { 0065 return; 0066 } 0067 0068 m_enabled = enabled; 0069 emit enabledChanged(m_enabled); 0070 } 0071 0072 void PositionSource::setPositionInfo(const QGeoPositionInfo &geoPositionInfo) 0073 { 0074 if (!geoPositionInfo.isValid()) { 0075 qCWarning(POSITIONSOURCE) << "Not valid position info from position source:" << geoPositionInfo; 0076 return; 0077 } 0078 0079 const auto geoCoordinate = geoPositionInfo.coordinate(); 0080 if (m_geoCoordinate == geoCoordinate) { 0081 return; 0082 } 0083 0084 m_geoCoordinate = geoCoordinate; 0085 emit coordinateChanged(m_geoCoordinate); 0086 } 0087 0088 QGeoCoordinate PositionSource::coordinate() const 0089 { 0090 return m_geoCoordinate; 0091 } 0092 0093 const QGeoPositionInfoSource *PositionSource::positionInfoSource() const 0094 { 0095 return m_geoPositionSource.get(); 0096 } 0097 0098 void PositionSource::setPositionSourceError(QGeoPositionInfoSource::Error positioningError) 0099 { 0100 qWarning(POSITIONSOURCE) << "Position source error:" << positioningError; 0101 } 0102 0103 QObject *PositionSource::qmlSingletonRegister(QQmlEngine *engine, QJSEngine *scriptEngine) 0104 { 0105 Q_UNUSED(engine) 0106 Q_UNUSED(scriptEngine) 0107 0108 return &self(); 0109 } 0110 0111 PositionSource &PositionSource::self() 0112 { 0113 static PositionSource self; 0114 return self; 0115 }