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