Fix wrong local positions in mouse events when no tlw was given
authorLaszlo Agocs <laszlo.p.agocs@nokia.com>
Wed, 23 May 2012 12:38:22 +0000 (15:38 +0300)
committerQt by Nokia <qt-info@nokia.com>
Wed, 23 May 2012 17:22:34 +0000 (19:22 +0200)
Calling handleMouseEvent() with w == 0 implies that the local position
is bogus and instead it should be calculated from the global position
once the target window is known.

Change-Id: If173d0570f6dcc8b7bc5d6f21fa1f69d06d9d702
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com>
src/gui/kernel/qguiapplication.cpp
src/gui/kernel/qwindowsysteminterface_qpa.cpp
tests/auto/gui/kernel/qwindow/tst_qwindow.cpp

index aa13def..7bf5a75 100644 (file)
@@ -1134,12 +1134,17 @@ void QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::Mo
     QWindow *window = e->window.data();
     modifier_buttons = e->modifiers;
 
-    if (!window)
-        window = QGuiApplication::topLevelAt(e->globalPos.toPoint());
-
     QPointF localPoint = e->localPos;
     QPointF globalPoint = e->globalPos;
 
+    if (!window) {
+        window = QGuiApplication::topLevelAt(globalPoint.toPoint());
+        if (window) {
+            QPointF delta = globalPoint - globalPoint.toPoint();
+            localPoint = window->mapFromGlobal(globalPoint.toPoint()) + delta;
+        }
+    }
+
     Qt::MouseButton button = Qt::NoButton;
     bool doubleClick = false;
 
index 0759b3c..3054b96 100644 (file)
@@ -133,19 +133,19 @@ void QWindowSystemInterface::handleSynchronousCloseEvent(QWindow *tlw)
 
 /*!
 
-\a tlw == 0 means that \a ev is in global coords only
-
+\a w == 0 means that the event is in global coords only, \a local will be ignored in this case
 
 */
-void QWindowSystemInterface::handleMouseEvent(QWindow *w, const QPointF & local, const QPointF & global, Qt::MouseButtons b, Qt::KeyboardModifiers mods) {
+void QWindowSystemInterface::handleMouseEvent(QWindow *w, const QPointF & local, const QPointF & global, Qt::MouseButtons b, Qt::KeyboardModifiers mods)
+{
     unsigned long time = QWindowSystemInterfacePrivate::eventTime.elapsed();
     handleMouseEvent(w, time, local, global, b, mods);
 }
 
-void QWindowSystemInterface::handleMouseEvent(QWindow *tlw, ulong timestamp, const QPointF & local, const QPointF & global, Qt::MouseButtons b, Qt::KeyboardModifiers mods)
+void QWindowSystemInterface::handleMouseEvent(QWindow *w, ulong timestamp, const QPointF & local, const QPointF & global, Qt::MouseButtons b, Qt::KeyboardModifiers mods)
 {
     QWindowSystemInterfacePrivate::MouseEvent * e =
-            new QWindowSystemInterfacePrivate::MouseEvent(tlw, timestamp, local, global, b, mods);
+            new QWindowSystemInterfacePrivate::MouseEvent(w, timestamp, local, global, b, mods);
     QWindowSystemInterfacePrivate::queueWindowSystemEvent(e);
 }
 
index ae5bf55..6e6db12 100644 (file)
@@ -328,6 +328,7 @@ public:
             mouseSequenceSignature += 'p';
             mousePressButton = event->button();
             mousePressScreenPos = event->screenPos();
+            mousePressLocalPos = event->localPos();
             if (spinLoopWhenPressed)
                 QCoreApplication::processEvents();
         }
@@ -401,7 +402,7 @@ public:
     int mousePressButton, mouseReleaseButton, mouseMoveButton;
     int mousePressedCount, mouseReleasedCount, mouseMovedCount, mouseDoubleClickedCount;
     QString mouseSequenceSignature;
-    QPointF mousePressScreenPos, mouseMoveScreenPos;
+    QPointF mousePressScreenPos, mouseMoveScreenPos, mousePressLocalPos;
     int touchPressedCount, touchReleasedCount, touchMovedCount;
     QEvent::Type touchEventType;
 
@@ -429,6 +430,7 @@ void tst_QWindow::testInputEvents()
     QCoreApplication::processEvents();
     QCOMPARE(window.mousePressButton, int(Qt::LeftButton));
     QCOMPARE(window.mouseReleaseButton, int(Qt::LeftButton));
+    QCOMPARE(window.mousePressLocalPos, local);
 
     QList<QWindowSystemInterface::TouchPoint> points;
     QWindowSystemInterface::TouchPoint tp1, tp2;
@@ -446,6 +448,24 @@ void tst_QWindow::testInputEvents()
     QCoreApplication::processEvents();
     QTRY_COMPARE(window.touchPressedCount, 2);
     QTRY_COMPARE(window.touchReleasedCount, 2);
+
+    // Now with null pointer as window. local param should not be utilized:
+    // handleMouseEvent() with tlw == 0 means the event is in global coords only.
+    window.mousePressButton = window.mouseReleaseButton = 0;
+    QPointF nonWindowGlobal(500, 500); // not inside the window
+    QWindowSystemInterface::handleMouseEvent(0, nonWindowGlobal, nonWindowGlobal, Qt::LeftButton);
+    QWindowSystemInterface::handleMouseEvent(0, nonWindowGlobal, nonWindowGlobal, Qt::NoButton);
+    QCoreApplication::processEvents();
+    QCOMPARE(window.mousePressButton, 0);
+    QCOMPARE(window.mouseReleaseButton, 0);
+    QPointF windowGlobal = window.mapToGlobal(local.toPoint());
+    QWindowSystemInterface::handleMouseEvent(0, windowGlobal, windowGlobal, Qt::LeftButton);
+    QWindowSystemInterface::handleMouseEvent(0, windowGlobal, windowGlobal, Qt::NoButton);
+    QCoreApplication::processEvents();
+    QCOMPARE(window.mousePressButton, int(Qt::LeftButton));
+    QCOMPARE(window.mouseReleaseButton, int(Qt::LeftButton));
+    QCOMPARE(window.mousePressScreenPos, windowGlobal);
+    QCOMPARE(window.mousePressLocalPos, local); // the local we passed was bogus, verify that qGuiApp calculated the proper one
 }
 
 void tst_QWindow::touchToMouseTranslation()