Modified MapView::mouseDoubleClickEvent.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 17 Jun 2010 05:05:17 +0000 (08:05 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 17 Jun 2010 05:05:17 +0000 (08:05 +0300)
src/map/mapview.cpp
src/map/mapview.h
tests/map/mapview/testmapview.cpp

index f1279a4..6dc56d0 100644 (file)
@@ -39,6 +39,8 @@ MapView::MapView(QWidget *parent)
     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
     connect(m_zoomAnimation, SIGNAL(finished()),
         this, SIGNAL(viewZoomFinished()));
+    connect(m_zoomAnimation, SIGNAL(finished()),
+            this, SLOT(disableAnchor()));
 
     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
 }
@@ -53,14 +55,25 @@ void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
 {
     qWarning() << __PRETTY_FUNCTION__ << event->pos().x() << event->pos().y();
+    qWarning() << __PRETTY_FUNCTION__ << mapToScene(event->pos()).toPoint().x() <<
+            mapToScene(event->pos()).toPoint().y();
+
+    QPoint centerPosition = mapToScene(QPoint(width() / 2, height() / 2)).toPoint();
+    QPoint pressPosition = mapToScene(event->pos()).toPoint();
+    QPoint scenePosition = centerPosition - ((centerPosition - pressPosition) / 2);
 
+    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
     emit zoomIn();
+//    emit viewScrolled(scenePosition);
+//    setTransformationAnchor(QGraphicsView::NoAnchor);
 }
 
 void MapView::mouseMoveEvent(QMouseEvent *event)
 {
     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
 
+    qWarning() << __PRETTY_FUNCTION__;
+
     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
 
     emit viewScrolled(m_scenePosition);
@@ -70,6 +83,7 @@ void MapView::mouseMoveEvent(QMouseEvent *event)
 
 void MapView::mousePressEvent(QMouseEvent *event)
 {
+    qWarning() << __PRETTY_FUNCTION__;
     qDebug() << __PRETTY_FUNCTION__;
 
     QGraphicsView::mousePressEvent(event);
@@ -78,6 +92,13 @@ void MapView::mousePressEvent(QMouseEvent *event)
     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
 }
 
+void MapView::mouseReleaseEvent(QMouseEvent *event)
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    QGraphicsView::mouseReleaseEvent(event);
+}
+
 void MapView::resizeEvent(QResizeEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
@@ -114,3 +135,15 @@ qreal MapView::viewScale()
 
     return transform().m11();
 }
+
+void MapView::disableAnchor()
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    QCoreApplication::postEvent(this, new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
+                                                      Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
+
+//    QGraphicsView::mouseReleaseEvent(new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
+//                                                     Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
+    setTransformationAnchor(QGraphicsView::AnchorViewCenter);
+}
index 74cf630..7b8fcc7 100644 (file)
@@ -65,13 +65,6 @@ protected:
     void mouseDoubleClickEvent(QMouseEvent *event);
 
     /**
-    * @brief Called when view is resized.
-    *
-    * @param event resize event
-    */
-    void resizeEvent(QResizeEvent *event);
-
-    /**
     * @brief Event handler for mouse move events
     *
     * Does calculate mouse movement delta from last event position and new view center
@@ -89,6 +82,15 @@ protected:
     */
     void mousePressEvent(QMouseEvent *event);
 
+    void mouseReleaseEvent(QMouseEvent *event);
+
+    /**
+    * @brief Called when view is resized.
+    *
+    * @param event resize event
+    */
+    void resizeEvent(QResizeEvent *event);
+
 /*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
@@ -107,6 +109,8 @@ public slots:
     */
     void setZoomLevel(int zoomLevel);
 
+    void disableAnchor();
+
 private:
     /**
     * @brief Set new view scale
index 60b0c72..beaf11f 100644 (file)
@@ -74,7 +74,7 @@ void TestMapView::doubleClick()
     QSignalSpy doubleClickSpy(m_mapView, SIGNAL(zoomIn()));
     QVERIFY(doubleClickSpy.isValid());
 
-    QTest::mouseDClick(m_mapView, Qt::LeftButton);
+    QTest::mouseDClick(m_mapView, Qt::Key_Tab);
     QCOMPARE(doubleClickSpy.count(), 1);
 }