From 815fed63fe9b64263e67ab3ba0012bb50b71afce Mon Sep 17 00:00:00 2001 From: Max Waterman Date: Wed, 11 Aug 2010 22:52:06 +0300 Subject: [PATCH] committing work in progress --- zouba/src/journey.cpp | 52 ++++++++++++++++++++++ zouba/src/journey.h | 44 +++++++++++++++++++ zouba/src/journey_p.cpp | 73 +++++++++++++++++++++++++++++++ zouba/src/journey_p.h | 38 ++++++++++++++++ zouba/src/journeys.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++ zouba/src/journeys.h | 30 +++++++++++++ zouba/src/location_p.cpp | 4 +- zouba/src/locationpicker.cpp | 10 +++++ zouba/src/locationpicker.h | 12 ++++++ zouba/src/locations.cpp | 4 -- zouba/src/locations.h | 1 - zouba/src/main.cpp | 13 +++--- zouba/src/ui.cpp | 30 ++++++------- zouba/src/ui.h | 2 +- zouba/src/uicontroller.cpp | 76 +++++++++++++++++--------------- zouba/src/uicontroller.h | 7 +-- zouba/zouba.pro | 6 +++ 17 files changed, 434 insertions(+), 66 deletions(-) create mode 100644 zouba/src/journey.cpp create mode 100644 zouba/src/journey.h create mode 100644 zouba/src/journey_p.cpp create mode 100644 zouba/src/journey_p.h create mode 100644 zouba/src/journeys.cpp create mode 100644 zouba/src/journeys.h create mode 100644 zouba/src/locationpicker.cpp create mode 100644 zouba/src/locationpicker.h diff --git a/zouba/src/journey.cpp b/zouba/src/journey.cpp new file mode 100644 index 0000000..da127a3 --- /dev/null +++ b/zouba/src/journey.cpp @@ -0,0 +1,52 @@ +#include "journey.h" + +#include "journey_p.h" + +#include +#include +#include + +QTM_USE_NAMESPACE + +Journey::Journey() : + q( new JourneyPrivate() ) +{ +} + +Journey::~Journey() +{ + delete q; + q=0; +} + +void Journey::setJourney( const QString &from, const QString &to, const QString &label ) +{ + q->setFrom( from ); + q->setTo( to ); + q->setLabel( label ); +} + +QString Journey::from() const +{ + return q->from(); +} + +QString Journey::to() const +{ + return q->to(); +} + +void Journey::setLabel( const QString &label ) const +{ + q->setLabel( label ); +} + +QString Journey::label() const +{ + return q->label(); +} + +bool Journey::isValid() const +{ + return q->isValid(); +} diff --git a/zouba/src/journey.h b/zouba/src/journey.h new file mode 100644 index 0000000..904d7ac --- /dev/null +++ b/zouba/src/journey.h @@ -0,0 +1,44 @@ +#ifndef JOURNEY_H +#define JOURNEY_H + +#include "journey_p.h" + +#include +#include +#include +#include +#include +#include + +QTM_USE_NAMESPACE + +class Journey : public QObject +{ +Q_OBJECT + +public: + Journey(); + + ~Journey(); + + + void setJourney( const QString &from, const QString &to, const QString &label=QString() ); + + QString from() const; + QString to() const; + + void setLabel( const QString &label ) const; + QString label() const; + + bool isValid() const; + +Q_SIGNALS: + void becomeValid(); + void becomeInValid(); + void busy( bool busy ); + +private: + JourneyPrivate *q; +}; + +#endif // JOURNEY_H diff --git a/zouba/src/journey_p.cpp b/zouba/src/journey_p.cpp new file mode 100644 index 0000000..391cd71 --- /dev/null +++ b/zouba/src/journey_p.cpp @@ -0,0 +1,73 @@ +#include "locations.h" +#include "location.h" + +#include "journey_p.h" + +JourneyPrivate::JourneyPrivate(): + m_from(), + m_to(), + m_label() +{ +} + +JourneyPrivate::~JourneyPrivate() +{ +} + +void JourneyPrivate::setFrom( const QString &from ) +{ + m_from = from; + + setValid( fromAndToAreValid() ); +} + +QString JourneyPrivate::from() const +{ + return m_from; +} + +void JourneyPrivate::setTo( const QString &to ) +{ + m_to = to; + + setValid( fromAndToAreValid() ); +} + +QString JourneyPrivate::to() const +{ + return m_to; +} + +void JourneyPrivate::setLabel( const QString &label ) +{ + m_label = label; +} + +QString JourneyPrivate::label() const +{ + return m_label; +} + +void JourneyPrivate::setValid( bool valid ) +{ + m_valid = valid; +} + +bool JourneyPrivate::isValid() const +{ + return m_valid; +} + +bool JourneyPrivate::fromAndToAreValid() const +{ + bool retVal = false; + + Locations locations; + Location *fromLocation = locations.location( m_from ); + Location *toLocation = locations.location( m_to ); + + retVal = fromLocation==0 && fromLocation->isValid() && + toLocation==0 && toLocation->isValid(); + + return retVal; +} diff --git a/zouba/src/journey_p.h b/zouba/src/journey_p.h new file mode 100644 index 0000000..865a306 --- /dev/null +++ b/zouba/src/journey_p.h @@ -0,0 +1,38 @@ +#ifndef JOURNEY_P_H +#define JOURNEY_P_H + +#include +#include +#include + +class JourneyPrivate : public QObject +{ + Q_OBJECT + +public: + JourneyPrivate(); + virtual ~JourneyPrivate(); + + void setFrom( const QString &from ); + QString from() const; + + void setTo( const QString &to ); + QString to() const; + + void setLabel( const QString &label ); + QString label() const; + + void setValid( bool valid ); + bool isValid() const; + +private: + bool fromAndToAreValid() const; + + QString m_from; + QString m_to; + QString m_label; + bool m_valid; +}; + +#endif // JOURNEY_P_H + diff --git a/zouba/src/journeys.cpp b/zouba/src/journeys.cpp new file mode 100644 index 0000000..956405b --- /dev/null +++ b/zouba/src/journeys.cpp @@ -0,0 +1,98 @@ +#include "journeys.h" + +#include +#include +#include +#include +#include +#include + +QHash Journeys::journeyHash; +bool Journeys::initialised = false; + +Journeys::Journeys() +{ + if ( !initialised ) { + restoreJourneys(); + initialised = true; + } +} + +Journeys::~Journeys() +{ +} + +bool Journeys::addJourney( Journey *journey ) +{ + bool succeeded=false; + + saveJourney( journey ); + + if ( !journeyHash.contains( journey->label() ) ) { + qDebug() << "Adding journey" << journey->label(); + journeyHash[ journey->label() ] = journey; + succeeded = true; + } else { + qDebug() << "FAILED to add journey" << journey->label(); + } + + return succeeded; +} + +void Journeys::restoreJourneys() +{ + QSettings settings; + + settings.beginGroup( "Journeys" ); + QStringList labels = settings.childGroups(); + + for( int i=0; isetJourney( from, to, label ); + + journeyHash[ label ] = journey; + } + + settings.endGroup(); +} + +void Journeys::saveJourney( Journey *journey ) +{ + qDebug() << "Saving journey" << journey->label(); + QSettings settings; + settings.beginGroup( "Journeys" ); + settings.beginGroup( journey->label() ); + settings.setValue( "from", journey->from() ); + settings.setValue( "to", journey->to() ); + settings.endGroup(); + settings.endGroup(); +} + +void Journeys::saveJourney() +{ + Journey *journey = qobject_cast(sender()); + + saveJourney( journey ); +} + +Journey *Journeys::journey( const QString &label ) +{ + qDebug() << "requesting journey" << label; + Journey *retVal = 0; + + if ( journeyHash.contains( label ) ) { + qDebug() << "found journey" << label; + retVal = journeyHash[ label ]; + } else { + qDebug() << "didn't find journey" << label; + } + + return retVal; +} diff --git a/zouba/src/journeys.h b/zouba/src/journeys.h new file mode 100644 index 0000000..491bd3e --- /dev/null +++ b/zouba/src/journeys.h @@ -0,0 +1,30 @@ +#ifndef JOURNEYS_H +#define JOURNEYS_H + +#include "journey.h" + +#include +#include +#include + +class Journeys: public QObject +{ + Q_OBJECT + +public: + Journeys(); + ~Journeys(); + + bool addJourney( Journey *journey ); + + Journey *journey( const QString &label ); + +private: + void saveJourney( Journey *journey ); + void saveJourney(); + void restoreJourneys(); + static QHash journeyHash; + static bool initialised; +}; + +#endif //JOURNEYS_H diff --git a/zouba/src/location_p.cpp b/zouba/src/location_p.cpp index fe9d555..5118e6c 100644 --- a/zouba/src/location_p.cpp +++ b/zouba/src/location_p.cpp @@ -18,8 +18,8 @@ LocationPrivate::LocationPrivate( const QString &x, const QString &y, const QStr LocationPrivate::LocationPrivate( const QString &label ) : m_label(label), m_address(), - m_x(0), - m_y(0), + m_x(), + m_y(), m_valid(false) { } diff --git a/zouba/src/locationpicker.cpp b/zouba/src/locationpicker.cpp new file mode 100644 index 0000000..a9cd870 --- /dev/null +++ b/zouba/src/locationpicker.cpp @@ -0,0 +1,10 @@ +#include "locationpicker.h" + +LocationPicker::LocationPicker( QObject *parent ) + QDialog( parent ) +{ +} + +LocationPicker::~LocationPicker() +{ +} diff --git a/zouba/src/locationpicker.h b/zouba/src/locationpicker.h new file mode 100644 index 0000000..7454bcd --- /dev/null +++ b/zouba/src/locationpicker.h @@ -0,0 +1,12 @@ +#ifndef LOCATIONPICKER_H +#define LOCATIONPICKER_H + +class LocationPicker: public QDialog +{ + Q_OBJECT + +public: + LocationPicker( QObject *parent=0 ); + ~LocationPicker(); +}; +#endif // LOCATIONPICKER_H diff --git a/zouba/src/locations.cpp b/zouba/src/locations.cpp index 358a2cc..c8e613e 100644 --- a/zouba/src/locations.cpp +++ b/zouba/src/locations.cpp @@ -13,10 +13,6 @@ bool Locations::initialised = false; Locations::Locations() { if ( !initialised ) { - QCoreApplication::setOrganizationName("ZouBa"); - QCoreApplication::setOrganizationDomain("zouba.yi.org"); - QCoreApplication::setOrganizationName("ZouBa"); - restoreLocations(); initialised = true; } diff --git a/zouba/src/locations.h b/zouba/src/locations.h index fa74faa..b15abc5 100644 --- a/zouba/src/locations.h +++ b/zouba/src/locations.h @@ -15,7 +15,6 @@ public: Locations(); ~Locations(); - static Locations *instance(); bool addLocation( Location *location ); Location *location( const QString &label ); diff --git a/zouba/src/main.cpp b/zouba/src/main.cpp index bd3898b..749b6b0 100644 --- a/zouba/src/main.cpp +++ b/zouba/src/main.cpp @@ -14,6 +14,9 @@ int main(int argc, char *argv[] ) { QApplication app(argc, argv); + QCoreApplication::setOrganizationName("ZouBa"); + QCoreApplication::setOrganizationDomain("zouba.yi.org"); + QCoreApplication::setOrganizationName("ZouBa"); QMainWindow *mainWindow = new QMainWindow; Ui *ui = new Ui;; @@ -36,27 +39,27 @@ int main(int argc, char *argv[] ) QObject::connect( uiController, SIGNAL( destinationChanged( Location* ) ), route, SLOT( setToLocation( Location* ) ) - ); + ); QObject::connect( uiController, SIGNAL( buttonClicked() ), gpsController, SLOT( getGps() ) - ); + ); QObject::connect( ui, SIGNAL( fakeGpsPressed( const QString & ) ), gpsController, SLOT( useFakeGps( const QString & ) ) - ); + ); QObject::connect( ui, SIGNAL( liveGpsPressed() ), gpsController, SLOT( useLiveGps() ) - ); + ); QObject::connect( route, SIGNAL( busy( bool ) ), ui, SLOT( setBusy( bool ) ) - ); + ); mainWindow->show(); diff --git a/zouba/src/ui.cpp b/zouba/src/ui.cpp index ee18f3a..a520a0b 100644 --- a/zouba/src/ui.cpp +++ b/zouba/src/ui.cpp @@ -21,7 +21,7 @@ Ui::Ui() : m_centralWidget(0), - m_destinationButtons(0), + m_journeyButtons(0), m_routeStack(0), m_usingFakeGps( false ), m_fakeLocationLabel( "work" ) @@ -62,20 +62,20 @@ void Ui::setupUi( QMainWindow *mainWindow ) m_centralWidget = new QWidget( m_mainWindow ); m_mainWindow->setCentralWidget( m_centralWidget); - QRadioButton *homeButton = new QRadioButton(); - homeButton->setObjectName( QString::fromUtf8("homeButton") ); - homeButton->setText( "GPS->HOME" ); - homeButton->setEnabled(false); + QRadioButton *gpsToHomeButton = new QRadioButton(); + gpsToHomeButton->setObjectName( QString::fromUtf8("gpsToHomeButton") ); + gpsToHomeButton->setText( "GPS->HOME" ); + gpsToHomeButton->setEnabled(false); - QRadioButton *workButton = new QRadioButton(); - workButton->setObjectName( QString::fromUtf8("workButton") ); - workButton->setText( "GPS->WORK" ); - workButton->setEnabled(false); + QRadioButton *gpstoWorkButton = new QRadioButton(); + gpstoWorkButton->setObjectName( QString::fromUtf8("gpstoWorkButton") ); + gpstoWorkButton->setText( "GPS->WORK" ); + gpstoWorkButton->setEnabled(false); - m_destinationButtons = new QButtonGroup(); - m_destinationButtons->addButton( homeButton, HomeButtonId ); - m_destinationButtons->addButton( workButton, WorkButtonId ); - m_destinationButtons->setExclusive( true ); + m_journeyButtons = new QButtonGroup(); + m_journeyButtons->addButton( gpsToHomeButton, HomeButtonId ); + m_journeyButtons->addButton( gpstoWorkButton, WorkButtonId ); + m_journeyButtons->setExclusive( true ); m_routeButtons = new QButtonGroup(); m_routeButtons->setExclusive( true ); @@ -102,8 +102,8 @@ void Ui::setupUi( QMainWindow *mainWindow ) topLayout->addWidget( m_routeDetailTable ); m_buttonLayout = new QGridLayout(); - m_buttonLayout->addWidget( homeButton, 0, 0 ); - m_buttonLayout->addWidget( workButton, 0, 1 ); + m_buttonLayout->addWidget( gpsToHomeButton, 0, 0 ); + m_buttonLayout->addWidget( gpstoWorkButton, 0, 1 ); m_mainLayout = new QVBoxLayout(); m_mainLayout->addLayout( topLayout ); diff --git a/zouba/src/ui.h b/zouba/src/ui.h index 94dc687..4fd2d4d 100644 --- a/zouba/src/ui.h +++ b/zouba/src/ui.h @@ -35,7 +35,7 @@ public: QMainWindow *m_mainWindow; QWidget *m_centralWidget; - QButtonGroup *m_destinationButtons; + QButtonGroup *m_journeyButtons; QButtonGroup *m_routeButtons; QVBoxLayout *m_routeStack; QTableWidget *m_routeDetailTable; diff --git a/zouba/src/uicontroller.cpp b/zouba/src/uicontroller.cpp index 8365378..be9436d 100644 --- a/zouba/src/uicontroller.cpp +++ b/zouba/src/uicontroller.cpp @@ -4,6 +4,8 @@ #include "ytv.h" #include "location.h" #include "locations.h" +#include "journey.h" +#include "journeys.h" #include #include @@ -15,73 +17,77 @@ UiController::UiController( Ui *ui ) : m_routeData(), - m_destination(), + m_journeys(), m_ui(ui), - m_currentDestination(-1), + m_currentJourney(-1), m_currentRoute(-1) { - Locations locations; - Location *homeLocation = locations.location( "home" ); - Location *workLocation = locations.location( "work" ); - - if ( homeLocation==0 ) { - homeLocation = new Location( "home" ); - locations.addLocation( homeLocation ); - } else if ( homeLocation->isValid() ) { + Journeys journeys; + Journey *gpsToHome = journeys.journey( "gps->home" ); + Journey *gpsToWork = journeys.journey( "gps->work" ); + + if ( gpsToHome==0 ) { + gpsToHome = new Journey(); + gpsToHome->setJourney( "gps", "home", "gps->home" ); + + journeys.addJourney( gpsToHome ); + } else if ( gpsToHome->isValid() ) { setHomeButtonValid(); } - if ( workLocation==0 ) { - workLocation = new Location( "work" ); - locations.addLocation( workLocation ); - } else if ( workLocation->isValid() ) { + if ( gpsToWork==0 ) { + gpsToWork = new Journey(); + gpsToWork->setJourney( "gps", "work", "gps->work" ); + + journeys.addJourney( gpsToWork ); + } else if ( gpsToWork->isValid() ) { setWorkButtonValid(); } connect( - homeLocation, SIGNAL( becomeValid() ), + gpsToHome, SIGNAL( becomeValid() ), this, SLOT( setHomeButtonValid() ) ); connect( - homeLocation, SIGNAL( becomeInValid() ), + gpsToHome, SIGNAL( becomeInValid() ), this, SLOT( setHomeButtonInValid() ) ); connect( - homeLocation, SIGNAL( becomeValid() ), - &locations, SLOT( saveLocation() ) + gpsToHome, SIGNAL( becomeValid() ), + &journeys, SLOT( saveJourney() ) ); connect( - homeLocation, SIGNAL( busy( bool ) ), + gpsToHome, SIGNAL( busy( bool ) ), ui, SLOT( setBusy( bool ) ) ); connect( - workLocation, SIGNAL( becomeValid() ), + gpsToWork, SIGNAL( becomeValid() ), this, SLOT( setWorkButtonValid() ) ); connect( - workLocation, SIGNAL( becomeInValid() ), + gpsToHome, SIGNAL( becomeInValid() ), this, SLOT( setWorkButtonInValid() ) ); connect( - workLocation, SIGNAL( becomeValid() ), - &locations, SLOT( saveLocation() ) + gpsToHome, SIGNAL( becomeValid() ), + &journeys, SLOT( saveJourney() ) ); connect( - workLocation, SIGNAL( busy( bool ) ), + gpsToHome, SIGNAL( busy( bool ) ), ui, SLOT( setBusy( bool ) ) ); - m_destination.append( homeLocation ); - m_destination.append( workLocation ); + m_journeys.append( gpsToHome ); + m_journeys.append( gpsToWork ); connect( - m_ui->m_destinationButtons, SIGNAL( buttonClicked( int ) ), + m_ui->m_journeyButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( changeDestination( int ) ) ); connect( - m_ui->m_routeButtons, SIGNAL( buttonClicked( int ) ), + m_ui->m_journeyButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( changeRoute( int ) ) ); } @@ -116,17 +122,17 @@ void UiController::setWorkButtonValid() void UiController::setButtonValid( int id, bool isValid ) { - m_ui->m_destinationButtons->button( id )->setEnabled( isValid ); + m_ui->m_journeyButtons->button( id )->setEnabled( isValid ); } void UiController::changeDestination( int id ) { - bool destinationHasChanged = ( m_currentDestination != id ); - qDebug() << "Destination has changed=" << destinationHasChanged; - if ( destinationHasChanged ) { - qDebug() << "Emitting destination changed (" << m_destination[id]->label() << ")"; - emit destinationChanged( m_destination[id] ); - m_currentDestination = id; + bool journeyHasChanged = ( m_currentJourney != id ); + qDebug() << "Journey has changed=" << journeyHasChanged; + if ( journeyHasChanged ) { + qDebug() << "Emitting journey changed (" << m_journeys[id]->label() << ")"; + emit journeyChanged( m_journeys[id] ); + m_currentJourney = id; } // always want to emit this so that the gps position is updated diff --git a/zouba/src/uicontroller.h b/zouba/src/uicontroller.h index 7cbbbf5..2812645 100644 --- a/zouba/src/uicontroller.h +++ b/zouba/src/uicontroller.h @@ -7,6 +7,7 @@ #include class Ui; +class Journey; class UiController : public QObject { @@ -21,7 +22,7 @@ public Q_SLOTS: Q_SIGNALS: void buttonClicked(); - void destinationChanged( Location *newDestination ); + void journeyChanged( Journey *newJourney ); private Q_SLOTS: void changeDestination( int id ); @@ -37,9 +38,9 @@ private: private: QList m_routeData; - QList m_destination; + QList m_journeys; Ui *m_ui; - int m_currentDestination; + int m_currentJourney; int m_currentRoute; }; #endif // UICONTROLLER_H diff --git a/zouba/zouba.pro b/zouba/zouba.pro index d978a4c..fb7ec0b 100644 --- a/zouba/zouba.pro +++ b/zouba/zouba.pro @@ -7,6 +7,9 @@ SOURCES += \ location.cpp \ location_p.cpp \ locations.cpp \ + journey.cpp \ + journey_p.cpp \ + journeys.cpp \ gpscontroller.cpp \ gpscontroller_p.cpp \ ui.cpp \ @@ -18,6 +21,9 @@ HEADERS += \ location.h \ location_p.h \ locations.h \ + journey.h \ + journey_p.h \ + journeys.h \ ytv.h \ gpscontroller.h \ gpscontroller_p.h \ -- 1.7.9.5