From 638875186db1376a4405bdb93d65468ac83f744b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sami=20R=C3=A4m=C3=B6?= Date: Wed, 16 Jun 2010 14:23:34 +0300 Subject: [PATCH] Self made kinetic scrolling first working version --- src/map/mapengine.cpp | 8 ++++---- src/map/mapscroller.cpp | 4 +++- src/map/mapscroller.h | 5 ++--- src/map/mapview.cpp | 27 +++++++++++++++++++++------ src/map/mapview.h | 8 ++++++-- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/map/mapengine.cpp b/src/map/mapengine.cpp index 12c8e3a..ca6d33e 100644 --- a/src/map/mapengine.cpp +++ b/src/map/mapengine.cpp @@ -88,6 +88,9 @@ MapEngine::MapEngine(QObject *parent) this, SIGNAL(locationItemClicked(QList))); m_scroller = &MapScroller::getInstance(); + + connect(m_scroller, SIGNAL(coordinateUpdated(QPoint)), + this, SLOT(setLocation(QPoint))); } MapEngine::~MapEngine() @@ -392,10 +395,7 @@ void MapEngine::setLocation(QPoint sceneCoordinate) qDebug() << __PRETTY_FUNCTION__; // jump to opposite side of the world if world horizontal limit is exceeded - if (sceneCoordinate.x() < MAP_MIN_PIXEL_X) - sceneCoordinate.setX(sceneCoordinate.x() + MAP_PIXELS_X); - else if (sceneCoordinate.x() > MAP_MAX_PIXEL_X) - sceneCoordinate.setX(sceneCoordinate.x() - MAP_PIXELS_X); + sceneCoordinate.setX(normalize(sceneCoordinate.x(), MAP_MIN_PIXEL_X, MAP_MAX_PIXEL_X)); // don't allow vertical scene coordinates go out of the map sceneCoordinate.setY(qBound(MAP_MIN_PIXEL_Y, sceneCoordinate.y(), MAP_MAX_PIXEL_Y)); diff --git a/src/map/mapscroller.cpp b/src/map/mapscroller.cpp index 9d9c626..49ccc9c 100644 --- a/src/map/mapscroller.cpp +++ b/src/map/mapscroller.cpp @@ -37,5 +37,7 @@ MapScroller& MapScroller::getInstance() void MapScroller::updateCurrentValue(const QVariant &value) { - qWarning() << __PRETTY_FUNCTION__ << value.toPointF().x() << value.toPointF().y(); + qDebug() << __PRETTY_FUNCTION__; + + emit coordinateUpdated(value.toPoint()); } diff --git a/src/map/mapscroller.h b/src/map/mapscroller.h index aef5953..2593a10 100644 --- a/src/map/mapscroller.h +++ b/src/map/mapscroller.h @@ -23,6 +23,7 @@ #ifndef MAPSCROLLER_H #define MAPSCROLLER_H +#include #include class MapScroller : public QVariantAnimation @@ -43,9 +44,7 @@ public: static MapScroller &getInstance(); signals: - -public slots: - + void coordinateUpdated(QPoint coordinate); }; #endif // MAPSCROLLER_H diff --git a/src/map/mapview.cpp b/src/map/mapview.cpp index ee797fe..06d4b9c 100644 --- a/src/map/mapview.cpp +++ b/src/map/mapview.cpp @@ -55,31 +55,44 @@ void MapView::centerToSceneCoordinates(QPoint sceneCoordinate) void MapView::mouseMoveEvent(QMouseEvent *event) { - m_scenePosition += m_mousePosition - mapToScene(event->pos()); + m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint(); qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition; - emit viewScrolled(m_scenePosition.toPoint()); + m_dragViewSpeed = m_mouseLastViewPosition - event->pos() ; +// qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y(); - m_mousePosition = mapToScene(event->pos()); + emit viewScrolled(m_scenePosition); + + m_mouseLastScenePosition = mapToScene(event->pos()).toPoint(); + m_mouseLastViewPosition = event->pos(); } void MapView::mousePressEvent(QMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__; + m_scroller->stop(); + QGraphicsView::mousePressEvent(event); - m_mousePosition = mapToScene(event->pos()); - m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1); + m_mouseLastScenePosition = mapToScene(event->pos()).toPoint(); + m_mouseLastViewPosition = event->pos(); + m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint(); + + m_dragViewSpeed = QPoint(); } void MapView::mouseReleaseEvent(QMouseEvent *event) { +// qWarning() << __PRETTY_FUNCTION__; + QGraphicsView::mouseReleaseEvent(event); + QPoint dragSceneSpeed = m_dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel)); + m_scroller->setStartValue(m_scenePosition); - m_scroller->setEndValue(m_scenePosition + m_scenePosition); + m_scroller->setEndValue(m_scenePosition + dragSceneSpeed * 5); m_scroller->setDuration(1000); m_scroller->start(); } @@ -104,6 +117,8 @@ void MapView::setZoomLevel(int zoomLevel) { qDebug() << __PRETTY_FUNCTION__; + m_zoomLevel = zoomLevel; + if (m_zoomAnimation) { m_zoomAnimation->stop(); m_zoomAnimation->setDuration(ZOOM_TIME); diff --git a/src/map/mapview.h b/src/map/mapview.h index e53c433..f936031 100644 --- a/src/map/mapview.h +++ b/src/map/mapview.h @@ -148,8 +148,12 @@ signals: * DATA MEMBERS ******************************************************************************/ private: - QPointF m_mousePosition; ///< Previous mouse event position - QPointF m_scenePosition; ///< New center position + int m_zoomLevel; + + QPoint m_dragViewSpeed; + QPoint m_mouseLastScenePosition; ///< Previous mouse event position + QPoint m_mouseLastViewPosition; + QPoint m_scenePosition; ///< New center position QPropertyAnimation *m_zoomAnimation; ///< Zoom animation MapScroller *m_scroller; -- 1.7.9.5