From 604adc01c00bee770485308cbe0e2b2897b45fa7 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Fri, 6 Nov 2020 11:24:49 +0100 Subject: [PATCH] Fix mouse velocity calculation The mouse event that is sent to QPlatformCursor is in native pixels, but the QSinglePointEvent constructor sets QEventPoint::globalLastPosition every time, and velocity is calculated by delta from globalLastPosition to globalPosition. We plan to rely on this velocity being correct in Qt Quick, in Flickable for example. So globalLastPosition and globalPosition need to be in the same coordinate system at the time QPointerEvent::setTimestamp() is called. Change-Id: I39f97a43f55f47a70cbd574861e920f3106e2125 Reviewed-by: Volker Hilsheimer --- src/gui/kernel/qguiapplication.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 1499c4b57e..3dcd0650e3 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -2151,6 +2151,7 @@ void QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::Mo bool positionChanged = QGuiApplicationPrivate::lastCursorPosition != e->globalPos; bool mouseMove = false; bool mousePress = false; + const QPointF lastGlobalPosition = QGuiApplicationPrivate::lastCursorPosition; QPointF globalPoint = e->globalPos; if (qIsNaN(e->globalPos.x()) || qIsNaN(e->globalPos.y())) { @@ -2279,13 +2280,19 @@ void QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::Mo const QPointF nativeGlobalPoint = QHighDpi::toNativePixels(globalPoint, screen); QMouseEvent ev(type, nativeLocalPoint, nativeLocalPoint, nativeGlobalPoint, button, e->buttons, e->modifiers, e->source, device); - ev.setTimestamp(e->timestamp); + // avoid incorrect velocity calculation: ev is in the native coordinate system, + // but we need to consistently use the logical coordinate system for velocity + // whenever QEventPoint::setTimestamp() is called + ev.QInputEvent::setTimestamp(e->timestamp); cursor->pointerEvent(ev); } } #endif QMouseEvent ev(type, localPoint, localPoint, globalPoint, button, e->buttons, e->modifiers, e->source, device); + // restore globalLastPosition to avoid invalidating the velocity calculations, + // because the QPlatformCursor mouse event above was in native coordinates + QMutableEventPoint::from(persistentEPD->eventPoint).setGlobalLastPosition(lastGlobalPosition); // ev now contains a detached copy of the QEventPoint from QPointingDevicePrivate::activePoints ev.setTimestamp(e->timestamp); if (window->d_func()->blockedByModalWindow && !qApp->d_func()->popupActive()) {