Changes: parses comprehensive route data
[ptas] / zouba / src / ui.cpp
1 #include "ui.h"
2
3 #include "messagetable.h"
4 #include "locations.h"
5 #include "ytv.h"
6
7 #include <QMainWindow>
8 #include <QRadioButton>
9 #include <QTableWidget>
10 #include <QString>
11 #include <QRect>
12 #include <QButtonGroup>
13 #include <QHeaderView>
14 #include <QObject>
15 #include <QMenuBar>
16 #include <QHBoxLayout>
17 #include <QVBoxLayout>
18 #include <QGridLayout>
19 #include <QSizePolicy>
20 #include <QInputDialog>
21 #include <QDebug>
22
23 MessageTable *Ui::messageTable = 0;
24
25 Ui::Ui() :
26   centralWidget(0),
27   destinationButtons(0),
28   routeStack(0),
29   usingFakeGps( false ),
30   messagesShown( false ),
31   fakeLocationLabel( "work" )
32 {
33 }
34
35 Ui::~Ui()
36 {
37 }
38
39 void Ui::setupUi( QMainWindow *mainWindow )
40 {
41   mainWindow->resize(800,480);
42   menu = mainWindow->menuBar()->addMenu("Settings");
43
44   QAction *setHomeAddressAction = new QAction("Set home address", this);
45   QAction *setWorkAddressAction = new QAction("Set work address", this);
46   toggleMessagesAction = new QAction("Show messages", this);
47   toggleFakeGpsAction  = new QAction("Use fake GPS", this);
48   menu->addAction(setHomeAddressAction);
49   menu->addAction(setWorkAddressAction);
50   menu->addAction(toggleMessagesAction);
51   menu->addAction(toggleFakeGpsAction);
52
53   connect(
54       setHomeAddressAction, SIGNAL(triggered()),
55       this, SLOT(setHomeAddress())
56       );
57   connect(
58       setWorkAddressAction, SIGNAL(triggered()),
59       this, SLOT(setWorkAddress())
60       );
61   connect(
62       toggleMessagesAction, SIGNAL(triggered()),
63       this, SLOT(toggleMessages())
64       );
65   connect(
66       toggleFakeGpsAction, SIGNAL(triggered()),
67       this, SLOT(toggleFakeGps())
68       );
69
70   centralWidget = new QWidget( mainWindow );
71   mainWindow->setCentralWidget(centralWidget);
72
73   QRadioButton *homeButton = new QRadioButton();
74   homeButton->setObjectName( QString::fromUtf8("homeButton") );
75   homeButton->setText( "GPS->HOME" );
76   homeButton->setEnabled(false);
77
78   QRadioButton *workButton = new QRadioButton();
79   workButton->setObjectName( QString::fromUtf8("workButton") );
80   workButton->setText( "GPS->WORK" );
81   workButton->setEnabled(false);
82
83   destinationButtons = new QButtonGroup();
84   destinationButtons->addButton( homeButton, HomeButtonId );
85   destinationButtons->addButton( workButton, WorkButtonId );
86   destinationButtons->setExclusive( true );
87
88   routeStack = new QVBoxLayout();
89   for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
90     QRadioButton *button = new QRadioButton();
91     button->setObjectName( "routeButton"+i );
92     button->setEnabled( false );
93
94     routeStack->addWidget( button, i );
95   }
96   routeStack->addStretch();
97
98   QHBoxLayout *topLayout = new QHBoxLayout();
99   topLayout->addLayout( routeStack );
100   topLayout->addStretch();
101
102   buttonLayout = new QGridLayout();
103   buttonLayout->addWidget( homeButton, 0, 0 );
104   buttonLayout->addWidget( workButton, 0, 1 );
105
106   messageTable = new MessageTable();
107   messageTable->setObjectName( QString::fromUtf8("messageTable") );
108   messageTable->hide();
109
110   QVBoxLayout *mainLayout = new QVBoxLayout();
111   mainLayout->addLayout( topLayout );
112   mainLayout->addWidget( messageTable );
113   mainLayout->addLayout( buttonLayout );
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( fakeLocationLabel );
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 }