Location selector and edit window finished
[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 #include <QString>
16 #ifdef Q_WS_MAEMO_5
17 #include <QMaemo5AbstractPickSelector>
18 #include <QMaemo5InformationBox>
19 #endif
20
21 UiController::UiController( UiClass *ui ) :
22         m_routeData(),
23         m_ui(ui),
24         m_currentRoute(-1)
25 {
26     Locations *locations = Locations::GetInstance();
27     if (locations->size() == 0)
28     {
29         locations->addEditLocation(new Location("Home"));
30         locations->addEditLocation(new Location("Work"));
31     }
32
33     QObject::connect(m_ui->m_routeButton, SIGNAL(clicked()), this, SLOT(findRoute()));
34 #ifdef Q_WS_MAEMO_5
35     QObject::connect(this->m_ui->m_fromButton->pickSelector(), SIGNAL(selected(const QString &)), this, SLOT(changeFrom()));
36     connect(m_ui->m_toButton->pickSelector(), SIGNAL(selected(const QString &)), this, SLOT(changeTo()));
37 #endif
38     connect(m_ui->m_routeButtons, SIGNAL(buttonClicked(int)), this, SLOT(displayRouteDetail(int)));
39 }
40
41 UiController::~UiController()
42 {
43 }
44
45 void UiController::changeRoute( int id )
46 {
47     bool routeHasChanged = ( m_currentRoute != id );
48     if ( routeHasChanged ) {
49         displayRouteDetail( id );
50     }
51 }
52
53 void UiController::displayRouteDetail( int id )
54 {
55     QTableWidget *table = m_ui->m_routeDetailTable;
56
57     if ( id < m_routeData.count() ) {
58         QList<LegData> &legDataList = m_routeData[ id ].m_legData;
59         table->setRowCount( legDataList.count() );
60
61         int row=0;
62         foreach( LegData thisLegData, legDataList ) {
63             QString thisHow = thisLegData.m_how;
64
65             bool thisIsLine = ( thisHow == "LINE" );
66             if ( thisIsLine ) {
67                 thisHow = thisLegData.m_lineCode;
68             }
69
70             QStringList tableStrings;
71             tableStrings
72                     << thisHow
73                     << thisLegData.m_tripTime
74                     << thisLegData.m_tripDistance
75                     << thisLegData.m_departureTime
76                     << thisLegData.m_arrivalTime;
77
78             int col=0;
79             foreach( QString thisString, tableStrings ) {
80                 QTableWidgetItem *newItem = new QTableWidgetItem();
81                 newItem->setText( thisString );
82                 table->setItem( row,col, newItem );
83                 ++col;
84             }
85
86             ++row;
87         }
88     } else {
89         table->setRowCount( 0 );
90     }
91
92     table->resizeColumnsToContents();
93 }
94
95 void UiController::displayRoute( const QList<RouteData> &routeData )
96 {
97     m_routeData = routeData;
98
99     qDebug() << "displaying route";
100
101     for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
102         QString label;
103
104         QWidget *widget = m_ui->m_routeStack->itemAt( i )->widget();
105         QRadioButton *button = qobject_cast<QRadioButton *>(widget);
106
107         if ( i<routeData.count() ) {
108             RouteData thisRouteData = routeData.at(i);
109             label = ( QStringList()
110                       << thisRouteData.m_departureTime
111                       << thisRouteData.m_lineCode ).join( "/" );
112             button->setEnabled( true );
113         } else {
114             button->setEnabled( false );
115         }
116
117         if ( i==0 ) {
118             button->setChecked( true );
119         } else {
120             button->setChecked( false );
121         }
122
123         button->setText( label );
124     }
125
126     displayRouteDetail( 0 );
127 }
128
129 void UiController::findRoute()
130 {
131     qDebug() << "Route search button clicked";
132     emit(routeSearchRequested());
133 }
134
135 /*void UiController::updateLocationSelectors()
136 {
137     m_ui->setLocations();
138 }*/
139
140 void UiController::changeFrom()
141 {
142     qDebug() << "From selection changed";
143     Locations *locations = Locations::GetInstance();
144     Location *from;
145
146 #ifdef Q_WS_MAEMO_5
147     const QString newValue = m_ui->m_fromButton->valueText();
148 #else
149     const QString newValue = "";
150 #endif
151     if (newValue == "GPS")
152     {
153         from = locations->getGpsLocation();
154         if (!from->isValid())
155         {
156             qDebug() << "GPS location is not valid.";
157 #ifdef Q_WS_MAEMO_5
158             QMaemo5InformationBox::information(this->m_ui->m_mainWindow, "GPS location has not been received yet. Wait a moment.");
159 #endif
160             connect(from, SIGNAL(becomeValid()), this, SLOT(gpsBecameValid()));
161             return;
162         }
163     }
164     else
165     {
166         from = locations->getLocation(newValue);
167         if (!from)
168             qDebug() << "No location with label " << newValue << " was found.";
169     }
170     if (from)
171     {
172         qDebug() << "Emitting signal of new from selection";
173         emit(fromChanged(from));
174     }
175 }
176
177 void UiController::gpsBecameValid()
178 {
179 #ifdef Q_WS_MAEMO_5
180     QMaemo5InformationBox::information(this->m_ui->m_mainWindow, "GPS location received.");
181 #endif
182     Location *gps = Locations::GetInstance()->getGpsLocation();
183     disconnect(gps, SIGNAL(becomeValid()), this, SLOT(gpsBecameValid()));
184     this->changeFrom();
185     this->changeTo();
186 }
187
188 void UiController::changeTo()
189 {
190     qDebug() << "To selection changed";
191     Locations *locations = Locations::GetInstance();
192     Location *to;
193
194 #ifdef Q_WS_MAEMO_5
195     const QString newValue = m_ui->m_toButton->valueText();
196 #else
197     const QString newValue = "";
198 #endif
199     if (newValue == "GPS")
200     {
201         to = locations->getGpsLocation();
202         if (!to->isValid())
203         {
204             qDebug() << "GPS location is not valid.";
205 #ifdef Q_WS_MAEMO_5
206             QMaemo5InformationBox::information(this->m_ui->m_mainWindow, "GPS location has not been received yet. Wait a moment.");
207 #endif
208             connect(to, SIGNAL(becomeValid()), this, SLOT(gpsBecameValid()));
209             return;
210         }
211     }
212     else
213     {
214         to = locations->getLocation(newValue);
215         if (!to)
216             qDebug() << "No location with label " << newValue << " was found.";
217     }
218     if (to)
219     {
220         qDebug() << "Emitting signal of new to selection";
221         emit(toChanged(to));
222     }
223 }