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