From d74ef6b41b59eef3192a30a77c552b9dcc400234 Mon Sep 17 00:00:00 2001 From: Max Waterman Date: Sun, 21 Mar 2010 20:52:33 +0200 Subject: [PATCH] Added dialogs to change home and work locations. --- zouba/gpscontroller.cpp | 20 +++++++++--------- zouba/gpscontroller.h | 4 ++-- zouba/location.cpp | 47 +++++++++++++++++++++++++++++++++--------- zouba/location.h | 18 ++++++++-------- zouba/location_p.cpp | 32 +++++++++++++++++++++++++---- zouba/location_p.h | 16 +++++++++++---- zouba/locations.cpp | 37 +++++++++++++++++++++++++++++++++ zouba/locations.h | 22 ++++++++++++++++++++ zouba/main.cpp | 12 ++++------- zouba/messagetable.h | 2 +- zouba/route.cpp | 52 +++++++++++++++++++++++------------------------ zouba/route.h | 16 ++++----------- zouba/route_p.cpp | 12 +++++------ zouba/route_p.h | 18 ++++++++-------- zouba/ui.cpp | 30 +++++++++++++++++++++++++++ zouba/ui.h | 3 +++ zouba/uicontroller.cpp | 12 +++++++---- zouba/uicontroller.h | 2 +- zouba/zouba.pro | 1 + 19 files changed, 251 insertions(+), 105 deletions(-) create mode 100644 zouba/locations.cpp create mode 100644 zouba/locations.h diff --git a/zouba/gpscontroller.cpp b/zouba/gpscontroller.cpp index b031b61..c8226ac 100644 --- a/zouba/gpscontroller.cpp +++ b/zouba/gpscontroller.cpp @@ -9,7 +9,7 @@ QTM_USE_NAMESPACE GpsController::GpsController() : m_location( QGeoPositionInfoSource::createDefaultSource(this) ), - updatesEnabled(false) + currentLocation(0) { m_location->setUpdateInterval( 1*60*1000 ); @@ -25,24 +25,24 @@ GpsController::~GpsController() { delete m_location; m_location = 0; + delete currentLocation; + currentLocation = 0; } void GpsController::updateLocation( QGeoPositionInfo positionInfo ) { qDebug() << "new GPS position"; - Location newLocation( positionInfo ); + delete currentLocation; + currentLocation = new Location( positionInfo ); - if ( updatesEnabled ) { - qDebug() << "from location changed"; - emit locationChanged( newLocation ); - updatesEnabled = false; - m_location->setUpdateInterval( 1*60*1000 ); - } + qDebug() << "from location changed"; + emit locationChanged( currentLocation ); } void GpsController::startGps() { - updatesEnabled = true; - m_location->setUpdateInterval( 1 ); + if ( currentLocation != 0 ) { + emit locationChanged( currentLocation ); + } } diff --git a/zouba/gpscontroller.h b/zouba/gpscontroller.h index 615a5fa..f220ce1 100644 --- a/zouba/gpscontroller.h +++ b/zouba/gpscontroller.h @@ -25,11 +25,11 @@ public Q_SLOTS: void startGps(); Q_SIGNALS: - void locationChanged( const Location &newLocation ); + void locationChanged( Location *newLocation ); private: QGeoPositionInfoSource *m_location; - bool updatesEnabled; + Location *currentLocation; }; #endif // GPSCONTROLLER_H diff --git a/zouba/location.cpp b/zouba/location.cpp index 80a665c..974d1e6 100644 --- a/zouba/location.cpp +++ b/zouba/location.cpp @@ -29,15 +29,18 @@ const double Location::KkjZoneInfo[6][2] = { QTM_USE_NAMESPACE -Location::Location( QString x, QString y ) : - q( new LocationPrivate( x, y ) ), +Location::Location( const QString &x, const QString &y, const QString &label ) : + q( new LocationPrivate( x, y, label ) ), manager( new QNetworkAccessManager(this) ) { - connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); + connect( + manager, SIGNAL( finished(QNetworkReply*) ), + this, SLOT( replyFinished(QNetworkReply*) ) + ); } -Location::Location( const QGeoPositionInfo &positionInfo ) : - q( new LocationPrivate() ), +Location::Location( const QGeoPositionInfo &positionInfo, const QString &label ) : + q( new LocationPrivate( label ) ), manager(0) { qreal latitude = positionInfo.coordinate().latitude(); @@ -55,9 +58,10 @@ Location::Location( const QGeoPositionInfo &positionInfo ) : Location::Location( const Location &from ) : QObject(0), - q( new LocationPrivate() ), + q( new LocationPrivate( from.label() ) ), manager(0) { + q->setAddress( from.address() ); q->setX( from.x() ); q->setY( from.y() ); q->setValid( from.isValid() ); @@ -67,8 +71,8 @@ Location::Location( const Location &from ) : } } -Location::Location() : - q( new LocationPrivate() ), +Location::Location( const QString &label ) : + q( new LocationPrivate( label ) ), manager( new QNetworkAccessManager(this) ) { connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); @@ -84,7 +88,8 @@ Location::~Location() Location &Location::operator=( const Location &from ) { - q = new LocationPrivate(); + q = new LocationPrivate( from.label() ); + q->setAddress( from.address() ); q->setX( from.x() ); q->setY( from.y() ); q->setValid( from.isValid() ); @@ -98,11 +103,13 @@ Location &Location::operator=( const Location &from ) return *this; } -void Location::resolveAddress( QString address ) +void Location::resolveAddress( const QString &address ) { qDebug() << "resolving address"; qDebug() << address; + q->setAddress( address ); + QUrl fullUrl( Ytv::Url ); fullUrl.addEncodedQueryItem( "key", address.toAscii().toPercentEncoding() ); @@ -133,6 +140,26 @@ QString Location::y() const return q->y(); } +void Location::setLabel( const QString &label ) const +{ + q->setLabel( label ); +} + +QString Location::label() const +{ + return q->label(); +} + +void Location::setAddress( const QString &address ) const +{ + q->setAddress( address ); +} + +QString Location::address() const +{ + return q->address(); +} + bool Location::isValid() const { return q->isValid(); diff --git a/zouba/location.h b/zouba/location.h index a7b76cc..30c1aea 100644 --- a/zouba/location.h +++ b/zouba/location.h @@ -17,11 +17,11 @@ class Location : public QObject Q_OBJECT public: - Location( QString x, QString y ); - Location( const QGeoPositionInfo &positionInfo ); + Location( const QString &x, const QString &y, const QString &label=QString() ); + Location( const QGeoPositionInfo &positionInfo, const QString &label=QString() ); Location( const Location &from ); Location &operator=( const Location &from ); - Location(); + Location( const QString &label=QString() ); ~Location(); @@ -29,10 +29,16 @@ public: QString y() const; + void setAddress( const QString &address ) const; + QString address() const; + + void setLabel( const QString &label ) const; + QString label() const; + bool isValid() const; public Q_SLOTS: - void resolveAddress( QString address ); + void resolveAddress( const QString &address ); Q_SIGNALS: void becomeValid(); @@ -44,10 +50,6 @@ private: LocationPrivate *q; QNetworkAccessManager *manager; - QString m_x; - QString m_y; - QString m_valid; - typedef uint KKJ; /** diff --git a/zouba/location_p.cpp b/zouba/location_p.cpp index 79fb912..b9861fb 100644 --- a/zouba/location_p.cpp +++ b/zouba/location_p.cpp @@ -5,14 +5,18 @@ #include #include -LocationPrivate::LocationPrivate( QString x, QString y ) : +LocationPrivate::LocationPrivate( const QString &x, const QString &y, const QString &label ) : + m_label(label), + m_address(), m_x(x), m_y(y), m_valid(true) { } -LocationPrivate::LocationPrivate() : +LocationPrivate::LocationPrivate( const QString &label ) : + m_label(label), + m_address(), m_x(0), m_y(0), m_valid(false) @@ -51,12 +55,32 @@ void LocationPrivate::parseReply( const QByteArray &reply ) } } +void LocationPrivate::setLabel( const QString &label) +{ + m_label = label; +} + +QString LocationPrivate::label() const +{ + return m_label; +} + +void LocationPrivate::setAddress( const QString &address) +{ + m_address = address; +} + +QString LocationPrivate::address() const +{ + return m_address; +} + void LocationPrivate::setX( uint x ) { m_x = QString( "%1" ).arg( x ); } -void LocationPrivate::setX( QString x ) +void LocationPrivate::setX( const QString &x ) { m_x = x; } @@ -71,7 +95,7 @@ void LocationPrivate::setY( uint y ) m_y = QString( "%1" ).arg( y ); } -void LocationPrivate::setY( QString y ) +void LocationPrivate::setY( const QString &y ) { m_y = y; } diff --git a/zouba/location_p.h b/zouba/location_p.h index f158720..c0778bc 100644 --- a/zouba/location_p.h +++ b/zouba/location_p.h @@ -10,22 +10,30 @@ class LocationPrivate : public QObject Q_OBJECT public: + QString m_label; + QString m_address; QString m_x; QString m_y; bool m_valid; - LocationPrivate( QString x, QString y ); + LocationPrivate( const QString &x, const QString &y, const QString &label ); - LocationPrivate(); + LocationPrivate( const QString &label ); virtual ~LocationPrivate(); void setX( uint x ); - void setX( QString x ); + void setX( const QString &x ); QString x() const; void setY( uint y ); - void setY( QString y ); + void setY( const QString &y ); QString y() const; + void setAddress( const QString &address ); + QString address() const; + + void setLabel( const QString &label ); + QString label() const; + void setValid( bool valid ); bool isValid() const; diff --git a/zouba/locations.cpp b/zouba/locations.cpp new file mode 100644 index 0000000..2d73fbe --- /dev/null +++ b/zouba/locations.cpp @@ -0,0 +1,37 @@ +#include "locations.h" + +#include + +QHash Locations::locationHash; + +Locations::Locations() +{ +} + +Locations::~Locations() +{ +} + +bool Locations::addLocation( Location *location ) +{ + bool succeeded=false; + + if ( !locationHash.contains( location->label() ) ) { + qDebug() << "Adding location" << location->label(); + locationHash[ location->label() ] = location; + succeeded = true; + } + + return succeeded; +} + +Location *Locations::location( const QString &label ) +{ + Location *retVal = 0; + + if ( locationHash.contains( label ) ) { + retVal = locationHash[ label ]; + } + + return retVal; +} diff --git a/zouba/locations.h b/zouba/locations.h new file mode 100644 index 0000000..fe32bf4 --- /dev/null +++ b/zouba/locations.h @@ -0,0 +1,22 @@ +#ifndef LOCATIONS_H +#define LOCATIONS_H + +#include "location.h" + +#include +#include + +class Locations +{ +public: + Locations(); + ~Locations(); + + bool addLocation( Location *location ); + + Location *location( const QString &label ); + +private: + static QHash locationHash; +}; +#endif // LOCATIONS_H diff --git a/zouba/main.cpp b/zouba/main.cpp index 46632e2..b9ca3e6 100644 --- a/zouba/main.cpp +++ b/zouba/main.cpp @@ -21,10 +21,6 @@ int main(int argc, char *argv[] ) Ui ui; ui.setupUi(mainWindow); - qDebug() << "1"; - qDebug() << "2"; - qDebug() << "3"; - UiController *uiController = new UiController( &ui ); Route *route = new Route(); GpsController *gpsController = new GpsController(); @@ -35,13 +31,13 @@ int main(int argc, char *argv[] ) ); QObject::connect( - gpsController, SIGNAL( locationChanged( Location ) ), - route, SLOT( setFromLocation( Location ) ) + gpsController, SIGNAL( locationChanged( Location* ) ), + route, SLOT( setFromLocation( Location* ) ) ); QObject::connect( - uiController, SIGNAL( destinationChanged( Location ) ), - route, SLOT( setToLocation( Location ) ) + uiController, SIGNAL( destinationChanged( Location* ) ), + route, SLOT( setToLocation( Location* ) ) ); QObject::connect( diff --git a/zouba/messagetable.h b/zouba/messagetable.h index 3c9cbee..cfd6933 100644 --- a/zouba/messagetable.h +++ b/zouba/messagetable.h @@ -9,7 +9,7 @@ class MessageTable : public QTableWidget Q_OBJECT enum { - NumberOfRows=20, + NumberOfRows=100, OneColumn=1 }; diff --git a/zouba/route.cpp b/zouba/route.cpp index 1c3beda..8604208 100644 --- a/zouba/route.cpp +++ b/zouba/route.cpp @@ -35,9 +35,9 @@ void Route::getRoute() QUrl fullUrl( Ytv::Url ); QStringList a; - a << q->fromLocation().x() << q->fromLocation().y(); + a << q->fromLocation()->x() << q->fromLocation()->y(); QStringList b; - b << q->toLocation().x() << q->toLocation().y(); + b << q->toLocation()->x() << q->toLocation()->y(); fullUrl.addQueryItem( "a", a.join(",") ); fullUrl.addQueryItem( "b", b.join(",") ); @@ -58,38 +58,47 @@ void Route::replyFinished( QNetworkReply * reply ) emit( routeReady( routeData ) ); } -void Route::setFromLocation( const Location &location ) +void Route::setFromLocation( Location *location ) { qDebug() << "setting new From location"; - if ( location.isValid() ) { + if ( location && location->isValid() ) { + qDebug() << "From is valid"; q->setFromLocation( location ); if ( q->toValid() ) { - getRoute(); + qDebug() << "To is also valid"; + getRoute(); + } else { + qDebug() << "To not valid - waiting"; } } else { - Location *locationPtr = qobject_cast(sender()); - if ( locationPtr ) { - q->setFromLocation( *locationPtr ); + qDebug() << "From is not valid"; + qDebug() << "getting From from signal sender"; + location = qobject_cast(sender()); + if ( location ) { + q->setFromLocation( location ); if ( q->toValid() ) { + qDebug() << "To is also valid"; getRoute(); + } else { + qDebug() << "To not valid - waiting"; } } else { - qDebug() << "locationPtr is zero - cast didn't work"; + qDebug() << "location is zero - cast didn't work"; } } } -const Location &Route::fromLocation() +Location *Route::fromLocation() const { return q->fromLocation(); } -void Route::setToLocation( const Location &location ) +void Route::setToLocation( Location *location ) { qDebug() << "setting new To location"; - if ( location.isValid() ) { + if ( location && location->isValid() ) { qDebug() << "To is valid"; q->setToLocation( location ); if ( q->fromValid() ) { @@ -101,9 +110,9 @@ void Route::setToLocation( const Location &location ) } else { qDebug() << "To is not valid"; qDebug() << "getting To from signal sender"; - Location *locationPtr = qobject_cast(sender()); - if ( locationPtr ) { - q->setToLocation( *locationPtr ); + Location *location = qobject_cast(sender()); + if ( location ) { + q->setToLocation( location ); if ( q->fromValid() ) { qDebug() << "From is also valid"; getRoute(); @@ -111,21 +120,12 @@ void Route::setToLocation( const Location &location ) qDebug() << "From not valid - waiting"; } } else { - qDebug() << "locationPtr is zero; cast failed"; + qDebug() << "location is zero; cast failed"; } } } -const Location &Route::toLocation() +Location *Route::toLocation() const { return q->toLocation(); } - -void Route::toggleDirection() -{ - Location oldFromLocation = fromLocation(); - setFromLocation( toLocation() ); - setToLocation( oldFromLocation ); - - getRoute(); -} diff --git a/zouba/route.h b/zouba/route.h index 1047cf5..5cdccd4 100644 --- a/zouba/route.h +++ b/zouba/route.h @@ -18,9 +18,6 @@ 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 */ @@ -30,13 +27,13 @@ public: \brief Get the from location \return The from location */ - const Location &fromLocation(); + Location *fromLocation() const; /*! \brief Get the to location \return The to location */ - const Location &toLocation(); + Location *toLocation() const; public Q_SLOTS: @@ -44,18 +41,13 @@ public Q_SLOTS: * \brief Sets the from location * \param fromLocation The from location */ - void setFromLocation( const Location &location=Location() ); + void setFromLocation( Location *location=0 ); /*! * \brief Sets the to location * \param toLocation The to location */ - void setToLocation( const Location &location=Location() ); - - /*! - * \brief Toggles the route direction. - */ - void toggleDirection(); + void setToLocation( Location *location=0 ); Q_SIGNALS: void routeReady( QList ); diff --git a/zouba/route_p.cpp b/zouba/route_p.cpp index f041392..a9df50e 100644 --- a/zouba/route_p.cpp +++ b/zouba/route_p.cpp @@ -8,8 +8,8 @@ RoutePrivate::RoutePrivate( QObject *parent ) : m_fromValid(false), m_toValid(false), - m_fromLocation(0,0), - m_toLocation(0,0) + m_fromLocation(0), + m_toLocation(0) { Q_UNUSED( parent ) } @@ -82,18 +82,18 @@ QList RoutePrivate::parseReply( const QByteArray &reply ) return retVal; } -void RoutePrivate::setFromLocation( const Location &location ) +void RoutePrivate::setFromLocation( Location *location ) { m_fromLocation = location; m_fromValid = true; } -const Location &RoutePrivate::fromLocation() +Location *RoutePrivate::fromLocation() const { return m_fromLocation; } -void RoutePrivate::setToLocation( const Location &toLocation ) +void RoutePrivate::setToLocation( Location *toLocation ) { m_toLocation = toLocation; m_toValid = true; @@ -120,7 +120,7 @@ QString RoutePrivate::parseJORECode( const QString &joreCode ) const return retVal; } -const Location &RoutePrivate::toLocation() +Location *RoutePrivate::toLocation() const { return m_toLocation; } diff --git a/zouba/route_p.h b/zouba/route_p.h index ed328da..fddce34 100644 --- a/zouba/route_p.h +++ b/zouba/route_p.h @@ -15,18 +15,18 @@ public: RoutePrivate( QObject *parent=0 ); ~RoutePrivate(); - Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation); - Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation); - QList parseReply( const QByteArray &reply ); - void setFromLocation( const Location &fromLocation ); + Q_PROPERTY(Location* fromLocation READ fromLocation WRITE setFromLocation); + Q_PROPERTY(Location* toLocation READ toLocation WRITE setToLocation); + + void setFromLocation( Location *fromLocation ); - const Location &fromLocation(); + Location *fromLocation() const; - void setToLocation( const Location &toLocation ); + void setToLocation( Location *toLocation ); - const Location &toLocation(); + Location *toLocation() const; bool toValid(); bool fromValid(); @@ -34,8 +34,8 @@ public: private: bool m_fromValid; bool m_toValid; - Location m_fromLocation; - Location m_toLocation; + Location *m_fromLocation; + Location *m_toLocation; QString parseJORECode( const QString &joreCode ) const; }; diff --git a/zouba/ui.cpp b/zouba/ui.cpp index 73a21ea..115da01 100644 --- a/zouba/ui.cpp +++ b/zouba/ui.cpp @@ -1,6 +1,7 @@ #include "ui.h" #include "messagetable.h" +#include "locations.h" #include #include @@ -14,6 +15,8 @@ #include #include #include +#include +#include MessageTable *Ui::messageTable = 0; @@ -93,8 +96,35 @@ void Ui::setupUi( QMainWindow *mainWindow ) void Ui::setHomeAddress() { + setAddress( "home" ); } void Ui::setWorkAddress() { + setAddress( "work" ); +} + +void Ui::setAddress( const QString &label ) +{ + bool ok; + QString address = QInputDialog::getText( + centralWidget, + tr("Enter address for \""+QString(label).toLatin1()+"\""), + tr("Address"), + QLineEdit::Normal, + "", + &ok + ); + + qDebug() << "ok=" << ok; + + if ( ok ) { + qDebug() << "new address" << address; + Locations locations; + Location *location = locations.location( label ); + qDebug() << "location" << location; + if ( location ) { + location->resolveAddress( address ); + } + } } diff --git a/zouba/ui.h b/zouba/ui.h index 4340f62..f6fda1b 100644 --- a/zouba/ui.h +++ b/zouba/ui.h @@ -49,5 +49,8 @@ Q_SIGNALS: private Q_SLOTS: void setHomeAddress(); void setWorkAddress(); + +private: + void setAddress( const QString &label ); }; #endif //UI_H diff --git a/zouba/uicontroller.cpp b/zouba/uicontroller.cpp index 3d0e0ca..891886a 100644 --- a/zouba/uicontroller.cpp +++ b/zouba/uicontroller.cpp @@ -4,6 +4,7 @@ #include "ytv.h" #include "location.h" #include "messagetable.h" +#include "locations.h" #include #include @@ -14,8 +15,12 @@ UiController::UiController( Ui *ui ) : ui(ui) { - Location *homeLocation = new Location(); - Location *workLocation = new Location(); + Location *homeLocation = new Location( "home" ); + Location *workLocation = new Location( "work" ); + + Locations locations; + locations.addLocation( homeLocation ); + locations.addLocation( workLocation ); connect( homeLocation, SIGNAL( becomeValid() ), @@ -36,7 +41,6 @@ UiController::UiController( Ui *ui ) : ui->destinationButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( changeDestination( int ) ) ); - } UiController::~UiController() @@ -64,7 +68,7 @@ void UiController::changeDestination( int id ) bool destinationHasChanged = ( currentDestination != id ); if ( destinationHasChanged ) { - emit destinationChanged( *(destination[id]) ); + emit destinationChanged( destination[id] ); } // always want to emit this so that the gps position is update diff --git a/zouba/uicontroller.h b/zouba/uicontroller.h index 89ce513..9657b8e 100644 --- a/zouba/uicontroller.h +++ b/zouba/uicontroller.h @@ -21,7 +21,7 @@ public Q_SLOTS: Q_SIGNALS: void buttonClicked(); - void destinationChanged( Location newDestination ); + void destinationChanged( Location *newDestination ); private Q_SLOTS: void changeDestination( int id ); diff --git a/zouba/zouba.pro b/zouba/zouba.pro index a594ab5..d56ef81 100644 --- a/zouba/zouba.pro +++ b/zouba/zouba.pro @@ -23,6 +23,7 @@ SOURCES += \ route_p.cpp \ uicontroller.cpp \ location.cpp \ + locations.cpp \ location_p.cpp \ gpscontroller.cpp \ ui.cpp \ -- 1.7.9.5