Merge branch 'master' into gps
[situare] / src / ui / mainwindow.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6       Kaj Wallin - kaj.wallin@ixonos.com
7       Jussi Laitinen jussi.laitinen@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include <QtGui>
25
26 #ifdef Q_WS_MAEMO_5
27 #include <QtMaemo5/QMaemo5InformationBox>
28 #endif // Q_WS_MAEMO_5
29
30 #include "mainwindow.h"
31 #include "listviewscreen.h"
32 #include "mapviewscreen.h"
33 #include "settingsdialog.h"
34 #include "facebookservice/facebookauthentication.h"
35 #include "situareservice/situareservice.h"
36
37 MainWindow::MainWindow(QWidget *parent)
38     : QMainWindow(parent)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     createViews();
43     setCentralWidget(m_situareViews);
44     createMenus();
45     setWindowTitle(tr("List view"));
46     this->hide();
47
48     m_locationDialog = new UpdateLocationDialog(this);
49     connect(m_listViewScreen,SIGNAL(launchUpdateDialog()),
50             this,SLOT(openLocationUpdateDialog()));
51
52     connect(this, SIGNAL(reverseGeoReady(QString)),
53             m_locationDialog, SLOT(setAddress(QString)));
54     connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
55             SIGNAL(statusUpdate(QString,bool)));
56
57     connect(this, SIGNAL(userLocationReady(User*)),
58             m_mapViewScreen, SIGNAL(userLocationReady(User*)));
59     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
60             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
61
62     connect(this, SIGNAL(userLocationReady(User*)),
63             m_listViewScreen, SLOT(userDataReceived(User*)));
64     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
65             m_listViewScreen, SLOT(friendInfoReceived(QList<User*>&)));
66     connect(m_listViewScreen, SIGNAL(updateFriendsData()),
67             this, SIGNAL(refreshUserData()));
68
69     connect(this, SIGNAL(enableAutoCentering(bool)),
70             m_mapViewScreen, SLOT(enableAutoCentering(bool)));
71     connect(this, SIGNAL(positionReceived(QPointF)),
72             m_mapViewScreen, SLOT(positionReceived(QPointF)));
73
74     this->toggleProgressIndicator(true);
75
76     //Debug, use settings instead
77     autoCenteringToggled(true);
78 }
79
80 MainWindow::~MainWindow()
81 {
82     qDebug() << __PRETTY_FUNCTION__;
83     delete m_toListViewAct;
84     delete m_toMapViewAct;
85     delete m_toSettingsAct;
86     delete m_situareViews;
87 }
88
89 void MainWindow::toggleProgressIndicator(bool value)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92 #ifdef Q_WS_MAEMO_5
93     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
94 #else
95     Q_UNUSED(value);
96 #endif // Q_WS_MAEMO_5
97 }
98
99 void MainWindow::createMenus()
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102     m_toListViewAct = new QAction(tr("List"), this);
103     m_toListViewAct->setObjectName(tr("List"));
104     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
105     m_toMapViewAct = new QAction(tr("Map"), this);
106     m_toMapViewAct->setObjectName(tr("Map"));
107     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
108     m_toSettingsAct = new QAction(tr("Settings"), this);
109     m_toSettingsAct->setObjectName(tr("Settings"));
110     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
111     m_gpsToggleAct = new QAction(tr("GPS enabled"), this);
112     m_gpsToggleAct->setCheckable(true);
113     m_gpsToggleAct->setChecked(true);
114     connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsActionToggled(bool)));
115     m_autoCenteringAct = new QAction(tr("Auto centering enabled"), this);
116     m_autoCenteringAct->setCheckable(true);
117     m_autoCenteringAct->setChecked(true);
118     connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));
119     m_viewMenu = menuBar()->addMenu(tr("View"));
120     m_viewMenu->addAction(m_toListViewAct);
121     m_viewMenu->addAction(m_toMapViewAct);
122     m_viewMenu->addAction(m_toSettingsAct);
123     m_viewMenu->addAction(m_gpsToggleAct);
124     m_viewMenu->addAction(m_autoCenteringAct);
125     m_viewMenu->setObjectName(tr("View Menu"));
126 }
127
128 void MainWindow::createViews()
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131     m_listViewScreen = new ListViewScreen(this);
132     m_mapViewScreen = new MapViewScreen(this);
133
134     m_situareViews = new QStackedWidget;
135     m_situareViews->addWidget(m_listViewScreen);
136     m_situareViews->addWidget(m_mapViewScreen);
137 }
138
139 void MainWindow::toListView()
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142     switchView(0);
143 }
144
145 void MainWindow::toMapView()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148     switchView(1);
149 }
150
151 void MainWindow::switchView(int nextIndex)
152 {
153     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
154     if (nextIndex < 0 || nextIndex > 1) {
155         qDebug() << "Illegal parameter value in MainWindow::switchView";
156         return;
157     }
158     m_situareViews->setCurrentIndex(nextIndex);
159     switch (m_situareViews->currentIndex()) {
160         case 0:
161             setWindowTitle(tr("List view"));
162             break;
163         case 1:
164             setWindowTitle(tr("Map view"));
165             break;
166         default:
167             qDebug() << "Illegal switch value in MainWindow::switchView";
168             break;
169     }
170 }
171
172 void MainWindow::openLocationUpdateDialog()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     emit requestReverseGeo();
177     m_locationDialog->exec();
178 }
179
180 void MainWindow::openSettingsDialog()
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183     SettingsDialog *dialog = new SettingsDialog(this);
184     dialog->show();
185 }
186
187 void MainWindow::gpsActionToggled(bool checked)
188 {
189     qDebug() << __PRETTY_FUNCTION__;
190
191     if (checked) {
192         emit enableGPS(true);
193         m_gpsToggleAct->setText(tr("GPS enabled"));
194         showMaemoInformationBox(tr("GPS enabled"));
195         m_autoCenteringAct->setEnabled(true);
196     }
197     else {
198         emit enableGPS(false);
199         m_gpsToggleAct->setText(tr("GPS disabled"));
200         showMaemoInformationBox(tr("GPS disabled"));
201         m_autoCenteringAct->setEnabled(false);
202     }
203 }
204
205 void MainWindow::gpsTimeout()
206 {
207     qDebug() << __PRETTY_FUNCTION__;
208
209     showMaemoInformationBox(tr("GPS timeout"));
210 }
211
212 void MainWindow::gpsError(const QString &message)
213 {
214     qDebug() << __PRETTY_FUNCTION__;
215
216     showMaemoInformationBox(message);
217 }
218
219 void MainWindow::autoCenteringToggled(bool checked)
220 {
221     qDebug() << __PRETTY_FUNCTION__ << " " << checked;
222
223     if (checked) {
224         emit enableAutoCentering(true);
225         m_autoCenteringAct->setText(tr("Auto centering enabled"));
226         showMaemoInformationBox(tr("Auto centering enabled"));
227     }
228     else {
229         emit enableAutoCentering(false);
230         m_autoCenteringAct->setText(tr("Auto centering disabled"));
231         showMaemoInformationBox(tr("Auto centering disabled"));
232     }
233 }
234
235 void MainWindow::showMaemoInformationBox(const QString &message)
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239 #ifdef Q_WS_MAEMO_5
240         QMaemo5InformationBox::information(this, message,
241                                            QMaemo5InformationBox::DefaultTimeout);
242 #endif // Q_WS_MAEMO_5
243 }