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