Changed package description to indicate Helsinki only
[ptas] / zouba / src / ui.cpp
1 #include "ui.h"
2
3 #include "messagetable.h"
4 #include "locations.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 <QSizePolicy>
18 #include <QInputDialog>
19 #include <QDebug>
20
21 MessageTable *Ui::messageTable = 0;
22
23 Ui::Ui() :
24   centralWidget(0),
25   destinationButtons(0),
26   routeTable(0),
27   usingFakeGps( false ),
28   messagesShown( false ),
29   fakeLocation()
30 {
31   Locations *locations = Locations::instance();
32   Location *workLocation = locations->location( "work" );
33   fakeLocation = new Location();
34   *fakeLocation = *workLocation;
35   fakeLocation->setLabel( "fakegps" );
36 }
37
38 Ui::~Ui()
39 {
40 }
41
42 void Ui::setupUi( QMainWindow *mainWindow )
43 {
44   mainWindow->resize(800,480);
45   menu = mainWindow->menuBar()->addMenu("Settings");
46
47   QAction *setHomeAddressAction = new QAction("Set home address", this);
48   QAction *setWorkAddressAction = new QAction("Set work address", this);
49   toggleMessagesAction = new QAction("Show messages", this);
50   toggleFakeGpsAction  = new QAction("Use fake GPS", this);
51   menu->addAction(setHomeAddressAction);
52   menu->addAction(setWorkAddressAction);
53   menu->addAction(toggleMessagesAction);
54   menu->addAction(toggleFakeGpsAction);
55
56   connect(
57       setHomeAddressAction, SIGNAL(triggered()),
58       this, SLOT(setHomeAddress())
59       );
60   connect(
61       setWorkAddressAction, SIGNAL(triggered()),
62       this, SLOT(setWorkAddress())
63       );
64   connect(
65       toggleMessagesAction, SIGNAL(triggered()),
66       this, SLOT(toggleMessages())
67       );
68   connect(
69       toggleFakeGpsAction, SIGNAL(triggered()),
70       this, SLOT(toggleFakeGps())
71       );
72
73   centralWidget = new QWidget( mainWindow );
74   mainWindow->setCentralWidget(centralWidget);
75
76   QRadioButton *homeButton = new QRadioButton();
77   homeButton->setObjectName( QString::fromUtf8("homeButton") );
78   homeButton->setText( "GPS->HOME" );
79   homeButton->setEnabled(false);
80   homeButton->setFixedSize( QSize( ButtonWidth, ButtonHeight ) );
81
82   QRadioButton *workButton = new QRadioButton();
83   workButton->setObjectName( QString::fromUtf8("workButton") );
84   workButton->setText( "GPS->WORK" );
85   workButton->setEnabled(false);
86
87   destinationButtons = new QButtonGroup();
88   destinationButtons->addButton( homeButton, HomeButtonId );
89   destinationButtons->addButton( workButton, WorkButtonId );
90   destinationButtons->setExclusive( true );
91
92   buttonLayout = new QVBoxLayout();
93   buttonLayout->addWidget( homeButton );
94   buttonLayout->addWidget( workButton );
95   buttonLayout->addStretch();
96
97   routeTable = new QTableWidget( 1, 2 );
98   QStringList columnHeaders;
99   columnHeaders << "Time" << "Bus";
100   routeTable->setHorizontalHeaderLabels( columnHeaders );
101   routeTable->verticalHeader()->hide();
102
103   QHBoxLayout *topLayout = new QHBoxLayout();
104   topLayout->addLayout( buttonLayout );
105   topLayout->addWidget( routeTable );
106
107   messageTable = new MessageTable();
108   messageTable->setObjectName( QString::fromUtf8("messageTable") );
109   messageTable->hide();
110
111   QVBoxLayout *mainLayout = new QVBoxLayout();
112   mainLayout->addLayout( topLayout );
113   mainLayout->addWidget( messageTable );
114
115   centralWidget->setLayout( mainLayout );
116 }
117
118 void Ui::setHomeAddress()
119 {
120   setAddress( "home" );
121 }
122
123 void Ui::setWorkAddress()
124 {
125   setAddress( "work" );
126 }
127
128 void Ui::toggleMessages()
129 {
130   messagesShown = !messagesShown;
131
132   if ( messagesShown ) {
133     showMessages();
134   } else {
135     hideMessages();
136   }
137 }
138
139 void Ui::hideMessages()
140 {
141   messageTable->hide();
142   toggleMessagesAction->setText( "Show messages" );
143 }
144
145 void Ui::showMessages()
146 {
147   messageTable->show();
148   toggleMessagesAction->setText( "Hide messages" );
149 }
150
151 void Ui::toggleFakeGps()
152 {
153   usingFakeGps = !usingFakeGps;
154
155   if ( usingFakeGps ) {
156     useFakeGps();
157   } else {
158     useLiveGps();
159   }
160 }
161
162 void Ui::useFakeGps()
163 {
164   emit fakeGpsPressed( fakeLocation );
165   toggleFakeGpsAction->setText( "Use Live GPS" );
166 }
167
168 void Ui::useLiveGps()
169 {
170   emit liveGpsPressed();
171   toggleFakeGpsAction->setText( "Use Fake GPS" );
172 }
173
174 void Ui::setAddress( const QString &label )
175 {
176   Locations *locations=Locations::instance();
177   Location *location=locations->location( label );
178
179   bool ok;
180   QString address = QInputDialog::getText(
181      centralWidget,
182      tr("Enter address for \""+QString(label).toLatin1()+"\""),
183      tr("Address"),
184      QLineEdit::Normal,
185      location->address(),
186      &ok
187      );
188
189   if ( ok ) {
190     qDebug() << "new address" << address;
191     Locations *locations = Locations::instance();
192     Location  *location  = locations->location( label );
193     qDebug() << "location" << location;
194     if ( location ) {
195       location->resolveAddress( address );
196     }
197   }
198 }