Repaired defects found in review and removed MapView dependency from MapEngine
[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 "common.h"
26 #include "mapengine.h"
27 #include "maptile.h"
28
29 MapTile::MapTile()
30 {
31     m_tileNumber = QPoint(UNDEFINED, UNDEFINED);
32     m_zoomLevel = UNDEFINED;
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       * @todo DONE use bit shifting
47       */
48     qreal zoomFactor = (qreal)(1 << (MAX_ZOOM_LEVEL - m_zoomLevel));
49     setScale(zoomFactor);
50 }
51
52 QPoint MapTile::tileNumber()
53 {
54     return m_tileNumber;
55 }
56
57 void MapTile::setTileNumber(QPoint tileNumber)
58 {
59     m_tileNumber = tileNumber;
60     setPosition();
61 }
62
63 void MapTile::setPosition()
64 {
65     /**
66       * @todo DONE use bit shifting
67       */
68     const int maxTileNumber = (1 << m_zoomLevel) - 1;
69
70     if ((m_zoomLevel >= MIN_ZOOM_LEVEL) && (m_zoomLevel <= MAX_ZOOM_LEVEL) &&
71             (m_tileNumber.x() >= 0) && (m_tileNumber.x() <= maxTileNumber) &&
72             (m_tileNumber.y() >= 0) && (m_tileNumber.y() <= maxTileNumber)) {
73         setPos(MapEngine::convertTileNumberToSceneCoordinate(m_zoomLevel, m_tileNumber));
74         return; // done
75     }
76     // else
77     setPos(UNDEFINED, UNDEFINED);
78     //Q_ASSERT_X(false, __PRETTY_FUNCTION__, "m_zoomLevel and/or m_tileNumber is undefined");
79 }
80