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