Integrated fetching of the maps from OSM server
[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 "mapfetcher.h"
31 #include "mapscene.h"
32 #include "mapview.h"
33
34
35 /// \brief Map engine
36 ///
37 /// Logic for controlling map functionality. Does also include static methods for
38 /// converting coordinates.
39 /// \author Sami Rämö - sami.ramo (at) ixonos.com
40 class MapEngine : public QObject
41 {
42     Q_OBJECT
43 public:
44     /// \brief Constructor
45     ///
46     /// \param mapView View for map
47     /// \param parent Parent
48     /// Does create and add scene to map view.
49     MapEngine(MapView *mapView, QWidget *parent = 0);
50
51     /// \brief Convert tile x & y numbers to MapScene coordinates
52     ///
53     /// \param zoomLevel Zoom level
54     /// \param tileNumber x & y number of the tile
55     /// \return MapScene coordinate
56     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
57     {
58         int x = tileNumber.x() * TILE_SIZE_X * pow(2, MAX_ZOOM_LEVEL - zoomLevel);
59         int y = tileNumber.y() * TILE_SIZE_Y * pow(2, MAX_ZOOM_LEVEL - zoomLevel);
60
61         return QPoint(x, y);
62     }
63
64     /// \brief Set view location
65     ///
66     /// \param latLonCoordinate Latitude & longitude coordinates for location
67     void setViewLocation(QPointF latLonCoordinate);
68
69     QPoint latLonToTile(qreal latitude, qreal longitude, int zoom);
70
71     /**
72     * @brief Transforms tile x value to longitude.
73     *
74     * @fn tileXToLongitude
75     * @param x tile x value
76     * @param zoom zoom value
77     * @return qreal longitude
78     */
79     qreal tileXToLongitude(int x, int zoom);
80
81     /**
82     * @brief Transforms tile y value to latitude.
83     *
84     * @fn tileYToLatitude
85     * @param y tile y value
86     * @param zoom zoom value
87     * @return qreal latitude
88     */
89     qreal tileYToLatitude(int y, int zoom);
90
91 private slots:
92     /**
93     * @brief Slot for received map tile images
94     *
95     * Does add MapTile objects to MapScene. Zoom level and location is parsed from URL.
96     * @fn mapImageReceived
97     * @param url URL of the received image
98     * @param pixmap Received pixmap
99     */
100     void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
101
102 public:
103     static const int TILE_SIZE_X = 256; ///< Tile image size in x direction
104     static const int TILE_SIZE_Y = 256; ///< Tile image size in y direction
105     static const int MIN_ZOOM_LEVEL = 0; ///< Minimum zoom level
106     static const int MAX_ZOOM_LEVEL = 18; ///< Maximum zoom level
107
108 private:
109     MapView *m_mapView; ///< View for viewing map
110     MapScene *m_mapScene; ///< Scene for map tiles
111     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
112     int m_zoomLevel; ///< Current zoom level
113 };
114
115 #endif // MAPENGINE_H