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),
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] );
129 m_currentDestination = id;
132 // always want to emit this so that the gps position is updated
133 // and the user gets new information
134 emit buttonClicked();
137 void UiController::changeRoute( int id )
139 bool routeHasChanged = ( m_currentRoute != id );
140 if ( routeHasChanged ) {
141 displayRouteDetail( id );
145 void UiController::displayRouteDetail( int id )
147 QTableWidget *table = m_ui->m_routeDetailTable;
149 if ( id < m_routeData.count() ) {
150 QList<LegData> &legDataList = m_routeData[ id ].m_legData;
151 table->setRowCount( legDataList.count() );
154 foreach( LegData thisLegData, legDataList ) {
155 QString thisHow = thisLegData.m_how;
157 bool thisIsLine = ( thisHow == "LINE" );
159 thisHow = thisLegData.m_lineCode;
162 QStringList tableStrings;
165 << thisLegData.m_tripTime
166 << thisLegData.m_tripDistance
167 << thisLegData.m_departureTime
168 << thisLegData.m_arrivalTime;
171 foreach( QString thisString, tableStrings ) {
172 QTableWidgetItem *newItem = new QTableWidgetItem();
173 newItem->setText( thisString );
174 table->setItem( row,col, newItem );
181 table->setRowCount( 0 );
184 table->resizeColumnsToContents();
187 void UiController::displayRoute( const QList<RouteData> &routeData )
189 m_routeData = routeData;
191 qDebug() << "displaying route";
193 for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
196 QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
197 QRadioButton *button = qobject_cast<QRadioButton *>(widget);
199 if ( i<routeData.count() ) {
200 RouteData thisRouteData = routeData.at(i);
201 label = ( QStringList()
202 << thisRouteData.m_departureTime
203 << thisRouteData.m_lineCode ).join( "/" );
204 button->setEnabled( true );
206 button->setEnabled( false );
210 button->setChecked( true );
212 button->setChecked( false );
215 button->setText( label );
218 displayRouteDetail( 0 );