01641bed281664c43c0856df2cd6ceadcb7beeba
[situare] / src / map / mapengine.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        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 #include <QDebug>
24 #include <QString>
25 #include <QStringList>
26 #include <QUrl>
27
28 #include "mapengine.h"
29 #include "maptile.h"
30
31 #include <QtCore>
32 #include <QtGlobal>
33
34 MapEngine::MapEngine(QObject *parent)
35     : QObject(parent)
36 {
37     m_mapScene = new MapScene(this);
38     m_zoomLevel = 14;
39
40     m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
41     connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
42             SLOT(mapImageReceived(QUrl, QPixmap)));
43 }
44
45 void MapEngine::setViewLocation(QPointF latLonCoordinate)
46 {
47     emit zoomLevelChanged(m_zoomLevel);
48
49     /// Fetch some map tiles for demo purposes
50     for (int x=9351; x<=9354; x++) {
51         for (int y=4261; y<=4264; y++) {
52             QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
53             m_mapFetcher->fetchMapImage(url);
54         }
55     }
56 }
57
58 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
59 {
60     QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
61                   .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
62
63     return QUrl(url);
64 }
65
66 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
67 {
68     QString path = url.path();
69     QStringList pathParts = path.split("/", QString::SkipEmptyParts);
70
71     int size = pathParts.size();
72
73     if (size < 3)
74         return;
75
76     zoom = (pathParts.at(size-3)).toInt();
77     x = (pathParts.at(size-2)).toInt();
78     QString yString = pathParts.at(size-1);
79     yString.chop(4);
80     y = yString.toInt();
81 }
82
83 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
84 {
85     int zoom = -1;
86     int x = -1;
87     int y = -1;
88
89     parseURL(url, zoom, x, y);
90
91     MapTile *mapTile = new MapTile();
92     mapTile->setZoomLevel(zoom);
93     mapTile->setTileNumber(QPoint(x, y));
94     mapTile->setPixmap(pixmap);
95     m_mapScene->addMapTile(mapTile);
96 }
97
98 QGraphicsScene* MapEngine::scene()
99 {
100     return dynamic_cast<QGraphicsScene *>(m_mapScene);
101 }
102
103 void MapEngine::setLocation(QPointF sceneCoordinate)
104 {
105     emit locationChanged(sceneCoordinate);
106 }