Merge branch 'master' into new_panels_with_context_buttons
[situare] / src / ui / mainwindow.cpp
index 446e952..576c238 100644 (file)
    USA.
 */
 
-#include <QtGui>
-#include <QtWebKit>
+#include <QAction>
+#include <QApplication>
+#include <QMenuBar>
+#include <QMessageBox>
 #include <QtAlgorithms>
+#include <QtWebKit>
 
+///< @todo sort
 #include "facebookservice/facebookauthentication.h"
 #include "map/mapcommon.h"
 #include "map/mapview.h"
 #include "error.h"
 #include "friendlistpanel.h"
 #include "fullscreenbutton.h"
+#include "indicatorbuttonpanel.h"
 #include "logindialog.h"
 #include "mapscale.h"
 #include "panelcommon.h"
+#include "routingpanel.h"
 #include "tabbedpanel.h"
 #include "searchdialog.h"
 #include "settingsdialog.h"
 #include "userinfopanel.h"
 #include "zoombuttonpanel.h"
-#include "indicatorbuttonpanel.h"
 
 #include "mainwindow.h"
 
@@ -60,13 +65,14 @@ MainWindow::MainWindow(QWidget *parent)
       m_errorShown(false),
       m_loggedIn(false),
       m_refresh(false),
+      m_mapCenterHorizontalShifting(0),
       m_progressIndicatorCount(0),
-      m_ownLocationCrosshair(0),
-      m_email(),
+      m_crosshair(0),
+      m_email(), ///< @todo WTF?!?!?!?
       m_password(),
       m_webView(0),
       m_fullScreenButton(0),
-      m_indicatorButton(0),
+      m_indicatorButtonPanel(0),
       m_mapScale(0),
       m_cookieJar(0)
 {
@@ -74,31 +80,27 @@ MainWindow::MainWindow(QWidget *parent)
 
     buildMap();
 
-    // build main layout
-    QHBoxLayout *layout = new QHBoxLayout;
-    layout->addWidget(m_mapView);
-    layout->setMargin(0);
-    layout->setSpacing(0);
-
-    setCentralWidget(new QWidget());
-    centralWidget()->setLayout(layout);
+    // map view is the only widget which size & location is handled automatically by the system
+    // default functionality
+    setCentralWidget(m_mapView);
 
     buildPanels();
 
     createMenus();
     setWindowTitle(tr("Situare"));
 
-    // set stacking order of widgets
-    m_zoomButtonPanel->stackUnder(m_tabbedPanel);
-    if(m_fullScreenButton) {
-        m_fullScreenButton->stackUnder(m_zoomButtonPanel);
-        m_osmLicense->stackUnder(m_fullScreenButton);
+    // set stacking order of widgets (from top to bottom)
+    // m_tabbedPanel is the topmost one
+    if (m_fullScreenButton) {
+        m_fullScreenButton->stackUnder(m_tabbedPanel);
+        m_crosshair->stackUnder(m_fullScreenButton);
     } else {
-        m_osmLicense->stackUnder(m_zoomButtonPanel);
+        m_crosshair->stackUnder(m_tabbedPanel);
     }
-    m_ownLocationCrosshair->stackUnder(m_osmLicense);
-    m_indicatorButtonPanel->stackUnder(m_ownLocationCrosshair);
-    m_mapScale->stackUnder(m_indicatorButtonPanel);
+    m_zoomButtonPanel->stackUnder(m_crosshair);
+    m_indicatorButtonPanel->stackUnder(m_zoomButtonPanel);
+    m_osmLicense->stackUnder(m_indicatorButtonPanel);
+    m_mapScale->stackUnder(m_osmLicense);
     m_mapView->stackUnder(m_mapScale);
 
     grabZoomKeys(true);
@@ -141,14 +143,22 @@ void MainWindow::automaticUpdateDialogFinished(int result)
     m_automaticUpdateLocationDialog->deleteLater();
 }
 
-void MainWindow::buildFullScreenButton()
+void MainWindow::buildCrosshair()
 {
     qDebug() << __PRETTY_FUNCTION__;
-#ifdef Q_WS_MAEMO_5
-    m_fullScreenButton = new FullScreenButton(this);
-    connect(m_fullScreenButton, SIGNAL(clicked()),
-            this, SLOT(toggleFullScreen()));
-#endif // Q_WS_MAEMO_5
+
+    m_crosshair = new QLabel(this);
+    QPixmap crosshairImage(":/res/images/sight.png");
+    m_crosshair->setPixmap(crosshairImage);
+    m_crosshair->setFixedSize(crosshairImage.size());
+    m_crosshair->hide();
+    m_crosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
+
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(moveCrosshair()));
+
+    connect(m_mapView, SIGNAL(horizontalShiftingChanged(int)),
+            this, SLOT(mapCenterHorizontalShiftingChanged(int)));
 }
 
 void MainWindow::buildFriendListPanel()
