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( becomeValid() ),
47 locations, SLOT( saveLocation() )
51 workLocation, SIGNAL( becomeValid() ),
52 this, SLOT( setWorkButtonValid() )
55 workLocation, SIGNAL( becomeValid() ),
56 locations, SLOT( saveLocation() )
59 m_destination.append( homeLocation );
60 m_destination.append( workLocation );
63 m_ui->m_destinationButtons, SIGNAL( buttonClicked( int ) ),
64 this, SLOT( changeDestination( int ) )
68 m_ui->m_routeButtons, SIGNAL( buttonClicked( int ) ),
69 this, SLOT( changeRoute( int ) )
73 UiController::~UiController()
77 void UiController::setHomeButtonValid()
79 qDebug() << "setting home button valid";
80 setButtonValid( Ui::HomeButtonId );
83 void UiController::setWorkButtonValid()
85 qDebug() << "setting work button valid";
86 setButtonValid( Ui::WorkButtonId );
89 void UiController::setButtonValid( int id )
91 m_ui->m_destinationButtons->button( id )->setEnabled(true);
94 void UiController::changeDestination( int id )
96 bool destinationHasChanged = ( m_currentDestination != id );
97 qDebug() << "Destination has changed=" << destinationHasChanged;
98 if ( destinationHasChanged ) {
99 qDebug() << "Emitting destination changed (" << m_destination[id]->label() << ")";
100 emit destinationChanged( m_destination[id] );
103 // always want to emit this so that the gps position is update
104 // and the user gets new information
105 emit buttonClicked();
108 void UiController::changeRoute( int id )
110 bool routeHasChanged = ( m_currentRoute != id );
111 if ( routeHasChanged ) {
112 displayRouteDetail( id );
116 void UiController::displayRouteDetail( int id )
118 QTableWidget *table = m_ui->m_routeDetailTable;
120 if ( id < m_routeData.count() ) {
121 QList<LegData> &legDataList = m_routeData[ id ].m_legData;
122 table->setRowCount( legDataList.count() );
125 foreach( LegData thisLegData, legDataList ) {
126 QString thisHow = thisLegData.m_how;
128 bool thisIsLine = ( thisHow == "LINE" );
130 thisHow = thisLegData.m_lineCode;
133 QStringList tableStrings;
136 << thisLegData.m_tripTime
137 << thisLegData.m_tripDistance
138 << thisLegData.m_departureTime
139 << thisLegData.m_arrivalTime;
142 foreach( QString thisString, tableStrings ) {
143 QTableWidgetItem *newItem = new QTableWidgetItem();
144 newItem->setText( thisString );
145 table->setItem( row,col, newItem );
152 table->setRowCount( 0 );
155 table->resizeColumnsToContents();
158 void UiController::displayRoute( const QList<RouteData> &routeData )
160 m_routeData = routeData;
162 qDebug() << "displaying route";
164 for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
167 QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
168 QRadioButton *button = qobject_cast<QRadioButton *>(widget);
170 if ( i<routeData.count() ) {
171 RouteData thisRouteData = routeData.at(i);
172 label = ( QStringList()
173 << thisRouteData.m_departureTime
174 << thisRouteData.m_lineCode ).join( "/" );
175 button->setEnabled( true );
177 button->setEnabled( false );
181 button->setChecked( true );
183 button->setChecked( false );
186 button->setText( label );
189 displayRouteDetail( 0 );