Added dialogs to change home and work locations.
[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   Location *homeLocation = new Location( "home" );
19   Location *workLocation = new Location( "work" );
20
21   Locations locations;
22   locations.addLocation( homeLocation );
23   locations.addLocation( workLocation );
24
25   connect(
26       homeLocation, SIGNAL( becomeValid() ),
27       this, SLOT( setHomeButtonValid() )
28   );
29   connect(
30       workLocation, SIGNAL( becomeValid() ),
31       this, SLOT( setWorkButtonValid() )
32   );
33
34   homeLocation->resolveAddress( Ytv::Home );
35   workLocation->resolveAddress( Ytv::Work );
36
37   destination.append( homeLocation );
38   destination.append( workLocation );
39
40   connect(
41       ui->destinationButtons, SIGNAL( buttonClicked( int ) ),
42       this, SLOT( changeDestination( int ) )
43   );
44 }
45
46 UiController::~UiController()
47 {
48 }
49
50 void UiController::setHomeButtonValid()
51 {
52   setButtonValid( Ui::HomeButtonId );
53 }
54
55 void UiController::setWorkButtonValid()
56 {
57   setButtonValid( Ui::WorkButtonId );
58 }
59
60 void UiController::setButtonValid( int id )
61 {
62   ui->destinationButtons->button( id )->setEnabled(true);
63 }
64
65 void UiController::changeDestination( int id )
66 {
67   qDebug() << "Button "+QString::number(id)+" clicked";
68
69   bool destinationHasChanged = ( currentDestination != id );
70   if ( destinationHasChanged ) {
71     emit destinationChanged( destination[id] );
72   }
73
74   // always want to emit this so that the gps position is update
75   // and the user gets new information
76   emit buttonClicked();
77 }
78
79 void UiController::displayRoute( const QList<RouteData> &routeData )
80 {
81   qDebug() << "displaying route";
82
83   ui->routeTable->setRowCount( routeData.count() );
84
85   for ( int i=0; i<routeData.count(); i++ ) {
86     QTableWidgetItem *timeItem = new QTableWidgetItem( routeData.at(i).arrivalTime );
87     ui->routeTable->setItem( i, 0, timeItem );
88
89     QTableWidgetItem *lineItem = new QTableWidgetItem( routeData.at(i).lineCode );
90     ui->routeTable->setItem( i, 1, lineItem );
91   }
92 }