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