Added Feature to show own location on the map
authorVille Tiensuu <ville.tiensuu@ixonos.com>
Wed, 28 Apr 2010 13:56:17 +0000 (16:56 +0300)
committerVille Tiensuu <ville.tiensuu@ixonos.com>
Wed, 28 Apr 2010 13:56:17 +0000 (16:56 +0300)
New files defines OwnLocationItem class, test for the class and images
for own location.
Mapengine is edited to include instance of OwnLocationItem as a data
member. And also to add own location item to the mapscene.

13 files changed:
images.qrc
res/images/led_red_g.png [new file with mode: 0755]
res/images/led_red_h.png [new file with mode: 0755]
res/images/led_red_s.png [new file with mode: 0755]
src/map/mapcommon.h
src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapview.cpp
src/map/ownlocationitem.cpp [new file with mode: 0644]
src/map/ownlocationitem.h [new file with mode: 0644]
src/src.pro
tests/map/ownlocationitem/ownlocationitem.pro [new file with mode: 0644]
tests/map/ownlocationitem/testownlocationitem.cpp [new file with mode: 0644]

index 660d37d..9d055b9 100644 (file)
@@ -16,5 +16,8 @@
         <file>res/images/sliding_bar_left.png</file>
         <file>res/images/sliding_bar_right.png</file>
         <file>res/images/clock.png</file>
+        <file>res/images/led_red_g.png</file>
+        <file>res/images/led_red_h.png</file>
+        <file>res/images/led_red_s.png</file>
     </qresource>
 </RCC>
diff --git a/res/images/led_red_g.png b/res/images/led_red_g.png
new file mode 100755 (executable)
index 0000000..a370b07
Binary files /dev/null and b/res/images/led_red_g.png differ
diff --git a/res/images/led_red_h.png b/res/images/led_red_h.png
new file mode 100755 (executable)
index 0000000..5cad335
Binary files /dev/null and b/res/images/led_red_h.png differ
diff --git a/res/images/led_red_s.png b/res/images/led_red_s.png
new file mode 100755 (executable)
index 0000000..65f38ae
Binary files /dev/null and b/res/images/led_red_s.png differ
index 22f78f0..f7e8f4f 100644 (file)
@@ -41,6 +41,9 @@ static const int MAP_ZOOM_PANEL_LEVEL = MIN_MAP_SCENE_NORMAL_LEVEL
                                         + MAX_MAP_ZOOM_LEVEL + 2; ///< Zepth of zoom panel (Z-axis)
 static const int MAP_ZOOM_PANEL_BUTTON_SIZE = 74; ///< Size of a zoom panel button
 
+static const int MAP_OWN_LOCATION_ICON_SIZE = 24; ///< Size of own location item icon
+static const int OWN_LOCATION_ICON_Z_LEVEL = MAP_ZOOM_PANEL_LEVEL - 1; ///< Z-Level of own location icon
+
 const qreal MAX_LATITUDE = 85.05112877980659237802;  ///< Maximum latitude value
 const qreal MIN_LATITUDE = -MAX_LATITUDE; ///< Minimum latitude value
 const qreal MIN_LONGITUDE = -180.0;  ///< Minimum longitude value
index 4617284..cc3fae0 100644 (file)
@@ -49,6 +49,9 @@ MapEngine::MapEngine(QObject *parent)
     m_mapScene->addItem(m_mapZoomPanel);
     connect(m_mapZoomPanel, SIGNAL(zoomInPressed()), this, SLOT(zoomIn()));
     connect(m_mapZoomPanel, SIGNAL(zoomOutPressed()), this, SLOT(zoomOut()));
+
+    m_ownLocation = new OwnLocationItem();
+    m_mapScene->addItem(m_ownLocation);
 }
 
 void MapEngine::init()
index 4b156ab..30d9fc1 100644 (file)
@@ -31,6 +31,7 @@
 #include "mapscene.h"
 #include "maptile.h"
 #include "mapzoompanel.h"
+#include "ownlocationitem.h"
 
 /**
 * @brief Map engine
@@ -264,6 +265,7 @@ private:
     MapScene *m_mapScene; ///< Scene for map tiles
     MapZoomPanel *m_mapZoomPanel; ///< Toolbar for zoom buttons
     QPoint m_centerTile; ///< Current center tile
+    OwnLocationItem *m_ownLocation; ///< Item to show own location
     QPoint m_sceneCoordinate; ///< Current center coordinate
     QRect m_viewGrid; ///< Current grid of tiles in view (includes margin)
     QSize m_viewSize; ///< Current view size
index 15de9cb..9f6ba4c 100644 (file)
@@ -25,7 +25,7 @@
 #include <QMouseEvent>
 
 #ifndef Q_WS_MAEMO_5
-    #include <QGLWidget>
+//   #include <QGLWidget>
 #endif // Q_WS_MAEMO_5
 
 #include "mapcommon.h"
@@ -40,7 +40,7 @@ MapView::MapView(QWidget *parent)
   * OpenGL can't be used in scratchbox.
   */
 #ifndef Q_WS_MAEMO_5
