Try max 4 upper level images from cache
[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 "maptile.h"
41 #include "network/networkaccessmanager.h"
42 #include "ownlocationitem.h"
43 #include "user/user.h"
44
45 #include "mapengine.h"
46
47 MapEngine::MapEngine(QObject *parent)
48     : QObject(parent),
49       m_autoCenteringEnabled(false),
50       m_zoomedIn(false),
51       m_zoomLevel(DEFAULT_ZOOM_LEVEL),
52       m_centerTile(QPoint(UNDEFINED, UNDEFINED)),
53       m_lastManualPosition(QPoint(0, 0)),
54       m_tilesGridSize(QSize(0, 0)),
55       m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     m_mapScene = new MapScene(this);
60
61     m_mapFetcher = new MapFetcher(NetworkAccessManager::instance(), this);
62     connect(this, SIGNAL(fetchImage(int, int, int)),
63             m_mapFetcher, SLOT(enqueueFetchMapImage(int, int, int)));
64     connect(m_mapFetcher, SIGNAL(mapImageReceived(int, int, int, QPixmap)),
65             this, SLOT(mapImageReceived(int, int, int, QPixmap)));
66     connect(m_mapFetcher, SIGNAL(error(QString)),
67             this, SIGNAL(error(QString)));
68
69     m_ownLocation = new OwnLocationItem();
70     m_ownLocation->hide(); // hide until first location info is received
71     m_mapScene->addItem(m_ownLocation);
72
73     m_gpsLocationItem = new GPSLocationItem();
74     m_mapScene->addItem(m_gpsLocationItem);
75
76     m_friendItemsHandler = new FriendItemsHandler(m_mapScene, this);
77     connect(this, SIGNAL(zoomLevelChanged(int)),
78             m_friendItemsHandler, SLOT(refactorFriendItems(int)));
79
80     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
81             m_friendItemsHandler, SLOT(friendListUpdated(QList<User*>&)));
82
83     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
84             this, SLOT(friendsPositionsUpdated()));
85
86     connect(m_friendItemsHandler, SIGNAL(locationItemClicked(QList<QString>)),
87             this, SIGNAL(locationItemClicked(QList<QString>)));
88 }
89
90 MapEngine::~MapEngine()
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     QSettings settings(DIRECTORY_NAME, FILE_NAME);
95     settings.setValue(MAP_LAST_POSITION,
96                       convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate));
97     settings.setValue(MAP_LAST_ZOOMLEVEL, m_zoomLevel);
98 }
99
100 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
105
106     QPoint topLeft;
107     topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
108     topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
109
110     return QRect(topLeft, m_tilesGridSize);
111 }
112
113 QPointF MapEngine::centerGeoCoordinate()
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     return convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate);
118 }
119
120 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     qreal longitude = latLonCoordinate.x();
125     qreal latitude = latLonCoordinate.y();
126
127     if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
128         return QPoint(UNDEFINED, UNDEFINED);
129     if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
130         return QPoint(UNDEFINED, UNDEFINED);
131
132     qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
133
134     qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
135     qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
136                                 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
137
138     return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
139 }
140
141 QPointF MapEngine::convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     double tileFactor = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
146     double xFactor = (sceneCoordinate.x() / (TILE_SIZE_X*tileFactor));
147     double yFactor = (sceneCoordinate.y() / (TILE_SIZE_Y*tileFactor));
148
149     tileFactor = 1 << zoomLevel;
150     double longitude = xFactor / tileFactor * 360.0 - 180;
151
152     double n = M_PI - 2.0 * M_PI * yFactor / tileFactor;
153     double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
154
155     return QPointF(longitude, latitude);
156 }
157
158 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
163     int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
164     int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
165
166     return QPoint(x, y);
167 }
168
169 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
174     int x = tileNumber.x() * TILE_SIZE_X * pow;
175     int y = tileNumber.y() * TILE_SIZE_Y * pow;
176
177     return QPoint(x, y);
178 }
179
180 bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
181 {
182     if (isAutoCenteringEnabled()) {
183         int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
184
185         QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
186                                       m_lastManualPosition.y() / zoomFactor);
187
188         QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
189                                       sceneCoordinate.y() / zoomFactor);
190
191         if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
192             (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
193             return true;
194     }
195
196     return false;
197 }
198
199 void MapEngine::friendsPositionsUpdated()
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
204 }
205
206 void MapEngine::getTiles(QPoint sceneCoordinate)
207 {
208     qDebug() << __PRETTY_FUNCTION__;
209
210     m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
211     updateViewTilesSceneRect();
212
213     int topLeftX = m_viewTilesGrid.topLeft().x();
214     int topLeftY = m_viewTilesGrid.topLeft().y();
215     int bottomRightX = m_viewTilesGrid.bottomRight().x();
216     int bottomRightY = m_viewTilesGrid.bottomRight().y();
217
218     int tileMaxVal = tileMaxValue(m_zoomLevel);
219
220     for (int x = topLeftX; x <= bottomRightX; ++x) {
221         for (int y = topLeftY; y <= bottomRightY; ++y) {
222
223             int tileX = x;
224             int tileY = y;
225
226             // span in horizontal direction if world limits has been reached
227 //            if (tileX < 0)
228 //                tileX += tileMaxVal + 1;
229 //            else if (tileX > tileMaxVal)
230 //                tileX -= tileMaxVal + 1;
231
232             // map doesn't span in vertical direction
233             if (tileY < 0 || tileY > tileMaxVal)
234                 continue;
235
236             if (!m_mapScene->tileInScene(tilePath(m_zoomLevel, tileX, tileY)))
237                 emit fetchImage(m_zoomLevel, normalize(tileX, 0, tileMaxVal), tileY);
238         }
239     }
240
241 //    QRect spanRect = m_mapScene->spanItems(m_scrollDirection, m_zoomLevel, m_sceneCoordinate, m_viewSize);
242 //    m_friendItemsHandler->spanHiddenFriendLocationItems(m_scrollDirection, spanRect, m_sceneCoordinate);
243 //    m_friendItemsHandler->refactorFriendItems(m_zoomLevel);
244
245
246 }
247
248 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
249 {
250     qDebug() << __PRETTY_FUNCTION__;
251
252     m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
253     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
254
255     if (m_autoCenteringEnabled)
256         setViewLocation(position);
257 }
258
259 void MapEngine::init()
260 {
261     qDebug() << __PRETTY_FUNCTION__;
262
263     QPointF startLocation;
264     QSettings settings(DIRECTORY_NAME, FILE_NAME);
265
266     if (settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString()
267         == ERROR_VALUE_NOT_FOUND_ON_SETTINGS || settings.value(MAP_LAST_ZOOMLEVEL,
268         ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
269
270         startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
271         m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
272     } else {
273         m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
274         startLocation = settings.value(MAP_LAST_POSITION,
275                                        ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
276     }
277
278     emit zoomLevelChanged(m_zoomLevel);
279     setViewLocation(QPointF(startLocation.x(), startLocation.y()));
280 }
281
282 bool MapEngine::isAutoCenteringEnabled()
283 {
284     return m_autoCenteringEnabled;
285 }
286
287 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
288 {
289     qDebug() << __PRETTY_FUNCTION__;
290
291     QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
292     QPoint temp = m_centerTile;
293     m_centerTile = centerTile;
294
295     return (centerTile != temp);
296 }
297
298 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
299 {
300 //    if (y != 3)
301 //        return;
302
303     qDebug() << __PRETTY_FUNCTION__; // << "x:" << x << "y:" << y;
304
305     // add normal tile
306     QPoint tileNumber(x, y);
307     m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
308
309     // add 1 so odd width is rounded up and even is rounded down
310     int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
311
312     // expand to east side? (don't need to expand over padding)
313     if (tileNumber.x() < (tilesGridWidthHalf - GRID_PADDING)) {
314         QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
315         m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
316 //        qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
317     }
318
319     // expand to west side? (don't need to expand over padding)
320     if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING - 1)) {
321         QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
322         m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
323 //        qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
324     }
325
326 //    // expanding is only done if received tile zoom level is same as current zoom level
327 //    if (zoomLevel == m_zoomLevel) {
328 //        // expand to east side?
329 //        if (m_viewTilesGrid.right() > tileMaxValue(zoomLevel)) {
330 //            int crossing = m_viewTilesGrid.right() - tileMaxValue(zoomLevel);
331 //            if (tileNumber.x() < crossing) {
332 //                QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
333 //                m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
334 //                qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
335 //            }
336 //        }
337
338 //        // expand to west side?
339 //        if (m_viewTilesGrid.left() < 0) {
340 //            int crossing = -m_viewTilesGrid.left();
341 //            if (tileNumber.x() > (tileMaxValue(zoomLevel) - crossing)) {
342 //                QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
343 //                m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
344 //                qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
345 //            }
346 //        }
347 //    }
348
349
350 //    int crossing =  m_tilesGridSize.width() - tileMaxValue(zoomLevel) - 1;
351 ////    qWarning() << __PRETTY_FUNCTION__ << "crossing:" << crossing;
352 //    if (crossing > 0) {
353 //        qWarning() << __PRETTY_FUNCTION__ << "grid was bigger than amount of tiles at this tile level";
354
355 //        if (x < crossing) {
356 //            // expand to east side
357 //            QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
358 //            m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
359 //            qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x();
360 //        }
361
362 //        if (x > (tileMaxValue(zoomLevel) - crossing)) {
363 //            // expand to west side
364 //            QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
365 //            m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
366 //            qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x();
367 //        }
368 //    }
369
370 //    m_mapScene->spanItems(m_scrollDirection, m_zoomLevel);
371 }
372
373 int MapEngine::normalize(int value, int min, int max)
374 {
375     qDebug() << __PRETTY_FUNCTION__; // << "value:" << value << "min:" << min << "max:" << max;
376     Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
377
378     while (value < min)
379         value += max - min + 1;
380
381     while (value > max)
382         value -= max - min + 1;
383
384     return value;
385 }
386
387 void MapEngine::receiveOwnLocation(User *user)
388 {
389     qDebug() << __PRETTY_FUNCTION__;
390
391     if(user) {
392         QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
393         if (m_ownLocation->pos().toPoint() != newPosition) {
394             m_ownLocation->setPos(newPosition);
395         }
396
397         if (!m_ownLocation->isVisible())
398             m_ownLocation->show();
399     }
400     else {
401         m_ownLocation->hide();
402     }
403
404     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
405 }
406
407 QGraphicsScene* MapEngine::scene()
408 {
409     qDebug() << __PRETTY_FUNCTION__;
410
411     return m_mapScene;
412 }
413
414 void MapEngine::setAutoCentering(bool enabled)
415 {
416     m_autoCenteringEnabled = enabled;
417 }
418
419 void MapEngine::setGPSEnabled(bool enabled)
420 {
421     m_gpsLocationItem->setEnabled(enabled);
422 }
423
424 void MapEngine::setLocation(QPoint sceneCoordinate)
425 {
426     qDebug() << __PRETTY_FUNCTION__;
427
428     // jump to opposite side of the world if world limit is exceeded
429     if (sceneCoordinate.x() < 0)
430         sceneCoordinate.setX(sceneCoordinate.x() + WORLD_PIXELS_X);
431     else if (sceneCoordinate.x() > WORLD_PIXELS_X - 1)
432         sceneCoordinate.setX(sceneCoordinate.x() - WORLD_PIXELS_X);
433
434     if (disableAutoCentering(sceneCoordinate))
435         emit mapScrolledManually();
436
437     m_sceneCoordinate = sceneCoordinate;
438     emit locationChanged(m_sceneCoordinate);
439
440     if (isCenterTileChanged(sceneCoordinate)) {
441         getTiles(sceneCoordinate);
442         m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
443     }
444
445     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
446 }
447
448 void MapEngine::setZoomLevel(int newZoomLevel)
449 {
450     qDebug() << __PRETTY_FUNCTION__;
451
452     m_zoomLevel = newZoomLevel;
453     zoomed();
454 }
455
456 void MapEngine::setTilesGridSize(const QSize &viewSize)
457 {
458     qDebug() << __PRETTY_FUNCTION__;
459
460     // there must be scrolling reserve of at least half tile added to tile amount
461     // calculated from view size
462     const qreal SCROLLING_RESERVE = 0.5;
463
464     // converting scene tile to tile number does cause grid centering inaccuracy of one tile
465     const int CENTER_TILE_INACCURACY = 1;
466
467     int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
468                     + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
469     int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
470                      + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
471
472     m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
473
474     m_tilesGridSize.setHeight(gridHeight);
475     m_tilesGridSize.setWidth(gridWidth);
476
477     qWarning() << __PRETTY_FUNCTION__ << "tiles grid:" << m_tilesGridSize.width()
478                                       << "*" << m_tilesGridSize.height()
479                                       << "=" << m_tilesGridSize.width() * m_tilesGridSize.height() << "tiles";
480 }
481
482 void MapEngine::setViewLocation(QPointF latLonCoordinate)
483 {
484     qDebug() << __PRETTY_FUNCTION__;
485
486     QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
487
488     m_lastManualPosition = sceneCoordinate;
489
490     setLocation(sceneCoordinate);
491 }
492
493 int MapEngine::tileMaxValue(int zoomLevel)
494 {
495     qDebug() << __PRETTY_FUNCTION__;
496
497     return (1 << zoomLevel) - 1;
498 }
499
500 QString MapEngine::tilePath(int zoomLevel, int x, int y)
501 {
502     qDebug() << __PRETTY_FUNCTION__;
503
504     QString tilePathString(QString::number(zoomLevel) + "/");
505     tilePathString.append(QString::number(x) + "/");
506     tilePathString.append(QString::number(y));
507
508     return tilePathString;
509 }
510
511 void MapEngine::updateViewTilesSceneRect()
512 {
513     qDebug() << __PRETTY_FUNCTION__;
514
515     const QPoint ONE_TILE = QPoint(1, 1);
516     const QPoint ONE_PIXEL = QPoint(1, 1);
517
518     QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
519     // one tile - one pixel is added because returned coordinates are pointing to upper left corner
520     // of the last tile.
521     QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
522                                                             m_viewTilesGrid.bottomRight()
523                                                              + ONE_TILE) - ONE_PIXEL;
524
525     m_mapScene->tilesSceneRectUpdated(QRect(topLeft, bottomRight));
526 }
527
528 void MapEngine::viewResized(const QSize &size)
529 {
530     qDebug() << __PRETTY_FUNCTION__;
531
532     m_viewSize = size;
533     setTilesGridSize(m_viewSize);
534
535     emit locationChanged(m_sceneCoordinate);
536     getTiles(m_sceneCoordinate);
537     m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
538     m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
539 }
540
541 void MapEngine::viewZoomFinished()
542 {
543     qDebug() << __PRETTY_FUNCTION__;
544
545     if (m_zoomedIn) {
546         m_zoomedIn = false;
547         m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
548     }
549
550     if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
551         emit maxZoomLevelReached();
552     else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
553         emit minZoomLevelReached();
554 }
555
556 void MapEngine::zoomed()
557 {
558     emit zoomLevelChanged(m_zoomLevel);
559     m_mapScene->setTilesDrawingLevels(m_zoomLevel);
560     getTiles(m_sceneCoordinate);
561     m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
562     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
563 }
564
565 void MapEngine::zoomIn()
566 {
567     qDebug() << __PRETTY_FUNCTION__;
568
569     if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
570         m_zoomLevel++;
571         m_zoomedIn = true;
572         zoomed();
573     }
574 }
575
576 void MapEngine::zoomOut()
577 {
578     qDebug() << __PRETTY_FUNCTION__;
579
580     if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
581         m_zoomLevel--;
582         zoomed();
583     }
584 }