From 9c5c987171fabed85e42d4bec40e8d86ba20f7e3 Mon Sep 17 00:00:00 2001 From: Max Waterman Date: Sun, 28 Feb 2010 12:33:24 +0200 Subject: [PATCH] Added address resolution, and moved everything up one directory. Added test for Location class. --- zouba/.gitignore | 7 + zouba/location.cpp | 95 ++++++++ zouba/location.h | 42 ++++ zouba/location_p.cpp | 74 ++++++ zouba/location_p.h | 35 +++ zouba/main.cpp | 50 ++++ zouba/output.xml | 330 ++++++++++++++++++++++++++ zouba/qt/location.h | 21 -- zouba/qt/main.cpp | 31 --- zouba/qt/output.xml | 330 -------------------------- zouba/qt/qt.pro | 18 -- zouba/qt/route.cpp | 86 ------- zouba/qt/route.h | 62 ----- zouba/qt/route_p.cpp | 91 -------- zouba/qt/route_p.h | 37 --- zouba/qt/routedata.h | 12 - zouba/qt/tests/ut_route/ut_route.cpp | 385 ------------------------------- zouba/qt/tests/ut_route/ut_route.h | 29 --- zouba/qt/tests/ut_route/ut_route.pro | 20 -- zouba/qt/zouba.ui | 97 -------- zouba/route.cpp | 94 ++++++++ zouba/route.h | 65 ++++++ zouba/route_p.cpp | 105 +++++++++ zouba/route_p.h | 42 ++++ zouba/routedata.h | 12 + zouba/tests/ut_location/ut_location.cpp | 54 +++++ zouba/tests/ut_location/ut_location.h | 28 +++ zouba/tests/ut_location/ut_location.pro | 20 ++ zouba/tests/ut_route/ut_route.cpp | 385 +++++++++++++++++++++++++++++++ zouba/tests/ut_route/ut_route.h | 29 +++ zouba/tests/ut_route/ut_route.pro | 25 ++ zouba/uicontroller.cpp | 18 ++ zouba/uicontroller.h | 24 ++ zouba/ytv.h | 13 ++ zouba/zouba.pro | 25 ++ zouba/zouba.ui | 97 ++++++++ 36 files changed, 1669 insertions(+), 1219 deletions(-) create mode 100644 zouba/.gitignore create mode 100644 zouba/location.cpp create mode 100644 zouba/location.h create mode 100644 zouba/location_p.cpp create mode 100644 zouba/location_p.h create mode 100644 zouba/main.cpp create mode 100644 zouba/output.xml delete mode 100644 zouba/qt/location.h delete mode 100644 zouba/qt/main.cpp delete mode 100644 zouba/qt/output.xml delete mode 100644 zouba/qt/qt.pro delete mode 100644 zouba/qt/route.cpp delete mode 100644 zouba/qt/route.h delete mode 100644 zouba/qt/route_p.cpp delete mode 100644 zouba/qt/route_p.h delete mode 100644 zouba/qt/routedata.h delete mode 100644 zouba/qt/tests/ut_route/ut_route.cpp delete mode 100644 zouba/qt/tests/ut_route/ut_route.h delete mode 100644 zouba/qt/tests/ut_route/ut_route.pro delete mode 100644 zouba/qt/zouba.ui create mode 100644 zouba/route.cpp create mode 100644 zouba/route.h create mode 100644 zouba/route_p.cpp create mode 100644 zouba/route_p.h create mode 100644 zouba/routedata.h create mode 100644 zouba/tests/ut_location/ut_location.cpp create mode 100644 zouba/tests/ut_location/ut_location.h create mode 100644 zouba/tests/ut_location/ut_location.pro create mode 100644 zouba/tests/ut_route/ut_route.cpp create mode 100644 zouba/tests/ut_route/ut_route.h create mode 100644 zouba/tests/ut_route/ut_route.pro create mode 100644 zouba/uicontroller.cpp create mode 100644 zouba/uicontroller.h create mode 100644 zouba/ytv.h create mode 100644 zouba/zouba.pro create mode 100644 zouba/zouba.ui diff --git a/zouba/.gitignore b/zouba/.gitignore new file mode 100644 index 0000000..57c2b15 --- /dev/null +++ b/zouba/.gitignore @@ -0,0 +1,7 @@ +Makefile +moc_location.cpp +moc_location_p.cpp +moc_route.cpp +moc_route_p.cpp +moc_uicontroller.cpp +ui_zouba.h diff --git a/zouba/location.cpp b/zouba/location.cpp new file mode 100644 index 0000000..6ebab31 --- /dev/null +++ b/zouba/location.cpp @@ -0,0 +1,95 @@ +#include "location.h" + +#include "location_p.h" + +#include "ytv.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +Location::Location( QString x, QString y ) : + q( new LocationPrivate( x, y ) ), + manager( new QNetworkAccessManager(this) ) +{ + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); +} + +Location::Location( const Location &from ) : + QObject( 0 ), + q( new LocationPrivate() ), + manager( new QNetworkAccessManager(this) ) +{ + q->setX( from.x() ); + q->setY( from.y() ); + q->setValid( from.isValid() ); + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); +} + +Location::Location() : + q( new LocationPrivate() ), + manager( new QNetworkAccessManager(this) ) +{ + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); +} + +Location::~Location() +{ + delete q; + q=0; + delete manager; + manager=0; +} + +Location &Location::operator=( const Location &from ) +{ + q->setX( from.x() ); + q->setY( from.y() ); + q->setValid( from.isValid() ); + manager = new QNetworkAccessManager(this); + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); + + return *this; +} + +void Location::resolveAddress( QString address ) +{ + QUrl fullUrl( ytv ); + + fullUrl.addEncodedQueryItem( "key", address.toAscii() ); + fullUrl.addQueryItem( "user", username ); + fullUrl.addQueryItem( "pass", password ); + + manager->get( QNetworkRequest( fullUrl ) ); +} + +void Location::replyFinished( QNetworkReply * reply ) +{ + q->parseReply( reply->readAll() ); + + if ( q->isValid() ) { + emit( becomeValid() ); + } +} + +QString Location::x() const +{ + return q->x(); +} + +QString Location::y() const +{ + return q->y(); +} + +bool Location::isValid() const +{ + return q->isValid(); +} diff --git a/zouba/location.h b/zouba/location.h new file mode 100644 index 0000000..f04ad98 --- /dev/null +++ b/zouba/location.h @@ -0,0 +1,42 @@ +#ifndef LOCATION_H +#define LOCATION_H + +#include "location_p.h" + +#include +#include +#include +#include + +class Location : public QObject +{ + Q_OBJECT + +public: + Location( QString x, QString y ); + Location( const Location &from ); + Location &operator=( const Location &from ); + Location(); + + ~Location(); + + void resolveAddress( QString address ); + + QString x() const; + + QString y() const; + + bool isValid() const; + +Q_SIGNALS: + void becomeValid(); + +private Q_SLOTS: + void replyFinished( QNetworkReply * reply ); + +private: + LocationPrivate *q; + QNetworkAccessManager *manager; +}; + +#endif // LOCATION_H diff --git a/zouba/location_p.cpp b/zouba/location_p.cpp new file mode 100644 index 0000000..318630d --- /dev/null +++ b/zouba/location_p.cpp @@ -0,0 +1,74 @@ +#include "location_p.h" + +#include +#include +#include + +LocationPrivate::LocationPrivate( QString x, QString y ) : + m_x(x), + m_y(y), + m_valid(true) +{ +} + +LocationPrivate::LocationPrivate() : + m_x(0), + m_y(0), + m_valid(false) +{ +} + +void LocationPrivate::parseReply( const QByteArray &reply ) +{ + QXmlStreamReader xml( reply ); + + while ( !xml.atEnd() ) { + xml.readNext(); + if ( xml.isStartElement() && xml.name() == "LOC" ) { + QXmlStreamAttributes attributes( xml.attributes() ); + QStringRef xAttribute( attributes.value("x") ); + QStringRef yAttribute( attributes.value("y") ); + QString newX( xAttribute.toString() ); + QString newY( yAttribute.toString() ); + + m_x = newX; + m_y = newY; + } + } + + if ( xml.hasError() ) { + qDebug() << "xml error"; + } else { + m_valid = true; + } +} + +void LocationPrivate::setX( QString x ) +{ + m_x = x; +} + +QString LocationPrivate::x() const +{ + return m_x; +} + +void LocationPrivate::setY( QString y ) +{ + m_y = y; +} + +QString LocationPrivate::y() const +{ + return m_y; +} + +void LocationPrivate::setValid( bool valid ) +{ + m_valid = valid; +} + +bool LocationPrivate::isValid() const +{ + return m_valid; +} diff --git a/zouba/location_p.h b/zouba/location_p.h new file mode 100644 index 0000000..c8d108d --- /dev/null +++ b/zouba/location_p.h @@ -0,0 +1,35 @@ +#ifndef LOCATION_P_H +#define LOCATION_P_H + +#include +#include +#include + +class LocationPrivate : public QObject +{ + Q_OBJECT + +public: + LocationPrivate( QString x, QString y ); + + LocationPrivate(); + + void setX( QString x ); + QString x() const; + + void setY( QString y ); + QString y() const; + + void setValid( bool valid ); + bool isValid() const; + + void parseReply( const QByteArray &reply ); + +private: + QString m_x; + QString m_y; + bool m_valid; +}; + +#endif // LOCATION_P_H + diff --git a/zouba/main.cpp b/zouba/main.cpp new file mode 100644 index 0000000..65190f9 --- /dev/null +++ b/zouba/main.cpp @@ -0,0 +1,50 @@ +#include "routedata.h" +#include "route.h" +#include "ui_zouba.h" +#include "uicontroller.h" +#include "location.h" + +#include +#include + +namespace { + Location home( "2549183", "6672570" ); + Location work( "2551042", "6672829" ); + QString homeKey( "taivaanvuohentie%207%2Chelsinki" ); + QString workKey( "it%E4merenkatu%2011%2Chelsinki" ); +} + +int main(int argc, char *argv[] ) +{ + QApplication app(argc, argv); + QMainWindow *widget = new QMainWindow; + Ui::MainWindow ui; + ui.setupUi(widget); + + UiController *uiController = new UiController( &ui ); + + Route *route = new Route(); + + QObject::connect( + route, SIGNAL( routeReady( RouteData ) ), + uiController, SLOT( displayRoute( RouteData ) ) + ); + + Location *from = new Location(); + Location *to = new Location(); + + QObject::connect( + from, SIGNAL( becomeValid() ), + route, SLOT( setFromLocation() ) + ); + QObject::connect( + to, SIGNAL( becomeValid() ), + route, SLOT( setToLocation() ) + ); + + from->resolveAddress( homeKey ); + to->resolveAddress( workKey ); + + widget->show(); + return app.exec(); +} diff --git a/zouba/output.xml b/zouba/output.xml new file mode 100644 index 0000000..1557ded --- /dev/null +++ b/zouba/output.xml @@ -0,0 +1,330 @@ +" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +" diff --git a/zouba/qt/location.h b/zouba/qt/location.h deleted file mode 100644 index 2f4c440..0000000 --- a/zouba/qt/location.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef LOCATION_H -#define LOCATION_H - -class Location -{ -public: - Location( QString x, QString y ) : - x(x), - y(y) - { - }; - - Location( QString address ) - { - Q_UNUSED( address ); - }; - - QString x; - QString y; -}; -#endif // LOCATION_H diff --git a/zouba/qt/main.cpp b/zouba/qt/main.cpp deleted file mode 100644 index ab38693..0000000 --- a/zouba/qt/main.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "route.h" -#include "ui_zouba.h" - -#include "location.h" - -#include - -namespace { - Location home( "2549183", "6672570" ); - Location work( "2551042", "6672829" ); -} - -int main(int argc, char *argv[] ) -{ - QApplication app(argc, argv); - QMainWindow *widget = new QMainWindow; - Ui::MainWindow ui; - ui.setupUi(widget); - - Route route( &ui ); - - route.setFromLocation( work ); - route.setToLocation( home ); - - route.get(); - - ui.TimeDisplay->setText( "HELLO" ); - - widget->show(); - return app.exec(); -} diff --git a/zouba/qt/output.xml b/zouba/qt/output.xml deleted file mode 100644 index 1557ded..0000000 --- a/zouba/qt/output.xml +++ /dev/null @@ -1,330 +0,0 @@ -" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" diff --git a/zouba/qt/qt.pro b/zouba/qt/qt.pro deleted file mode 100644 index 66a76ac..0000000 --- a/zouba/qt/qt.pro +++ /dev/null @@ -1,18 +0,0 @@ -CONFIG += \ - qt \ - -QT += \ - network \ - -TEMPLATE = app -FORMS = zouba.ui - -SOURCES = \ - main.cpp \ - route.cpp \ - route_p.cpp \ - -HEADERS += \ - route.h \ - route_p.h \ - diff --git a/zouba/qt/route.cpp b/zouba/qt/route.cpp deleted file mode 100644 index 2b150f0..0000000 --- a/zouba/qt/route.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "route_p.h" -#include "route.h" - -#include "routedata.h" -#include "location.h" - -#include "ui_zouba.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace { - QUrl ytv( "http://api.reittiopas.fi/public-ytv/fi/api/" ); - QString username( "zouba" ); - QString password( "caf9r3ee" ); - - QString homeKey( "taivaanvuohentie%207%2Chelsinki" ); - QString workKey( "it%E4merenkatu%2011%2Chelsinki" ); - -} - -Route::Route( Ui::MainWindow *ui ) : - q( new RoutePrivate( this ) ), - manager( new QNetworkAccessManager(this) ), - ui( ui ) -{ - connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); -} - -Route::~Route() -{ - delete manager; - manager = 0; -} - -void Route::get() -{ - QUrl fullUrl( ytv ); - - QStringList a; - a << q->fromLocation().x << q->fromLocation().y; - QStringList b; - b << q->toLocation().x << q->toLocation().y; - - fullUrl.addQueryItem( "a", a.join(",") ); - fullUrl.addQueryItem( "b", b.join(",") ); - fullUrl.addQueryItem( "user", username ); - fullUrl.addQueryItem( "pass", password ); - - manager->get( QNetworkRequest( fullUrl ) ); -} - -void Route::replyFinished( QNetworkReply * reply ) -{ - RouteData routeData = q->parseReply( reply->readAll() ); - - ui->BusNoDisplay->setText( routeData.lineCode ); - ui->TimeDisplay->setText( routeData.arrivalTime ); -} - -void Route::setFromLocation( Location fromLocation ) -{ - q->setFromLocation( fromLocation ); -} - -Location Route::fromLocation() -{ - return q->fromLocation(); -} - -void Route::setToLocation( Location toLocation ) -{ - q->setToLocation( toLocation ); -} - -Location Route::toLocation() -{ - return q->toLocation(); -} - diff --git a/zouba/qt/route.h b/zouba/qt/route.h deleted file mode 100644 index 4e8bbf6..0000000 --- a/zouba/qt/route.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef ROUTE_H -#define ROUTE_H - -#include "ui_zouba.h" - -#include "location.h" - -#include -#include -#include - -class RoutePrivate; - -class Route: public QObject -{ - Q_OBJECT - -public: - Route( Ui::MainWindow *ui ); - ~Route(); - - Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); - Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation); - - /*! - * \brief Gets the route data from the server - */ - void get(); - - /*! - * \brief Sets the from location - * \param fromLocation The from location - */ - void setFromLocation( Location fromLocation ); - - /*! - \brief Get the from location - \return The from location - */ - Location fromLocation(); - - /*! - * \brief Sets the to location - * \param toLocation The to location - */ - void setToLocation( Location toLocation ); - - /*! - \brief Get the to location - \return The to location - */ - Location toLocation(); - -public Q_SLOTS: - void replyFinished(QNetworkReply*); - -private: - RoutePrivate *q; - QNetworkAccessManager *manager; - Ui::MainWindow *ui; -}; -#endif // ROUTE_H diff --git a/zouba/qt/route_p.cpp b/zouba/qt/route_p.cpp deleted file mode 100644 index df3c2d1..0000000 --- a/zouba/qt/route_p.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "route_p.h" -#include "location.h" - -#include -#include - -RoutePrivate::RoutePrivate( QObject *parent ) : - m_fromLocation(0,0), - m_toLocation(0,0) -{ - Q_UNUSED( parent ) -} - -RoutePrivate::~RoutePrivate() -{ -} - -RouteData RoutePrivate::parseReply( const QByteArray &reply ) -{ - RouteData retVal; - - QXmlStreamReader xml( reply ); - - bool inLine = false; - bool inStop = false; - while ( !xml.atEnd() ) { - xml.readNext(); - if ( xml.isStartElement() && xml.name() == "LINE" ) { - QString lineCode( xml.attributes().value("code").toString() ); - - retVal.lineCode = parseJORECode( lineCode ); - - inLine = true; - } else - if ( inLine && xml.name() == "STOP" ) { - inStop = true; - } else - if ( inLine && inStop && xml.name() == "ARRIVAL" ) { - QString arrivalTime( xml.attributes().value("time").toString() ); - - retVal.arrivalTime = arrivalTime; - - inLine = false; - } else - if ( xml.isEndElement() && xml.name() == "STOP" ) { - inStop = false; - } else - if ( xml.isEndElement() && xml.name() == "LINE" ) { - inLine = false; - } - } - - if ( xml.hasError() ) { - qDebug() << "xml error"; - } - - return retVal; -} - -void RoutePrivate::setFromLocation( Location fromLocation ) -{ - m_fromLocation = fromLocation; -} - -Location RoutePrivate::fromLocation() -{ - return m_fromLocation; -} - -void RoutePrivate::setToLocation( Location toLocation ) -{ - m_toLocation = toLocation; -} - -QString RoutePrivate::parseJORECode( const QString &joreCode ) const -{ - QString areaTransportTypeCode( joreCode.mid(0,1) ); - QString lineCode( joreCode.mid(1,3) ); - QString letterVariant( joreCode.mid(4,1) ); - QString letterNumberVariant( joreCode.mid(5,1) ); - QString direction( joreCode.mid(6,1) ); - - lineCode.setNum( lineCode.toInt() ); - - return lineCode+letterVariant; -} - -Location RoutePrivate::toLocation() -{ - return m_toLocation; -} diff --git a/zouba/qt/route_p.h b/zouba/qt/route_p.h deleted file mode 100644 index 520b89d..0000000 --- a/zouba/qt/route_p.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef ROUTE_P_H -#define ROUTE_P_H - -#include "routedata.h" - -#include "location.h" - -#include - -class RoutePrivate: public QObject -{ - Q_OBJECT - -public: - RoutePrivate( QObject *parent=0 ); - ~RoutePrivate(); - - Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); - Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation); - - RouteData parseReply( const QByteArray &reply ); - - void setFromLocation( Location fromLocation ); - - Location fromLocation(); - - void setToLocation( Location toLocation ); - - Location toLocation(); - -private: - Location m_fromLocation; - Location m_toLocation; - - QString parseJORECode( const QString &joreCode ) const; -}; -#endif // ROUTE_P_H diff --git a/zouba/qt/routedata.h b/zouba/qt/routedata.h deleted file mode 100644 index a2abd31..0000000 --- a/zouba/qt/routedata.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef ROUTEDATA_H -#define ROUTEDATA_H - -#include - -struct RouteData -{ - QString lineCode; - QString arrivalTime; -}; - -#endif // ROUTEDATA_H diff --git a/zouba/qt/tests/ut_route/ut_route.cpp b/zouba/qt/tests/ut_route/ut_route.cpp deleted file mode 100644 index 6df0194..0000000 --- a/zouba/qt/tests/ut_route/ut_route.cpp +++ /dev/null @@ -1,385 +0,0 @@ -#include -#include -#include -#include "ut_route.h" - -QByteArray sampleInput( -"\ -\ -\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ -\ -" - -); - -void Ut_Route::init() -{ - m_subject = new RoutePrivate(); -} - -void Ut_Route::cleanup() -{ - delete m_subject; - m_subject = 0; -} - -void Ut_Route::initTestCase() -{ -} - -void Ut_Route::cleanupTestCase() -{ -} - -void Ut_Route::testParseReply() -{ - RouteData routeData = m_subject->parseReply( sampleInput ); - - QCOMPARE( routeData.lineCode, QString( "110T" ) ); - QCOMPARE( routeData.arrivalTime, QString( "1834" ) ); -} - -void Ut_Route::testSetFromLocation() -{ - Location work( "2551042", "6672829" ); - m_subject->setFromLocation( work ); - QCOMPARE( work.x, m_subject->fromLocation().x ); - QCOMPARE( work.y, m_subject->fromLocation().y ); -} - -void Ut_Route::testSetToLocation() -{ - Location work( "2551042", "6672829" ); - m_subject->setToLocation( work ); - QCOMPARE( work.x, m_subject->toLocation().x ); - QCOMPARE( work.y, m_subject->toLocation().y ); -} - -QTEST_MAIN(Ut_Route) diff --git a/zouba/qt/tests/ut_route/ut_route.h b/zouba/qt/tests/ut_route/ut_route.h deleted file mode 100644 index 54f3c3f..0000000 --- a/zouba/qt/tests/ut_route/ut_route.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef UT_HTTPCLIENT_H -#define UT_HTTPCLIENT_H - -#include -#include - -#include - -Q_DECLARE_METATYPE(RoutePrivate*); - -class Ut_Route : public QObject -{ - Q_OBJECT - -public: - -private slots: - void init(); - void cleanup(); - void initTestCase(); - void cleanupTestCase(); - void testParseReply(); - void testSetFromLocation(); - void testSetToLocation(); - -private: - RoutePrivate *m_subject; -}; -#endif // UT_HTTPCLIENT_H diff --git a/zouba/qt/tests/ut_route/ut_route.pro b/zouba/qt/tests/ut_route/ut_route.pro deleted file mode 100644 index 820f8fb..0000000 --- a/zouba/qt/tests/ut_route/ut_route.pro +++ /dev/null @@ -1,20 +0,0 @@ -CONFIG += \ - qt \ - debug \ - -QT += \ - testlib \ - -INCLUDEPATH += \ - ../.. \ - -TEMPLATE = app - -SOURCES = \ - ut_route.cpp \ - ../../route_p.cpp \ - -HEADERS += \ - ut_route.h \ - ../../route_p.h \ - diff --git a/zouba/qt/zouba.ui b/zouba/qt/zouba.ui deleted file mode 100644 index 6eaa8b6..0000000 --- a/zouba/qt/zouba.ui +++ /dev/null @@ -1,97 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 640 - 480 - - - - ZuoBa - - - - - - 0 - 0 - 230 - 82 - - - - - - - QFormLayout::AllNonFixedFieldsGrow - - - - - Time - - - - - - - TimeDisplay - - - - - - - BusNo - - - - - - - BusNoDisplay - - - - - - - - - Work - - - - - - - Home - - - - - - - - - - - - - - 0 - 0 - 640 - 25 - - - - - - - - diff --git a/zouba/route.cpp b/zouba/route.cpp new file mode 100644 index 0000000..331ae54 --- /dev/null +++ b/zouba/route.cpp @@ -0,0 +1,94 @@ +#include "route_p.h" +#include "route.h" + +#include "routedata.h" +#include "location.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ytv.h" + +Route::Route() : + q( new RoutePrivate( this ) ), + manager( new QNetworkAccessManager(this) ) +{ + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); +} + +Route::~Route() +{ + delete manager; + manager = 0; +} + +void Route::getRoute() +{ + QUrl fullUrl( ytv ); + + QStringList a; + a << q->fromLocation().x() << q->fromLocation().y(); + QStringList b; + b << q->toLocation().x() << q->toLocation().y(); + + fullUrl.addQueryItem( "a", a.join(",") ); + fullUrl.addQueryItem( "b", b.join(",") ); + fullUrl.addQueryItem( "user", username ); + fullUrl.addQueryItem( "pass", password ); + + manager->get( QNetworkRequest( fullUrl ) ); +} + +void Route::replyFinished( QNetworkReply * reply ) +{ + RouteData routeData = q->parseReply( reply->readAll() ); + + emit( routeReady( routeData ) ); +} + +void Route::setFromLocation( const Location &location ) +{ + if ( location.isValid() ) { + q->setFromLocation( location ); + } else { + Location *locationPtr = qobject_cast(sender()); + if ( locationPtr ) { + q->setFromLocation( *locationPtr ); + if ( q->toValid() ) { + getRoute(); + } + } + } +} + +const Location &Route::fromLocation() +{ + return q->fromLocation(); +} + +void Route::setToLocation( const Location &location ) +{ + if ( location.isValid() ) { + q->setToLocation( location ); + } else { + Location *locationPtr = qobject_cast(sender()); + if ( locationPtr ) { + q->setToLocation( *locationPtr ); + if ( q->fromValid() ) { + getRoute(); + } + } + } +} + +const Location &Route::toLocation() +{ + return q->toLocation(); +} + diff --git a/zouba/route.h b/zouba/route.h new file mode 100644 index 0000000..badebda --- /dev/null +++ b/zouba/route.h @@ -0,0 +1,65 @@ +#ifndef ROUTE_H +#define ROUTE_H + +#include "routedata.h" +#include "location.h" + +#include +#include +#include + +class RoutePrivate; + +class Route: public QObject +{ + Q_OBJECT + +public: + Route(); + ~Route(); + + Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); + Q_PROPERTY(Location toLocation READ toLocation WRITE setToLocation); + + /*! + * \brief Gets the route data from the server + */ + void getRoute(); + + /*! + \brief Get the from location + \return The from location + */ + const Location &fromLocation(); + + /*! + \brief Get the to location + \return The to location + */ + const Location &toLocation(); + +public Q_SLOTS: + + /*! + * \brief Sets the from location + * \param fromLocation The from location + */ + void setFromLocation( const Location &location=Location() ); + + /*! + * \brief Sets the to location + * \param toLocation The to location + */ + void setToLocation( const Location &location=Location() ); + +Q_SIGNALS: + void routeReady( RouteData ); + +private Q_SLOTS: + void replyFinished( QNetworkReply* ); + +private: + RoutePrivate *q; + QNetworkAccessManager *manager; +}; +#endif // ROUTE_H diff --git a/zouba/route_p.cpp b/zouba/route_p.cpp new file mode 100644 index 0000000..f2cb747 --- /dev/null +++ b/zouba/route_p.cpp @@ -0,0 +1,105 @@ +#include "route_p.h" +#include "location.h" + +#include +#include + +RoutePrivate::RoutePrivate( QObject *parent ) : + m_fromValid(false), + m_toValid(false), + m_fromLocation(0,0), + m_toLocation(0,0) +{ + Q_UNUSED( parent ) +} + +RoutePrivate::~RoutePrivate() +{ +} + +RouteData RoutePrivate::parseReply( const QByteArray &reply ) +{ + RouteData retVal; + + QXmlStreamReader xml( reply ); + + bool inLine = false; + bool inStop = false; + while ( !xml.atEnd() ) { + xml.readNext(); + if ( xml.isStartElement() && xml.name() == "LINE" ) { + QString lineCode( xml.attributes().value("code").toString() ); + + retVal.lineCode = parseJORECode( lineCode ); + + inLine = true; + } else + if ( inLine && xml.name() == "STOP" ) { + inStop = true; + } else + if ( inLine && inStop && xml.name() == "ARRIVAL" ) { + QString arrivalTime( xml.attributes().value("time").toString() ); + + retVal.arrivalTime = arrivalTime; + + inLine = false; + } else + if ( xml.isEndElement() && xml.name() == "STOP" ) { + inStop = false; + } else + if ( xml.isEndElement() && xml.name() == "LINE" ) { + inLine = false; + } + } + + if ( xml.hasError() ) { + qDebug() << "xml error"; + } + + return retVal; +} + +void RoutePrivate::setFromLocation( const Location &location ) +{ + m_fromLocation = location; + m_fromValid = true; +} + +const Location &RoutePrivate::fromLocation() +{ + return m_fromLocation; +} + +void RoutePrivate::setToLocation( const Location &toLocation ) +{ + m_toLocation = toLocation; + m_toValid = true; +} + +QString RoutePrivate::parseJORECode( const QString &joreCode ) const +{ + QString areaTransportTypeCode( joreCode.mid(0,1) ); + QString lineCode( joreCode.mid(1,3) ); + QString letterVariant( joreCode.mid(4,1) ); + QString letterNumberVariant( joreCode.mid(5,1) ); + QString direction( joreCode.mid(6,1) ); + + lineCode.setNum( lineCode.toInt() ); + + return lineCode+letterVariant; +} + +const Location &RoutePrivate::toLocation() +{ + return m_toLocation; +} + +bool RoutePrivate::fromValid() +{ + return m_fromValid; +} + +bool RoutePrivate::toValid() +{ + return m_toValid; +} diff --git a/zouba/route_p.h b/zouba/route_p.h new file mode 100644 index 0000000..836a4a7 --- /dev/null +++ b/zouba/route_p.h @@ -0,0 +1,42 @@ +#ifndef ROUTE_P_H +#define ROUTE_P_H + +#include "routedata.h" + +#include "location.h" + +#include + +class RoutePrivate: public QObject +{ + Q_OBJECT + +public: + RoutePrivate( QObject *parent=0 ); + ~RoutePrivate(); + + Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); + Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation); + + RouteData parseReply( const QByteArray &reply ); + + void setFromLocation( const Location &fromLocation ); + + const Location &fromLocation(); + + void setToLocation( const Location &toLocation ); + + const Location &toLocation(); + + bool toValid(); + bool fromValid(); + +private: + bool m_fromValid; + bool m_toValid; + Location m_fromLocation; + Location m_toLocation; + + QString parseJORECode( const QString &joreCode ) const; +}; +#endif // ROUTE_P_H diff --git a/zouba/routedata.h b/zouba/routedata.h new file mode 100644 index 0000000..a2abd31 --- /dev/null +++ b/zouba/routedata.h @@ -0,0 +1,12 @@ +#ifndef ROUTEDATA_H +#define ROUTEDATA_H + +#include + +struct RouteData +{ + QString lineCode; + QString arrivalTime; +}; + +#endif // ROUTEDATA_H diff --git a/zouba/tests/ut_location/ut_location.cpp b/zouba/tests/ut_location/ut_location.cpp new file mode 100644 index 0000000..85228b2 --- /dev/null +++ b/zouba/tests/ut_location/ut_location.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include "ut_location.h" + +QByteArray sampleInput( +"\ +\ +\ +\ +\ +\ +" + +); + +void Ut_Location::init() +{ + m_subject = new LocationPrivate(); +} + +void Ut_Location::cleanup() +{ + delete m_subject; + m_subject = 0; +} + +void Ut_Location::initTestCase() +{ +} + +void Ut_Location::cleanupTestCase() +{ +} + +void Ut_Location::testParseReply() +{ + m_subject->parseReply( sampleInput ); + + QCOMPARE( m_subject->x(), QString( "2549183" ) ); + QCOMPARE( m_subject->y(), QString( "6672570" ) ); +} + +void Ut_Location::testSet() +{ + QString x( "2549183" ); + QString y( "6672570" ); + m_subject->setX( x ); + m_subject->setY( y ); + QCOMPARE( x, m_subject->x() ); + QCOMPARE( y, m_subject->y() ); +} + +QTEST_MAIN(Ut_Location) diff --git a/zouba/tests/ut_location/ut_location.h b/zouba/tests/ut_location/ut_location.h new file mode 100644 index 0000000..1b44ace --- /dev/null +++ b/zouba/tests/ut_location/ut_location.h @@ -0,0 +1,28 @@ +#ifndef UT_LOCATION_H +#define UT_LOCATION_H + +#include +#include + +#include + +Q_DECLARE_METATYPE(LocationPrivate*); + +class Ut_Location : public QObject +{ + Q_OBJECT + +public: + +private slots: + void init(); + void cleanup(); + void initTestCase(); + void cleanupTestCase(); + void testParseReply(); + void testSet(); + +private: + LocationPrivate *m_subject; +}; +#endif // UT_LOCATION_H diff --git a/zouba/tests/ut_location/ut_location.pro b/zouba/tests/ut_location/ut_location.pro new file mode 100644 index 0000000..8d8ba3c --- /dev/null +++ b/zouba/tests/ut_location/ut_location.pro @@ -0,0 +1,20 @@ +CONFIG += \ + qt \ + debug \ + +QT += \ + testlib \ + +INCLUDEPATH += \ + ../.. \ + +TEMPLATE = app + +SOURCES = \ + ut_location.cpp \ + ../../location_p.cpp \ + +HEADERS += \ + ut_location.h \ + ../../location_p.h \ + diff --git a/zouba/tests/ut_route/ut_route.cpp b/zouba/tests/ut_route/ut_route.cpp new file mode 100644 index 0000000..294c1c0 --- /dev/null +++ b/zouba/tests/ut_route/ut_route.cpp @@ -0,0 +1,385 @@ +#include +#include +#include +#include "ut_route.h" + +QByteArray sampleInput( +"\ +\ +\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +\ +" + +); + +void Ut_Route::init() +{ + m_subject = new RoutePrivate(); +} + +void Ut_Route::cleanup() +{ + delete m_subject; + m_subject = 0; +} + +void Ut_Route::initTestCase() +{ +} + +void Ut_Route::cleanupTestCase() +{ +} + +void Ut_Route::testParseReply() +{ + RouteData routeData = m_subject->parseReply( sampleInput ); + + QCOMPARE( routeData.lineCode, QString( "110T" ) ); + QCOMPARE( routeData.arrivalTime, QString( "1834" ) ); +} + +void Ut_Route::testSetFromLocation() +{ + Location work( "2551042", "6672829" ); + m_subject->setFromLocation( work ); + QCOMPARE( work.x(), m_subject->fromLocation().x() ); + QCOMPARE( work.y(), m_subject->fromLocation().y() ); +} + +void Ut_Route::testSetToLocation() +{ + Location work( "2551042", "6672829" ); + m_subject->setToLocation( work ); + QCOMPARE( work.x(), m_subject->toLocation().x() ); + QCOMPARE( work.y(), m_subject->toLocation().y() ); +} + +QTEST_MAIN(Ut_Route) diff --git a/zouba/tests/ut_route/ut_route.h b/zouba/tests/ut_route/ut_route.h new file mode 100644 index 0000000..8571d3b --- /dev/null +++ b/zouba/tests/ut_route/ut_route.h @@ -0,0 +1,29 @@ +#ifndef UT_ROUTE_H +#define UT_ROUTE_H + +#include +#include + +#include + +Q_DECLARE_METATYPE(RoutePrivate*); + +class Ut_Route : public QObject +{ + Q_OBJECT + +public: + +private slots: + void init(); + void cleanup(); + void initTestCase(); + void cleanupTestCase(); + void testParseReply(); + void testSetFromLocation(); + void testSetToLocation(); + +private: + RoutePrivate *m_subject; +}; +#endif // UT_ROUTE_H diff --git a/zouba/tests/ut_route/ut_route.pro b/zouba/tests/ut_route/ut_route.pro new file mode 100644 index 0000000..4b697df --- /dev/null +++ b/zouba/tests/ut_route/ut_route.pro @@ -0,0 +1,25 @@ +CONFIG += \ + qt \ + debug \ + +QT += \ + testlib \ + network \ + +INCLUDEPATH += \ + ../.. \ + +TEMPLATE = app + +SOURCES = \ + ut_route.cpp \ + ../../route_p.cpp \ + ../../location.cpp \ + ../../location_p.cpp \ + +HEADERS += \ + ut_route.h \ + ../../route_p.h \ + ../../location.h \ + ../../location_p.h \ + diff --git a/zouba/uicontroller.cpp b/zouba/uicontroller.cpp new file mode 100644 index 0000000..f389d66 --- /dev/null +++ b/zouba/uicontroller.cpp @@ -0,0 +1,18 @@ +#include "uicontroller.h" +#include "route.h" +#include "ui_zouba.h" + +UiController::UiController( Ui::MainWindow *ui ) : + ui(ui) +{ +} + +UiController::~UiController() +{ +} + +void UiController::displayRoute( const RouteData &routeData ) +{ + ui->BusNoDisplay->setText( routeData.lineCode ); + ui->TimeDisplay->setText( routeData.arrivalTime ); +} diff --git a/zouba/uicontroller.h b/zouba/uicontroller.h new file mode 100644 index 0000000..0ea3010 --- /dev/null +++ b/zouba/uicontroller.h @@ -0,0 +1,24 @@ +#ifndef UICONTROLLER_H +#define UICONTROLLER_H + +#include "ui_zouba.h" +#include "routedata.h" + +#include + +class UiController : public QObject +{ + Q_OBJECT + +public: + UiController( Ui::MainWindow *ui ); + ~UiController(); + +public Q_SLOTS: + void displayRoute( const RouteData &routeData ); + +private: + Ui::MainWindow *ui; +}; +#endif // UICONTROLLER_H + diff --git a/zouba/ytv.h b/zouba/ytv.h new file mode 100644 index 0000000..658262f --- /dev/null +++ b/zouba/ytv.h @@ -0,0 +1,13 @@ + +#include +#include + +namespace { + QUrl ytv( "http://api.reittiopas.fi/public-ytv/fi/api/" ); + QString username( "zouba" ); + QString password( "caf9r3ee" ); + + QString homeKey( "taivaanvuohentie%207%2Chelsinki" ); + QString workKey( "it%E4merenkatu%2011%2Chelsinki" ); +} + diff --git a/zouba/zouba.pro b/zouba/zouba.pro new file mode 100644 index 0000000..4c32159 --- /dev/null +++ b/zouba/zouba.pro @@ -0,0 +1,25 @@ +CONFIG += \ + qt \ + debug \ + +QT += \ + network \ + +TEMPLATE = app +FORMS = zouba.ui + +SOURCES = \ + main.cpp \ + route.cpp \ + route_p.cpp \ + uicontroller.cpp \ + location.cpp \ + location_p.cpp \ + +HEADERS += \ + route.h \ + route_p.h \ + uicontroller.h \ + location.h \ + location_p.h \ + diff --git a/zouba/zouba.ui b/zouba/zouba.ui new file mode 100644 index 0000000..6eaa8b6 --- /dev/null +++ b/zouba/zouba.ui @@ -0,0 +1,97 @@ + + + MainWindow + + + + 0 + 0 + 640 + 480 + + + + ZuoBa + + + + + + 0 + 0 + 230 + 82 + + + + + + + QFormLayout::AllNonFixedFieldsGrow + + + + + Time + + + + + + + TimeDisplay + + + + + + + BusNo + + + + + + + BusNoDisplay + + + + + + + + + Work + + + + + + + Home + + + + + + + + + + + + + + 0 + 0 + 640 + 25 + + + + + + + + -- 1.7.9.5