1 #include "uicontroller.h"
11 #include <QPushButton>
13 #include <QButtonGroup>
14 #include <QRadioButton>
15 #include <QVBoxLayout>
16 #include <QTableWidgetItem>
18 UiController::UiController( Ui *ui ) :
26 Journey *gpsToHome = journeys.journey( "gps->home" );
27 Journey *gpsToWork = journeys.journey( "gps->work" );
30 gpsToHome = new Journey();
31 gpsToHome->setJourney( "gps", "home", "gps->home" );
33 journeys.addJourney( gpsToHome );
34 } else if ( gpsToHome->isValid() ) {
39 gpsToWork = new Journey();
40 gpsToWork->setJourney( "gps", "work", "gps->work" );
42 journeys.addJourney( gpsToWork );
43 } else if ( gpsToWork->isValid() ) {
48 gpsToHome, SIGNAL( becomeValid() ),
49 this, SLOT( setHomeButtonValid() )
52 gpsToHome, SIGNAL( becomeInValid() ),
53 this, SLOT( setHomeButtonInValid() )
56 gpsToHome, SIGNAL( becomeValid() ),
57 &journeys, SLOT( saveJourney() )
60 gpsToHome, SIGNAL( busy( bool ) ),
61 ui, SLOT( setBusy( bool ) )
65 gpsToWork, SIGNAL( becomeValid() ),
66 this, SLOT( setWorkButtonValid() )
69 gpsToHome, SIGNAL( becomeInValid() ),
70 this, SLOT( setWorkButtonInValid() )
73 gpsToHome, SIGNAL( becomeValid() ),
74 &journeys, SLOT( saveJourney() )
77 gpsToHome, SIGNAL( busy( bool ) ),
78 ui, SLOT( setBusy( bool ) )
81 m_journeys.append( gpsToHome );
82 m_journeys.append( gpsToWork );
85 m_ui->m_journeyButtons, SIGNAL( buttonClicked( int ) ),
86 this, SLOT( changeDestination( int ) )
90 m_ui->m_journeyButtons, SIGNAL( buttonClicked( int ) ),
91 this, SLOT( changeRoute( int ) )
95 UiController::~UiController()
99 void UiController::setHomeButtonInValid()
101 qDebug() << "setting home button invalid";
102 setButtonValid( Ui::HomeButtonId, false );
105 void UiController::setHomeButtonValid()
107 qDebug() << "setting home button valid";
108 setButtonValid( Ui::HomeButtonId, true );
111 void UiController::setWorkButtonInValid()
113 qDebug() << "setting work button invalid";
114 setButtonValid( Ui::WorkButtonId, false );
117 void UiController::setWorkButtonValid()
119 qDebug() << "setting work button valid";
120 setButtonValid( Ui::WorkButtonId, true );
123 void UiController::setButtonValid( int id, bool isValid )
125 m_ui->m_journeyButtons->button( id )->setEnabled( isValid );
128 void UiController::changeDestination( int id )
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;
138 // always want to emit this so that the gps position is updated
139 // and the user gets new information
140 emit buttonClicked();
143 void UiController::changeRoute( int id )
145 bool routeHasChanged = ( m_currentRoute != id );
146 if ( routeHasChanged ) {
147 displayRouteDetail( id );
151 void UiController::displayRouteDetail( int id )
153 QTableWidget *table = m_ui->m_routeDetailTable;
155 if ( id < m_routeData.count() ) {
156 QList<LegData> &legDataList = m_routeData[ id ].m_legData;
157 table->setRowCount( legDataList.count() );
160 foreach( LegData thisLegData, legDataList ) {
161 QString thisHow = thisLegData.m_how;
163 bool thisIsLine = ( thisHow == "LINE" );
165 thisHow = thisLegData.m_lineCode;
168 QStringList tableStrings;
171 << thisLegData.m_tripTime
172 << thisLegData.m_tripDistance
173 << thisLegData.m_departureTime
174 << thisLegData.m_arrivalTime;
177 foreach( QString thisString, tableStrings ) {
178 QTableWidgetItem *newItem = new QTableWidgetItem();
179 newItem->setText( thisString );
180 table->setItem( row,col, newItem );
187 table->setRowCount( 0 );
190 table->resizeColumnsToContents();
193 void UiController::displayRoute( const QList<RouteData> &routeData )
195 m_routeData = routeData;
197 qDebug() << "displaying route";
199 for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
202 QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
203 QRadioButton *button = qobject_cast<QRadioButton *>(widget);
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 );
212 button->setEnabled( false );
216 button->setChecked( true );
218 button->setChecked( false );
221 button->setText( label );
224 displayRouteDetail( 0 );