Fixed: problem with invalid addresses; version 0.8
[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 "locations.h"
7
8 #include <QObject>
9 #include <QPushButton>
10 #include <QDebug>
11 #include <QButtonGroup>
12 #include <QRadioButton>
13 #include <QVBoxLayout>
14 #include <QTableWidgetItem>
15
16 UiController::UiController( Ui *ui ) :
17   m_routeData(),
18   m_destination(),
19   m_ui(ui),
20   m_currentDestination(-1),
21   m_currentRoute(-1)
22 {
23   Locations *locations = Locations::instance();
24   Location *homeLocation = locations->location( "home" );
25   Location *workLocation = locations->location( "work" );
26
27   if ( homeLocation==0 ) {
28     homeLocation = new Location( "home" );
29     locations->addLocation( homeLocation );
30   } else if ( homeLocation->isValid() ) {
31     setHomeButtonValid();
32   }
33
34   if ( workLocation==0 ) {
35     workLocation = new Location( "work" );
36     locations->addLocation( workLocation );
37   } else if ( workLocation->isValid() ) {
38     setWorkButtonValid();
39   }
40
41   connect(
42       homeLocation, SIGNAL( becomeValid() ),
43       this, SLOT( setHomeButtonValid() )
44   );
45   connect(
46       homeLocation, SIGNAL( becomeInValid() ),
47       this, SLOT( setHomeButtonInValid() )
48   );
49   connect(
50       homeLocation, SIGNAL( becomeValid() ),
51       locations, SLOT( saveLocation() )
52       );
53   connect(
54       homeLocation, SIGNAL( busy( bool ) ),
55       ui, SLOT( setBusy( bool ) )
56       );
57
58   connect(
59       workLocation, SIGNAL( becomeValid() ),
60       this, SLOT( setWorkButtonValid() )
61   );
62   connect(
63       workLocation, SIGNAL( becomeInValid() ),
64       this, SLOT( setWorkButtonInValid() )
65   );
66   connect(
67       workLocation, SIGNAL( becomeValid() ),
68       locations, SLOT( saveLocation() )
69       );
70   connect(
71       workLocation, SIGNAL( busy( bool ) ),
72       ui, SLOT( setBusy( bool ) )
73       );
74
75   m_destination.append( homeLocation );
76   m_destination.append( workLocation );
77
78   connect(
79       m_ui->m_destinationButtons, SIGNAL( buttonClicked( int ) ),
80       this, SLOT( changeDestination( int ) )
81   );
82
83   connect(
84       m_ui->m_routeButtons, SIGNAL( buttonClicked( int ) ),
85       this, SLOT( changeRoute( int ) )
86   );
87 }
88
89 UiController::~UiController()
90 {
91 }
92
93 void UiController::setHomeButtonInValid()
94 {
95   qDebug() << "setting home button invalid";
96   setButtonValid( Ui::HomeButtonId, false );
97 }
98
99 void UiController::setHomeButtonValid()
100 {
101   qDebug() << "setting home button valid";
102   setButtonValid( Ui::HomeButtonId, true );
103 }
104
105 void UiController::setWorkButtonInValid()
106 {
107   qDebug() << "setting work button invalid";
108   setButtonValid( Ui::WorkButtonId, false );
109 }
110
111 void UiController::setWorkButtonValid()
112 {
113   qDebug() << "setting work button valid";
114   setButtonValid( Ui::WorkButtonId, true );
115 }
116
117 void UiController::setButtonValid( int id, bool isValid )
118 {
119   m_ui->m_destinationButtons->button( id )->setEnabled( isValid );
120 }
121
122 void UiController::changeDestination( int id )
123 {
124   bool destinationHasChanged = ( m_currentDestination != id );
125   qDebug() << "Destination has changed=" << destinationHasChanged;
126   if ( destinationHasChanged ) {
127     qDebug() << "Emitting destination changed (" << m_destination[id]->label() << ")";
128     emit destinationChanged( m_destination[id] );
129   }
130
131   // always want to emit this so that the gps position is update
132   // and the user gets new information
133   emit buttonClicked();
134 }
135
136 void UiController::changeRoute( int id )
137 {
138   bool routeHasChanged = ( m_currentRoute != id );
139   if ( routeHasChanged ) {
140     displayRouteDetail( id );
141   }
142 }
143
144 void UiController::displayRouteDetail( int id )
145 {
146   QTableWidget *table = m_ui->m_routeDetailTable;
147
148   if ( id < m_routeData.count() ) {
149     QList<LegData> &legDataList = m_routeData[ id ].m_legData;
150     table->setRowCount( legDataList.count() );
151
152     int row=0;
153     foreach( LegData thisLegData, legDataList ) {
154       QString thisHow = thisLegData.m_how;
155
156       bool thisIsLine = ( thisHow == "LINE" );
157       if ( thisIsLine ) {
158         thisHow = thisLegData.m_lineCode;
159       }
160
161       QStringList tableStrings;
162       tableStrings
163         << thisHow
164         << thisLegData.m_tripTime
165         << thisLegData.m_tripDistance
166         << thisLegData.m_departureTime
167         << thisLegData.m_arrivalTime;
168
169       int col=0;
170       foreach( QString thisString, tableStrings ) {
171         QTableWidgetItem *newItem = new QTableWidgetItem();
172         newItem->setText( thisString );
173         table->setItem( row,col, newItem );
174         ++col;
175       }
176
177       ++row;
178     }
179   } else {
180     table->setRowCount( 0 );
181   }
182
183   table->resizeColumnsToContents();
184 }
185
186 void UiController::displayRoute( const QList<RouteData> &routeData )
187 {
188   m_routeData = routeData;
189
190   qDebug() << "displaying route";
191
192   for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
193     QString label;
194
195     QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
196     QRadioButton *button = qobject_cast<QRadioButton *>(widget);
197
198     if ( i<routeData.count() ) {
199       RouteData thisRouteData = routeData.at(i);
200       label = ( QStringList()
201           << thisRouteData.m_departureTime
202           << thisRouteData.m_lineCode ).join( "/" );
203       button->setEnabled( true );
204     } else {
205       button->setEnabled( false );
206     }
207
208     if ( i==0 ) {
209       button->setChecked( true );
210     } else {
211       button->setChecked( false );
212     }
213
214     button->setText( label );
215   }
216
217   displayRouteDetail( 0 );
218 }