Incorporated changes from bus project.
[ptas] / src / route.cpp
1 #include "route_p.h"
2 #include "route.h"
3
4 #include "routedata.h"
5 #include "location.h"
6
7 #include <QNetworkAccessManager>
8 #include <QNetworkReply>
9 #include <QUrl>
10 #include <QObject>
11 #include <QDebug>
12 #include <QStringList>
13 #include <QString>
14 #include <QXmlStreamReader>
15 #include <QWidget>
16
17 #include "ytv.h"
18
19 Route::Route() :
20     q(new RoutePrivate(this)),
21     manager(new QNetworkAccessManager(this))
22 {
23     connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
24 }
25
26 Route::~Route()
27 {
28     delete manager;
29     manager = 0;
30 }
31
32 void Route::getRoute()
33 {
34     qDebug() << "getting route from Ytv";
35
36     QUrl fullUrl(Ytv::Url);
37
38     QStringList a;
39     a << q->fromLocation()->longitude() << q->fromLocation()->latitude();
40     QStringList b;
41     b << q->toLocation()->longitude() << q->toLocation()->latitude();
42
43     fullUrl.addQueryItem("user", Ytv::Username);
44     fullUrl.addQueryItem("pass", Ytv::Password);
45     fullUrl.addQueryItem("request", Ytv::Route);
46     fullUrl.addQueryItem("format", Ytv::Xml);
47     fullUrl.addQueryItem("language", Ytv::English);
48     fullUrl.addQueryItem("epsg_in", Ytv::WGS84);
49     fullUrl.addQueryItem("epsg_out", Ytv::WGS84);
50     fullUrl.addQueryItem("from", a.join(","));
51     fullUrl.addQueryItem("to", b.join(","));
52     fullUrl.addQueryItem("show", QString::number(Ytv::ShowFiveResults));
53     fullUrl.addQueryItem("walkspeed", QString::number(Ytv::WalkSpeedFast));
54     fullUrl.addQueryItem("optimize", QString::number(Ytv::OptimizeLeastWalking));
55
56     manager->get(QNetworkRequest(fullUrl));
57     qDebug() << "getting url" << fullUrl.toEncoded();
58     qDebug() << "waiting for reply from Ytv";
59     emit(busy(true));
60 }
61
62 void Route::replyFinished(QNetworkReply * reply)
63 {
64     qDebug() << "have reply from Ytv";
65     QList<RouteData> routeData = q->parseReply(reply->readAll());
66
67     emit(routeReady(routeData));
68     emit(busy(false));
69 }
70
71 void Route::setFromLocation(Location *location)
72 {
73     qDebug() << "setting new From location (" << location->label() << ")";
74
75     if (location && location->isValid()) {
76         qDebug() << "From is valid";
77         q->setFromLocation(location);
78         if (q->toValid()) {
79             qDebug() << "To is also valid";
80             getRoute();
81         } else {
82             qDebug() << "To not valid - waiting";
83         }
84     } else {
85         qDebug() << "ERROR:From is not valid";
86         qDebug() << "location=" << location;
87         if (location) {
88             qDebug() << "location->isValid()=" << location->isValid();
89         }
90     }
91 }
92
93 Location *Route::fromLocation() const
94 {
95     return q->fromLocation();
96 }
97
98 void Route::setToLocation(Location *location)
99 {
100     qDebug() << "setting new To location (" << location->label() << ")";
101
102     if (location && location->isValid()) {
103         qDebug() << "To is valid";
104         q->setToLocation(location);
105         if (q->fromValid()) {
106             qDebug() << "From is also valid";
107             getRoute();
108         } else {
109             qDebug() << "From not valid - waiting";
110         }
111     } else {
112         qDebug() << "ERROR:From is not valid";
113         qDebug() << "location=" << location;
114         if (location) {
115             qDebug() << "location->isValid()=" << location->isValid();
116         }
117     }
118 }
119
120 Location *Route::toLocation() const
121 {
122     return q->toLocation();
123 }