Incorporated changes from bus project.
[ptas] / src / ui.cpp
1 #include "ui.h"
2
3 #include "locations.h"
4 #include "ytv.h"
5
6 #include <QMainWindow>
7 #include <QRadioButton>
8 #include <QTableWidget>
9 #include <QString>
10 #include <QRect>
11 #include <QButtonGroup>
12 #include <QHeaderView>
13 #include <QObject>
14 #include <QMenuBar>
15 #include <QHBoxLayout>
16 #include <QVBoxLayout>
17 #include <QGridLayout>
18 #include <QSizePolicy>
19 #include <QInputDialog>
20 #include <QDebug>
21
22 Ui::Ui() :
23     m_centralWidget(0),
24     m_destinationButtons(0),
25     m_routeStack(0),
26     m_usingFakeGps(false),
27     m_fakeLocationLabel("work")
28 {
29 }
30
31 Ui::~Ui()
32 {
33 }
34
35 void Ui::setupUi(QMainWindow *mainWindow)
36 {
37     m_mainWindow = mainWindow;
38     m_mainWindow->resize(800,480);
39
40     m_menu = mainWindow->menuBar()->addMenu("Settings");
41
42     QAction *setHomeAddressAction = new QAction("Set home address", this);
43     QAction *setWorkAddressAction = new QAction("Set work address", this);
44     m_toggleFakeGpsAction  = new QAction("Use fake GPS", this);
45     m_menu->addAction(setHomeAddressAction);
46     m_menu->addAction(setWorkAddressAction);
47     m_menu->addAction(m_toggleFakeGpsAction);
48
49     connect(
50                 setHomeAddressAction, SIGNAL(triggered()),
51                 this, SLOT(setHomeAddress())
52                 );
53     connect(
54                 setWorkAddressAction, SIGNAL(triggered()),
55                 this, SLOT(setWorkAddress())
56                 );
57     connect(
58                 m_toggleFakeGpsAction, SIGNAL(triggered()),
59                 this, SLOT(toggleFakeGps())
60                 );
61
62     m_centralWidget = new QWidget(m_mainWindow);
63     m_mainWindow->setCentralWidget(m_centralWidget);
64
65     QRadioButton *homeButton = new QRadioButton();
66     homeButton->setObjectName(QString::fromUtf8("homeButton"));
67     homeButton->setText("GPS->HOME");
68     homeButton->setEnabled(false);
69
70     QRadioButton *workButton = new QRadioButton();
71     workButton->setObjectName(QString::fromUtf8("workButton"));
72     workButton->setText("GPS->WORK");
73     workButton->setEnabled(false);
74
75     m_destinationButtons = new QButtonGroup();
76     m_destinationButtons->addButton(homeButton, HomeButtonId);
77     m_destinationButtons->addButton(workButton, WorkButtonId);
78     m_destinationButtons->setExclusive(true);
79
80     m_routeButtons = new QButtonGroup();
81     m_routeButtons->setExclusive(true);
82     m_routeStack = new QVBoxLayout();
83     for (int i=0; i<Ytv::ShowFiveResults; ++i) {
84         QRadioButton *button = new QRadioButton();
85         button->setObjectName("routeButton"+i);
86         button->setEnabled(false);
87
88         m_routeStack->addWidget(button, i);
89         m_routeButtons->addButton(button, i);
90     }
91     m_routeStack->addStretch();
92
93     QStringList headers(QStringList() << "How" << "Time" << "Dist" << "Dep" << "Arr");
94     m_routeDetailTable = new QTableWidget();
95     m_routeDetailTable->setColumnCount(headers.count());
96     m_routeDetailTable->setHorizontalHeaderLabels(headers);
97     m_routeDetailTable->resizeColumnsToContents();
98     m_routeDetailTable->setSelectionMode(QAbstractItemView::NoSelection);
99
100     QHBoxLayout *topLayout = new QHBoxLayout();
101     topLayout->addLayout(m_routeStack);
102     topLayout->addWidget(m_routeDetailTable);
103
104     m_buttonLayout = new QGridLayout();
105     m_buttonLayout->addWidget(homeButton, 0, 0);
106     m_buttonLayout->addWidget(workButton, 0, 1);
107
108     m_mainLayout = new QVBoxLayout();
109     m_mainLayout->addLayout(topLayout);
110     m_mainLayout->addLayout(m_buttonLayout);
111
112     m_centralWidget->setLayout(m_mainLayout);
113 }
114
115 void Ui::setHomeAddress()
116 {
117     setAddress("home");
118 }
119
120 void Ui::setWorkAddress()
121 {
122     setAddress("work");
123 }
124
125 void Ui::toggleFakeGps()
126 {
127     m_usingFakeGps = !m_usingFakeGps;
128
129     if (m_usingFakeGps) {
130         useFakeGps();
131     } else {
132         useLiveGps();
133     }
134 }
135
136 void Ui::useFakeGps()
137 {
138     emit fakeGpsPressed(m_fakeLocationLabel);
139     m_toggleFakeGpsAction->setText("Use Live GPS");
140 }
141
142 void Ui::useLiveGps()
143 {
144     emit liveGpsPressed();
145     m_toggleFakeGpsAction->setText("Use Fake GPS");
146 }
147
148 void Ui::setAddress(const QString &label)
149 {
150     Locations locations;
151     Location *location=locations.location(label);
152
153     bool ok;
154     QString address = QInputDialog::getText(
155                 m_centralWidget,
156                 tr("Enter address for \""+QString(label).toLatin1()+"\""),
157                 tr("Address"),
158                 QLineEdit::Normal,
159                 location->address(),
160                 &ok
161                 );
162
163     if (ok) {
164         qDebug() << "new address" << address;
165         Locations locations;
166         Location  *location  = locations.location(label);
167         qDebug() << "location" << location;
168         if (location) {
169             location->resolveAddress(address);
170         }
171     }
172 }
173
174 void Ui::setBusy(bool busy)
175 {
176     m_mainWindow->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
177 }