aboutsummaryrefslogtreecommitdiffstats
path: root/app/qcheapruler.cpp
blob: 890fd632ec26f63f0ba371f59b6587e3dd5d2d1c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "qcheapruler.hpp"
#include "naviapi_adaptor.h"

#include <QString>

QCheapRuler::QCheapRuler()
{
    //set the default current position
    // m_currentPosition = QGeoCoordinate(36.136261, -115.151254);
    m_currentPosition = QGeoCoordinate(35.692396, 139.691102);
}

QCheapRuler::~QCheapRuler()
{
}

//get route distance
double QCheapRuler::distance() const
{
    return m_distance;
}

//get current distance along the route
double QCheapRuler::currentDistance() const
{
    return m_currentDistance;
}

//set current position below the coordinate info from navigation service
void QCheapRuler::setCurrentPosition(double latitude, double longitude)
{
    //set coordinate info and notify the changes when latitude or longitude info has changed
    if((m_currentPosition.latitude() != latitude)
     ||(m_currentPosition.longitude() != longitude))
    {
        m_currentPosition.setLatitude(latitude);
        m_currentPosition.setLongitude(longitude);
        emit currentPositionChanged();
    }
}

void QCheapRuler::setCurrentDistance(double distance)
{
    //set current distance info and notify the changes when the info has changed
    //but it will not send notify when it start or stop demo
    if((m_currentDistance != distance)
    &&(distance != 0.0))
    {
        m_currentDistance = distance;
        emit currentDistanceChanged();
    }
}

//get current position(coordinate)
QGeoCoordinate QCheapRuler::currentPosition() const
{
    return m_currentPosition;
}

QJSValue QCheapRuler::path() const
{
    // Should neveer be called.
    return QJSValue();
}

//set route path and get the total distance
void QCheapRuler::setPath(const QJSValue &value)
{
    if (!value.isArray())
        return;

    m_path.clear();
    quint32 length = value.property(QStringLiteral("length")).toUInt();

    //push back the coordinate info along the route
    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);
    }

    //count the total distance along the route
    double distance = ruler().lineDistance(m_path);
    if (m_distance != distance) {
        m_distance = distance;
    }

    emit pathChanged();
}

//init the route and postion info when start in the first time.(can be called by qml)
void QCheapRuler::initRouteInfo()
{
    //send "getRouteInfo" message to the navigation service
    QDBusMessage message = QDBusMessage::createSignal("/", "org.agl.naviapi", "getRouteInfo");
    if(!QDBusConnection::sessionBus().send(message))
    {
       qDebug() << "initRouteInfo" << "sessionBus.send(): getRouteInfo failed";
    }
}

//init the CheapRuler class
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);
    }
}