Modified MainWindow auto centering toggling.
[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
77 MainWindow::~MainWindow()
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80     delete m_toListViewAct;
81     delete m_toMapViewAct;
82     delete m_toSettingsAct;
83     delete m_situareViews;
84 }
85
86 void MainWindow::toggleProgressIndicator(bool value)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89 #ifdef Q_WS_MAEMO_5
90     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
91 #else
92     Q_UNUSED(value);
93 #endif // Q_WS_MAEMO_5
94 }
95
96 void MainWindow::createMenus()
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99     m_toListViewAct = new QAction(tr("List"), this);
100     m_toListViewAct->setObjectName(tr("List"));
101     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
102     m_toMapViewAct = new QAction(tr("Map"), this);
103     m_toMapViewAct->setObjectName(tr("Map"));
104     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
105     m_toSettingsAct = new QAction(tr("Settings"), this);
106     m_toSettingsAct->setObjectName(tr("Settings"));
107     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
108     m_gpsToggleAct = new QAction(tr("GPS enabled"), this);
109     m_gpsToggleAct->setCheckable(true);
110     m_gpsToggleAct->setChecked(true);
111     connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsActionToggled(bool)));
112     m_autoCenteringAct = new QAction(tr("Auto centering enabled"), this);
113     m_autoCenteringAct->setCheckable(true);
114     m_autoCenteringAct->setChecked(true);
115     connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));
116     m_viewMenu = menuBar()->addMenu(tr("View"));
117     m_viewMenu->addAction(m_toListViewAct);
118     m_viewMenu->addAction(m_toMapViewAct);
119     m_viewMenu->addAction(m_toSettingsAct);
120     m_viewMenu->addAction(m_gpsToggleAct);
121     m_viewMenu->addAction(m_autoCenteringAct);
122     m_viewMenu->setObjectName(tr("View Menu"));
123 }
124
125 void MainWindow::createViews()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128     m_listViewScreen = new ListViewScreen(this);
129     m_mapViewScreen = new MapViewScreen(this);
130
131     m_situareViews = new QStackedWidget;
132     m_situareViews->addWidget(m_listViewScreen);
133     m_situareViews->addWidget(m_mapViewScreen);
134 }
135
136 void MainWindow::toListView()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139     switchView(0);
140 }
141
142 void MainWindow::toMapView()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145     switchView(1);
146 }
147
148 void MainWindow::switchView(int nextIndex)
149 {
150     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
151     if (nextIndex < 0 || nextIndex > 1) {
152         qDebug() << "Illegal parameter value in MainWindow::switchView";
153         return;
154     }
155     m_situareViews->setCurrentIndex(nextIndex);
156     switch (m_situareViews->currentIndex()) {
157         case 0:
158             setWindowTitle(tr("List view"));
159             break;
160         case 1:
161             setWindowTitle(tr("Map view"));
162             break;
163         default:
164             qDebug() << "Illegal switch value in MainWindow::switchView";
165             break;
166     }
167 }
168
169 void MainWindow::openLocationUpdateDialog()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     emit requestReverseGeo();
174     m_locationDialog->exec();
175 }
176
177 void MainWindow::openSettingsDialog()
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180     SettingsDialog *dialog = new SettingsDialog(this);
181     dialog->show();
182 }
183
184 void MainWindow::gpsActionToggled(bool checked)
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     if (checked) {
189         emit enableGPS(true);
190         m_gpsToggleAct->setText(tr("GPS enabled"));
191         showMaemoInformationBox(tr("GPS enabled"));
192         m_autoCenteringAct->setVisible(true);
193     }
194     else {
195         emit enableGPS(false);
196         m_gpsToggleAct->setText(tr("GPS disabled"));
197         showMaemoInformationBox(tr("GPS disabled"));
198         m_autoCenteringAct->setVisible(false);
199
200     }
201 }
202
203 void MainWindow::gpsTimeout()
204 {
205     qDebug() << __PRETTY_FUNCTION__;
206
207     showMaemoInformationBox(tr("GPS timeout"));
208 }
209
210 void MainWindow::gpsError(const QString &message)
211 {
212     qDebug() << __PRETTY_FUNCTION__;
213
214     showMaemoInformationBox(message);
215 }
216
217 void MainWindow::autoCenteringToggled(bool checked)
218 {
219     qDebug() << __PRETTY_FUNCTION__ << " " << checked;
220
221     if (checked) {
222         emit enableAutoCentering(true);
223         m_autoCenteringAct->setText(tr("Auto centering enabled"));
224         showMaemoInformationBox(tr("Auto centering enabled"));
225     }
226     else {
227         emit enableAutoCentering(false);
228         m_autoCenteringAct->setText(tr("Auto centering disabled"));
229         showMaemoInformationBox(tr("Auto centering disabled"));
230     }
231 }
232
233 void MainWindow::showMaemoInformationBox(const QString &message)
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237 #ifdef Q_WS_MAEMO_5
238         QMaemo5InformationBox::information(this, message,
239                                            QMaemo5InformationBox::DefaultTimeout);
240 #endif // Q_WS_MAEMO_5
241 }