@@ -164,10 +174,30 @@ void MainWindow::buildFriendListPanel()
             m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
 
     connect(m_friendsListPanel, SIGNAL(findFriend(GeoCoordinate)),
-            this, SIGNAL(findFriend(GeoCoordinate)));
+            this, SIGNAL(centerToCoordinates(GeoCoordinate)));
 
     connect(this, SIGNAL(friendImageReady(User*)),
             m_friendsListPanel, SLOT(friendImageReady(User*)));
+
+    connect(m_friendsListPanel, SIGNAL(routeToFriend(const GeoCoordinate&)),
+            this, SIGNAL(routeTo(const GeoCoordinate&)));
+}
+
+void MainWindow::buildFullScreenButton()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+#ifdef Q_WS_MAEMO_5
+    m_fullScreenButton = new FullScreenButton(this);
+
+    if (m_fullScreenButton) {
+        connect(m_fullScreenButton, SIGNAL(clicked()),
+                this, SLOT(toggleFullScreen()));
+
+        connect(qApp, SIGNAL(showFullScreenButton()),
+                m_fullScreenButton, SLOT(invoke()));
+    }
+#endif // Q_WS_MAEMO_5
 }
 
 void MainWindow::buildIndicatorButtonPanel()
@@ -221,21 +251,6 @@ void MainWindow::buildInformationBox(const QString &message, bool modal)
     queueDialog(msgBox);
 }
 
-void MainWindow::buildManualLocationCrosshair()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ownLocationCrosshair = new QLabel(this);
-    QPixmap crosshairImage(":/res/images/sight.png");
-    m_ownLocationCrosshair->setPixmap(crosshairImage);
-    m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
-    m_ownLocationCrosshair->hide();
-    m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
-
-    connect(m_mapView, SIGNAL(viewResized(QSize)),
-            this, SLOT(drawOwnLocationCrosshair(QSize)));
-}
-
 void MainWindow::buildMap()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -244,7 +259,7 @@ void MainWindow::buildMap()
 
     buildZoomButtonPanel();
     buildOsmLicense();
-    buildManualLocationCrosshair();
+    buildCrosshair();
     buildFullScreenButton();
     buildIndicatorButtonPanel();
     buildMapScale();
@@ -265,7 +280,7 @@ void MainWindow::buildMap()
             this, SLOT(drawMapScale(QSize)));
 
     connect(m_mapView, SIGNAL(viewResized(QSize)),
-             this, SLOT(setViewPortSize(QSize)));
+             this, SLOT(moveCrosshair()));
 
     connect(this, SIGNAL(zoomLevelChanged(int)),
             m_mapView, SLOT(setZoomLevel(int)));
@@ -306,16 +321,70 @@ void MainWindow::buildPanels()
 
     buildUserInfoPanel();
     buildFriendListPanel();
+    buildRoutingPanel();
 
     m_tabbedPanel = new TabbedPanel(this);
     m_tabbedPanel->addTab(m_userInfoPanel, QIcon(":/res/images/user_info.png"));
     m_tabbedPanel->addTab(m_friendsListPanel, QIcon(":/res/images/friend_list.png"));
-
-    connect(m_tabbedPanel, SIGNAL(panelOpened()),
-            m_friendsListPanel, SLOT(clearFriendListFilter()));
+    m_tabbedPanel->addTab(m_routingPanel, QIcon(":/res/images/routing.png"));
 
     connect(m_mapView, SIGNAL(viewResized(QSize)),
             m_tabbedPanel, SLOT(resizePanel(QSize)));
