From: Max Waterman Date: Thu, 18 Mar 2010 17:46:07 +0000 (+0200) Subject: Changes: X-Git-Url: http://vcs.maemo.org/git/?p=ptas;a=commitdiff_plain;h=ef04e25d328c588e1a450a9122e165921a2b15f5 Changes: 1) general refactoring 2) moved magic numbers to constants and other minor work 3) made buttons bigger 4) enabled multiple results 5) keeping gps on --- diff --git a/zouba/gpscontroller.cpp b/zouba/gpscontroller.cpp index 61fbdbc..49335c5 100644 --- a/zouba/gpscontroller.cpp +++ b/zouba/gpscontroller.cpp @@ -8,37 +8,36 @@ QTM_USE_NAMESPACE GpsController::GpsController() : - m_location( QGeoPositionInfoSource::createDefaultSource(this) ) + m_location( QGeoPositionInfoSource::createDefaultSource(this) ), + updatesEnabled(false) { - qDebug() << __PRETTY_FUNCTION__; m_location->setUpdateInterval( 1*60*1000 ); connect( m_location, SIGNAL( positionUpdated( QGeoPositionInfo ) ), this, SLOT( updateLocation( QGeoPositionInfo ) ) - ); + ); - m_location->stopUpdates(); + m_location->startUpdates(); } GpsController::~GpsController() { - qDebug() << __PRETTY_FUNCTION__; delete m_location; m_location = 0; } void GpsController::updateLocation( QGeoPositionInfo positionInfo ) { - qDebug() << __PRETTY_FUNCTION__; Location newLocation( positionInfo ); - emit locationChanged( newLocation ); - m_location->stopUpdates(); + if ( updatesEnabled ) { + emit locationChanged( newLocation ); + updatesEnabled = false; + } } void GpsController::startGps() { - qDebug() << __PRETTY_FUNCTION__; - m_location->startUpdates(); + updatesEnabled = true; } diff --git a/zouba/gpscontroller.h b/zouba/gpscontroller.h index 8628828..615a5fa 100644 --- a/zouba/gpscontroller.h +++ b/zouba/gpscontroller.h @@ -29,6 +29,7 @@ Q_SIGNALS: private: QGeoPositionInfoSource *m_location; + bool updatesEnabled; }; #endif // GPSCONTROLLER_H diff --git a/zouba/location.cpp b/zouba/location.cpp index 1feaddc..349777d 100644 --- a/zouba/location.cpp +++ b/zouba/location.cpp @@ -100,11 +100,11 @@ Location &Location::operator=( const Location &from ) void Location::resolveAddress( QString address ) { - QUrl fullUrl( ytv ); + QUrl fullUrl( Ytv::Url ); fullUrl.addEncodedQueryItem( "key", address.toAscii().toPercentEncoding() ); - fullUrl.addQueryItem( "user", username ); - fullUrl.addQueryItem( "pass", password ); + fullUrl.addQueryItem( "user", Ytv::Username ); + fullUrl.addQueryItem( "pass", Ytv::Password ); manager->get( QNetworkRequest( fullUrl ) ); } diff --git a/zouba/main.cpp b/zouba/main.cpp index c65b91d..97ef478 100644 --- a/zouba/main.cpp +++ b/zouba/main.cpp @@ -24,8 +24,8 @@ int main(int argc, char *argv[] ) GpsController *gpsController = new GpsController(); QObject::connect( - route, SIGNAL( routeReady( RouteData ) ), - uiController, SLOT( displayRoute( RouteData ) ) + route, SIGNAL( routeReady( QList ) ), + uiController, SLOT( displayRoute( QList ) ) ); QObject::connect( diff --git a/zouba/route.cpp b/zouba/route.cpp index e8a2790..d38ac0e 100644 --- a/zouba/route.cpp +++ b/zouba/route.cpp @@ -30,8 +30,7 @@ Route::~Route() void Route::getRoute() { - qDebug() << __PRETTY_FUNCTION__; - QUrl fullUrl( ytv ); + QUrl fullUrl( Ytv::Url ); QStringList a; a << q->fromLocation().x() << q->fromLocation().y(); @@ -40,42 +39,34 @@ void Route::getRoute() fullUrl.addQueryItem( "a", a.join(",") ); fullUrl.addQueryItem( "b", b.join(",") ); - fullUrl.addQueryItem( "show", "1" ); - fullUrl.addQueryItem( "walkspeed", "3" ); - fullUrl.addQueryItem( "user", username ); - fullUrl.addQueryItem( "pass", password ); + fullUrl.addQueryItem( "show", QString::number(Ytv::FiveResults) ); + fullUrl.addQueryItem( "walkspeed", QString::number(Ytv::Fast) ); + fullUrl.addQueryItem( "user", Ytv::Username ); + fullUrl.addQueryItem( "pass", Ytv::Password ); manager->get( QNetworkRequest( fullUrl ) ); } void Route::replyFinished( QNetworkReply * reply ) { - qDebug() << __PRETTY_FUNCTION__; - RouteData routeData = q->parseReply( reply->readAll() ); + QList routeData = q->parseReply( reply->readAll() ); emit( routeReady( routeData ) ); } void Route::setFromLocation( const Location &location ) { - qDebug() << __PRETTY_FUNCTION__; if ( location.isValid() ) { - qDebug() << "from location is valid"; q->setFromLocation( location ); if ( q->toValid() ) { - qDebug() << "to is also valid; getting route"; getRoute(); } } else { - qDebug() << "location is NOT valid - obtaining from sender"; Location *locationPtr = qobject_cast(sender()); if ( locationPtr ) { q->setFromLocation( *locationPtr ); if ( q->toValid() ) { - qDebug() << "to is also valid; getting route"; getRoute(); - } else { - qDebug() << "to is NOT valid"; } } else { qDebug() << "locationPtr is zero - cast didn't work"; @@ -90,26 +81,17 @@ const Location &Route::fromLocation() void Route::setToLocation( const Location &location ) { - qDebug() << __PRETTY_FUNCTION__; if ( location.isValid() ) { - qDebug() << "to is valid"; q->setToLocation( location ); if ( q->fromValid() ) { - qDebug() << "from is also valid; getting route"; getRoute(); - } else { - qDebug() << "from is NOT valid"; } } else { - qDebug() << "to is not valid; getting from sender"; Location *locationPtr = qobject_cast(sender()); if ( locationPtr ) { q->setToLocation( *locationPtr ); if ( q->fromValid() ) { - qDebug() << "from is also valid; getting route"; getRoute(); - } else { - qDebug() << "from is not valid"; } } else { qDebug() << "locationPtr is zero; cast failed"; diff --git a/zouba/route.h b/zouba/route.h index 139b67f..1047cf5 100644 --- a/zouba/route.h +++ b/zouba/route.h @@ -58,7 +58,7 @@ public Q_SLOTS: void toggleDirection(); Q_SIGNALS: - void routeReady( RouteData ); + void routeReady( QList ); private Q_SLOTS: void replyFinished( QNetworkReply* ); diff --git a/zouba/route_p.cpp b/zouba/route_p.cpp index 9e91620..19105a7 100644 --- a/zouba/route_p.cpp +++ b/zouba/route_p.cpp @@ -3,6 +3,7 @@ #include #include +#include RoutePrivate::RoutePrivate( QObject *parent ) : m_fromValid(false), @@ -17,10 +18,10 @@ RoutePrivate::~RoutePrivate() { } -RouteData RoutePrivate::parseReply( const QByteArray &reply ) +QList RoutePrivate::parseReply( const QByteArray &reply ) { - qDebug() << __PRETTY_FUNCTION__; - RouteData retVal; + QList retVal; + RouteData routeData; QXmlStreamReader xml( reply ); @@ -29,13 +30,12 @@ RouteData RoutePrivate::parseReply( const QByteArray &reply ) bool inLine = false; bool inStop = false; - while ( !(haveLine && haveTime) && !xml.atEnd() ) { + while ( !xml.atEnd() ) { xml.readNext(); - if ( !haveLine && xml.isStartElement() && xml.name() == "LINE" ) { + if ( xml.isStartElement() && xml.name() == "LINE" ) { QString lineCode( xml.attributes().value("code").toString() ); - qDebug() << "lineCode" << lineCode; - retVal.lineCode = parseJORECode( lineCode ); + routeData.lineCode = parseJORECode( lineCode ); haveLine = true; inLine = true; @@ -43,20 +43,29 @@ RouteData RoutePrivate::parseReply( const QByteArray &reply ) if ( inLine && xml.name() == "STOP" ) { inStop = true; } else - if ( !haveTime && inLine && inStop && xml.name() == "ARRIVAL" ) { + if ( inLine && inStop && xml.name() == "ARRIVAL" ) { QString arrivalTime( xml.attributes().value("time").toString() ); - qDebug() << "arrivalTime" << arrivalTime; - retVal.arrivalTime = arrivalTime.rightJustified(4).insert(2,":"); + routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":"); haveTime = true; inLine = false; } else if ( xml.isEndElement() && xml.name() == "STOP" ) { inStop = false; + haveTime = false; } else if ( xml.isEndElement() && xml.name() == "LINE" ) { inLine = false; + haveLine = false; + } + + if ( haveLine && haveTime ) { + retVal.append( routeData ); + + // only want first STOP per LINE + haveTime = false; + haveLine = false; } } @@ -86,15 +95,23 @@ void RoutePrivate::setToLocation( const Location &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; + QString retVal; + + 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() ); + + retVal = lineCode; + + if ( letterVariant != " " ) { + retVal += letterVariant; + } + + return retVal; } const Location &RoutePrivate::toLocation() diff --git a/zouba/route_p.h b/zouba/route_p.h index 836a4a7..ed328da 100644 --- a/zouba/route_p.h +++ b/zouba/route_p.h @@ -18,7 +18,7 @@ public: Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation); - RouteData parseReply( const QByteArray &reply ); + QList parseReply( const QByteArray &reply ); void setFromLocation( const Location &fromLocation ); diff --git a/zouba/ui.cpp b/zouba/ui.cpp index ab82b1b..e0ceae0 100644 --- a/zouba/ui.cpp +++ b/zouba/ui.cpp @@ -29,13 +29,13 @@ void Ui::setupUi( QMainWindow *mainWindow ) QPushButton *homeButton = new QPushButton( centralWidget ); homeButton->setObjectName( QString::fromUtf8("homeButton") ); homeButton->setText( "HOME" ); - homeButton->setGeometry( QRect( 0, 0, 150, 40 ) ); + homeButton->setGeometry( QRect( 0, 0, ButtonWidth, ButtonHeight ) ); homeButton->setEnabled(false); QPushButton *workButton = new QPushButton( centralWidget ); workButton->setObjectName( QString::fromUtf8("workButton") ); workButton->setText( "WORK" ); - workButton->setGeometry( QRect( 0, 40, 150, 40 ) ); + workButton->setGeometry( QRect( 0, ButtonHeight, ButtonWidth, ButtonHeight ) ); workButton->setEnabled(false); destinationButtons = new QButtonGroup( centralWidget ); @@ -44,7 +44,7 @@ void Ui::setupUi( QMainWindow *mainWindow ) table = new QTableWidget( 1, 2, centralWidget ); table->setObjectName( QString::fromUtf8("table") ); - table->setGeometry( QRect( 151, 0, 650, 480 ) ); + table->setGeometry( QRect( ButtonWidth+1, 0, ScreenWidth-ButtonWidth, ScreenHeight ) ); QStringList columnHeaders; columnHeaders << "Time" << "Bus"; table->setHorizontalHeaderLabels( columnHeaders ); diff --git a/zouba/ui.h b/zouba/ui.h index 886b50b..77ba935 100644 --- a/zouba/ui.h +++ b/zouba/ui.h @@ -18,6 +18,16 @@ public: WorkButtonId=1 }; + enum { + ScreenWidth=800, + ScreenHeight=480 + }; + + enum { + ButtonWidth=300, + ButtonHeight=70 + }; + QWidget *centralWidget; QButtonGroup *destinationButtons; QTableWidget *table; diff --git a/zouba/uicontroller.cpp b/zouba/uicontroller.cpp index d2c1c71..cb49179 100644 --- a/zouba/uicontroller.cpp +++ b/zouba/uicontroller.cpp @@ -25,8 +25,8 @@ UiController::UiController( Ui *ui ) : this, SLOT( setWorkButtonValid() ) ); - homeLocation->resolveAddress( home ); - workLocation->resolveAddress( work ); + homeLocation->resolveAddress( Ytv::Home ); + workLocation->resolveAddress( Ytv::Work ); destination.append( homeLocation ); destination.append( workLocation ); @@ -69,15 +69,15 @@ void UiController::changeDestination( int id ) emit buttonClicked(); } -void UiController::displayRoute( const RouteData &routeData ) +void UiController::displayRoute( const QList &routeData ) { - qDebug() << __PRETTY_FUNCTION__; - qDebug() << "routeData.arrivalTime" << routeData.arrivalTime; - qDebug() << "routeData.lineCode" << routeData.lineCode; + ui->table->setRowCount( routeData.count() ); - QTableWidgetItem *timeItem = new QTableWidgetItem( routeData.arrivalTime ); - ui->table->setItem( 0, 0, timeItem ); + for ( int i=0; itable->setItem( i, 0, timeItem ); - QTableWidgetItem *lineItem = new QTableWidgetItem( routeData.lineCode ); - ui->table->setItem( 0, 1, lineItem ); + QTableWidgetItem *lineItem = new QTableWidgetItem( routeData.at(i).lineCode ); + ui->table->setItem( i, 1, lineItem ); + } } diff --git a/zouba/uicontroller.h b/zouba/uicontroller.h index d7c2afb..89ce513 100644 --- a/zouba/uicontroller.h +++ b/zouba/uicontroller.h @@ -17,7 +17,7 @@ public: ~UiController(); public Q_SLOTS: - void displayRoute( const RouteData &routeData ); + void displayRoute( const QList &routeData ); Q_SIGNALS: void buttonClicked(); diff --git a/zouba/ytv.h b/zouba/ytv.h index b0e1617..430e3cb 100644 --- a/zouba/ytv.h +++ b/zouba/ytv.h @@ -1,12 +1,28 @@ #include #include -namespace { - const QString ytv( "http://api.reittiopas.fi/public-ytv/fi/api/" ); - const QString username( "zouba" ); - const QString password( "caf9r3ee" ); - - const QString home( QByteArray::fromPercentEncoding( "Taivaanvuohentie%207%2CHelsinki" ) ); - const QString work( QByteArray::fromPercentEncoding( "It%E4merenkatu%2011%2CHelsinki" ) ); -} +namespace Ytv { + const QString Url( "http://api.reittiopas.fi/public-ytv/fi/api/" ); + const QString Username( "zouba" ); + const QString Password( "caf9r3ee" ); + + const QString Home( QByteArray::fromPercentEncoding( "Taivaanvuohentie%207%2CHelsinki" ) ); + const QString Work( QByteArray::fromPercentEncoding( "It%E4merenkatu%2011%2CHelsinki" ) ); + + enum { + Slow=1, + Fast=2, + Normal=3, + Running=4, + Cycling=5, + NoWalkSpeeds=5 + }; + + enum { + OneResult=1, + ThreeResults=3, + FiveResults=5 + }; + +};