Added menu option to show/hide messages table.
[ptas] / zouba / 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   menu->addAction(setHomeAddressAction);
44   menu->addAction(setWorkAddressAction);
45   menu->addAction(showMessagesAction);
46
47   connect(
48       setHomeAddressAction, SIGNAL(triggered()),
49       this, SLOT(setHomeAddress())
50       );
51   connect(
52       setWorkAddressAction, SIGNAL(triggered()),
53       this, SLOT(setWorkAddress())
54       );
55   connect(
56       hideMessagesAction, SIGNAL(triggered()),
57       this, SLOT(hideMessages())
58       );
59   connect(
60       showMessagesAction, SIGNAL(triggered()),
61       this, SLOT(showMessages())
62       );
63
64   centralWidget = new QWidget( mainWindow );
65   mainWindow->setCentralWidget(centralWidget);
66
67   QRadioButton *homeButton = new QRadioButton();
68   homeButton->setObjectName( QString::fromUtf8("homeButton") );
69   homeButton->setText( "GPS->HOME" );
70   homeButton->setEnabled(false);
71   homeButton->setFixedSize( QSize( ButtonWidth, ButtonHeight ) );
72
73   QRadioButton *workButton = new QRadioButton();
74   workButton->setObjectName( QString::fromUtf8("workButton") );
75   workButton->setText( "GPS->WORK" );
76   workButton->setEnabled(false);
77
78   destinationButtons = new QButtonGroup();
79   destinationButtons->addButton( homeButton, HomeButtonId );
80   destinationButtons->addButton( workButton, WorkButtonId );
81   destinationButtons->setExclusive( true );
82
83   buttonLayout = new QVBoxLayout();
84   buttonLayout->addWidget( homeButton );
85   buttonLayout->addWidget( workButton );
86   buttonLayout->addStretch();
87
88   routeTable = new QTableWidget( 1, 2 );
89   QStringList columnHeaders;
90   columnHeaders << "Time" << "Bus";
91   routeTable->setHorizontalHeaderLabels( columnHeaders );
92   routeTable->verticalHeader()->hide();
93
94   QHBoxLayout *topLayout = new QHBoxLayout();
95   topLayout->addLayout( buttonLayout );
96   topLayout->addWidget( routeTable );
97
98   messageTable = new MessageTable();
99   messageTable->setObjectName( QString::fromUtf8("messageTable") );
100   messageTable->hide();
101
102   QVBoxLayout *mainLayout = new QVBoxLayout();
103   mainLayout->addLayout( topLayout );
104   mainLayout->addWidget( messageTable );
105
106   centralWidget->setLayout( mainLayout );
107 }
108
109 void Ui::setHomeAddress()
110 {
111   setAddress( "home" );
112 }
113
114 void Ui::setWorkAddress()
115 {
116   setAddress( "work" );
117 }
118
119 void Ui::hideMessages()
120 {
121   messageTable->hide();
122   menu->removeAction( hideMessagesAction );
123   menu->addAction( showMessagesAction );
124 }
125
126 void Ui::showMessages()
127 {
128   messageTable->show();
129   menu->removeAction( showMessagesAction );
130   menu->addAction( hideMessagesAction );
131 }
132
133 void Ui::setAddress( const QString &label )
134 {
135   Locations *locations=Locations::instance();
136   Location *location=locations->location( label );
137
138   bool ok;
139   QString address = QInputDialog::getText(
140      centralWidget,
141      tr("Enter address for \""+QString(label).toLatin1()+"\""),
142      tr("Address"),
143      QLineEdit::Normal,
144      location->address(),
145      &ok
146      );
147
148   qDebug() << "ok=" << ok;
149
150   if ( ok ) {
151     qDebug() << "new address" << address;
152     Locations *locations = Locations::instance();
153     Location  *location  = locations->location( label );
154     qDebug() << "location" << location;
155     if ( location ) {
156       location->resolveAddress( address );
157     }
158   }
159 }