First phase of userdata rewrite
[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        Pekka Nissinen - pekka.nissinen@ixonos.com
8        Ville Tiensuu - ville.tiensuu@ixonos.com
9
10    Situare is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as published by the Free Software Foundation.
13
14    Situare is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Situare; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22    USA.
23 */
24
25 #include <QtAlgorithms>
26 #include <QDebug>
27 #include <QString>
28 #include <QStringList>
29 #include <QUrl>
30 #include <QHash>
31 #include <QHashIterator>
32 #include <QRect>
33
34 #include "common.h"
35 #include "frienditemshandler.h"
36 #include "gpslocationitem.h"
37 #include "mapcommon.h"
38 #include "mapfetcher.h"
39 #include "mapscene.h"
40 #include "mapscroller.h"
41 #include "maptile.h"
42 #include "network/networkaccessmanager.h"
43 #include "ownlocationitem.h"
44 #include "user/user.h"
45
46 #include "mapengine.h"
47
48 const int SMOOTH_CENTERING_TIME_MS = 1000;
49
50 MapEngine::MapEngine(QObject *parent)
51     : QObject(parent),
52       m_autoCenteringEnabled(false),
53       m_scrollStartedByGps(false),
54       m_smoothScrollRunning(false),
55       m_zoomedIn(false),
56       m_zoomLevel(DEFAULT_ZOOM_LEVEL),
57       m_centerTile(QPoint(UNDEFINED, UNDEFINED)),
58       m_lastAutomaticPosition(QPoint(0, 0)),
59       m_tilesGridSize(QSize(0, 0)),
60       m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     m_mapScene = new MapScene(this);
65
66     m_mapFetcher = new MapFetcher(NetworkAccessManager::instance(), this);
67     connect(this, SIGNAL(fetchImage(int, int, int)),
68             m_mapFetcher, SLOT(enqueueFetchMapImage(int, int, int)));
69     connect(m_mapFetcher, SIGNAL(mapImageReceived(int, int, int, QPixmap)),
70             this, SLOT(mapImageReceived(int, int, int, QPixmap)));
71     connect(m_mapFetcher, SIGNAL(error(int, int)),
72             this, SIGNAL(error(int, int)));
73
74     m_ownLocation = new OwnLocationItem();
75     m_ownLocation->hide(); // hide until first location info is received
76     m_mapScene->addItem(m_ownLocation);
77
78     m_gpsLocationItem = new GPSLocationItem();
79     m_mapScene->addItem(m_gpsLocationItem);
80
81     m_friendItemsHandler = new FriendItemsHandler(m_mapScene, this);
82     connect(this, SIGNAL(zoomLevelChanged(int)),
83             m_friendItemsHandler, SLOT(refactorFriendItems(int)));
84
85     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
86             m_friendItemsHandler, SLOT(friendListUpdated(QList<User*>&)));
87
88     connect(this, SIGNAL(friendImageReady(User*)),
89             m_friendItemsHandler, SLOT(friendImageReady(User*)));
90
91     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
92             this, SLOT(friendsPositionsUpdated()));
93
94     connect(m_friendItemsHandler, SIGNAL(locationItemClicked(QList<QString>)),
95             this, SIGNAL(locationItemClicked(QList<QString>)));
96
97     m_scroller = &MapScroller::getInstance();
98
99     connect(m_scroller, SIGNAL(coordinateUpdated(QPoint)),
100             this, SLOT(setCenterPosition(QPoint)));
101
102     connect(m_scroller, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)),
103             this, SLOT(scrollerStateChanged(QAbstractAnimation::State)));
104 }
105
106 MapEngine::~MapEngine()
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109
110     QSettings settings(DIRECTORY_NAME, FILE_NAME);
111     settings.setValue(MAP_LAST_POSITION,
112                       convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate));
113     settings.setValue(MAP_LAST_ZOOMLEVEL, m_zoomLevel);
114 }
115
116 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
121
122     QPoint topLeft;
123     topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
124     topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
125
126     return QRect(topLeft, m_tilesGridSize);
127 }
128
129 QPointF MapEngine::centerGeoCoordinate()
130 {
131     qDebug() << __PRETTY_FUNCTION__;
132
133     return convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate);
134 }
135
136 void MapEngine::centerToCoordinates(QPointF latLonCoordinate)
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     scrollToPosition(convertLatLonToSceneCoordinate(latLonCoordinate));
141 }
142
143 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     qreal longitude = latLonCoordinate.x();
148     qreal latitude = latLonCoordinate.y();
149
150     if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
151         return QPoint(UNDEFINED, UNDEFINED);
152     if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
153         return QPoint(UNDEFINED, UNDEFINED);
154
155     qreal z = static_cast<qreal>(MapEngine::tilesPerSide(MAX_MAP_ZOOM_LEVEL));
156
157     qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
158     qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
159                                 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
160
161     return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
162 }
163
164 QPointF MapEngine::convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     double tileFactor = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
169     double xFactor = (sceneCoordinate.x() / (TILE_SIZE_X*tileFactor));
170     double yFactor = (sceneCoordinate.y() / (TILE_SIZE_Y*tileFactor));
171
172     tileFactor = 1 << zoomLevel;
173     double longitude = xFactor / tileFactor * 360.0 - 180;
174
175     double n = M_PI - 2.0 * M_PI * yFactor / tileFactor;
176     double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
177
178     return QPointF(longitude, latitude);
179 }
180
181 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
182 {
183     qDebug() << __PRETTY_FUNCTION__;
184
185     int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
186     int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
187     int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
188
189     return QPoint(x, y);
190 }
191
192 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
197     int x = tileNumber.x() * TILE_SIZE_X * pow;
198     int y = tileNumber.y() * TILE_SIZE_Y * pow;
199
200     return QPoint(x, y);
201 }
202
203 void MapEngine::disableAutoCenteringIfRequired(QPoint sceneCoordinate)
204 {
205     if (isAutoCenteringEnabled()) {
206         int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
207
208         QPoint oldPixelValue = QPoint(m_lastAutomaticPosition.x() / zoomFactor,
209                                       m_lastAutomaticPosition.y() / zoomFactor);
210
211         QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
212                                       sceneCoordinate.y() / zoomFactor);
213
214         if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE)
215             || (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE)) {
216
217             emit mapScrolledManually();
218         }
219     }
220 }
221
222 void MapEngine::friendsPositionsUpdated()
223 {
224     qDebug() << __PRETTY_FUNCTION__;
225
226     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
227 }
228
229 void MapEngine::getTiles(QPoint sceneCoordinate)
230 {
231     qDebug() << __PRETTY_FUNCTION__;
232
233     m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
234     updateViewTilesSceneRect();
235     m_mapScene->setTilesGrid(m_viewTilesGrid);
236
237     int topLeftX = m_viewTilesGrid.topLeft().x();
238     int topLeftY = m_viewTilesGrid.topLeft().y();
239     int bottomRightX = m_viewTilesGrid.bottomRight().x();
240     int bottomRightY = m_viewTilesGrid.bottomRight().y();
241
242     int tileMaxVal = tileMaxIndex(m_zoomLevel);
243
244     for (int x = topLeftX; x <= bottomRightX; ++x) {
245         for (int y = topLeftY; y <= bottomRightY; ++y) {
246
247             // map doesn't span in vertical direction, so y index must be inside the limits
248             if (y >= MAP_TILE_MIN_INDEX && y <= tileMaxVal) {
249                 if (!m_mapScene->tileInScene(tilePath(m_zoomLevel, x, y)))
250                     emit fetchImage(m_zoomLevel, normalize(x, MAP_TILE_MIN_INDEX, tileMaxVal), y);
251             }
252         }
253     }
254 }
255
256 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
257 {
258     qDebug() << __PRETTY_FUNCTION__;
259
260     m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
261     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
262
263     if (m_autoCenteringEnabled) {
264         m_lastAutomaticPosition = convertLatLonToSceneCoordinate(position);
265         m_scrollStartedByGps = true;
266         scrollToPosition(m_lastAutomaticPosition);
267     }
268 }
269
270 qreal MapEngine::greatCircleDistance(QPointF firstLocation, QPointF secondLocation)
271 {
272     qDebug() << __PRETTY_FUNCTION__;
273
274     const qreal TORAD = (M_PI/180);
275
276     qreal dLat = (secondLocation.y() - firstLocation.y())*TORAD;
277     qreal dLon = (secondLocation.x() - firstLocation.x())*TORAD;
278     qreal a = pow(sin(dLat/2),2) + cos(firstLocation.y()*TORAD) * cos(secondLocation.y()*TORAD)
279               * pow(sin(dLon/2),2);
280     qreal c = 2 * atan2(sqrt(a), sqrt(1-a));
281
282     return (EARTH_RADIUS * c);
283 }
284
285 void MapEngine::init()
286 {
287     qDebug() << __PRETTY_FUNCTION__;
288
289     QPointF startLocation;
290     QSettings settings(DIRECTORY_NAME, FILE_NAME);
291
292     if (settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString()
293         == ERROR_VALUE_NOT_FOUND_ON_SETTINGS || settings.value(MAP_LAST_ZOOMLEVEL,
294         ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
295
296         startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
297         m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
298     } else {
299         m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
300         startLocation = settings.value(MAP_LAST_POSITION,
301                                        ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
302     }
303
304     emit zoomLevelChanged(m_zoomLevel);
305     centerToCoordinates(QPointF(startLocation.x(), startLocation.y()));
306 }
307
308 bool MapEngine::isAutoCenteringEnabled()
309 {
310     return m_autoCenteringEnabled;
311 }
312
313 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
314 {
315     qDebug() << __PRETTY_FUNCTION__;
316
317     QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
318     QPoint temp = m_centerTile;
319     m_centerTile = centerTile;
320
321     return (centerTile != temp);
322 }
323
324 qreal MapEngine::sceneResolution()
325 {
326     qDebug() << __PRETTY_FUNCTION__;
327
328     const int SHIFT = 200;
329     const int KM_TO_M = 1000;
330     qreal scale = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
331     QPointF centerCoordinate = centerGeoCoordinate();
332     QPoint shiftedSceneCoordinate = QPoint(m_sceneCoordinate.x() + SHIFT*scale
333                                            , m_sceneCoordinate.y());
334     QPointF shiftedCoordinate = convertSceneCoordinateToLatLon(m_zoomLevel, shiftedSceneCoordinate);
335     qreal dist = greatCircleDistance(centerCoordinate, shiftedCoordinate) * KM_TO_M;
336     return (dist / SHIFT);
337 }
338
339 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
340 {
341     qDebug() << __PRETTY_FUNCTION__;
342
343     // add normal tile inside the world
344     QPoint tileNumber(x, y);
345     m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
346
347     // note: add 1 so odd width is rounded up and even is rounded down
348     int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
349
350     // duplicate to east side? (don't need to duplicate over padding)
351     if (tileNumber.x() < (tilesGridWidthHalf - GRID_PADDING)) {
352         QPoint adjustedTileNumber(tileNumber.x() + tileMaxIndex(zoomLevel) + 1, tileNumber.y());
353         m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
354     }
355
356     // duplicate to west side? (don't need to duplicate over padding)
357     if (tileNumber.x() > (tileMaxIndex(zoomLevel) - tilesGridWidthHalf + GRID_PADDING)) {
358         QPoint adjustedTileNumber(tileNumber.x() - tileMaxIndex(zoomLevel) - 1, tileNumber.y());
359         m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
360     }
361 }
362
363 int MapEngine::normalize(int value, int min, int max)
364 {
365     qDebug() << __PRETTY_FUNCTION__;
366     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
367
368     while (value < min)
369         value += max - min + 1;
370
371     while (value > max)
372         value -= max - min + 1;
373
374     return value;
375 }
376
377 void MapEngine::receiveOwnLocation(User *user)
378 {
379     qDebug() << __PRETTY_FUNCTION__;
380
381     if(user) {
382         QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
383         if (m_ownLocation->pos().toPoint() != newPosition) {
384             m_ownLocation->setPos(newPosition);
385         }
386
387         if (!m_ownLocation->isVisible())
388             m_ownLocation->show();
389     } else {
390         m_ownLocation->hide();
391     }
392
393     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
394 }
395
396 QGraphicsScene* MapEngine::scene()
397 {
398     qDebug() << __PRETTY_FUNCTION__;
399
400     return m_mapScene;
401 }
402
403 void MapEngine::scrollerStateChanged(QAbstractAnimation::State newState)
404 {
405     qDebug() << __PRETTY_FUNCTION__;
406
407     if (m_smoothScrollRunning
408         && newState != QAbstractAnimation::Running) {
409             m_smoothScrollRunning = false;
410
411             // don't disable auto centering if current animation was stopped by new update from GPS
412             if (!m_scrollStartedByGps)
413                 disableAutoCenteringIfRequired(m_sceneCoordinate);
414     }
415
416     m_scrollStartedByGps = false;
417 }
418
419 void MapEngine::scrollToPosition(QPoint scenePosition)
420 {
421     qDebug() << __PRETTY_FUNCTION__;
422
423     m_scroller->stop();
424     m_scroller->setEasingCurve(QEasingCurve::InOutQuart);
425     m_scroller->setDuration(SMOOTH_CENTERING_TIME_MS);
426     m_scroller->setStartValue(m_sceneCoordinate);
427     m_scroller->setEndValue(scenePosition);
428     m_smoothScrollRunning = true;
429     m_scroller->start();
430 }
431
432 void MapEngine::setAutoCentering(bool enabled)
433 {
434     qDebug() << __PRETTY_FUNCTION__;
435
436     m_autoCenteringEnabled = enabled;
437 }
438
439 void MapEngine::setCenterPosition(QPoint scenePosition)
440 {
441     qDebug() << __PRETTY_FUNCTION__;
442
443     // jump to opposite side of the world if world horizontal limit is exceeded
444     scenePosition.setX(normalize(scenePosition.x(), MAP_MIN_PIXEL_X, MAP_MAX_PIXEL_X));
445
446     // don't allow vertical scene coordinates go out of the map
447     scenePosition.setY(qBound(MAP_MIN_PIXEL_Y, scenePosition.y(), MAP_MAX_PIXEL_Y));
448
449     if (!m_smoothScrollRunning)
450         disableAutoCenteringIfRequired(scenePosition);
451
452     m_sceneCoordinate = scenePosition;
453     emit locationChanged(m_sceneCoordinate);
454
455     if (isCenterTileChanged(scenePosition)) {
456         getTiles(scenePosition);
457         m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
458     }
459
460     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
461     emit newMapResolution(sceneResolution());
462 }
463
464 void MapEngine::setGPSEnabled(bool enabled)
465 {
466     qDebug() << __PRETTY_FUNCTION__;
467
468     m_gpsLocationItem->setEnabled(enabled);
469 }
470
471 void MapEngine::setZoomLevel(int newZoomLevel)
472 {
473     qDebug() << __PRETTY_FUNCTION__;
474
475     m_zoomLevel = newZoomLevel;
476     zoomed();
477 }
478
479 void MapEngine::setTilesGridSize(const QSize &viewSize)
480 {
481     qDebug() << __PRETTY_FUNCTION__;
482
483     // there must be scrolling reserve of at least half tile added to tile amount
484     // calculated from view size
485     const qreal SCROLLING_RESERVE = 0.5;
486
487     // converting scene tile to tile number does cause grid centering inaccuracy of one tile
488     const int CENTER_TILE_INACCURACY = 1;
489
490     int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
491                     + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
492     int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
493                      + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
494
495     m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
496
497     m_tilesGridSize.setHeight(gridHeight);
498     m_tilesGridSize.setWidth(gridWidth);
499 }
500
501 int MapEngine::tileMaxIndex(int zoomLevel)
502 {
503     qDebug() << __PRETTY_FUNCTION__;
504
505     // subtract one because first tile index is zero
506     return tilesPerSide(zoomLevel) - 1;
507 }
508
509 QString MapEngine::tilePath(int zoomLevel, int x, int y)
510 {
511     qDebug() << __PRETTY_FUNCTION__;
512
513     QString tilePathString(QString::number(zoomLevel) + "/");
514     tilePathString.append(QString::number(x) + "/");
515     tilePathString.append(QString::number(y));
516
517     return tilePathString;
518 }
519
520 int MapEngine::tilesPerSide(int zoomLevel)
521 {
522     return (1 << zoomLevel);
523 }
524
525 void MapEngine::updateViewTilesSceneRect()
526 {
527     qDebug() << __PRETTY_FUNCTION__;
528
529     const QPoint ONE_TILE = QPoint(1, 1);
530     const QPoint ONE_PIXEL = QPoint(1, 1);
531
532     QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
533     // one tile - one pixel is added because returned coordinates are pointing to upper left corner
534     // of the last tile.
535     QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
536                                                             m_viewTilesGrid.bottomRight()
537                                                              + ONE_TILE) - ONE_PIXEL;
538
539     m_mapScene->tilesSceneRectUpdated(QRect(topLeft, bottomRight));
540 }
541
542 void MapEngine::viewResized(const QSize &size)
543 {
544     qDebug() << __PRETTY_FUNCTION__;
545
546     m_viewSize = size;
547     setTilesGridSize(m_viewSize);
548
549     emit locationChanged(m_sceneCoordinate);
550     getTiles(m_sceneCoordinate);
551     m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
552     m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
553 }
554
555 void MapEngine::viewZoomFinished()
556 {
557     qDebug() << __PRETTY_FUNCTION__;
558
559     if (m_zoomedIn) {
560         m_zoomedIn = false;
561         m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
562     }
563
564     if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
565         emit maxZoomLevelReached();
566     else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
567         emit minZoomLevelReached();
568 }
569
570 void MapEngine::zoomed()
571 {
572     emit zoomLevelChanged(m_zoomLevel);
573     m_mapScene->setTilesDrawingLevels(m_zoomLevel);
574     m_mapScene->setZoomLevel(m_zoomLevel);
575     getTiles(m_sceneCoordinate);
576     m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
577     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
578     emit newMapResolution(sceneResolution());
579 }
580
581 void MapEngine::zoomIn()
582 {
583     qDebug() << __PRETTY_FUNCTION__;
584
585     if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
586         m_zoomLevel++;
587         m_zoomedIn = true;
588         zoomed();
589     }
590 }
591
592 void MapEngine::zoomOut()
593 {
594     qDebug() << __PRETTY_FUNCTION__;
595
596     if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
597         m_zoomLevel--;
598         zoomed();
599     }
600 }