Incorporated changes from bus project.
[ptas] / 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_locations(new Locations),
18     m_routeData(),
19     m_destination(),
20     m_ui(ui),
21     m_currentDestination(-1),
22     m_currentRoute(-1)
23 {
24     Location *homeLocation = m_locations->location("home");
25     Location *workLocation = m_locations->location("work");
26
27     if (homeLocation==0) {
28         homeLocation = new Location("home");
29         m_locations->addLocation(homeLocation);
30     } else if (homeLocation->isValid()) {
31         setHomeButtonValid();
32     }
33
34     if (workLocation==0) {
35         workLocation = new Location("work");
36         m_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(becomeInValid()),
47                 this, SLOT(setHomeButtonInValid())
48                 );
49     connect(
50                 homeLocation, SIGNAL(becomeValid()),
51                 m_locations, SLOT(saveLocation())
52                 );
53     connect(
54                 homeLocation, SIGNAL(busy(bool)),
55                 ui, SLOT(setBusy(bool))
56                 );
57
58     connect(
59                 workLocation, SIGNAL(becomeValid()),
60                 this, SLOT(setWorkButtonValid())
61                 );
62     connect(
63                 workLocation, SIGNAL(becomeInValid()),
64                 this, SLOT(setWorkButtonInValid())
65                 );
66     connect(
67                 workLocation, SIGNAL(becomeValid()),
68                 m_locations, SLOT(saveLocation())
69                 );
70     connect(
71                 workLocation, SIGNAL(busy(bool)),
72                 ui, SLOT(setBusy(bool))
73                 );
74
75     m_destination.append(homeLocation);
76     m_destination.append(workLocation);
77
78     connect(
79                 m_ui->m_destinationButtons, SIGNAL(buttonClicked(int)),
80                 this, SLOT(changeDestination(int))
81                 );
82
83     connect(
84                 m_ui->m_routeButtons, SIGNAL(buttonClicked(int)),
85                 this, SLOT(changeRoute(int))
86                 );
87 }
88
89 UiController::~UiController()
90 {
91 }
92
93 void UiController::setHomeButtonInValid()
94 {
95     qDebug() << "setting home button invalid";
96     setButtonValid(Ui::HomeButtonId, false);
97 }
98
99 void UiController::setHomeButtonValid()
100 {
101     qDebug() << "setting home button valid";
102     setButtonValid(Ui::HomeButtonId, true);
103 }
104
105 void UiController::setWorkButtonInValid()
106 {
107     qDebug() << "setting work button invalid";
108     setButtonValid(Ui::WorkButtonId, false);
109 }
110
111 void UiController::setWorkButtonValid()
112 {
113     qDebug() << "setting work button valid";
114     setButtonValid(Ui::WorkButtonId, true);
115 }
116
117 void UiController::setButtonValid(int id, bool isValid)
118 {
119     m_ui->m_destinationButtons->button(id)->setEnabled(isValid);
120 }
121
122 void UiController::changeDestination(int id)
123 {
124     bool destinationHasChanged = (m_currentDestination != id);
125     qDebug() << "Destination has changed=" << destinationHasChanged;
126     if (destinationHasChanged) {
127         qDebug() << "Emitting destination changed (" << m_destination[id]->label() << ")";
128         emit destinationChanged(m_destination[id]);
129         m_currentDestination = id;
130     }
131
132     // always want to emit this so that the gps position is updated
133     // and the user gets new information
134     emit buttonClicked();
135 }
136
137 void UiController::changeRoute(int id)
138 {
139     bool routeHasChanged = (m_currentRoute != id);
140     if (routeHasChanged) {
141         displayRouteDetail(id);
142     }
143 }
144
145 void UiController::displayRouteDetail(int id)
146 {
147     QTableWidget *table = m_ui->m_routeDetailTable;
148
149     if (id < m_routeData.count()) {
150         QList<LegData> &legDataList = m_routeData[ id ].m_legData;
151         table->setRowCount(legDataList.count());
152
153         int row=0;
154         foreach(LegData thisLegData, legDataList) {
155             QString thisHow = thisLegData.m_how;
156
157             bool notWalk = (thisHow != "walk");
158             if (notWalk) {
159                 thisHow = thisLegData.m_lineCode;
160             }
161
162             QStringList tableStrings;
163             tableStrings
164                     << thisHow
165                     << thisLegData.m_tripTime
166                     << thisLegData.m_tripDistance
167                     << thisLegData.m_departureTime
168                     << thisLegData.m_arrivalTime;
169
170             int col=0;
171             foreach(QString thisString, tableStrings) {
172                 QTableWidgetItem *newItem = new QTableWidgetItem();
173                 newItem->setText(thisString);
174                 table->setItem(row,col, newItem);
175                 ++col;
176             }
177
178             ++row;
179         }
180     } else {
181         table->setRowCount(0);
182     }
183
184     table->resizeColumnsToContents();
185 }
186
187 void UiController::displayRoute(const QList<RouteData> &routeData)
188 {
189     m_routeData = routeData;
190
191     qDebug() << "displaying route";
192
193     for (int i=0; i<Ytv::ShowFiveResults; ++i) {
194         QString label;
195
196         QWidget *widget = m_ui->m_routeStack->itemAt(i)->widget();
197         QRadioButton *button = qobject_cast<QRadioButton *>(widget);
198
199         if (i<routeData.count()) {
200             RouteData thisRouteData = routeData.at(i);
201             label = (QStringList()
202                      << thisRouteData.m_departureTime
203                      << thisRouteData.m_lineCode).join("/");
204             button->setEnabled(true);
205         } else {
206             button->setEnabled(false);
207         }
208
209         if (i==0) {
210             button->setChecked(true);
211         } else {
212             button->setChecked(false);
213         }
214
215         button->setText(label);
216     }
217
218     displayRouteDetail(0);
219 }