-    setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer)));
+  //  setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer)));
 #endif // !Q_WS_MAEMO_5
 
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
diff --git a/src/map/ownlocationitem.cpp b/src/map/ownlocationitem.cpp
new file mode 100644 (file)
index 0000000..ef3d5ef
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Ville Tiensuu - ville.tiensuu@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 <QDebug>
+
+#include "ownlocationitem.h"
+#include "mapengine.h"
+#include "mapcommon.h"
+
+OwnLocationItem::OwnLocationItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QPixmap ownLocationIcon(":/res/images/led_red_h.png");
+    setPixmap(ownLocationIcon);
+
+    QPointF defaultLocation(DEFAULT_LONGITUDE,DEFAULT_LATITUDE);
+    //QPoint position = MapEngine::convertLatLonToSceneCoordinate(home);
+
+    setPos(MapEngine::convertLatLonToSceneCoordinate(defaultLocation));
+    setZValue(OWN_LOCATION_ICON_Z_LEVEL);
+    setOffset(-MAP_OWN_LOCATION_ICON_SIZE/2, -MAP_OWN_LOCATION_ICON_SIZE/2);
+    setFlag(QGraphicsItem::ItemIgnoresTransformations);
+}
+
+OwnLocationItem::OwnLocationItem(const qreal &longitude, const qreal &latitude)
+{
+    QPointF ownPosition(longitude,latitude);
+
+    QPixmap ownLocationIcon(":/res/images/led_red_h.png");
+    setPixmap(ownLocationIcon);
+
+    setPos(MapEngine::convertLatLonToSceneCoordinate(ownPosition));
+    setZValue(OWN_LOCATION_ICON_Z_LEVEL);
+    setOffset(-MAP_OWN_LOCATION_ICON_SIZE/2, -MAP_OWN_LOCATION_ICON_SIZE/2);
+    setFlag(QGraphicsItem::ItemIgnoresTransformations);
+}
+
+OwnLocationItem::OwnLocationItem(const QPointF & ownPosition)
+{
+    QPixmap ownLocationIcon(":/res/images/led_red_h.png");
+    setPixmap(ownLocationIcon);
+
+    setPos(MapEngine::convertLatLonToSceneCoordinate(ownPosition));
+    setZValue(OWN_LOCATION_ICON_Z_LEVEL);
+    setOffset(-MAP_OWN_LOCATION_ICON_SIZE/2, -MAP_OWN_LOCATION_ICON_SIZE/2);
+    setFlag(QGraphicsItem::ItemIgnoresTransformations);
+}
+
+void OwnLocationItem::setPosition(const QPointF & newPosition)
+{
+    setPos(MapEngine::convertLatLonToSceneCoordinate(newPosition));
+}
+
+QPoint OwnLocationItem::position() const
+{
+    QPointF currentPosition;
+    currentPosition = pos();
+
+    return currentPosition.toPoint();
+}
+
+void OwnLocationItem::hideOwnLocation()
+{
+    hide();
+}
+
+void OwnLocationItem::showOwnLocation()
+{
+    show();
+}
diff --git a/src/map/ownlocationitem.h b/src/map/ownlocationitem.h
new file mode 100644 (file)
index 0000000..96b83bf
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Ville Tiensuu - ville.tiensuu@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 OWNLOCATIONITEM_H
+#define OWNLOCATIONITEM_H
+
+#include <QGraphicsPixmapItem>
+
+/**
+* @brief Class that shows own location icon on the map
+*
+* @class OwnLocationItem ownlocationitem.h "map/ownlocationitem.h"
+* @author Ville Tiensuu
+*/
+class OwnLocationItem : public QGraphicsPixmapItem
+{
+public:
+    /**
+    * @brief Default constructor of OwnLocationItem.
+    *        Sets position to default location.
+    *        Loads and sets default pixmap that is show on the map.
+    *        Sets default Z-value to show icon on top of maps but under zoom buttons.
+    *        Sets offset so that achor of the picture is at the origin. this feature is
+    *        needed to center icon on the middle of the location.
+    *        Sets item to ignore transformations. this feature is needed to make icon on the map
+    *        immune to scaling
+    */
+    OwnLocationItem();
+
+    /**
+    * @brief Overloaded constructor for OwnLocationItem.
+    *        Otherwise same as default constructor, but position is set according to parameters.
+    *
+    * @param longitude Longitude coordinate of position.
+    * @param latitude Latitude coordinate of position.
+    */
+    OwnLocationItem(const qreal & longitude, const qreal & latitude);
+
+    /**
+    * @brief Overloaded constructor for OwnLocationItem.
+    *        Otherwise same as default constructor, but position is set according to parameter.
+    *
+    * @param ownPosition Position in QPoinF format
+    */
+    OwnLocationItem(const QPointF & ownPosition);
+
+    /*******************************************************************************
+    * MEMBER FUNCTIONS AND SLOTS
+    ******************************************************************************/
+    public:
+
+    /**
+    * @brief Hides OwnLocationItem so that it is invisible on the map
+    *
+    */
+    void hideOwnLocation();
+
+    /**
+    * @brief returns position of OwnLocationItem
+    *
+    * @return QPoint position of OwnLocationItem
+    */
+    QPoint position() const;
+
+    /**
+    * @brief sets position of OwnLocationItem as specified in parameter.
+    *        Position defines where icon is show on the map.
+    *
+    * @param newPosition Parameter that specifies new position.
+    */
+    void setPosition(const QPointF & newPosition);
+
+    /**
+    * @brief Sets OwnLocationItem to be visible on the map.
+    *        OwnLocationImte is visible by default, function is needed only when own location
+    *        needs to be set back visible after hideOwnLocation function call.
+    */
+    void showOwnLocation();
+};
+
+#endif // OWNLOCATIONITEM_H
index c0c920b..d0faee4 100644 (file)
@@ -22,6 +22,7 @@ SOURCES += main.cpp \
     map/mapfetcher.cpp \
     map/mapzoompanel.cpp \
     map/mapbutton.cpp \
