Changes:
authorMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Thu, 18 Mar 2010 17:46:07 +0000 (19:46 +0200)
committerMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Thu, 18 Mar 2010 18:55:18 +0000 (20:55 +0200)
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

13 files changed:
zouba/gpscontroller.cpp
zouba/gpscontroller.h
zouba/location.cpp
zouba/main.cpp
zouba/route.cpp
zouba/route.h
zouba/route_p.cpp
zouba/route_p.h
zouba/ui.cpp
zouba/ui.h
zouba/uicontroller.cpp
zouba/uicontroller.h
zouba/ytv.h

index 61fbdbc..49335c5 100644 (file)
@@ -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;
 }
index 8628828..615a5fa 100644 (file)
@@ -29,6 +29,7 @@ Q_SIGNALS:
 
 private:
   QGeoPositionInfoSource *m_location;
+  bool updatesEnabled;
 };
 
 #endif // GPSCONTROLLER_H
index 1feaddc..349777d 100644 (file)
@@ -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 ) );
 }
index c65b91d..97ef478 100644 (file)
@@ -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<RouteData> ) ),
+      uiController, SLOT( displayRoute( QList<RouteData> ) )
       );
 
   QObject::connect(
index e8a2790..d38ac0e 100644 (file)
@@ -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> 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<Location*>(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<Location*>(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";
index 139b67f..1047cf5 100644 (file)
@@ -58,7 +58,7 @@ public Q_SLOTS:
   void toggleDirection();
 
 Q_SIGNALS:
-  void routeReady( RouteData );
+  void routeReady( QList<RouteData> );
 
 private Q_SLOTS:
   void replyFinished( QNetworkReply* );
index 9e91620..19105a7 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QXmlStreamReader>
 #include <QDebug>
+#include <QList>
 
 RoutePrivate::RoutePrivate( QObject *parent ) :
     m_fromValid(false),
@@ -17,10 +18,10 @@ RoutePrivate::~RoutePrivate()
 {
 }
 
-RouteData RoutePrivate::parseReply( const QByteArray &reply )
+QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
 {
-  qDebug() << __PRETTY_FUNCTION__;
-  RouteData retVal;
+  QList<RouteData> 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()
index 836a4a7..ed328da 100644 (file)
@@ -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<RouteData> parseReply( const QByteArray &reply );
 
   void setFromLocation( const Location &fromLocation );
 
index ab82b1b..e0ceae0 100644 (file)
@@ -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 );
index 886b50b..77ba935 100644 (file)
@@ -18,6 +18,16 @@ public:
     WorkButtonId=1
   };
 
+  enum {
+    ScreenWidth=800,
+    ScreenHeight=480
+  };
+
+  enum {
+    ButtonWidth=300,
+    ButtonHeight=70
+  };
+
   QWidget *centralWidget;
   QButtonGroup *destinationButtons;
   QTableWidget *table;
index d2c1c71..cb49179 100644 (file)
@@ -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> &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; i<routeData.count(); i++ ) {
+    QTableWidgetItem *timeItem = new QTableWidgetItem( routeData.at(i).arrivalTime );
+    ui->table->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 );
+  }
 }
index d7c2afb..89ce513 100644 (file)
@@ -17,7 +17,7 @@ public:
   ~UiController();
 
 public Q_SLOTS:
-  void displayRoute( const RouteData &routeData );
+  void displayRoute( const QList<RouteData> &routeData );
 
 Q_SIGNALS:
   void buttonClicked();
index b0e1617..430e3cb 100644 (file)
@@ -1,12 +1,28 @@
 #include <QUrl>
 #include <QString>
 
-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
+  };
+
+};