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