Changes: layout change ready for multiple routes; change table selection modes
[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   fakeLocation()
31 {
32   Locations *locations = Locations::instance();
33   Location *workLocation = locations->location( "work" );
34   fakeLocation = new Location();
35   *fakeLocation = *workLocation;
36   fakeLocation->setLabel( "fakegps" );
37 }
38
39 Ui::~Ui()
40 {
41 }
42
43 void Ui::setupUi( QMainWindow *mainWindow )
44 {
45   mainWindow->resize(800,480);
46   menu = mainWindow->menuBar()->addMenu("Settings");
47
48   QAction *setHomeAddressAction = new QAction("Set home address", this);
49   QAction *setWorkAddressAction = new QAction("Set work address", this);
50   toggleMessagesAction = new QAction("Show messages", this);
51   toggleFakeGpsAction  = new QAction("Use fake GPS", this);
52   menu->addAction(setHomeAddressAction);
53   menu->addAction(setWorkAddressAction);
54   menu->addAction(toggleMessagesAction);
55   menu->addAction(toggleFakeGpsAction);
56
57   connect(
58       setHomeAddressAction, SIGNAL(triggered()),
59       this, SLOT(setHomeAddress())
60       );
61   connect(
62       setWorkAddressAction, SIGNAL(triggered()),
63       this, SLOT(setWorkAddress())
64       );
65   connect(
66       toggleMessagesAction, SIGNAL(triggered()),
67       this, SLOT(toggleMessages())
68       );
69   connect(
70       toggleFakeGpsAction, SIGNAL(triggered()),
71       this, SLOT(toggleFakeGps())
72       );
73
74   centralWidget = new QWidget( mainWindow );
75   mainWindow->setCentralWidget(centralWidget);
76
77   QRadioButton *homeButton = new QRadioButton();
78   homeButton->setObjectName( QString::fromUtf8("homeButton") );
79   homeButton->setText( "GPS->HOME" );
80   homeButton->setEnabled(false);
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   routeTable = new QTableWidget( 1, 2 );
93   QStringList columnHeaders;
94   columnHeaders << "Time" << "Bus";
95   routeTable->setHorizontalHeaderLabels( columnHeaders );
96   routeTable->verticalHeader()->hide();
97   routeTable->setSelectionMode( QAbstractItemView::SingleSelection );
98
99   QHBoxLayout *topLayout = new QHBoxLayout();
100   topLayout->addWidget( routeTable );
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( 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 }