summaryrefslogtreecommitdiffstats
path: root/app/qcheapruler.cpp
blob: 4e520d27b86db94391592e7e1f40a61513eea61a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
    }
}