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