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 qDebug() << "Destination button "+QString::number(id)+" clicked";
98 bool destinationHasChanged = ( m_currentDestination != id );
99 qDebug() << "Destination has changed=" << destinationHasChanged;
100 if ( destinationHasChanged ) {
101 qDebug() << "Emitting destination changed (" << m_destination[id]->label() << ")";
102 emit destinationChanged( m_destination[id] );
105 // always want to emit this so that the gps position is update
106 // and the user gets new information
107 emit buttonClicked();
110 void UiController::changeRoute( int id )
112 qDebug() << "Route button "+QString::number(id)+" clicked";
114 bool routeHasChanged = ( m_currentRoute != id );
115 if ( routeHasChanged ) {
116 displayRouteDetail( id );
120 void UiController::displayRouteDetail( int id )
122 QTableWidget *table = m_ui->m_routeDetailTable;
124 if ( id < m_routeData.count() ) {
125 QList<LegData> &legDataList = m_routeData[ id ].m_legData;
126 table->setRowCount( legDataList.count() );
129 foreach( LegData thisLegData, legDataList ) {
131 QStringList tableStrings;
134 << thisLegData.m_tripTime
135 << thisLegData.m_tripDistance
136 << thisLegData.m_departureTime
137 << thisLegData.m_arrivalTime
138 << thisLegData.m_lineCode;
141 foreach( QString thisString, tableStrings ) {
142 QTableWidgetItem *newItem = new QTableWidgetItem();
143 newItem->setText( thisString );
144 table->setItem( row,col, newItem );
151 table->setRowCount( 0 );
153 table->resizeColumnsToContents();
156 void UiController::displayRoute( const QList<RouteData> &routeData )
158 m_routeData = routeData;
160 qDebug() << "displaying route";
162 for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
165 QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
166 QRadioButton *button = qobject_cast<QRadioButton *>(widget);
168 if ( i<routeData.count() ) {
169 RouteData thisRouteData = routeData.at(i);
170 label = ( QStringList()
171 << thisRouteData.m_departureTime
172 << thisRouteData.m_lineCode ).join( "/" );
173 button->setEnabled( true );
175 button->setEnabled( false );
178 button->setText( label );
181 QTableWidget *table = m_ui->m_routeDetailTable;
182 table->setRowCount( 0 );
183 table->resizeColumnsToContents();