Merge branch 'distance_scale'
authorKaj Wallin <kaj.wallin@ixonos.com>
Wed, 9 Jun 2010 08:52:02 +0000 (11:52 +0300)
committerKaj Wallin <kaj.wallin@ixonos.com>
Wed, 9 Jun 2010 08:52:02 +0000 (11:52 +0300)
Conflicts:
src/ui/mainwindow.h

Did minor fixes to documentation. Added functional test for map scale

Reviewed by: Marko Niemelä

13 files changed:
doc/testing/functionality-tests.doc
src/engine/engine.cpp
src/map/friendgroupitem.cpp
src/map/mapcommon.h
src/map/mapengine.cpp
src/map/mapengine.h
src/src.pro
src/ui/mainwindow.cpp
src/ui/mainwindow.h
src/ui/mapscale.cpp [new file with mode: 0644]
src/ui/mapscale.h [new file with mode: 0644]
src/ui/zoombutton.h
src/ui/zoombuttonpanel.h

index 9083e27..3d3e566 100644 (file)
Binary files a/doc/testing/functionality-tests.doc and b/doc/testing/functionality-tests.doc differ
index f569991..b40e693 100644 (file)
@@ -474,6 +474,9 @@ void SituareEngine::signalsFromMapEngine()
 
     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
+
+    connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
+            m_ui, SIGNAL(newMapResolution(qreal)));
 }
 
 void SituareEngine::signalsFromSituareService()
index 4611bca..e7acab4 100644 (file)
@@ -127,6 +127,8 @@ void FriendGroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
 void FriendGroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
+    Q_UNUSED(event);
+
     qDebug() << __PRETTY_FUNCTION__;
 
     if (m_clickEvent) {
index 65ed145..2a2f070 100644 (file)
@@ -73,6 +73,8 @@ const int DEFAULT_ZOOM_LEVEL = 14;       ///< Default zoom level
 const qreal DEFAULT_LONGITUDE = 0.0000;  ///< Default longitude value
 const qreal DEFAULT_LATITUDE = 0.0000; ///< Default latitude value
 
+const qreal EARTH_RADIUS = 6371.01;         ///< Earth radius in km
+
 const int GRID_PADDING = 1;  ///< Grid padding used in tile grid calculation
 
 const QString OSM_LICENSE = QString::fromUtf8("© OpenStreetMap contributors, CC-BY-SA");
index ea55393..140e250 100644 (file)
@@ -278,6 +278,21 @@ bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
     return (centerTile != temp);
 }
 
+qreal MapEngine::sceneResolution()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    const int SHIFT = 200;
+    const int KM_TO_M = 1000;
+    qreal scale = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
+    QPointF centerCoordinate = centerGeoCoordinate();
+    QPoint shiftedSceneCoordinate = QPoint(m_sceneCoordinate.x() + SHIFT*scale
+                                           , m_sceneCoordinate.y());
+    QPointF shiftedCoordinate = convertSceneCoordinateToLatLon(m_zoomLevel, shiftedSceneCoordinate);
+    qreal dist = greatCircleDistance(centerCoordinate, shiftedCoordinate) * KM_TO_M;
+    return (dist / SHIFT);
+}
+
 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -296,6 +311,21 @@ void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &ima
    }
 }
 
+qreal MapEngine::greatCircleDistance(QPointF firstLocation, QPointF secondLocation)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    const qreal TORAD = (M_PI/180);
+
+    qreal dLat = (secondLocation.y() - firstLocation.y())*TORAD;
+    qreal dLon = (secondLocation.x() - firstLocation.x())*TORAD;
+    qreal a = pow(sin(dLat/2),2) + cos(firstLocation.y()*TORAD) * cos(secondLocation.y()*TORAD)
+              * pow(sin(dLon/2),2);
+    qreal c = 2 * atan2(sqrt(a), sqrt(1-a));
+
+    return (EARTH_RADIUS * c);
+}
+
 void MapEngine::receiveOwnLocation(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -345,6 +375,8 @@ void MapEngine::setLocation(QPoint sceneCoordinate)
         getTiles(sceneCoordinate);
         m_mapScene->removeOutOfViewTiles();
     }
