diff --git a/src/lib/positionsource/positionsource.cpp b/src/lib/positionsource/positionsource.cpp index a77d425..0e07704 100644 --- a/src/lib/positionsource/positionsource.cpp +++ b/src/lib/positionsource/positionsource.cpp @@ -1,92 +1,112 @@ +/* + * Copyright 2019 Patrick José Pereira + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + #include "positionsource.h" #include "debug.h" #include PositionSource::PositionSource() { QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); createPositionSource(); } void PositionSource::createPositionSource() { // The smartpointer will take care of the pointer, that's why we are using nullptr as parent. m_geoPositionSource.reset(QGeoPositionInfoSource::createDefaultSource(nullptr)); if (m_geoPositionSource.isNull()) { qCWarning(POSITIONSOURCE) << "Position source is not available for this device."; return; } qCDebug(POSITIONSOURCE) << "New position source:" << m_geoPositionSource->sourceName(); // Set the update interval to 3s. m_geoPositionSource->setUpdateInterval(3000); m_geoPositionSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods); m_geoPositionSource->startUpdates(); connect(m_geoPositionSource.get(), QOverload::of(&QGeoPositionInfoSource::error), this, &PositionSource::setPositionSourceError); connect(m_geoPositionSource.get(), &QGeoPositionInfoSource::positionUpdated, this, &PositionSource::setPositionInfo); } bool PositionSource::enabled() const { return m_enabled; } void PositionSource::setEnabled(bool enabled) { if (m_enabled == enabled) { return; } m_enabled = enabled; emit enabledChanged(m_enabled); } void PositionSource::setPositionInfo(const QGeoPositionInfo &geoPositionInfo) { if (!geoPositionInfo.isValid()) { qCWarning(POSITIONSOURCE) << "Not valid position info from position source:" << geoPositionInfo; return; } const auto geoCoordinate = geoPositionInfo.coordinate(); if (m_geoCoordinate == geoCoordinate) { return; } m_geoCoordinate = geoCoordinate; emit coordinateChanged(m_geoCoordinate); } QGeoCoordinate PositionSource::coordinate() const { return m_geoCoordinate; } const QGeoPositionInfoSource *PositionSource::positionInfoSource() const { return m_geoPositionSource.get(); } void PositionSource::setPositionSourceError(QGeoPositionInfoSource::Error positioningError) { qWarning(POSITIONSOURCE) << "Position source error:" << positioningError; } QObject *PositionSource::qmlSingletonRegister(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) return &self(); } PositionSource &PositionSource::self() { static PositionSource self; return self; } diff --git a/src/lib/positionsource/positionsource.h b/src/lib/positionsource/positionsource.h index 0db879e..1b45e91 100644 --- a/src/lib/positionsource/positionsource.h +++ b/src/lib/positionsource/positionsource.h @@ -1,101 +1,121 @@ +/* + * Copyright 2019 Patrick José Pereira + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + #pragma once #include #include class QJSEngine; class QQmlEngine; /** * @brief Manage the position source of the ground control station. * */ class PositionSource : public QObject { Q_OBJECT Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) Q_PROPERTY(QGeoCoordinate coordinate READ coordinate NOTIFY coordinateChanged) public: /** * @brief Return PositionSource status, if it's enabled. * * @return true PositionSource is enable and working. * @return false Positionsource is disabled. */ bool enabled() const; /** * @brief Turn PositionSource to enable or disable. * * @param enabled */ void setEnabled(bool enabled); /** * @brief Set a new position info. * * @param geoPositionInfo */ void setPositionInfo(const QGeoPositionInfo &geoPositionInfo); /** * @brief Return the last valid coordinate. * * @return QGeoCoordinate */ QGeoCoordinate coordinate() const; /** * @brief Return the position source for this class. * * @return const QGeoPositionInfoSource* */ const QGeoPositionInfoSource *positionInfoSource() const; /** * @brief Return PositionSource pointer. * * @return PositionSource* */ static PositionSource &self(); /** * @brief Return a pointer of this singleton to the qml register function. * * @param engine * @param scriptEngine * @return QObject* */ static QObject *qmlSingletonRegister(QQmlEngine *engine, QJSEngine *scriptEngine); Q_SIGNALS: void enabledChanged(bool enabled); void coordinateChanged(const QGeoCoordinate &geoCoordinate); private: Q_DISABLE_COPY(PositionSource) /** * @brief Construct a new Position Source object. * */ PositionSource(); /** * @brief Create the position source for this class. * */ void createPositionSource(); /** * @brief Set position source error type. * * @param positioningError */ void setPositionSourceError(QGeoPositionInfoSource::Error positioningError); bool m_enabled; QGeoCoordinate m_geoCoordinate; QSharedPointer m_geoPositionSource; };