+
+    connect(m_friendsListPanel, SIGNAL(showPanelRequested(QWidget*)),
+            m_tabbedPanel, SLOT(showPanel(QWidget*)));
+
+    connect(m_routingPanel, SIGNAL(showPanelRequested(QWidget*)),
+            m_tabbedPanel, SLOT(showPanel(QWidget*)));
+
+    connect(m_tabbedPanel, SIGNAL(panelClosed()),
+            m_friendsListPanel, SLOT(anyPanelClosed()));
+
+    connect(m_tabbedPanel, SIGNAL(panelOpened()),
+            m_friendsListPanel, SLOT(anyPanelOpened()));
+
+    connect(m_tabbedPanel, SIGNAL(panelClosed()),
+            m_routingPanel, SLOT(clearListsSelections()));
+
+    connect(m_tabbedPanel, SIGNAL(panelClosed()),
+            m_mapView, SLOT(disableCenterShift()));
+
+    connect(m_tabbedPanel, SIGNAL(panelOpened()),
+            m_mapView, SLOT(enableCenterShift()));
+
+    connect(m_tabbedPanel, SIGNAL(panelClosed()),
+            m_userInfoPanel, SIGNAL(collapse()));
+
+    connect(m_tabbedPanel, SIGNAL(currentChanged(int)),
+            m_userInfoPanel, SIGNAL(collapse()));
+}
+
+void MainWindow::buildRoutingPanel()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_routingPanel = new RoutingPanel(this);
+
+    connect(this, SIGNAL(locationDataParsed(const QList<Location>&)),
+            m_routingPanel, SLOT(populateLocationListView(const QList<Location>&)));
+
+    connect(m_routingPanel, SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
+            this, SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)));
+
+    connect(m_routingPanel, SIGNAL(routeToLocation(const GeoCoordinate&)),
+            this, SIGNAL(routeTo(const GeoCoordinate&)));
+
+    connect(this, SIGNAL(routeParsed(Route&)),
+            m_routingPanel, SLOT(setRoute(Route&)));
+
+    connect(m_routingPanel, SIGNAL(routeWaypointItemClicked(GeoCoordinate)),
+            this, SIGNAL(centerToCoordinates(GeoCoordinate)));
+
+    connect(m_routingPanel, SIGNAL(requestSearchLocation()),
+            this, SLOT(startLocationSearch()));
+
+    connect(m_routingPanel, SIGNAL(clearRoute()),
+            this, SIGNAL(clearRoute()));
 }
 
 void MainWindow::buildUserInfoPanel()
@@ -334,7 +403,7 @@ void MainWindow::buildUserInfoPanel()
             m_userInfoPanel, SIGNAL(clearUpdateLocationDialogData()));
 
     connect(m_userInfoPanel, SIGNAL(findUser(GeoCoordinate)),
-            this, SIGNAL(findUser(GeoCoordinate)));
+            this, SIGNAL(centerToCoordinates(GeoCoordinate)));
 
     connect(m_userInfoPanel, SIGNAL(requestReverseGeo()),
             this, SIGNAL(requestReverseGeo()));
@@ -439,17 +508,11 @@ void MainWindow::createMenus()
     connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
             this, SIGNAL(gpsTriggered(bool)));
 
-    /// @todo remove when not needed!
-    m_searchLocationAct = new QAction(tr("Location search"), this);
-    connect(m_searchLocationAct, SIGNAL(triggered()),
-            this, SLOT(startLocationSearch()));
-
     // build the actual menu
     m_viewMenu = menuBar()->addMenu(tr("Main"));
     m_viewMenu->addAction(m_loginAct);
     m_viewMenu->addAction(m_toSettingsAct);
     m_viewMenu->addAction(m_gpsToggleAct);
-    m_viewMenu->addAction(m_searchLocationAct); /// @todo remove when not needed!
     m_viewMenu->setObjectName(tr("Menu"));
 }
 
@@ -502,9 +565,10 @@ void MainWindow::drawFullScreenButton(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
 
-    if(m_fullScreenButton)
+    if (m_fullScreenButton) {
         m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width(),
                                  size.height() - m_fullScreenButton->size().height());
+    }
 }
 
 void MainWindow::drawMapScale(const QSize &size)
@@ -513,7 +577,6 @@ void MainWindow::drawMapScale(const QSize &size)
 
     const int LEFT_SCALE_MARGIN = 10;
     const int BOTTOM_SCALE_MARGIN = 2;
-//    qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
 
     m_mapScale->move(LEFT_SCALE_MARGIN,
                      size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
@@ -528,16 +591,6 @@ void MainWindow::drawOsmLicense(const QSize &size)
                        size.height() - m_osmLicense->fontMetrics().height());
 }
 
-void MainWindow::drawOwnLocationCrosshair(const QSize &size)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (m_ownLocationCrosshair != 0) {
-        m_ownLocationCrosshair->move(size.width()/2 - m_ownLocationCrosshair->pixmap()->width()/2,
-                            size.height()/2 - m_ownLocationCrosshair->pixmap()->height()/2);
-    }
-}
-
 void MainWindow::errorDialogFinished(int status)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -727,32 +780,34 @@ void MainWindow::loginUsingCookies()
 
 }
 
-void MainWindow::openSettingsDialog()
+void MainWindow::mapCenterHorizontalShiftingChanged(int shifting)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    m_mapCenterHorizontalShifting = shifting;
+    moveCrosshair();
+}
 
