diff options
author | 2018-11-02 14:13:07 +0900 | |
---|---|---|
committer | 2018-11-02 14:13:07 +0900 | |
commit | 30d24cbc67ef416672a3386b10be8fe6711c65d7 (patch) | |
tree | ecc8813e6742661e1521d6399d04cc4abc54587f /app/qcheapruler.cpp | |
parent | ae47635e56c4af4d816a76e5186e945768543679 (diff) |
use mapbox as navigation
Diffstat (limited to 'app/qcheapruler.cpp')
-rw-r--r-- | app/qcheapruler.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/app/qcheapruler.cpp b/app/qcheapruler.cpp new file mode 100644 index 0000000..4e520d2 --- /dev/null +++ b/app/qcheapruler.cpp @@ -0,0 +1,99 @@ +#include "qcheapruler.hpp" + +#include <QString> + +double QCheapRuler::distance() const +{ + return m_distance; +} + +double QCheapRuler::currentDistance() const +{ + return m_currentDistance; +} + +void QCheapRuler::setCurrentDistance(double current) +{ + if (m_path.empty()) { + m_currentDistance = 0.; + m_currentPosition = QGeoCoordinate(0., 0.); + m_segmentIndex = 0; + + return; + } + + double currentDistance = qMin(qMax(0., current), distance()); + if (m_currentDistance != currentDistance) { + m_currentDistance = currentDistance; + emit currentDistanceChanged(); + } + + cr::point c = ruler().along(m_path, m_currentDistance); + if (m_currentPosition != QGeoCoordinate(c.y, c.x)) { + m_currentPosition = QGeoCoordinate(c.y, c.x); + emit currentPositionChanged(); + } + + unsigned segmentIndex = ruler().pointOnLine(m_path, c).second; + if (m_segmentIndex != segmentIndex) { + m_segmentIndex = segmentIndex; + emit segmentIndexChanged(); + } +} + +QGeoCoordinate QCheapRuler::currentPosition() const +{ + return m_currentPosition; +} + +unsigned QCheapRuler::segmentIndex() const +{ + return m_segmentIndex; +} + +QJSValue QCheapRuler::path() const +{ + // Should neveer be called. + return QJSValue(); +} + +void QCheapRuler::setPath(const QJSValue &value) +{ + if (!value.isArray()) + return; + + m_path.clear(); + quint32 length = value.property(QStringLiteral("length")).toUInt(); + + for (unsigned i = 0; i < length; ++i) { + auto property = value.property(i); + cr::point coordinate = { 0., 0. }; + + if (property.hasProperty(QStringLiteral("latitude"))) + coordinate.y = property.property(QStringLiteral("latitude")).toNumber(); + + if (property.hasProperty(QStringLiteral("longitude"))) + coordinate.x = property.property(QStringLiteral("longitude")).toNumber(); + + m_path.push_back(coordinate); + } + + double distance = ruler().lineDistance(m_path); + if (m_distance != distance) { + m_distance = distance; + emit distanceChanged(); + } + + setCurrentDistance(0.); + + emit pathChanged(); +} + +cr::CheapRuler QCheapRuler::ruler() const +{ + if (m_path.empty()) { + return cr::CheapRuler(0., cr::CheapRuler::Kilometers); + } else { + return cr::CheapRuler(m_currentPosition.latitude(), cr::CheapRuler::Kilometers); + } +} |