5bffa136b7bcb56c6bca3a53e60ed4d8b8faf0d9
[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 <math.h>
27
28 #include <QtCore>
29
30 #include "mapview.h"
31 #include "mapscene.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 class MapEngine : public QObject
40 {
41     Q_OBJECT
42 public:
43     /// \brief Constructor
44     ///
45     /// \param mapView View for map
46     /// \param parent Parent
47     /// Does create and add scene to map view.
48     MapEngine(MapView *mapView, QWidget *parent = 0);
49
50     /// \brief Convert tile x & y numbers to MapScene coordinates
51     ///
52     /// \param zoomLevel Zoom level
53     /// \param tileNumber x & y number of the tile
54     /// \return MapScene coordinate
55     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
56     {
57         int x = tileNumber.x() * TILE_SIZE_X * pow(2, MAX_ZOOM_LEVEL - zoomLevel);
58         int y = tileNumber.y() * TILE_SIZE_Y * pow(2, MAX_ZOOM_LEVEL - zoomLevel);
59
60         return QPoint(x, y);
61     }
62
63     /// \brief Set view location
64     ///
65     /// \param latLonCoordinate Latitude & longitude coordinates for location
66     void setViewLocation(QPointF latLonCoordinate);
67
68     QPoint latLonToTile(qreal latitude, qreal longitude, int zoom);
69
70     /**
71     * @brief Transforms tile x value to longitude.
72     *
73     * @fn tileXToLongitude
74     * @param x tile x value
75     * @param zoom zoom value
76     * @return qreal longitude
77     */
78     qreal tileXToLongitude(int x, int zoom);
79
80     /**
81     * @brief Transforms tile y value to latitude.
82     *
83     * @fn tileYToLatitude
84     * @param y tile y value
85     * @param zoom zoom value
86     * @return qreal latitude
87     */
88     qreal tileYToLatitude(int y, int zoom);
89
90 private slots:
91     void mapImageReceived(const QUrl *url, const QPixmap *pixmap);
92
93 public:
94     static const int TILE_SIZE_X = 256; ///< Tile image size in x direction
95     static const int TILE_SIZE_Y = 256; ///< Tile image size in y direction
96     static const int MIN_ZOOM_LEVEL = 0; ///< Minimum zoom level
97     static const int MAX_ZOOM_LEVEL = 18; ///< Maximum zoom level
98
99 private:
100     MapView *m_mapView; ///< View for viewing map
101     MapScene *m_mapScene; ///< Scene for map tiles
102     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
103     int m_zoomLevel; ///< Current zoom level
104 };
105
106 #endif // MAPENGINE_H