Fixed map jumping when starting a drag
[situare] / src / map / mapview.cpp
index c86b93a..8cdcf29 100644 (file)
@@ -45,7 +45,8 @@ const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
 
 MapView::MapView(QWidget *parent)
     : QGraphicsView(parent),
-      m_doubleTapZoomRunning(false)
+      m_doubleTapZoomRunning(false),
+      m_panelIsOpen(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -66,11 +67,27 @@ MapView::MapView(QWidget *parent)
             this, SLOT(doubleTapZoomFinished()));
 }
 
-void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate)
+void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction)
 {
     qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
 
-    centerOn(coordinate.toPointF());
+    QPointF target = coordinate.toPointF();
+
+    if (!isUserDragAction) {
+        target += m_centerHorizontalShift;
+    }
+
+    centerOn(target);
+}
+
+void MapView::disableCenterShift()
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    m_panelIsOpen = false;
+    updateCenterShift();
+
+    ///< @todo Update center position
 }
 
 void MapView::doubleTapZoomFinished()
@@ -81,6 +98,16 @@ void MapView::doubleTapZoomFinished()
     emit zoomIn();
 }
 
+void MapView::enableCenterShift()
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    m_panelIsOpen = true;
+    updateCenterShift();
+
+    ///< @todo Update center position
+}
+
 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -124,7 +151,7 @@ void MapView::mouseMoveEvent(QMouseEvent *event)
     m_time.start();
     m_index++;
 
-    emit viewScrolled(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+    emit viewScrolled(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()), true);
 
     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
     m_mouseLastViewPosition = event->pos();
@@ -207,6 +234,8 @@ void MapView::resizeEvent(QResizeEvent *event)
     m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
 
     emit viewResized(event->size());
+
+    updateCenterShift();
 }
 
 void MapView::setViewScale(qreal viewScale)
@@ -233,6 +262,20 @@ void MapView::setZoomLevel(int zoomLevel)
 
         m_zoomAnimation->start();
     }
+
+    updateCenterShift();
+}
+
+void MapView::updateCenterShift()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    int shift = 0;
+
+    if (m_panelIsOpen)
+        shift = 200;
+
+    m_centerHorizontalShift = QPointF(shift * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel)), 0);
 }
 
 qreal MapView::viewScale()