MapEngine::MapEngine(QObject *parent)
: QObject(parent),
m_autoCenteringEnabled(false),
+ m_scrollStartedByGps(false),
+ m_smoothScrollRunning(false),
m_zoomedIn(false),
m_zoomLevel(DEFAULT_ZOOM_LEVEL),
m_centerTile(QPoint(UNDEFINED, UNDEFINED)),
- m_lastManualPosition(QPoint(0, 0)),
+ m_lastAutomaticPosition(QPoint(0, 0)),
m_tilesGridSize(QSize(0, 0)),
m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
{
connect(m_scroller, SIGNAL(coordinateUpdated(QPoint)),
this, SLOT(setCenterPosition(QPoint)));
+
+ connect(m_scroller, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)),
+ this, SLOT(scrollerStateChanged(QAbstractAnimation::State)));
}
MapEngine::~MapEngine()
{
qDebug() << __PRETTY_FUNCTION__;
- QPoint scenePosition = convertLatLonToSceneCoordinate(latLonCoordinate);
-
- m_lastManualPosition = scenePosition;
-
- scrollToPosition(scenePosition);
+ scrollToPosition(convertLatLonToSceneCoordinate(latLonCoordinate));
}
QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
return QPoint(x, y);
}
-bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
+void MapEngine::disableAutoCenteringIfRequired(QPoint sceneCoordinate)
{
if (isAutoCenteringEnabled()) {
int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
- QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
- m_lastManualPosition.y() / zoomFactor);
+ QPoint oldPixelValue = QPoint(m_lastAutomaticPosition.x() / zoomFactor,
+ m_lastAutomaticPosition.y() / zoomFactor);
QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
sceneCoordinate.y() / zoomFactor);
- if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
- (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
- return true;
- }
+ if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE)
+ || (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE)) {
- return false;
+ emit mapScrolledManually();
+ }
+ }
}
void MapEngine::friendsPositionsUpdated()
m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
- if (m_autoCenteringEnabled)
- centerToCoordinates(position);
+ if (m_autoCenteringEnabled) {
+ m_lastAutomaticPosition = convertLatLonToSceneCoordinate(position);
+ m_scrollStartedByGps = true;
+ scrollToPosition(m_lastAutomaticPosition);
+ }
}
qreal MapEngine::greatCircleDistance(QPointF firstLocation, QPointF secondLocation)
return m_mapScene;
}
+void MapEngine::scrollerStateChanged(QAbstractAnimation::State newState)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_smoothScrollRunning
+ && newState != QAbstractAnimation::Running) {
+ m_smoothScrollRunning = false;
+
+ // don't disable auto centering if current animation was stopped by new update from GPS
+ if (!m_scrollStartedByGps)
+ disableAutoCenteringIfRequired(m_sceneCoordinate);
+ }
+
+ m_scrollStartedByGps = false;
+}
+
void MapEngine::scrollToPosition(QPoint scenePosition)
{
+ qDebug() << __PRETTY_FUNCTION__;
+
m_scroller->stop();
m_scroller->setEasingCurve(QEasingCurve::InOutQuart);
m_scroller->setDuration(SMOOTH_CENTERING_TIME_MS);
m_scroller->setStartValue(m_sceneCoordinate);
m_scroller->setEndValue(scenePosition);
+ m_smoothScrollRunning = true;
m_scroller->start();
}
void MapEngine::setAutoCentering(bool enabled)
{
- m_autoCenteringEnabled = enabled;
-}
+ qDebug() << __PRETTY_FUNCTION__;
-void MapEngine::setGPSEnabled(bool enabled)
-{
- m_gpsLocationItem->setEnabled(enabled);
+ m_autoCenteringEnabled = enabled;
}
void MapEngine::setCenterPosition(QPoint scenePosition)
// don't allow vertical scene coordinates go out of the map
scenePosition.setY(qBound(MAP_MIN_PIXEL_Y, scenePosition.y(), MAP_MAX_PIXEL_Y));
- if (disableAutoCentering(scenePosition))
- emit mapScrolledManually();
+ if (!m_smoothScrollRunning)
+ disableAutoCenteringIfRequired(scenePosition);
m_sceneCoordinate = scenePosition;
emit locationChanged(m_sceneCoordinate);
emit newMapResolution(sceneResolution());
}
+void MapEngine::setGPSEnabled(bool enabled)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_gpsLocationItem->setEnabled(enabled);
+}
+
void MapEngine::setZoomLevel(int newZoomLevel)
{
qDebug() << __PRETTY_FUNCTION__;
QRect calculateTileGrid(QPoint sceneCoordinate);
/**
- * @brief Check if auto centering should be disabled.
+ * @brief Request disabling of auto centering if centered too far from the real location.
*
* @param sceneCoordinate scene's center coordinate
- * @return bool true if auto centering should be disabled
*/
- bool disableAutoCentering(QPoint sceneCoordinate);
+ void disableAutoCenteringIfRequired(QPoint sceneCoordinate);
/**
* @brief Get new tiles.
void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
/**
+ * @brief Called when MapScroller state is changed
+ *
+ * Does check if the smooth scroll effect was interrupted and should the auto centering
+ * feature to be disabled.
+ *
+ * @param newState New state
+ */
+ void scrollerStateChanged(QAbstractAnimation::State newState);
+
+ /**
* @brief Scroll smoothly to given scene position
*
* @param scenePosition Target position in the scene
* DATA MEMBERS
******************************************************************************/
private:
- bool m_autoCenteringEnabled; ///< Auto centering enabled
- bool m_zoomedIn; ///< Flag for checking if zoomed in when zoom is finished
+ bool m_autoCenteringEnabled; ///< Auto centering enabled
+ bool m_scrollStartedByGps; ///< Smooth scroll is started by GPS?
+ bool m_smoothScrollRunning; ///< Smooth scroll is running?
+ bool m_zoomedIn; ///< Flag for checking if zoomed in when zoom is finished
- int m_zoomLevel; ///< Current zoom level
+ int m_zoomLevel; ///< Current zoom level
- QPoint m_centerTile; ///< Current center tile
- QPoint m_lastManualPosition; ///< Last manually set position in scene coordinate
- QPoint m_sceneCoordinate; ///< Current center coordinate
+ QPoint m_centerTile; ///< Current center tile
+ QPoint m_lastAutomaticPosition; ///< Last automatically set position in scene coordinate
+ QPoint m_sceneCoordinate; ///< Current center coordinate
- QRect m_viewTilesGrid; ///< Current grid of tiles in view (includes margin)
+ QRect m_viewTilesGrid; ///< Current grid of tiles in view (includes margin)
- QSize m_tilesGridSize; ///< Current size of the tiles grid
- QSize m_viewSize; ///< Current view size
+ QSize m_tilesGridSize; ///< Current size of the tiles grid
+ QSize m_viewSize; ///< Current view size
FriendItemsHandler *m_friendItemsHandler; ///< Handler for friend and group items
GPSLocationItem *m_gpsLocationItem; ///< Item pointing current location from GPS