+
+    emit newMapResolution(sceneResolution());
 }
 
 void MapEngine::setZoomLevel(int newZoomLevel)
@@ -424,6 +456,8 @@ void MapEngine::viewZoomFinished()
         emit maxZoomLevelReached();
     else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
         emit minZoomLevelReached();
+
+    emit newMapResolution(sceneResolution());
 }
 
 void MapEngine::zoomIn()
index 034ed0b..94681a7 100644 (file)
@@ -112,6 +112,18 @@ public:
     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber);
 
     /**
+     * @brief Calculate great-circle distance between two geographic coordinates
+     *
+     * Calculate great-circle distance between two given geographic locations using
+     * haversine formula
+     *
+     * @param firstLocation Coordinates of the first location
+     * @param secondLocation Coordinates of the second location
+     * @return qreal Distance in kilometers
+     */
+    qreal greatCircleDistance(QPointF firstLocation, QPointF secondLocation);
+
+    /**
     * @brief MapEngine initializer
     *
     * Set initial location and zoom level for the engine. locationChanged and
@@ -145,6 +157,7 @@ public:
     static QString tilePath(int zoomLevel, int x, int y);
 
 public slots:
+
     /**
     * @brief Slot to catch user own location data
     *
@@ -235,6 +248,13 @@ private:
     bool isCenterTileChanged(QPoint sceneCoordinate);
 
     /**
+     * @brief Calculate scale at the map center of the map in meters/pixel
+     *
+     * @return qreal Scale of the map in meters/pixel
+     */
+    qreal sceneResolution();
+
+    /**
     * @brief Calculate maximum value for tile in this zoom level.
     *
     * @param zoomLevel zoom level
@@ -347,6 +367,11 @@ signals:
     void minZoomLevelReached();
 
     /**
+     * @brief Signal to pass the scale of the map to map scale
+     */
+    void newMapResolution(qreal scale);
+
+    /**
     * @brief Request view changing zoom level
     *
     * @param newZoomLevel New zoom level
index e560eb4..ca2a88d 100644 (file)
@@ -48,7 +48,8 @@ SOURCES += main.cpp \
     network/networkaccessmanager.cpp \
     network/networkhandler.cpp \
     network/networkcookiejar.cpp \
-    network/networkreply.cpp
+    network/networkreply.cpp \
+    ui/mapscale.cpp
 HEADERS += ui/mainwindow.h \
     map/mapengine.h \
     map/mapview.h \
@@ -94,7 +95,8 @@ HEADERS += ui/mainwindow.h \
     network/networkaccessmanager.h \
     network/networkhandler.h \
     network/networkcookiejar.h \
-    network/networkreply.h
+    network/networkreply.h \
+    ui/mapscale.h
 QT += network \
     webkit
 
index 1443de1..09c699f 100644 (file)
@@ -35,6 +35,7 @@
 #include "settingsdialog.h"
 #include "userinfopanel.h"
 #include "zoombuttonpanel.h"
+#include "mapscale.h"
 
 #include "mainwindow.h"
 
@@ -61,6 +62,7 @@ MainWindow::MainWindow(QWidget *parent)
     m_password(),
     m_fullScreenButton(0),
     m_webView(0),
+    m_mapScale(0),
     m_cookieJar(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -90,10 +92,12 @@ MainWindow::MainWindow(QWidget *parent)
     if(m_fullScreenButton) {
         m_fullScreenButton->stackUnder(m_zoomButtonPanel);
         m_osmLicense->stackUnder(m_fullScreenButton);
-    } else
+    } else {
         m_osmLicense->stackUnder(m_zoomButtonPanel);
+    }
     m_ownLocationCrosshair->stackUnder(m_osmLicense);
-    m_mapView->stackUnder(m_ownLocationCrosshair);
+    m_mapScale->stackUnder(m_ownLocationCrosshair);
+    m_mapView->stackUnder(m_mapScale);
 
     this->toggleProgressIndicator(true);
 
@@ -208,6 +212,7 @@ void MainWindow::buildMap()
     buildOsmLicense();
     buildManualLocationCrosshair();
     buildFullScreenButton();
+    buildMapScale();
 
     connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
             this, SIGNAL(mapViewScrolled(QPoint)));
@@ -221,6 +226,9 @@ void MainWindow::buildMap()
     connect(m_mapView, SIGNAL(viewResized(QSize)),
             this, SLOT(drawFullScreenButton(QSize)));
 
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            this, SLOT(drawMapScale(QSize)));
+
     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
              this, SLOT(setViewPortSize(int, int)));
 
@@ -231,6 +239,13 @@ void MainWindow::buildMap()
             this, SIGNAL(viewZoomFinished()));
 }
 
+void MainWindow::buildMapScale()
+{
+    m_mapScale = new MapScale(this);
+    connect(this, SIGNAL(newMapResolution(qreal)),
+            m_mapScale, SLOT(updateMapResolution(qreal)));
+}
+
 void MainWindow::buildOsmLicense()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -439,6 +454,16 @@ void MainWindow::drawFullScreenButton(const QSize &size)
     }
 }
 
+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(PANEL_PEEK_AMOUNT + LEFT_SCALE_MARGIN,
+                     size.height() - m_mapScale->size().height() - BOTTOM_SCALE_MARGIN);
+}
+
 void MainWindow::drawOsmLicense(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
index 18c7406..611fde0 100644 (file)
@@ -37,6 +37,7 @@ class QNetworkReply;
 
 class FacebookAuthentication;
 class FriendListPanel;
+class MapScale;
 class MapScene;
 class MapView;
 class SituareService;
@@ -199,6 +200,11 @@ private:
     void buildMap();
 
     /**
+     * @brief Build map scale and connect slots
+     */
+    void buildMapScale();
+
+    /**
       * @brief Build OSM license and connect slots
       */
     void buildOsmLicense();
