Works now without any existing location settings, enabling a new user to start using it.
[ptas] / zouba / uicontroller.cpp
1 #include "uicontroller.h"
2 #include "route.h"
3 #include "ui.h"
4 #include "ytv.h"
5 #include "location.h"
6 #include "messagetable.h"
7 #include "locations.h"
8
9 #include <QObject>
10 #include <QTableWidgetItem>
11 #include <QPushButton>
12 #include <QDebug>
13 #include <QButtonGroup>
14
15 UiController::UiController( Ui *ui ) :
16   ui(ui)
17 {
18   Locations *locations = Locations::instance();
19   Location *homeLocation = locations->location( "home" );
20   Location *workLocation = locations->location( "work" );
21
22   if ( homeLocation==0 ) {
23     homeLocation = new Location( "home" );
24     locations->addLocation( homeLocation );
25   } else if ( homeLocation->isValid() ) {
26     setHomeButtonValid();
27   }
28
29   if ( workLocation==0 ) {
30     workLocation = new Location( "work" );
31     locations->addLocation( workLocation );
32   } else if ( workLocation->isValid() ) {
33     setWorkButtonValid();
34   }
35
36   connect(
37       homeLocation, SIGNAL( becomeValid() ),
38       this, SLOT( setHomeButtonValid() )
39   );
40   connect(
41       homeLocation, SIGNAL( becomeValid() ),
42       locations, SLOT( saveLocation() )
43       );
44
45   connect(
46       workLocation, SIGNAL( becomeValid() ),
47       this, SLOT( setWorkButtonValid() )
48   );
49   connect(
50       workLocation, SIGNAL( becomeValid() ),
51       locations, SLOT( saveLocation() )
52       );
53
54   destination.append( homeLocation );
55   destination.append( workLocation );
56
57   connect(
58       ui->destinationButtons, SIGNAL( buttonClicked( int ) ),
59       this, SLOT( changeDestination( int ) )
60   );
61 }
62
63 UiController::~UiController()
64 {
65 }
66
67 void UiController::setHomeButtonValid()
68 {
69   qDebug() << "setting home button valid";
70   setButtonValid( Ui::HomeButtonId );
71 }
72
73 void UiController::setWorkButtonValid()
74 {
75   setButtonValid( Ui::WorkButtonId );
76 }
77
78 void UiController::setButtonValid( int id )
79 {
80   ui->destinationButtons->button( id )->setEnabled(true);
81 }
82
83 void UiController::changeDestination( int id )
84 {
85   qDebug() << "Button "+QString::number(id)+" clicked";
86
87   bool destinationHasChanged = ( currentDestination != id );
88   if ( destinationHasChanged ) {
89     emit destinationChanged( destination[id] );
90   }
91
92   // always want to emit this so that the gps position is update
93   // and the user gets new information
94   emit buttonClicked();
95 }
96
97 void UiController::displayRoute( const QList<RouteData> &routeData )
98 {
99   qDebug() << "displaying route";
100
101   ui->routeTable->setRowCount( routeData.count() );
102
103   for ( int i=0; i<routeData.count(); i++ ) {
104     QTableWidgetItem *timeItem = new QTableWidgetItem( routeData.at(i).arrivalTime );
105     ui->routeTable->setItem( i, 0, timeItem );
106
107     QTableWidgetItem *lineItem = new QTableWidgetItem( routeData.at(i).lineCode );
108     ui->routeTable->setItem( i, 1, lineItem );
109   }
110 }