+    map/ownlocationitem.cpp \
     ui/pixmap.cpp \
     ui/infotab.cpp \
     ui/updatelocation/updatelocationdialog.cpp \
@@ -42,6 +43,7 @@ HEADERS += ui/mainwindow.h \
     map/mapcommon.h \
     map/mapzoompanel.h \
     map/mapbutton.h \
+    map/ownlocationitem.h \
     ui/pixmap.h \
     ui/infotab.h \
     ui/updatelocation/updatelocationdialog.h \
@@ -63,7 +65,7 @@ QT += network \
 
 # use don't use OpenGL when building in scratchbox
 !maemo5 { 
-    QT += opengl
+#    QT += opengl
     message(OpenGL built in)
     message(Make sure you have OpenGL development headers installed)
     message(install headers with: sudo apt-get install libgl-dev libglu-dev)
diff --git a/tests/map/ownlocationitem/ownlocationitem.pro b/tests/map/ownlocationitem/ownlocationitem.pro
new file mode 100644 (file)
index 0000000..b48aaae
--- /dev/null
@@ -0,0 +1,32 @@
+######################################################################
+# Automatically generated by qmake (2.01a) Tue Apr 27 14:52:31 2010
+######################################################################
+
+CONFIG += qtestlib
+QT += network
+TEMPLATE = app
+TARGET = ownlocation
+DEPENDPATH += .
+INCLUDEPATH += . \
+     ../../../src/
+
+# Input
+SOURCES += testownlocationitem.cpp \
+     ../../../src/map/mapscene.cpp \
+     ../../../src/map/maptile.cpp \
+     ../../../src/map/mapengine.cpp \
+     ../../../src/map/mapfetcher.cpp \
+     ../../../src/map/ownlocationitem.cpp \
+     ../../../src/map/mapview.cpp \
+     ../../../src/map/mapzoompanel.cpp \
+    ../../../src/map/mapbutton.cpp
+
+HEADERS += ../../../src/map/ownlocationitem.h \
+     ../../../src/map/mapscene.h \
+     ../../../src/map/maptile.h \
+     ../../../src/map/mapengine.h \
+     ../../../src/map/mapfetcher.h \
+     ../../../src/map/mapcommon.h \
+     ../../../src/map/mapview.h \
+    ../../../src/map/mapzoompanel.h \
+    ../../../src/map/mapbutton.h
diff --git a/tests/map/ownlocationitem/testownlocationitem.cpp b/tests/map/ownlocationitem/testownlocationitem.cpp
new file mode 100644 (file)
index 0000000..2eedd2e
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        Ville Tiensuu - ville.tiensuu@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 <QtTest/QtTest>
+#include <QDebug>
+#include <QList>
+
+#include "map/ownlocationitem.h"
+#include "map/mapscene.h"
+#include "map/maptile.h"
+#include "map/mapengine.h"
+#include "map/mapcommon.h"
+#include "map/mapview.h"
+
+namespace TestOwnLocation  //  Test data for function is defined in namespace
+{   
+    const qreal xCoordinate = 65.525;
+    const qreal yCoordinate = 25.345;
+    const QPointF testLocationPoint(65.525, 25.345);
+    const QPointF defaultLocationPoint(DEFAULT_LONGITUDE,DEFAULT_LATITUDE);
+    const QPointF locationIconOffset(-MAP_OWN_LOCATION_ICON_SIZE/2, -MAP_OWN_LOCATION_ICON_SIZE/2);
+    const int itemIgnoresTransformationsFlagValue = 0x20;
+}
+
+using namespace TestOwnLocation;
+
+
+/**
+* @brief Class that test OwnLocationItem
+*
+* @author Ville Tiensuu
+*/
+class TestOwnLocationItem: public QObject
+ {
+     Q_OBJECT
+ private slots:
+
+    /**
+    * @brief Test method for constructors.
+    *        Creates instance of OwnLocationItem using three different constructors and tests
+    *        that positions are set correctly.
+    *        Tests that Z-values are set correctly.
+    *        Tests that offses are set correctly.
+    *        Tests that ItemIgnoresTransformations flag is set.
+    */
+     void testConstructors();
+
+     /**
+     * @brief Creates instance of OwnLocationItem.
+     *        Sets test position for location item in geographic coordinates.
+     *        Reads position from location item in scene coordinates and
+     *        verifies that it correctly formed.
+     */
+     void testSetAndGetPosition();
+
+     /**
+     * @brief Creates instance of OwnLocationItem.
+     *        Verifies that instance is visible.
+     *        Hides instance and verifies that it is not visible.
+     */
+     void testHideAndShowPosition();
+ };
+
+ void TestOwnLocationItem::testConstructors()
+ {
+     OwnLocationItem ownLocationItem1;
+     OwnLocationItem ownLocationItem2(xCoordinate, yCoordinate);
+     OwnLocationItem ownLocationItem3(testLocationPoint);
+
+     // Test Positions
+     QCOMPARE(ownLocationItem1.position(),
+              MapEngine::convertLatLonToSceneCoordinate(defaultLocationPoint));
+
+     QCOMPARE(ownLocationItem2.position(),
+              MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
+
+     QCOMPARE(ownLocationItem3.position(),
+              MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
+
+     // Test Z-values
+     QCOMPARE(static_cast<int>(ownLocationItem1.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
+     QCOMPARE(static_cast<int>(ownLocationItem2.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
+     QCOMPARE(static_cast<int>(ownLocationItem3.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
+
+     // Test Offsets
+     QCOMPARE(ownLocationItem1.offset(), locationIconOffset);
+     QCOMPARE(ownLocationItem2.offset(), locationIconOffset);
+     QCOMPARE(ownLocationItem3.offset(), locationIconOffset);
+
+     // Test ItemIgnoresTransformations Flags
+     QGraphicsItem::GraphicsItemFlags ownLocationItem1Flags = ownLocationItem1.flags();
+     QGraphicsItem::GraphicsItemFlags ownLocationItem2Flags = ownLocationItem1.flags();
+     QGraphicsItem::GraphicsItemFlags ownLocationItem3Flags = ownLocationItem1.flags();
+
+     QCOMPARE(ownLocationItem1Flags, itemIgnoresTransformationsFlagValue);
+     QCOMPARE(ownLocationItem2Flags, itemIgnoresTransformationsFlagValue);
+     QCOMPARE(ownLocationItem3Flags, itemIgnoresTransformationsFlagValue);
+ }
+
+ void TestOwnLocationItem::testSetAndGetPosition()
+ {
+     OwnLocationItem ownLocation;
+     ownLocation.setPosition(testLocationPoint);
+
+     QCOMPARE(ownLocation.position(),
+              MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
+ }
+
+ void TestOwnLocationItem::testHideAndShowPosition()
+ {
+     OwnLocationItem ownLocation;
+
+     QCOMPARE(ownLocation.isVisible(), true);     
+
+     ownLocation.hideOwnLocation();
+
+     QCOMPARE(ownLocation.isVisible(), false);
+
+ }
+
+ QTEST_MAIN(TestOwnLocationItem)
+ #include "testownlocationitem.moc"