a5440a387ec33511d2275cd66771d15ab5912c8e
[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   qDebug() << "Destination button "+QString::number(id)+" clicked";
97
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] );
103   }
104
105   // always want to emit this so that the gps position is update
106   // and the user gets new information
107   emit buttonClicked();
108 }
109
110 void UiController::changeRoute( int id )
111 {
112   qDebug() << "Route button "+QString::number(id)+" clicked";
113
114   bool routeHasChanged = ( m_currentRoute != id );
115   if ( routeHasChanged ) {
116     displayRouteDetail( id );
117   }
118 }
119
120 void UiController::displayRouteDetail( int id )
121 {
122   QTableWidget *table = m_ui->m_routeDetailTable;
123
124   if ( id < m_routeData.count() ) {
125     QList<LegData> &legDataList = m_routeData[ id ].m_legData;
126     table->setRowCount( legDataList.count() );
127
128     int row=0;
129     foreach( LegData thisLegData, legDataList ) {
130
131       QStringList tableStrings;
132       tableStrings
133         << thisLegData.m_how
134         << thisLegData.m_tripTime
135         << thisLegData.m_tripDistance
136         << thisLegData.m_departureTime
137         << thisLegData.m_arrivalTime
138         << thisLegData.m_lineCode;
139
140       int col=0;
141       foreach( QString thisString, tableStrings ) {
142         QTableWidgetItem *newItem = new QTableWidgetItem();
143         newItem->setText( thisString );
144         table->setItem( row,col, newItem );
145         ++col;
146       }
147
148       ++row;
149     }
150   } else {
151     table->setRowCount( 0 );
152   }
153
154   table->resizeColumnsToContents();
155 }
156
157 void UiController::displayRoute( const QList<RouteData> &routeData )
158 {
159   m_routeData = routeData;
160
161   qDebug() << "displaying route";
162
163   for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
164     QString label;
165
166     QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
167     QRadioButton *button = qobject_cast<QRadioButton *>(widget);
168
169     if ( i<routeData.count() ) {
170       RouteData thisRouteData = routeData.at(i);
171       label = ( QStringList()
172           << thisRouteData.m_departureTime
173           << thisRouteData.m_lineCode ).join( "/" );
174       button->setEnabled( true );
175     } else {
176       button->setEnabled( false );
177     }
178
179     if ( i==0 ) {
180       button->setChecked( true );
181     } else {
182       button->setChecked( false );
183     }
184
185     button->setText( label );
186   }
187
188   displayRouteDetail( 0 );
189 }