@@ -274,6 +280,13 @@ private slots:
     void drawFullScreenButton(const QSize &size);
 
     /**
+    * @brief Slot for drawing the map distance scale
+    *
+    * @param size Size of the screen
+    */
+    void drawMapScale(const QSize &size);
+
+    /**
     * @brief Slot for drawing the Open Street Map license text
     *
     * @param size Size of the screen
@@ -459,6 +472,11 @@ signals:
     void notificateUpdateFailing(const QString &message);
 
     /**
+     * @brief Forwarding signal from MapEngine to MapScale
+     */
+    void newMapResolution(qreal scale);
+
+    /**
     * @brief Signal for refreshing user data.
     *
     */
@@ -565,6 +583,7 @@ private:
     QWebView *m_webView;                    ///< Shows facebook login page
 
     FriendListPanel *m_friendsListPanel;    ///< Instance of friends list panel
+    MapScale *m_mapScale;                   ///< Instance of the map scale
     MapView *m_mapView;                     ///< Instance of the map view
     NetworkCookieJar *m_cookieJar;          ///< Placeholder for QNetworkCookies
     PanelSideBar *m_userPanelSidebar;       ///< User panel side bar
@@ -573,7 +592,6 @@ private:
     ZoomButtonPanel *m_zoomButtonPanel;     ///< Instance of zoom button panel
 
     SettingsDialog *m_settingsDialog;       ///< Settings dialog
-
 };
 
 #endif // MAINWINDOW_H
