Removed review defect comments and fixed few other comments
[situare] / src / map / mapengine.h
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        Jussi Laitinen - jussi.laitinen@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #ifndef MAPENGINE_H
24 #define MAPENGINE_H
25
26 #include <QtCore>
27
28 #include "common.h"
29 #include "mapfetcher.h"
30 #include "mapscene.h"
31 #include "mapview.h"
32
33 /**
34 * @brief Map engine
35 *
36 * Logic for controlling map functionality. Does also include static methods for
37 * converting coordinates.
38 * @author Sami Rämö - sami.ramo (at) ixonos.com
39 */
40 class MapEngine : public QObject
41 {
42     Q_OBJECT
43 public:
44     /**
45     * @brief Constructor
46     *
47     * @param parent Parent
48     */
49     MapEngine(QWidget *parent = 0);
50     
51     /**
52     * @brief Convert tile x & y numbers to MapScene coordinates
53     *
54     * @param zoomLevel Zoom level
55     * @param tileNumber x & y numbers of the tile
56     * @return QPoint MapScene coordinate
57     */
58
59     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
60     {
61         int pow = 1 << (MAX_ZOOM_LEVEL - zoomLevel);
62         int x = tileNumber.x() * TILE_SIZE_X * pow;
63         int y = tileNumber.y() * TILE_SIZE_Y * pow;
64
65         return QPoint(x, y);
66     }
67
68     /**
69     * @brief Getter for scene
70     *
71     * @return QGraphicsScene
72     */
73     QGraphicsScene* scene();
74
75     /**
76     * @brief Set view location
77     *
78     * @param latLonCoordinate Latitude & longitude coordinates for location
79     */
80     void setViewLocation(QPointF latLonCoordinate);
81
82     /**
83     * @brief Converts latitude, longitude and zoom to tile x, y values.
84     *
85     * @param zoomLevel zoom level
86     * @param latLonCoordinate latitude and longitude values
87     * @return QPoint tile x,y value
88     */
89     static QPoint convertLatLonToTile(int zoomLevel, QPointF latLonCoordinate) {
90         qDebug() << __PRETTY_FUNCTION__;
91
92         qreal longitude = latLonCoordinate.x();
93         qreal latitude = latLonCoordinate.y();
94
95         qreal z = static_cast<qreal>(1 << zoomLevel);
96
97         qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
98         qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
99                                     / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
100
101         return QPoint(qFloor(x*z), qFloor(y*z));
102     }
103
104 private:
105     /**
106     * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
107     *
108     * @param zoomLevel Zoom level
109     * @param tileNumbers Tile x & y numbers
110     * @return URL for the required tile
111     */
112     QUrl buildURL(int zoomLevel, QPoint tileNumbers);
113
114     /**
115     * @brief Parses given URL to zoom, x and y values. Parsed values are
116     * placed in variables given as parameters.
117     *
118     * @param url url to parse
119     * @param [out] zoom zoom variable
120     * @param [out] x x variable
121     * @param [out] y y variable
122     */
123     void parseURL(const QUrl &url, int &zoom, int &x, int &y);
124
125 private slots:
126     /**
127     * @brief Slot for received map tile images
128     *
129     * Does add MapTile objects to MapScene. Zoom level and location is parsed from URL.
130     * @param url URL of the received image
131     * @param pixmap Received pixmap
132     */
133     void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
134
135 signals:
136     /**
137     * @brief Signal for zoom level change
138     *
139     * @param newZoomLevel New zoom level
140     */
141     void zoomLevelChanged(int newZoomLevel);
142
143 private:
144     MapScene *m_mapScene; ///< Scene for map tiles
145     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
146     int m_zoomLevel; ///< Current zoom level
147 };
148
149 #endif // MAPENGINE_H