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;
}
private:
QGeoPositionInfoSource *m_location;
+ bool updatesEnabled;
};
#endif // GPSCONTROLLER_H
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 ) );
}
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(
void Route::getRoute()
{
- qDebug() << __PRETTY_FUNCTION__;
- QUrl fullUrl( ytv );
+ QUrl fullUrl( Ytv::Url );
QStringList a;
a << q->fromLocation().x() << q->fromLocation().y();
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";
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";
void toggleDirection();
Q_SIGNALS:
- void routeReady( RouteData );
+ void routeReady( QList<RouteData> );
private Q_SLOTS:
void replyFinished( QNetworkReply* );
#include <QXmlStreamReader>
#include <QDebug>
+#include <QList>
RoutePrivate::RoutePrivate( QObject *parent ) :
m_fromValid(false),
{
}
-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 );
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;
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;
}
}
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()
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 );
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 );
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 );
WorkButtonId=1
};
+ enum {
+ ScreenWidth=800,
+ ScreenHeight=480
+ };
+
+ enum {
+ ButtonWidth=300,
+ ButtonHeight=70
+ };
+
QWidget *centralWidget;
QButtonGroup *destinationButtons;
QTableWidget *table;
this, SLOT( setWorkButtonValid() )
);
- homeLocation->resolveAddress( home );
- workLocation->resolveAddress( work );
+ homeLocation->resolveAddress( Ytv::Home );
+ workLocation->resolveAddress( Ytv::Work );
destination.append( homeLocation );
destination.append( workLocation );
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 );
+ }
}
~UiController();
public Q_SLOTS:
- void displayRoute( const RouteData &routeData );
+ void displayRoute( const QList<RouteData> &routeData );
Q_SIGNALS:
void buttonClicked();
#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
+ };
+
+};