diff --git a/src/ui/mapscale.cpp b/src/ui/mapscale.cpp
new file mode 100644 (file)
index 0000000..1ea485a
--- /dev/null
@@ -0,0 +1,150 @@
+ /*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        Kaj Wallin - kaj.wallin@ixonos.com
+
+    Situare is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    version 2 as published by the Free Software Foundation.
+
+    Situare is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Situare; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+    USA.
+ */
+
+#include <QPainter>
+#include <QPen>
+#include <QLine>
+#include <QDebug>
+#include "mapscale.h"
+#include <math.h>
+
+const int TARGET_WIDTH = 140;                   ///< Target width of the scale in pixels
+const qreal M_TO_FT = 3.2808399;                ///< Meter to feet conversion value
+const qreal FT_TO_MI = 5280;                    ///< Feet to mile conversion
+const qreal M_TO_KM = 1000;                     ///< Meters to kilometers conversion
+const int MAPSCALE_HEIGHT = 31;                 ///< Height of the the map scale
+const int CENTERLINE_Y = MAPSCALE_HEIGHT / 2;   ///< Y position of the centerline
+const int MAPSCALE_STOP_HEIGHT = 7;             ///< Height of each perpendicular stop on the scale
+const int SCALE_TEXT_X = 3;                     ///< X coordinate for the scale texts
+
+MapScale::MapScale(QWidget *parent) :
+    QWidget(parent),
+    m_centerLineImperial(0),
+    m_centerLineMetric(0),
+    m_imperialText(""),
+    m_metricText("")
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+//    updateMapScale(10.0);
+    setAttribute(Qt::WA_TransparentForMouseEvents, true);
+}
+
+void MapScale::paintEvent(QPaintEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(event);
+
+    resize(ceil(fmax(m_centerLineMetric,m_centerLineImperial)) + 1, MAPSCALE_HEIGHT);
+
+    QLineF centerLine(0, CENTERLINE_Y, fmax(m_centerLineMetric, m_centerLineImperial), CENTERLINE_Y);
+    QLineF startKm(1, CENTERLINE_Y, 1, CENTERLINE_Y - MAPSCALE_STOP_HEIGHT);
+    QLineF stopKm(m_centerLineMetric, CENTERLINE_Y,
+                  m_centerLineMetric, CENTERLINE_Y - MAPSCALE_STOP_HEIGHT);
+
+    QLineF startMi(1, CENTERLINE_Y, 1, CENTERLINE_Y + MAPSCALE_STOP_HEIGHT);
+    QLineF stopMi(m_centerLineImperial, CENTERLINE_Y,
+                  m_centerLineImperial, CENTERLINE_Y + MAPSCALE_STOP_HEIGHT);
+
+    QPainter painter(this);
+    QPen pen(Qt::black, 2);
+
+    painter.setFont(QFont("Nokia Sans", 13, QFont::Normal));
+    painter.setPen(pen);
+    painter.drawLine(centerLine);
+    painter.drawLine(startKm);
+    painter.drawLine(stopKm);
+    painter.drawText(SCALE_TEXT_X, MAPSCALE_HEIGHT / 2 - 2, m_metricText);
+
+    painter.drawLine(startMi);
+    painter.drawLine(stopMi);
+    painter.drawText(SCALE_TEXT_X, MAPSCALE_HEIGHT, m_imperialText);
+}
+
+qreal MapScale::roundToBaseScale(qreal value)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    int scale = 0;
+    qreal baseLine;
+    while(value > 1){
+        value = value/10;
+        scale++;
+    }
+    if(value < 0.15)
+        baseLine = 1;
+    else if (value < 0.35)
+        baseLine = 2;
+    else if (value < 0.75)
+        baseLine = 5;
+    else
+        baseLine = 10;
+    baseLine = baseLine * (pow(10,scale-1));
+    return baseLine;
+}
+
+void MapScale::updateMapResolution(const qreal &resolution)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    //Calculate distance scale in metric units
+    qreal genericMetricScale = TARGET_WIDTH * resolution;
+    qreal baseMetricScale = roundToBaseScale(genericMetricScale);
+    if(resolution != 0)
+        m_centerLineMetric = baseMetricScale / resolution;
+    else
+        m_centerLineMetric = baseMetricScale / 0.000001;
+
+    if(baseMetricScale < M_TO_KM)
+    {
+        m_metricText.setNum(baseMetricScale);
+        m_metricText.append(" m");
+    } else {
+        m_metricText.setNum(baseMetricScale/M_TO_KM);
+        m_metricText.append(" km");
+    }
+
+    //Calculate distance scale in imperial units
+    qreal imperialScaleResolution = resolution * M_TO_FT;
+    qreal genericImperialScale = TARGET_WIDTH * imperialScaleResolution;
+    qreal baseImperialScale;
+
+    if(genericImperialScale < FT_TO_MI) {
+        baseImperialScale = roundToBaseScale(genericImperialScale);
+        if(imperialScaleResolution != 0)
+            m_centerLineImperial = baseImperialScale / imperialScaleResolution;
+        else
+            m_centerLineImperial = baseImperialScale / 0.000001;
+        m_imperialText.setNum(baseImperialScale);
+        m_imperialText.append(" ft");
+    } else {
+        baseImperialScale = roundToBaseScale(genericImperialScale / FT_TO_MI);
+        if(imperialScaleResolution != 0)
+            m_centerLineImperial = (baseImperialScale*FT_TO_MI) / imperialScaleResolution;
+        else
+            m_centerLineImperial = baseImperialScale / 0.000001;
+        m_imperialText.setNum(baseImperialScale);
+        m_imperialText.append(" mi");
+    }
+
+    update();
+}
diff --git a/src/ui/mapscale.h b/src/ui/mapscale.h
new file mode 100644 (file)
index 0000000..0d3c004
--- /dev/null
@@ -0,0 +1,89 @@
+ /*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        Kaj Wallin - kaj.wallin@ixonos.com
+
+    Situare is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    version 2 as published by the Free Software Foundation.
+
+    Situare is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Situare; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+    USA.
+ */
+
+
+#ifndef MAPSCALE_H
+#define MAPSCALE_H
+
+#include <QWidget>
+
+class QLineF;
+
+/**
+ * @brief Map distance scale
+ *
+ * @author Kaj Wallin - kaj.wallin (at) ixonos.com
+ */
+class MapScale : public QWidget
+{
+    Q_OBJECT
+
+public:
+    /**
+     * @brief Constructor
+     *
+     * @param parent Parent
+     */
+    MapScale(QWidget *parent = 0);
+
+/*******************************************************************************
+ * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
+ ******************************************************************************/
+    /**
+     * @brief Event handler for paint events
+     *
+     * Paints the scale
+     * @param event Paint event
+     */
+    void paintEvent(QPaintEvent *event);
+
+/******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+private:
+    /**
+     * @brief Rounding function for distances
+     *
+     * Rounds the given value to closest 1,2,5 or 10 in the original scale
+     * @param value Value to be rounded
+     * @return qreal Rounded value
+     */
+    qreal roundToBaseScale(qreal value);
+
+public slots:
+    /**
+     * @brief Slot to update the scale with latest resolution
+     *
+     * @param resolution Resolution of the map in meters/pixel
+     */
+    void updateMapResolution(const qreal &resolution);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    qreal m_centerLineImperial;     ///< Length of the imperial scale
+    qreal m_centerLineMetric;       ///< Length of the metric scale
+    QString m_imperialText;         ///< Text description of the imperial scale
+    QString m_metricText;           ///< Text description of the metric scale
+};
+
+#endif // MAPSCALE_H
index 745981f..4533254 100644 (file)
@@ -68,6 +68,9 @@ protected:
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
 public:
+    /**
+     * @brief Relative position of the event inside the widget
+     */
     const QPoint& eventPosition();
 
 /*******************************************************************************
index 8da59de..5d649ac 100644 (file)
@@ -44,8 +44,6 @@ public:
      * @brief Constructor
      *
      * @param parent Parent
-     * @param x Panel x coordinate
-     * @param y Panel y coordinate
      */
     ZoomButtonPanel(QWidget *parent = 0);