diff options
Diffstat (limited to 'app/qcheapruler.cpp')
-rw-r--r-- | app/qcheapruler.cpp | 127 |
1 files changed, 35 insertions, 92 deletions
diff --git a/app/qcheapruler.cpp b/app/qcheapruler.cpp index 50b2116..65d359d 100644 --- a/app/qcheapruler.cpp +++ b/app/qcheapruler.cpp @@ -1,73 +1,53 @@ #include "qcheapruler.hpp" +#include "naviapi_adaptor.h" #include <QString> QCheapRuler::QCheapRuler() { - readCoordinateFromFile(); - m_Timer = new QTimer(this); + //set the default current position + m_currentPosition = QGeoCoordinate(36.136261, -115.151254); } QCheapRuler::~QCheapRuler() { - m_Timer->stop(); - m_routpoint.clear(); } +//get route distance double QCheapRuler::distance() const { return m_distance; } +//get current distance along the route double QCheapRuler::currentDistance() const { return m_currentDistance; } -void QCheapRuler::setCurrentCoordinate(QString m_latitude,QString m_longitude) +//set current position below the coordinate info from navigation service +void QCheapRuler::setCurrentPosition(double latitude, double longitude, double distance) { - double latitude = m_latitude.toDouble(); - double longitude = m_longitude.toDouble(); - setCurrentPosition(latitude,longitude); -} - -void QCheapRuler::setCurrentPosition(double latitude,double longitude) -{ - if((latitude == 0.0)&&(longitude == 0.0)) - { - emit arrivedDest(); - } - else + //set coordinate info and notify the changes when latitude or longitude info has changed + if((m_currentPosition.latitude() != latitude) + ||(m_currentPosition.longitude() != longitude)) { - if((m_currentPosition.latitude() != latitude) - ||(m_currentPosition.longitude() != longitude)) - { - cr::point pre_postion = cr::point(m_currentPosition.longitude(),m_currentPosition.latitude()); - cr::point current_postion = cr::point(longitude,latitude); - m_currentDistance += ruler().distance(pre_postion,current_postion); - emit currentDistanceChanged(); - - m_currentPosition.setLatitude(latitude); - m_currentPosition.setLongitude(longitude); - emit currentPositionChanged(); - } + m_currentPosition.setLatitude(latitude); + m_currentPosition.setLongitude(longitude); + emit currentPositionChanged(); } -} -void QCheapRuler::readRoutePosition() -{ - m_index ++; - if(m_index < m_routpoint.size()) - { - qDebug("m_index:%d lan:%f lon:%f!\n",m_index,m_routpoint.at(m_index).y,m_routpoint.at(m_index).x); - setCurrentPosition(m_routpoint.at(m_index).y,m_routpoint.at(m_index).x); - } - else + //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_Timer->stop(); + m_currentDistance = distance; + emit currentDistanceChanged(); } } +//get current position(coordinate) QGeoCoordinate QCheapRuler::currentPosition() const { return m_currentPosition; @@ -79,25 +59,7 @@ QJSValue QCheapRuler::path() const return QJSValue(); } -void QCheapRuler::startnaviDemo() -{ - if (m_routpoint.empty()) { - return; - } - - m_currentDistance = 0.; - m_index = 0; - connect(m_Timer, SIGNAL(timeout()), this, SLOT(readRoutePosition())); - m_Timer->start(100); -} - -void QCheapRuler::stopnaviDemo() -{ - m_currentDistance = 0.; - m_index = 0; - m_Timer->stop(); -} - +//set route path and get the total distance void QCheapRuler::setPath(const QJSValue &value) { if (!value.isArray()) @@ -106,6 +68,7 @@ void QCheapRuler::setPath(const QJSValue &value) 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. }; @@ -119,6 +82,7 @@ void QCheapRuler::setPath(const QJSValue &value) 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; @@ -127,6 +91,18 @@ void QCheapRuler::setPath(const QJSValue &value) 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()) { @@ -135,36 +111,3 @@ cr::CheapRuler QCheapRuler::ruler() const return cr::CheapRuler(m_currentPosition.latitude(), cr::CheapRuler::Kilometers); } } - -void QCheapRuler::readCoordinateFromFile() -{ - m_routpoint.clear(); - - QFile *file = new QFile("/var/local/lib/afm/applications/tbtnavi/0.1/Coordinate.txt"); - if(!file->exists()) - { - file = new QFile(":/qml/Coordinate.txt"); - if(!file->exists()) - { - qDebug("Coordinate File not exit!\n"); - return; - } - } - - if(!file->open(QIODevice::ReadOnly | QIODevice::Text)) - { - qDebug("Can not open Coordinate File!\n"); - return; - } - - QTextStream m_StreamIn(file); - while (!m_StreamIn.atEnd()) { - QString line = m_StreamIn.readLine(); - - QStringList sections = line.split(QRegExp(",")); - cr::point temp = cr::point(sections.at(1).trimmed().toDouble(),sections.at(0).trimmed().toDouble()); - m_routpoint.push_back(temp); - } - - file->close(); -} |