-    SettingsDialog *settingsDialog = new SettingsDialog(this);
-    settingsDialog->enableSituareSettings((m_loggedIn && m_gpsToggleAct->isChecked()));
-    connect(settingsDialog, SIGNAL(accepted()), this, SLOT(settingsDialogAccepted()));
+void MainWindow::moveCrosshair()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    settingsDialog->show();
+    if (m_crosshair) {
+        int mapHeight = m_mapView->size().height();
+        int mapWidth = m_mapView->size().width();
+        m_crosshair->move(mapWidth / 2 - m_crosshair->pixmap()->width() / 2
+                          - m_mapCenterHorizontalShifting,
+                          mapHeight / 2 - m_crosshair->pixmap()->height() / 2);
+    }
 }
 
-void MainWindow::readAutomaticLocationUpdateSettings()
+void MainWindow::openSettingsDialog()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
-    QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
-                                      .toTime();
+    SettingsDialog *settingsDialog = new SettingsDialog(this);
+    settingsDialog->enableSituareSettings((m_loggedIn && m_gpsToggleAct->isChecked()));
+    connect(settingsDialog, SIGNAL(accepted()), this, SLOT(settingsDialogAccepted()));
 
-    if (automaticUpdateEnabled && automaticUpdateInterval.isValid()) {
-        QTime time;
-        emit enableAutomaticLocationUpdate(true, time.msecsTo(automaticUpdateInterval));
-    } else {
-        emit enableAutomaticLocationUpdate(false);
-    }
+    settingsDialog->show();
 }
 
 void MainWindow::queueDialog(QDialog *dialog)
@@ -773,6 +828,23 @@ void MainWindow::queueDialog(QDialog *dialog)
         showInformationBox();
 }
 
+void MainWindow::readAutomaticLocationUpdateSettings()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
+    QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
+                                      .toTime();
+
+    if (automaticUpdateEnabled && automaticUpdateInterval.isValid()) {
+        QTime time;
+        emit enableAutomaticLocationUpdate(true, time.msecsTo(automaticUpdateInterval));
+    } else {
+        emit enableAutomaticLocationUpdate(false);
+    }
+}
+
 void MainWindow::saveCookies()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -794,6 +866,18 @@ void MainWindow::saveCookies()
     settings.setValue(COOKIES, list);
 }
 
+void MainWindow::setCrosshairVisibility(bool visibility)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (visibility) {
+        m_crosshair->show();
+        moveCrosshair();
+    } else {
+        m_crosshair->hide();
+    }
+}
+
 void MainWindow::setGPSButtonEnabled(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -815,18 +899,6 @@ void MainWindow::setMapViewScene(QGraphicsScene *scene)
     m_mapView->setScene(scene);
 }
 
-void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (visibility) {
-        m_ownLocationCrosshair->show();
-        drawOwnLocationCrosshair(m_viewPortSize);
-    } else {
-        m_ownLocationCrosshair->hide();
-    }
-}
-
 void MainWindow::settingsDialogAccepted()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -841,13 +913,6 @@ void MainWindow::setUsername(const QString &username)
     m_email = username;
 }
 
-void MainWindow::setViewPortSize(const QSize &size)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_viewPortSize = size;
-}
-
 void MainWindow::showEnableAutomaticUpdateLocationDialog(const QString &text)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -862,16 +927,6 @@ void MainWindow::showEnableAutomaticUpdateLocationDialog(const QString &text)
     m_automaticUpdateLocationDialog->show();
 }
 
-void MainWindow::toggleFullScreen()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if(windowState() == Qt::WindowNoState)
-        showFullScreen();
-    else
-        showNormal();
-}
-
 void MainWindow::showErrorInformationBox()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -901,7 +956,7 @@ void MainWindow::showPanels()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    drawFullScreenButton(m_viewPortSize);
+///< @todo check how this is called and can this method be removed
 
 //    if(m_loggedIn) {
 //        if(!m_friendsListPanel->isVisible()) {
@@ -940,6 +995,16 @@ void MainWindow::startLoginProcess()
     queueDialog(loginDialog);
 }
 
+void MainWindow::toggleFullScreen()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(windowState() == Qt::WindowNoState)
+        showFullScreen();
+    else
+        showNormal();
+}
+
 void MainWindow::toggleProgressIndicator(bool value)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -964,6 +1029,8 @@ void MainWindow::updateItemVisibility()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+///< @todo can this be removed?
+
 //    if(!m_loggedIn) {
 //        m_friendsListPanel->closePanel();
 //        m_friendsListPanel->hide();