Changes for setting the zValue of the MapTile
[situare] / src / map / maptile.cpp
index 8232075..c538f5d 100644 (file)
@@ -1,40 +1,68 @@
- /*
-    Situare - A location system for Facebook
-    Copyright (C) 2010  Ixonos Plc. Authors:
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
 
-        Sami Rämö - sami.ramo@ixonos.com
+       Sami Rämö - sami.ramo@ixonos.com
 
-    Situare is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License
-    as published by the Free Software Foundation; either version 2
-    of the License, or (at your option) any later version.
+   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.
+   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.
- */
+   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 <QTransform>
 
+#include "mapcommon.h"
+#include "mapengine.h"
 #include "maptile.h"
 
 MapTile::MapTile()
-{
+    : m_tileNumber(QPoint(UNDEFINED, UNDEFINED))
+    , m_zoomLevel(UNDEFINED)
+{    
+    setPos(UNDEFINED, UNDEFINED);
+    setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
 }
 
-quint8 MapTile::zoomLevel()
+int MapTile::zoomLevel()
 {
     return m_zoomLevel;
 }
 
-void MapTile::setZoomLevel(quint8 zoomLevel)
+void MapTile::setZoomLevel(int tileZoomLevel, int currentViewZoomLevel)
 {
-    m_zoomLevel = zoomLevel;
+    m_zoomLevel = tileZoomLevel;
+    setPosition();
+
+    setSceneLevel(currentViewZoomLevel);
+
+    qreal zoomFactor = static_cast<qreal>(1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
+    setScale(zoomFactor);
+}
+
+void MapTile::setSceneLevel(int currentZoomLevel)
+{
+    if (currentZoomLevel < m_zoomLevel) {
+        qreal z = static_cast<qreal>(MIN_MAP_SCENE_NORMAL_LEVEL + currentZoomLevel
+                                     - (m_zoomLevel - currentZoomLevel)) + 0.5;
+        setZValue(z);
+    }
+    else
+        setZValue(static_cast<qreal>(MIN_MAP_SCENE_NORMAL_LEVEL + m_zoomLevel));
+
+//    qDebug() << __PRETTY_FUNCTION__ << "Tile:" << m_tileNumber
+//                                    << "m_zoomLevel" << m_zoomLevel
+//                                    << "zValue:" << zValue();
 }
 
 QPoint MapTile::tileNumber()
@@ -45,4 +73,22 @@ QPoint MapTile::tileNumber()
 void MapTile::setTileNumber(QPoint tileNumber)
 {
     m_tileNumber = tileNumber;
+    setPosition();
 }
+
+void MapTile::setPosition()
+{
+    const int maxTileNumber = (1 << m_zoomLevel) - 1;
+
+    if ((m_zoomLevel >= MIN_MAP_ZOOM_LEVEL) && (m_zoomLevel <= MAX_MAP_ZOOM_LEVEL) &&
+        (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();
+    }
+    else {
+        setPos(UNDEFINED, UNDEFINED);
+    }
+}
+