Set MapScene size and created MapEngine init method
authorSami Rämö <sami.ramo@ixonos.com>
Fri, 9 Apr 2010 12:20:10 +0000 (15:20 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Fri, 9 Apr 2010 12:20:10 +0000 (15:20 +0300)
* Set MapScene size to amount of pixels of the closest
  zoom level
* Created init method for MapEngine to set initial location
  and zoomlevel

doc/map/map_class_diagram.dia
src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapscene.cpp
src/map/mapscene.h
src/map/maptile.cpp
src/map/mapview.cpp
src/ui/mapviewscreen.cpp

index cd035d6..f68c489 100644 (file)
Binary files a/doc/map/map_class_diagram.dia and b/doc/map/map_class_diagram.dia differ
index 01641be..66c5939 100644 (file)
@@ -42,11 +42,12 @@ MapEngine::MapEngine(QObject *parent)
             SLOT(mapImageReceived(QUrl, QPixmap)));
 }
 
-void MapEngine::setViewLocation(QPointF latLonCoordinate)
+void MapEngine::init()
 {
     emit zoomLevelChanged(m_zoomLevel);
+    setViewLocation(QPointF(25.5000, 65.0000));
 
-    /// Fetch some map tiles for demo purposes
+    // Fetch some map tiles for demo purposes
     for (int x=9351; x<=9354; x++) {
         for (int y=4261; y<=4264; y++) {
             QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
@@ -55,6 +56,11 @@ void MapEngine::setViewLocation(QPointF latLonCoordinate)
     }
 }
 
+void MapEngine::setViewLocation(QPointF latLonCoordinate)
+{
+    setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
+}
+
 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
 {
     QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
index 58fd052..9136533 100644 (file)
@@ -46,6 +46,15 @@ public:
     * @param parent Parent
     */
     MapEngine(QObject *parent = 0);
+
+    /**
+    * @brief MapEngine initializer
+    *
+    * Set initial location and zoom level for the engine. locationChanged and
+    * zoomLevelChanged signals are emitted, so init should be called after
+    * those signals are connected to MapView.
+    */
+    void init();
     
     /**
     * @brief Convert tile x & y numbers to MapScene coordinates
@@ -72,7 +81,8 @@ public:
     QGraphicsScene* scene();
 
     /**
-    * @brief Set view location
+    * @brief Helper for setting view location based on latitude and longitude
+    * coordinates
     *
     * @param latLonCoordinate Latitude & longitude coordinates for location
     */
@@ -85,27 +95,26 @@ public:
     * @param latLonCoordinate latitude and longitude values
     * @return QPoint tile x,y value
     */
-    static QPoint convertLatLonToTile(int zoomLevel, QPointF latLonCoordinate)
+    static QPointF convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
     {
+        /// @todo CREATE TEST CASE & CHECK CALCULATION
         qDebug() << __PRETTY_FUNCTION__;
 
         qreal longitude = latLonCoordinate.x();
         qreal latitude = latLonCoordinate.y();
 
-        if ((zoomLevel > MAX_ZOOM_LEVEL) || (zoomLevel < MIN_ZOOM_LEVEL))
-            return QPoint(UNDEFINED, UNDEFINED);
         if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
             return QPoint(UNDEFINED, UNDEFINED);
         if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
             return QPoint(UNDEFINED, UNDEFINED);
 
-        qreal z = static_cast<qreal>(1 << zoomLevel);
+        qreal z = static_cast<qreal>(1 << MAX_ZOOM_LEVEL);
 
         qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
         qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
                                     / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
 
-        return QPoint(qFloor(x*z), qFloor(y*z));
+        return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y);
     }
 
 private:
index f39e808..29484f9 100644 (file)
 MapScene::MapScene(QObject *parent)
     : QGraphicsScene(parent)
 {
+    /// @todo Write test case for setting scene size
+    const int maxTilesPerSide = (1 << MAX_ZOOM_LEVEL);
+    const int maxPixelsPerSide = maxTilesPerSide * TILE_SIZE_X;
+    setSceneRect(0, 0, maxPixelsPerSide, maxPixelsPerSide);
 }
 
 void MapScene::addMapTile(MapTile *mapTile)
index 2425d62..a4692e6 100644 (file)
@@ -37,6 +37,7 @@ public:
     /**
     * @brief Constructor
     *
+    * Scene size is set to the amount of pixels on closest zoom level
     * @param parent Parent
     */
     MapScene(QObject *parent = 0);
index 704d85f..3a5d24f 100644 (file)
@@ -66,6 +66,7 @@ void MapTile::setPosition()
             (m_tileNumber.x() >= 0) && (m_tileNumber.x() <= maxTileNumber) &&
             (m_tileNumber.y() >= 0) && (m_tileNumber.y() <= maxTileNumber)) {
         setPos(MapEngine::convertTileNumberToSceneCoordinate(m_zoomLevel, m_tileNumber));
+        //qDebug() << __PRETTY_FUNCTION__ << "tile position:" << pos();
         return; // done
     }
     // else
index 1b84e23..50cc57d 100644 (file)
@@ -60,6 +60,7 @@ void MapView::mouseMoveEvent(QMouseEvent *event)
     m_scenePosition += m_mousePosition - mapToScene(event->pos());
 
     emit viewScrolled(m_scenePosition);
+    //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
 
     m_mousePosition = mapToScene(event->pos());
 }
@@ -73,5 +74,6 @@ void MapView::mousePressEvent(QMouseEvent *event)
 
 void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
 {
+    //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
     centerOn(sceneCoordinate);
 }
index 7f99866..62515e3 100644 (file)
@@ -40,5 +40,5 @@ MapViewScreen::MapViewScreen(QWidget *parent)
    mapViewLayout->addWidget(mapView);
    setLayout(mapViewLayout);
 
-   mapEngine->setViewLocation(QPointF(25.5000, 65.0000));
+   mapEngine->init();
 }