GpsController::GpsController() :
m_location( QGeoPositionInfoSource::createDefaultSource(this) ),
- updatesEnabled(false)
+ currentLocation(0)
{
m_location->setUpdateInterval( 1*60*1000 );
{
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 );
+ }
}
void startGps();
Q_SIGNALS:
- void locationChanged( const Location &newLocation );
+ void locationChanged( Location *newLocation );
private:
QGeoPositionInfoSource *m_location;
- bool updatesEnabled;
+ Location *currentLocation;
};
#endif // GPSCONTROLLER_H
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();
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() );
}
}
-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*) ) );
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() );
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() );
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();
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();
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();
LocationPrivate *q;
QNetworkAccessManager *manager;
- QString m_x;
- QString m_y;
- QString m_valid;
-
typedef uint KKJ;
/**
#include <QByteArray>
#include <QDebug>
-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)
}
}
+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;
}
m_y = QString( "%1" ).arg( y );
}
-void LocationPrivate::setY( QString y )
+void LocationPrivate::setY( const QString &y )
{
m_y = y;
}
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;
--- /dev/null
+#include "locations.h"
+
+#include <QDebug>
+
+QHash<QString,Location *> 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;
+}
--- /dev/null
+#ifndef LOCATIONS_H
+#define LOCATIONS_H
+
+#include "location.h"
+
+#include <QHash>
+#include <QString>
+
+class Locations
+{
+public:
+ Locations();
+ ~Locations();
+
+ bool addLocation( Location *location );
+
+ Location *location( const QString &label );
+
+private:
+ static QHash<QString,Location *> locationHash;
+};
+#endif // LOCATIONS_H
Ui ui;
ui.setupUi(mainWindow);
- qDebug() << "1";
- qDebug() << "2";
- qDebug() << "3";
-
UiController *uiController = new UiController( &ui );
Route *route = new Route();
GpsController *gpsController = new GpsController();
);
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(
Q_OBJECT
enum {
- NumberOfRows=20,
+ NumberOfRows=100,
OneColumn=1
};
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(",") );
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<Location*>(sender());
- if ( locationPtr ) {
- q->setFromLocation( *locationPtr );
+ qDebug() << "From is not valid";
+ qDebug() << "getting From from signal sender";
+ location = qobject_cast<Location*>(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() ) {
} else {
qDebug() << "To is not valid";
qDebug() << "getting To from signal sender";
- Location *locationPtr = qobject_cast<Location*>(sender());
- if ( locationPtr ) {
- q->setToLocation( *locationPtr );
+ Location *location = qobject_cast<Location*>(sender());
+ if ( location ) {
+ q->setToLocation( location );
if ( q->fromValid() ) {
qDebug() << "From is also valid";
getRoute();
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();
-}
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
*/
\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:
* \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<RouteData> );
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 )
}
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;
return retVal;
}
-const Location &RoutePrivate::toLocation()
+Location *RoutePrivate::toLocation() const
{
return m_toLocation;
}
RoutePrivate( QObject *parent=0 );
~RoutePrivate();
- Q_PROPERTY(Location fromLocation READ fromLocation WRITE setFromLocation);
- Q_PROPERTY(Location toLocation READ toLocation WRITE setFromLocation);
-
QList<RouteData> 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();
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;
};
#include "ui.h"
#include "messagetable.h"
+#include "locations.h"
#include <QMainWindow>
#include <QRadioButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSizePolicy>
+#include <QInputDialog>
+#include <QDebug>
MessageTable *Ui::messageTable = 0;
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 );
+ }
+ }
}
private Q_SLOTS:
void setHomeAddress();
void setWorkAddress();
+
+private:
+ void setAddress( const QString &label );
};
#endif //UI_H
#include "ytv.h"
#include "location.h"
#include "messagetable.h"
+#include "locations.h"
#include <QObject>
#include <QTableWidgetItem>
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() ),
ui->destinationButtons, SIGNAL( buttonClicked( int ) ),
this, SLOT( changeDestination( int ) )
);
-
}
UiController::~UiController()
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
Q_SIGNALS:
void buttonClicked();
- void destinationChanged( Location newDestination );
+ void destinationChanged( Location *newDestination );
private Q_SLOTS:
void changeDestination( int id );
route_p.cpp \
uicontroller.cpp \
location.cpp \
+ locations.cpp \
location_p.cpp \
gpscontroller.cpp \
ui.cpp \