Added GPSPositionInterface and GPSPositionMockup classes.
[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 "mapviewscreen.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
34 #include "situareservice/situareservice.h"
35
36 MainWindow::MainWindow(QWidget *parent)
37     : QMainWindow(parent)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_mapViewScreen = new MapViewScreen(this);
42     setCentralWidget(m_mapViewScreen);
43     createMenus();
44     setWindowTitle(tr("Situare"));
45     hide();
46
47     m_locationDialog = new UpdateLocationDialog(this);
48
49     connect(this, SIGNAL(reverseGeoReady(QString)),
50             m_locationDialog, SLOT(setAddress(QString)));
51     connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
52             SIGNAL(statusUpdate(QString,bool)));
53
54     connect(this, SIGNAL(userLocationReady(User*)),
55             m_mapViewScreen, SIGNAL(userLocationReady(User*)));
56     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
57             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
58
59         connect(this, SIGNAL(enableAutoCentering(bool)),
60             m_mapViewScreen, SLOT(enableAutoCentering(bool)));
61     connect(this, SIGNAL(positionReceived(QPointF)),
62             m_mapViewScreen, SLOT(positionReceived(QPointF)));
63
64     this->toggleProgressIndicator(true);
65 }
66
67 void MainWindow::toggleProgressIndicator(bool value)
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70 #ifdef Q_WS_MAEMO_5
71     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
72 #else
73     Q_UNUSED(value);
74 #endif // Q_WS_MAEMO_5
75 }
76
77 void MainWindow::createMenus()
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80     m_toSettingsAct = new QAction(tr("Settings"), this);
81     m_toSettingsAct->setObjectName(tr("Settings"));
82     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
83         m_gpsToggleAct = new QAction(tr("GPS enabled"), this);
84     m_gpsToggleAct->setCheckable(true);
85     m_gpsToggleAct->setChecked(true);
86     connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsActionToggled(bool)));
87     m_autoCenteringAct = new QAction(tr("Auto centering enabled"), this);
88     m_autoCenteringAct->setCheckable(true);
89     m_autoCenteringAct->setChecked(true);
90     connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));    
91         
92         m_viewMenu = menuBar()->addMenu(tr("Main"));
93
94     m_viewMenu->addAction(m_toSettingsAct);
95         m_viewMenu->addAction(m_gpsToggleAct);
96     m_viewMenu->addAction(m_autoCenteringAct);
97     m_viewMenu->setObjectName(tr("Menu"));
98 }
99
100 void MainWindow::openLocationUpdateDialog()
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     emit requestReverseGeo();
105     m_locationDialog->exec();
106 }
107
108 void MainWindow::openSettingsDialog()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111     SettingsDialog *dialog = new SettingsDialog(this);
112     dialog->show();
113 }
114
115 void MainWindow::gpsActionToggled(bool checked)
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     if (checked) {
120         emit enableGPS(true);
121         m_gpsToggleAct->setText(tr("GPS enabled"));
122         showMaemoInformationBox(tr("GPS enabled"));
123         m_autoCenteringAct->setVisible(true);
124     }
125     else {
126         emit enableGPS(false);
127         m_gpsToggleAct->setText(tr("GPS disabled"));
128         showMaemoInformationBox(tr("GPS disabled"));
129         m_autoCenteringAct->setVisible(false);
130
131     }
132 }
133
134 void MainWindow::gpsTimeout()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     showMaemoInformationBox(tr("GPS timeout"));
139 }
140
141 void MainWindow::gpsError(const QString &message)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     showMaemoInformationBox(message);
146 }
147
148 void MainWindow::autoCenteringToggled(bool checked)
149 {
150     qDebug() << __PRETTY_FUNCTION__ << " " << checked;
151
152     if (checked) {
153         emit enableAutoCentering(true);
154         m_autoCenteringAct->setText(tr("Auto centering enabled"));
155         showMaemoInformationBox(tr("Auto centering enabled"));
156     }
157     else {
158         emit enableAutoCentering(false);
159         m_autoCenteringAct->setText(tr("Auto centering disabled"));
160         showMaemoInformationBox(tr("Auto centering disabled"));
161     }
162 }
163
164 void MainWindow::showMaemoInformationBox(const QString &message)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168 #ifdef Q_WS_MAEMO_5
169     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
170 #else
171     Q_UNUSED(message);
172 #endif
173 }