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