Changes:
[ptas] / zouba / 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
16 #include "ytv.h"
17
18 Route::Route() :
19   q( new RoutePrivate( this ) ),
20   manager( new QNetworkAccessManager(this) )
21 {
22   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
23 }
24
25 Route::~Route()
26 {
27   delete manager;
28   manager = 0;
29 }
30
31 void Route::getRoute()
32 {
33   QUrl fullUrl( Ytv::Url );
34
35   QStringList a;
36   a << q->fromLocation().x() << q->fromLocation().y();
37   QStringList b;
38   b << q->toLocation().x() << q->toLocation().y();
39
40   fullUrl.addQueryItem( "a", a.join(",") );
41   fullUrl.addQueryItem( "b", b.join(",") );
42   fullUrl.addQueryItem( "show", QString::number(Ytv::FiveResults) );
43   fullUrl.addQueryItem( "walkspeed", QString::number(Ytv::Fast) );
44   fullUrl.addQueryItem( "user", Ytv::Username );
45   fullUrl.addQueryItem( "pass", Ytv::Password );
46
47   manager->get( QNetworkRequest( fullUrl ) );
48 }
49
50 void Route::replyFinished( QNetworkReply * reply )
51 {
52   QList<RouteData> routeData = q->parseReply( reply->readAll() );
53
54   emit( routeReady( routeData ) );
55 }
56
57 void Route::setFromLocation( const Location &location )
58 {
59   if ( location.isValid() ) {
60     q->setFromLocation( location );
61     if ( q->toValid() ) {
62         getRoute();
63     }
64   } else {
65     Location *locationPtr = qobject_cast<Location*>(sender());
66     if ( locationPtr ) {
67       q->setFromLocation( *locationPtr );
68       if ( q->toValid() ) {
69         getRoute();
70       }
71     } else {
72       qDebug() << "locationPtr is zero - cast didn't work";
73     }
74   }
75 }
76
77 const Location &Route::fromLocation()
78 {
79   return q->fromLocation();
80 }
81
82 void Route::setToLocation( const Location &location )
83 {
84   if ( location.isValid() ) {
85     q->setToLocation( location );
86     if ( q->fromValid() ) {
87       getRoute();
88     }
89   } else {
90     Location *locationPtr = qobject_cast<Location*>(sender());
91     if ( locationPtr ) {
92       q->setToLocation( *locationPtr );
93       if ( q->fromValid() ) {
94         getRoute();
95       }
96     } else {
97       qDebug() << "locationPtr is zero; cast failed";
98     }
99   }
100 }
101
102 const Location &Route::toLocation()
103 {
104   return q->toLocation();
105 }
106
107 void Route::toggleDirection()
108 {
109   Location oldFromLocation = fromLocation();
110   setFromLocation( toLocation() );
111   setToLocation( oldFromLocation );
112
113   getRoute();
114 }