Finalised review and removed review comments
[situare] / src / map / maptile.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Sami Rämö - sami.ramo@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QDebug>
23 #include <QTransform>
24
25 #include "mapcommon.h"
26 #include "mapengine.h"
27 #include "maptile.h"
28
29 MapTile::MapTile()
30     : m_tileNumber(QPoint(UNDEFINED, UNDEFINED))
31     , m_zoomLevel(UNDEFINED)
32 {    
33     setPos(UNDEFINED, UNDEFINED);
34 }
35
36 int MapTile::zoomLevel()
37 {
38     return m_zoomLevel;
39 }
40
41 void MapTile::setZoomLevel(int zoomLevel)
42 {
43     m_zoomLevel = zoomLevel;
44     setPosition();
45
46     setZValue(static_cast<qreal>(MIN_MAP_SCENE_NORMAL_LEVEL + zoomLevel));
47
48     qreal zoomFactor = static_cast<qreal>(1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
49     setScale(zoomFactor);
50 }
51
52 void MapTile::setSceneLevel(int currentZoomLevel)
53 {
54     if (currentZoomLevel < m_zoomLevel) {
55         qreal z = static_cast<qreal>(MIN_MAP_SCENE_NORMAL_LEVEL + currentZoomLevel
56                                      - (m_zoomLevel - currentZoomLevel)) + 0.5;
57         setZValue(z);
58     }
59     else
60         setZValue(static_cast<qreal>(MIN_MAP_SCENE_NORMAL_LEVEL + m_zoomLevel));
61
62 //    qDebug() << __PRETTY_FUNCTION__ << "Tile:" << m_tileNumber
63 //                                    << "m_zoomLevel" << m_zoomLevel
64 //                                    << "zValue:" << zValue();
65 }
66
67 QPoint MapTile::tileNumber()
68 {
69     return m_tileNumber;
70 }
71
72 void MapTile::setTileNumber(QPoint tileNumber)
73 {
74     m_tileNumber = tileNumber;
75     setPosition();
76 }
77
78 void MapTile::setPosition()
79 {
80     const int maxTileNumber = (1 << m_zoomLevel) - 1;
81
82     if ((m_zoomLevel >= MIN_MAP_ZOOM_LEVEL) && (m_zoomLevel <= MAX_MAP_ZOOM_LEVEL) &&
83         (m_tileNumber.x() >= 0) && (m_tileNumber.x() <= maxTileNumber) &&
84         (m_tileNumber.y() >= 0) && (m_tileNumber.y() <= maxTileNumber)) {
85
86         setPos(MapEngine::convertTileNumberToSceneCoordinate(m_zoomLevel, m_tileNumber));
87         //qDebug() << __PRETTY_FUNCTION__ << "tile position:" << pos();
88     }
89     else {
90         setPos(UNDEFINED, UNDEFINED);
91     }
92 }
93