committing work in progress refactoring
authorMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Wed, 11 Aug 2010 19:52:06 +0000 (22:52 +0300)
committerMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Wed, 11 Aug 2010 19:52:06 +0000 (22:52 +0300)
17 files changed:
zouba/src/journey.cpp [new file with mode: 0644]
zouba/src/journey.h [new file with mode: 0644]
zouba/src/journey_p.cpp [new file with mode: 0644]
zouba/src/journey_p.h [new file with mode: 0644]
zouba/src/journeys.cpp [new file with mode: 0644]
zouba/src/journeys.h [new file with mode: 0644]
zouba/src/location_p.cpp
zouba/src/locationpicker.cpp [new file with mode: 0644]
zouba/src/locationpicker.h [new file with mode: 0644]
zouba/src/locations.cpp
zouba/src/locations.h
zouba/src/main.cpp
zouba/src/ui.cpp
zouba/src/ui.h
zouba/src/uicontroller.cpp
zouba/src/uicontroller.h
zouba/zouba.pro

diff --git a/zouba/src/journey.cpp b/zouba/src/journey.cpp
new file mode 100644 (file)
index 0000000..da127a3
--- /dev/null
@@ -0,0 +1,52 @@
+#include "journey.h"
+
+#include "journey_p.h"
+
+#include <QString>
+#include <QObject>
+#include <QDebug>
+
+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 (file)
index 0000000..904d7ac
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef JOURNEY_H
+#define JOURNEY_H
+
+#include "journey_p.h"
+
+#include <QString>
+#include <QObject>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QGeoPositionInfo>
+#include <math.h>
+
+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 (file)
index 0000000..391cd71
--- /dev/null
@@ -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 (file)
index 0000000..865a306
--- /dev/null
@@ -0,0 +1,38 @@
+#ifndef JOURNEY_P_H
+#define JOURNEY_P_H
+
+#include <QObject>
+#include <QString>
+#include <QByteArray>
+
+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 (file)
index 0000000..956405b
--- /dev/null
@@ -0,0 +1,98 @@
+#include "journeys.h"
+
+#include <QDebug>
+#include <QHash>
+#include <QSettings>
+#include <QString>
+#include <QStringList>
+#include <QCoreApplication>
+
+QHash<QString,Journey *> 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; i<labels.size(); ++i ) {
+    QString label = labels[i];
+    settings.beginGroup( label );
+    QString from = settings.value( "from" ).toString();
+    QString to   = settings.value( "to" ).toString();
+    settings.endGroup();
+
+    qDebug() << "restoring" << label;
+    Journey *journey = new Journey();
+    journey->setJourney( 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<Journey*>(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 (file)
index 0000000..491bd3e
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef JOURNEYS_H
+#define JOURNEYS_H
+
+#include "journey.h"
+
+#include <QHash>
+#include <QString>
+#include <QObject>
+
+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<QString,Journey *> journeyHash;
+  static bool initialised;
+};
+
+#endif //JOURNEYS_H
index fe9d555..5118e6c 100644 (file)
@@ -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 (file)
index 0000000..a9cd870
--- /dev/null
@@ -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 (file)
index 0000000..7454bcd
--- /dev/null
@@ -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
index 358a2cc..c8e613e 100644 (file)
@@ -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;
     }
index fa74faa..b15abc5 100644 (file)
@@ -15,7 +15,6 @@ public:
   Locations();
   ~Locations();
 
-  static Locations *instance();
   bool addLocation( Location *location );
 
   Location *location( const QString &label );
index bd3898b..749b6b0 100644 (file)
@@ -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();
 
index ee18f3a..a520a0b 100644 (file)
@@ -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 );
index 94dc687..4fd2d4d 100644 (file)
@@ -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;
index 8365378..be9436d 100644 (file)
@@ -4,6 +4,8 @@
 #include "ytv.h"
 #include "location.h"
 #include "locations.h"
+#include "journey.h"
+#include "journeys.h"
 
 #include <QObject>
 #include <QPushButton>
 
 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
index 7cbbbf5..2812645 100644 (file)
@@ -7,6 +7,7 @@
 #include <QObject>
 
 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<RouteData> m_routeData;
-  QList<Location*> m_destination;
+  QList<Journey*>  m_journeys;
   Ui *m_ui;
-  int m_currentDestination;
+  int m_currentJourney;
   int m_currentRoute;
 };
 #endif // UICONTROLLER_H
index d978a4c..fb7ec0b 100644 (file)
@@ -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 \