1 #include "uicontroller.h"
11 #include <QButtonGroup>
12 #include <QRadioButton>
13 #include <QVBoxLayout>
14 #include <QTableWidgetItem>
16 UiController::UiController( Ui *ui ) :
20 m_currentDestination(-1),
23 Locations *locations = Locations::instance();
24 Location *homeLocation = locations->location( "home" );
25 Location *workLocation = locations->location( "work" );
27 if ( homeLocation==0 ) {
28 homeLocation = new Location( "home" );
29 locations->addLocation( homeLocation );
30 } else if ( homeLocation->isValid() ) {
34 if ( workLocation==0 ) {
35 workLocation = new Location( "work" );
36 locations->addLocation( workLocation );
37 } else if ( workLocation->isValid() ) {
42 homeLocation, SIGNAL( becomeValid() ),
43 this, SLOT( setHomeButtonValid() )
46 homeLocation, SIGNAL( becomeInValid() ),
47 this, SLOT( setHomeButtonInValid() )
50 homeLocation, SIGNAL( becomeValid() ),
51 locations, SLOT( saveLocation() )
54 homeLocation, SIGNAL( busy( bool ) ),
55 ui, SLOT( setBusy( bool ) )
59 workLocation, SIGNAL( becomeValid() ),
60 this, SLOT( setWorkButtonValid() )
63 workLocation, SIGNAL( becomeInValid() ),
64 this, SLOT( setWorkButtonInValid() )
67 workLocation, SIGNAL( becomeValid() ),
68 locations, SLOT( saveLocation() )
71 workLocation, SIGNAL( busy( bool ) ),
72 ui, SLOT( setBusy( bool ) )
75 m_destination.append( homeLocation );
76 m_destination.append( workLocation );
79 m_ui->m_destinationButtons, SIGNAL( buttonClicked( int ) ),
80 this, SLOT( changeDestination( int ) )
84 m_ui->m_routeButtons, SIGNAL( buttonClicked( int ) ),
85 this, SLOT( changeRoute( int ) )
89 UiController::~UiController()
93 void UiController::setHomeButtonInValid()
95 qDebug() << "setting home button invalid";
96 setButtonValid( Ui::HomeButtonId, false );
99 void UiController::setHomeButtonValid()
101 qDebug() << "setting home button valid";
102 setButtonValid( Ui::HomeButtonId, true );
105 void UiController::setWorkButtonInValid()
107 qDebug() << "setting work button invalid";
108 setButtonValid( Ui::WorkButtonId, false );
111 void UiController::setWorkButtonValid()
113 qDebug() << "setting work button valid";
114 setButtonValid( Ui::WorkButtonId, true );
117 void UiController::setButtonValid( int id, bool isValid )
119 m_ui->m_destinationButtons->button( id )->setEnabled( isValid );
122 void UiController::changeDestination( int id )
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] );
131 // always want to emit this so that the gps position is update
132 // and the user gets new information
133 emit buttonClicked();
136 void UiController::changeRoute( int id )
138 bool routeHasChanged = ( m_currentRoute != id );
139 if ( routeHasChanged ) {
140 displayRouteDetail( id );
144 void UiController::displayRouteDetail( int id )
146 QTableWidget *table = m_ui->m_routeDetailTable;
148 if ( id < m_routeData.count() ) {
149 QList<LegData> &legDataList = m_routeData[ id ].m_legData;
150 table->setRowCount( legDataList.count() );
153 foreach( LegData thisLegData, legDataList ) {
154 QString thisHow = thisLegData.m_how;
156 bool thisIsLine = ( thisHow == "LINE" );
158 thisHow = thisLegData.m_lineCode;
161 QStringList tableStrings;
164 << thisLegData.m_tripTime
165 << thisLegData.m_tripDistance
166 << thisLegData.m_departureTime
167 << thisLegData.m_arrivalTime;
170 foreach( QString thisString, tableStrings ) {
171 QTableWidgetItem *newItem = new QTableWidgetItem();
172 newItem->setText( thisString );
173 table->setItem( row,col, newItem );
180 table->setRowCount( 0 );
183 table->resizeColumnsToContents();
186 void UiController::displayRoute( const QList<RouteData> &routeData )
188 m_routeData = routeData;
190 qDebug() << "displaying route";
192 for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
195 QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
196 QRadioButton *button = qobject_cast<QRadioButton *>(widget);
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 );
205 button->setEnabled( false );
209 button->setChecked( true );
211 button->setChecked( false );
214 button->setText( label );
217 displayRouteDetail( 0 );