Changes: disabled selection on the route detail table
[ptas] / zouba / src / uicontroller.cpp
1 #include "uicontroller.h"
2 #include "route.h"
3 #include "ui.h"
4 #include "ytv.h"
5 #include "location.h"
6 #include "locations.h"
7
8 #include <QObject>
9 #include <QPushButton>
10 #include <QDebug>
11 #include <QButtonGroup>
12 #include <QRadioButton>
13 #include <QVBoxLayout>
14 #include <QTableWidgetItem>
15
16 UiController::UiController( Ui *ui ) :
17   m_routeData(),
18   m_destination(),
19   m_ui(ui),
20   m_currentDestination(-1),
21   m_currentRoute(-1)
22 {
23   Locations *locations = Locations::instance();
24   Location *homeLocation = locations->location( "home" );
25   Location *workLocation = locations->location( "work" );
26
27   if ( homeLocation==0 ) {
28     homeLocation = new Location( "home" );
29     locations->addLocation( homeLocation );
30   } else if ( homeLocation->isValid() ) {
31     setHomeButtonValid();
32   }
33
34   if ( workLocation==0 ) {
35     workLocation = new Location( "work" );
36     locations->addLocation( workLocation );
37   } else if ( workLocation->isValid() ) {
38     setWorkButtonValid();
39   }
40
41   connect(
42       homeLocation, SIGNAL( becomeValid() ),
43       this, SLOT( setHomeButtonValid() )
44   );
45   connect(
46       homeLocation, SIGNAL( becomeValid() ),
47       locations, SLOT( saveLocation() )
48       );
49
50   connect(
51       workLocation, SIGNAL( becomeValid() ),
52       this, SLOT( setWorkButtonValid() )
53   );
54   connect(
55       workLocation, SIGNAL( becomeValid() ),
56       locations, SLOT( saveLocation() )
57       );
58
59   m_destination.append( homeLocation );
60   m_destination.append( workLocation );
61
62   connect(
63       m_ui->m_destinationButtons, SIGNAL( buttonClicked( int ) ),
64       this, SLOT( changeDestination( int ) )
65   );
66
67   connect(
68       m_ui->m_routeButtons, SIGNAL( buttonClicked( int ) ),
69       this, SLOT( changeRoute( int ) )
70   );
71 }
72
73 UiController::~UiController()
74 {
75 }
76
77 void UiController::setHomeButtonValid()
78 {
79   qDebug() << "setting home button valid";
80   setButtonValid( Ui::HomeButtonId );
81 }
82
83 void UiController::setWorkButtonValid()
84 {
85   qDebug() << "setting work button valid";
86   setButtonValid( Ui::WorkButtonId );
87 }
88
89 void UiController::setButtonValid( int id )
90 {
91   m_ui->m_destinationButtons->button( id )->setEnabled(true);
92 }
93
94 void UiController::changeDestination( int id )
95 {
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] );
101   }
102
103   // always want to emit this so that the gps position is update
104   // and the user gets new information
105   emit buttonClicked();
106 }
107
108 void UiController::changeRoute( int id )
109 {
110   bool routeHasChanged = ( m_currentRoute != id );
111   if ( routeHasChanged ) {
112     displayRouteDetail( id );
113   }
114 }
115
116 void UiController::displayRouteDetail( int id )
117 {
118   QTableWidget *table = m_ui->m_routeDetailTable;
119
120   if ( id < m_routeData.count() ) {
121     QList<LegData> &legDataList = m_routeData[ id ].m_legData;
122     table->setRowCount( legDataList.count() );
123
124     int row=0;
125     foreach( LegData thisLegData, legDataList ) {
126       QString thisHow = thisLegData.m_how;
127
128       bool thisIsLine = ( thisHow == "LINE" );
129       if ( thisIsLine ) {
130         thisHow = thisLegData.m_lineCode;
131       }
132
133       QStringList tableStrings;
134       tableStrings
135         << thisHow
136         << thisLegData.m_tripTime
137         << thisLegData.m_tripDistance
138         << thisLegData.m_departureTime
139         << thisLegData.m_arrivalTime;
140
141       int col=0;
142       foreach( QString thisString, tableStrings ) {
143         QTableWidgetItem *newItem = new QTableWidgetItem();
144         newItem->setText( thisString );
145         table->setItem( row,col, newItem );
146         ++col;
147       }
148
149       ++row;
150     }
151   } else {
152     table->setRowCount( 0 );
153   }
154
155   table->resizeColumnsToContents();
156 }
157
158 void UiController::displayRoute( const QList<RouteData> &routeData )
159 {
160   m_routeData = routeData;
161
162   qDebug() << "displaying route";
163
164   for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
165     QString label;
166
167     QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
168     QRadioButton *button = qobject_cast<QRadioButton *>(widget);
169
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 );
176     } else {
177       button->setEnabled( false );
178     }
179
180     if ( i==0 ) {
181       button->setChecked( true );
182     } else {
183       button->setChecked( false );
184     }
185
186     button->setText( label );
187   }
188
189   displayRouteDetail( 0 );
190 }