Default screen size, init order, map not drawing all tiles
[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       Sami Rämö - sami.ramo@ixonos.com
9
10    Situare is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as published by the Free Software Foundation.
13
14    Situare is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Situare; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22    USA.
23 */
24
25 #include <QtGui>
26
27 #ifdef Q_WS_MAEMO_5
28 #include <QtMaemo5/QMaemo5InformationBox>
29 #endif // Q_WS_MAEMO_5
30
31 #include "mainwindow.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
34 #include "common.h"
35
36 #include "friendlistpanel.h"
37 #include "map/mapview.h"
38 #include "panelsidebar.h"
39 #include "userpanel.h"
40 #include "zoombuttonpanel.h"
41
42 #include <QtGui/QX11Info>
43 #include <X11/Xlib.h>
44 #include <X11/Xatom.h>
45
46 // values for setting screen size in desktop matching N900 screen size
47 const int N900_APP_WIDTH = 800;
48 const int N900_APP_HEIGHT = 449;
49
50 MainWindow::MainWindow(QWidget *parent)
51     : QMainWindow(parent),
52     m_drawOwnLocationCrosshair(false),
53     m_refresh(false),
54     m_email(),    
55     m_password(),
56     m_loginUrl(),
57     m_webView(0)
58 {
59     qDebug() << __PRETTY_FUNCTION__;
60
61     buildMap();
62
63     // build main layout
64     QHBoxLayout *layout = new QHBoxLayout;
65     layout->addWidget(m_mapView);
66     layout->setMargin(0);
67     setCentralWidget(new QWidget());
68     centralWidget()->setLayout(layout);
69
70     buildFriendListPanel();
71     buildUserInfoPanel();
72
73     createMenus();
74     setWindowTitle(tr("Situare"));
75
76     m_locationDialog = new UpdateLocationDialog(this);
77
78     // set stacking order of widgets
79     m_zoomButtonPanel->stackUnder(m_userPanel);
80     m_osmLicense->stackUnder(m_zoomButtonPanel);
81     m_ownLocationCrosshair->stackUnder(m_osmLicense);
82     m_mapView->stackUnder(m_ownLocationCrosshair);
83
84     this->toggleProgressIndicator(true);
85
86     grabZoomKeys(true);
87
88     // set screen size in desktop matching N900 screen size
89     resize(N900_APP_WIDTH, N900_APP_HEIGHT);
90 }
91
92 MainWindow::~MainWindow()
93 {
94     qDebug() << __PRETTY_FUNCTION__;
95
96     grabZoomKeys(false);
97
98     if(m_webView)
99         delete m_webView;
100 }
101
102 void MainWindow::buildFriendListPanel()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     m_friendsListPanel = new FriendListPanel(this);
107     PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
108
109     m_friendsListPanel->stackUnder(friendsListPanelSidebar);
110
111     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
112             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
113
114     connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
115             this, SIGNAL(findFriend(QPointF)));
116
117     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
118             m_friendsListPanel, SLOT(reDrawFriendsPanel(int, int)));
119
120     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
121             friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
122 }
123
124 void MainWindow::buildManualLocationCrosshair()
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     m_ownLocationCrosshair = new QLabel(this);
129     QPixmap crosshairImage(":/res/images/sight.png");
130     m_ownLocationCrosshair->setPixmap(crosshairImage);
131     m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
132
133     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
134             this, SLOT(drawOwnLocationCrosshair(int, int)));
135 }
136
137 void MainWindow::buildMap()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     m_mapView = new MapView(this);
142
143     buildZoomButtonPanel();
144
145     m_ownLocationCrosshair = 0;
146     buildOsmLicense();
147     buildManualLocationCrosshair();
148
149     connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
150             this, SIGNAL(mapViewScrolled(QPoint)));
151
152     connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
153             m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
154
155     connect(m_mapView, SIGNAL(viewResized(QSize)),
156             this, SIGNAL(mapViewResized(QSize)));
157
158     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
159              this, SLOT(setViewPortSize(int, int)));
160
161     connect(this, SIGNAL(zoomLevelChanged(int)),
162             m_mapView, SLOT(setZoomLevel(int)));
163
164     connect(m_mapView, SIGNAL(viewZoomFinished()),
165             this, SIGNAL(viewZoomFinished()));
166 }
167
168 void MainWindow::buildOsmLicense()
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     m_osmLicense = new QLabel(this);
173     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
174     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
175     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
176     m_osmLicense->setFont(QFont("Nokia Sans", 9));
177     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
178                          m_osmLicense->fontMetrics().height());
179
180     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
181             this, SLOT(drawOsmLicense(int, int)));
182 }
183
184 void MainWindow::buildUserInfoPanel()
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     m_userPanel = new UserInfoPanel(this);
189     PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
190
191     userPanelSidebar->stackUnder(m_friendsListPanel);
192     m_userPanel->stackUnder(userPanelSidebar);
193
194     connect(this, SIGNAL(userLocationReady(User*)),
195             m_userPanel, SLOT(userDataReceived(User*)));
196
197     connect(m_userPanel, SIGNAL(requestReverseGeo()),
198             this, SIGNAL(requestReverseGeo()));
199
200     connect(this, SIGNAL(reverseGeoReady(QString)),
201             m_userPanel, SIGNAL(reverseGeoReady(QString)));
202
203     connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
204             this, SIGNAL(statusUpdate(QString,bool)));
205
206     connect(m_userPanel, SIGNAL(refreshUserData()),
207             this, SIGNAL(refreshUserData()));
208
209     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
210             m_userPanel, SLOT(reDrawUserPanel(int, int)));
211 }
212
213 void MainWindow::buildZoomButtonPanel()
214 {
215     qDebug() << __PRETTY_FUNCTION__;
216
217     m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
218                                             ZOOM_BUTTON_PANEL_POSITION_Y);
219
220     connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
221             this, SIGNAL(zoomIn()));
222
223     connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
224             this, SIGNAL(zoomOut()));
225
226     connect(this, SIGNAL(zoomLevelChanged(int)),
227             m_zoomButtonPanel, SLOT(resetButtons()));
228
229     connect(this, SIGNAL(maxZoomLevelReached()),
230             m_zoomButtonPanel, SLOT(disableZoomInButton()));
231
232     connect(this, SIGNAL(minZoomLevelReached()),
233             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
234 }
235
236 void MainWindow::createMenus()
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     // settings
241     m_toSettingsAct = new QAction(tr("Settings"), this);
242     m_toSettingsAct->setObjectName(tr("Settings"));
243     connect(m_toSettingsAct, SIGNAL(triggered()),
244         this, SLOT(openSettingsDialog()));
245
246     // GPS
247     m_gpsToggleAct = new QAction(tr("GPS"), this);
248     m_gpsToggleAct->setCheckable(true);
249
250     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
251             this, SIGNAL(gpsTriggered(bool)));
252
253     // automatic centering
254     m_autoCenteringAct = new QAction(tr("Auto centering"), this);
255     m_autoCenteringAct->setCheckable(true);
256     connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
257         this, SIGNAL(autoCenteringTriggered(bool)));
258
259     // build the actual menu
260     m_viewMenu = menuBar()->addMenu(tr("Main"));
261     m_viewMenu->addAction(m_toSettingsAct);
262     m_viewMenu->addAction(m_gpsToggleAct);
263     m_viewMenu->addAction(m_autoCenteringAct);
264     m_viewMenu->setObjectName(tr("Menu"));
265 }
266
267 void MainWindow::drawOsmLicense(int width, int height)
268 {
269     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
270
271     m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
272                         height - m_osmLicense->fontMetrics().height());
273 }
274
275 void MainWindow::drawOwnLocationCrosshair(int width, int height)
276 {
277     qDebug() << __PRETTY_FUNCTION__;
278
279     if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
280         m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
281                             height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
282     }
283 }
284
285 void MainWindow::gpsError(const QString &message)
286 {
287     qDebug() << __PRETTY_FUNCTION__;
288
289     showMaemoInformationBox(message);
290 }
291
292 void MainWindow::gpsTimeout()
293 {
294     qDebug() << __PRETTY_FUNCTION__;
295
296     showMaemoInformationBox(tr("GPS timeout"));
297 }
298
299 void MainWindow::grabZoomKeys(bool grab)
300 {
301     qDebug() << __PRETTY_FUNCTION__;
302
303 #ifdef Q_WS_MAEMO_5
304     // Can't grab keys unless we have a window id
305     if (!winId())
306         return;
307
308     unsigned long val = (grab) ? 1 : 0;
309     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
310     if (!atom)
311         return;
312
313     XChangeProperty (QX11Info::display(),
314                      winId(),
315                      atom,
316                      XA_INTEGER,
317                      32,
318                      PropModeReplace,
319                      reinterpret_cast<unsigned char *>(&val),
320                      1);
321 #else
322     Q_UNUSED(grab);
323 #endif // Q_WS_MAEMO_5
324 }
325
326 void MainWindow::keyPressEvent(QKeyEvent* event)
327 {
328     qDebug() << __PRETTY_FUNCTION__;
329
330     switch (event->key()) {
331     case Qt::Key_F7:
332         event->accept();
333         emit zoomIn();
334         break;
335
336     case Qt::Key_F8:
337         event->accept();
338         emit zoomOut();
339         break;
340     }
341     QWidget::keyPressEvent(event);
342 }
343
344 void MainWindow::loadDone(bool done)
345 {
346     qDebug() << __PRETTY_FUNCTION__;
347
348     // for the first time the login page is opened, we need to refresh it to get cookies working
349     if(m_refresh) {
350         m_webView->reload();
351         m_refresh = false;
352     }
353
354     if (done)
355     {
356         QWebFrame* frame = m_webView->page()->currentFrame();
357         if (frame!=NULL)
358         {
359             // set email box
360             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
361
362             foreach (QWebElement element, emailCollection) {
363                 element.setAttribute("value", m_email.toAscii());
364             }
365             // set password box
366             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
367             foreach (QWebElement element, passwordCollection) {
368                 element.setAttribute("value", m_password.toAscii());
369             }
370             // find connect button
371             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
372             foreach (QWebElement element, buttonCollection)
373             {
374                 QPoint pos(element.geometry().center());
375
376                 // send a mouse click event to the web page
377                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
378                                    Qt::NoModifier);
379                 QApplication::sendEvent(m_webView->page(), &event0);
380                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
381                                    Qt::NoModifier);
382                 QApplication::sendEvent(m_webView->page(), &event1);
383             }
384         }
385     }
386 }
387
388 void MainWindow::loginDialogDone(const QString &email, const QString &password)
389 {
390     qDebug() << __PRETTY_FUNCTION__;
391
392     m_email = email;
393     m_password = password;
394 }
395
396 void MainWindow::loginFailed()
397 {
398     qDebug() << __PRETTY_FUNCTION__;
399
400     m_email.clear();
401     m_password.clear();
402
403     toggleProgressIndicator(false);
404
405 #ifdef Q_WS_MAEMO_5
406     QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
407                                        QMaemo5InformationBox::NoTimeout);
408
409 #endif // Q_WS_MAEMO_5
410
411     if(m_loginDialog->exec() != QDialog::Accepted) {
412         // if login dialog was canceled we need to stop processing webview
413         // stop and disconnect m_webView;
414         m_webView->stop();
415         disconnect(m_webView, SIGNAL(loadFinished(bool)),
416                    this, SLOT(loadDone(bool)));
417         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
418                    this, SLOT(updateCredentials(const QUrl &)));
419
420         emit cancelLoginProcess();
421     }
422     else {
423         // re-load login page for webview
424         toggleProgressIndicator(true);
425         m_webView->load(m_loginUrl);
426     }
427 }
428
429 void MainWindow::openSettingsDialog()
430 {
431     qDebug() << __PRETTY_FUNCTION__;
432
433     SettingsDialog *dialog = new SettingsDialog(this);
434     dialog->show();
435 }
436
437 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
438 {
439     qDebug() << __PRETTY_FUNCTION__;
440
441     m_autoCenteringAct->setChecked(enabled);
442 }
443
444 void MainWindow::setGPSButtonEnabled(bool enabled)
445 {
446     qDebug() << __PRETTY_FUNCTION__;
447
448     m_gpsToggleAct->setChecked(enabled);
449
450     if (enabled)
451         setOwnLocationCrosshairVisibility(false);
452     else
453         setOwnLocationCrosshairVisibility(true);
454
455     m_autoCenteringAct->setVisible(enabled);
456 }
457
458 void MainWindow::setMapViewScene(QGraphicsScene *scene)
459 {
460     qDebug() << __PRETTY_FUNCTION__;
461
462     m_mapView->setScene(scene);
463 }
464
465 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
466 {
467     qDebug() << __PRETTY_FUNCTION__;
468
469     if (visibility) {
470         m_ownLocationCrosshair->show();
471         m_drawOwnLocationCrosshair = true;
472         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
473     }
474     else {
475         m_ownLocationCrosshair->hide();
476         m_drawOwnLocationCrosshair = false;
477     }
478 }
479
480 void MainWindow::setViewPortSize(int width, int height)
481 {
482     qDebug() << __PRETTY_FUNCTION__;
483
484     m_viewPortWidth = width;
485     m_viewPortHeight = height;
486 }
487
488 void MainWindow::showMaemoInformationBox(const QString &message)
489 {
490     qDebug() << __PRETTY_FUNCTION__;
491
492 #ifdef Q_WS_MAEMO_5
493     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
494 #else
495     Q_UNUSED(message);
496 #endif
497 }
498
499 void MainWindow::startLoginProcess(const QUrl &url)
500 {
501     qDebug() << __PRETTY_FUNCTION__;
502
503     m_loginUrl = url;
504     m_webView = new QWebView;
505     m_loginDialog = new LoginDialog(this);
506
507     connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
508             this, SIGNAL(updateCredentials(QUrl)));
509     connect(m_webView, SIGNAL(loadFinished(bool)),
510             this, SLOT(loadDone(bool)));
511
512     connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
513             this, SLOT(loginDialogDone(QString,QString)));
514
515     m_webView->hide();
516
517     if(m_loginDialog->exec() != QDialog::Accepted) {
518         // if login dialog was canceled we need to stop processing webview
519         // stop and disconnect m_webView;
520         m_webView->stop();
521         disconnect(m_webView, SIGNAL(loadFinished(bool)),
522                    this, SLOT(loadDone(bool)));
523         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
524                    this, SLOT(updateCredentials(const QUrl &)));
525
526         emit cancelLoginProcess();
527     }
528     else {
529         m_webView->load(m_loginUrl);
530         toggleProgressIndicator(true);
531         m_refresh = true;
532     }
533 }
534
535 void MainWindow::toggleProgressIndicator(bool value)
536 {
537     qDebug() << __PRETTY_FUNCTION__;
538
539 #ifdef Q_WS_MAEMO_5
540     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
541 #else
542     Q_UNUSED(value);
543 #endif // Q_WS_MAEMO_5
544 }