Created new ui for the program. Almost everything that worked previously
[ptas] / zouba / src / ui.cpp
1 #include "ui.h"
2
3 #include "logic/locations.h"
4 #include "logic/ytv.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 #include <QMenu>
22 #include <QPushButton>
23 #ifdef Q_WS_MAEMO_5
24 #include <QMaemo5ValueButton>
25 #include <QMaemo5ListPickSelector>
26 #endif
27 #include <QStandardItemModel>
28
29 UiClass::UiClass() :
30         m_centralWidget(NULL),
31         m_routeStack(NULL),
32         m_locDisp(NULL)
33 {
34 }
35
36 UiClass::~UiClass()
37 {
38     delete m_locDisp;
39 }
40
41 void UiClass::setupUi( QMainWindow *mainWindow )
42 {
43     m_mainWindow = mainWindow;
44 #ifdef Q_WS_MAEMO_5
45     m_mainWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
46 #endif
47     //m_mainWindow->resize(800,480);
48
49     m_locDisp = new LocationsDisplayWindow(mainWindow);
50
51     m_menu = mainWindow->menuBar();
52
53     /*QAction *setHomeAddressAction = new QAction("Set home address", this);
54     QAction *setWorkAddressAction = new QAction("Set work address", this);*/
55     QAction *modifyLocationsAction = new QAction("Modify locations", this);
56     m_UseGpsAction  = new QAction("Use GPS", this);
57     m_UseGpsAction->setCheckable(true);
58     m_UseGpsAction->setChecked(true);
59     connect(this->m_UseGpsAction, SIGNAL(toggled(bool)), this, SLOT(setLocations()));
60     /*m_menu->addAction(setHomeAddressAction);
61     m_menu->addAction(setWorkAddressAction);*/
62     m_menu->addAction(m_UseGpsAction);
63     m_menu->addAction(modifyLocationsAction);
64
65     /*connect(
66             setHomeAddressAction, SIGNAL(triggered()),
67             this, SLOT(setHomeAddress())
68             );
69     connect(
70             setWorkAddressAction, SIGNAL(triggered()),
71             this, SLOT(setWorkAddress())
72             );*/
73
74     connect(modifyLocationsAction, SIGNAL(triggered()), m_locDisp, SLOT(show()));
75
76     Locations* locations = Locations::GetInstance();
77     connect(locations, SIGNAL(locationsChanged()), this, SLOT(setLocations()));
78
79     m_centralWidget = new QWidget( m_mainWindow );
80     m_mainWindow->setCentralWidget( m_centralWidget);
81
82     m_locationsModel = new QStandardItemModel(0,1);
83     this->setLocations();
84
85 #ifdef Q_WS_MAEMO_5
86     m_fromButton = new QMaemo5ValueButton(QString::fromUtf8("From"));
87     m_fromButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
88     QMaemo5ListPickSelector *fromSelector = new QMaemo5ListPickSelector();
89     fromSelector->setModel(m_locationsModel);
90     m_fromButton->setPickSelector(fromSelector);
91
92     m_toButton = new QMaemo5ValueButton(QString::fromUtf8("To"));
93     m_toButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
94     QMaemo5ListPickSelector *toSelector = new QMaemo5ListPickSelector();
95     toSelector->setModel(m_locationsModel);
96     m_toButton->setPickSelector(toSelector);
97 #endif
98
99     m_routeButton = new QPushButton("Route");
100
101     m_routeButtons = new QButtonGroup();
102     m_routeButtons->setExclusive( true );
103     m_routeStack = new QVBoxLayout();
104     for ( int i=0; i<Ytv::ShowFiveResults; ++i ) {
105         QRadioButton *button = new QRadioButton();
106         button->setObjectName( "routeButton"+i );
107         button->setEnabled( false );
108
109         m_routeStack->addWidget( button, i );
110         m_routeButtons->addButton( button, i );
111     }
112     m_routeStack->addStretch();
113
114     QStringList headers( QStringList() << "How" << "Time" << "Dist" << "Dep" << "Arr" );
115     m_routeDetailTable = new QTableWidget();
116     m_routeDetailTable->setColumnCount( headers.count() );
117     m_routeDetailTable->setHorizontalHeaderLabels( headers );
118     m_routeDetailTable->resizeColumnsToContents();
119     m_routeDetailTable->setSelectionMode( QAbstractItemView::NoSelection );
120
121     QHBoxLayout *topLayout = new QHBoxLayout();
122     topLayout->addLayout( m_routeStack );
123     topLayout->addWidget( m_routeDetailTable );
124
125     m_buttonLayout = new QGridLayout();
126 #ifdef Q_WS_MAEMO_5
127     m_buttonLayout->addWidget(m_fromButton, 0, 0);
128     m_buttonLayout->addWidget(m_toButton, 0, 1);
129 #endif
130     m_buttonLayout->addWidget(m_routeButton, 0, 2);
131
132     m_mainLayout = new QVBoxLayout();
133     m_mainLayout->addLayout( topLayout );
134     m_mainLayout->addLayout( m_buttonLayout );
135
136     m_centralWidget->setLayout( m_mainLayout );
137 }
138
139 void UiClass::setLocations()
140 {
141     qDebug() << "Setting locations for main menu selectors.";
142     Locations *locations = Locations::GetInstance();
143
144     m_locationsModel->clear();
145     QStandardItem *item;
146     if (this->m_UseGpsAction->isChecked())
147     {
148         item = new QStandardItem(QString("GPS"));
149         item->setTextAlignment(Qt::AlignCenter);
150         item->setEditable(false);
151         m_locationsModel->appendRow(item);
152     }
153
154     for (int index = 1; index <= locations->size(); ++index)
155     {
156         item = new QStandardItem(locations->getLocation(index)->label());
157         item->setTextAlignment(Qt::AlignCenter);
158         item->setEditable(false);
159         m_locationsModel->appendRow(item);
160     }
161 }
162
163 void UiClass::setHomeAddress()
164 {
165     setAddress( "home" );
166 }
167
168 void UiClass::setWorkAddress()
169 {
170     setAddress( "work" );
171 }
172
173 void UiClass::setAddress( const QString &label )
174 {
175     /*Locations locations;
176     Location *location=locations.location( label );
177
178     bool ok;
179     QString address = QInputDialog::getText(
180             m_centralWidget,
181             tr("Enter address for \""+QString(label).toLatin1()+"\""),
182             tr("Address"),
183             QLineEdit::Normal,
184             location->address(),
185             &ok
186             );
187
188     if ( ok ) {
189         qDebug() << "new address" << address;
190         Locations locations;
191         Location  *location  = locations.location( label );
192         qDebug() << "location" << location;
193         if ( location ) {
194             //location->resolveAddress( address );
195         }
196     }*/
197 }
198
199 /*void Ui::modifyLocations()
200 {
201     LocationsDisplay
202 }*/
203
204 void UiClass::setBusy( bool busy )
205 {
206 #ifdef Q_WS_MAEMO_5
207     m_mainWindow->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
208 #endif
209 }