28d423220bd76a3c34686b706ffdb1c834cb2cf8
[ptas] / zouba / src / 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 <QPushButton>
11 #include <QDebug>
12 #include <QButtonGroup>
13 #include <QRadioButton>
14 #include <QVBoxLayout>
15
16 UiController::UiController( Ui *ui ) :
17   ui(ui)
18 {
19   Locations *locations = Locations::instance();
20   Location *homeLocation = locations->location( "home" );
21   Location *workLocation = locations->location( "work" );
22
23   if ( homeLocation==0 ) {
24     homeLocation = new Location( "home" );
25     locations->addLocation( homeLocation );
26   } else if ( homeLocation->isValid() ) {
27     setHomeButtonValid();
28   }
29
30   if ( workLocation==0 ) {
31     workLocation = new Location( "work" );
32     locations->addLocation( workLocation );
33   } else if ( workLocation->isValid() ) {
34     setWorkButtonValid();
35   }
36
37   connect(
38       homeLocation, SIGNAL( becomeValid() ),
39       this, SLOT( setHomeButtonValid() )
40   );
41   connect(
42       homeLocation, SIGNAL( becomeValid() ),
43       locations, SLOT( saveLocation() )
44       );
45
46   connect(
47       workLocation, SIGNAL( becomeValid() ),
48       this, SLOT( setWorkButtonValid() )
49   );
50   connect(
51       workLocation, SIGNAL( becomeValid() ),
52       locations, SLOT( saveLocation() )
53       );
54
55   destination.append( homeLocation );
56   destination.append( workLocation );
57
58   connect(
59       ui->destinationButtons, SIGNAL( buttonClicked( int ) ),
60       this, SLOT( changeDestination( int ) )
61   );
62 }
63
64 UiController::~UiController()
65 {
66 }
67
68 void UiController::setHomeButtonValid()
69 {
70   qDebug() << "setting home button valid";
71   setButtonValid( Ui::HomeButtonId );
72 }
73
74 void UiController::setWorkButtonValid()
75 {
76   qDebug() << "setting work button valid";
77   setButtonValid( Ui::WorkButtonId );
78 }
79
80 void UiController::setButtonValid( int id )
81 {
82   ui->destinationButtons->button( id )->setEnabled(true);
83 }
84
85 void UiController::changeDestination( int id )
86 {
87   qDebug() << "Button "+QString::number(id)+" clicked";
88
89   bool destinationHasChanged = ( currentDestination != id );
90   if ( destinationHasChanged ) {
91     emit destinationChanged( destination[id] );
92   }
93
94   // always want to emit this so that the gps position is update
95   // and the user gets new information
96   emit buttonClicked();
97 }
98
99 void UiController::displayRoute( const QList<RouteData> &routeData )
100 {
101   qDebug() << "displaying route";
102
103   for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
104     QString label;
105
106     QWidget *widget = ui->routeStack->itemAt( i )->widget();
107     QRadioButton *button = qobject_cast<QRadioButton *>(widget);
108
109     if ( i<routeData.count() ) {
110       RouteData thisRouteData = routeData.at(i);
111       label = ( QStringList()
112           << thisRouteData.arrivalTime
113           << thisRouteData.lineCode ).join( "/" );
114       button->setEnabled( true );
115     } else {
116       button->setEnabled( false );
117     }
118
119     button->setText( label );
120   }
121
122 }