QHighDpi: global geometry scaling functions

Add functions for scaling global coordinates:
	toNativeGlobalPosition()
	fromNativeGlobalPosition()

These correctly handle the cases where a window spans several
screens.

Change-Id: I268762499cd8d86a3c417342ddaf2fb6dab4dd20
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
bb10
Morten Johan Sørvig 2020-05-27 01:03:52 +02:00
parent b8f5ed0d5a
commit 73a93981ca
2 changed files with 31 additions and 15 deletions

View File

@ -263,6 +263,22 @@ T toNativeLocalPosition(const T &value, const C *context)
return scale(value, QHighDpiScaling::factor(context));
}
template <typename T, typename C>
T fromNativeGlobalPosition(const T &value, const C *context)
{
QHighDpiScaling::ScaleAndOrigin so =
QHighDpiScaling::scaleAndOrigin(context, position(value, QHighDpiScaling::Point::Native));
return scale(value, qreal(1) / so.factor, so.origin);
}
template <typename T, typename C>
T toNativeGlobalPosition(const T &value, const C *context)
{
QHighDpiScaling::ScaleAndOrigin so =
QHighDpiScaling::scaleAndOrigin(context, position(value, QHighDpiScaling::Point::DeviceIndependent));
return scale(value, so.factor, so.origin);
}
template <typename T>
inline T fromNative(const T &value, qreal scaleFactor, QPoint origin = QPoint(0, 0))
{

View File

@ -222,7 +222,7 @@ QT_DEFINE_QPA_EVENT_HANDLER(void, handleEnterEvent, QWindow *window, const QPoin
{
if (window) {
QWindowSystemInterfacePrivate::EnterEvent *e
= new QWindowSystemInterfacePrivate::EnterEvent(window, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativePixels(global, window));
= new QWindowSystemInterfacePrivate::EnterEvent(window, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativeGlobalPosition(global, window));
QWindowSystemInterfacePrivate::handleWindowSystemEvent<Delivery>(e);
}
}
@ -403,7 +403,7 @@ QT_DEFINE_QPA_EVENT_HANDLER(bool, handleMouseEvent, QWindow *window, ulong times
"QWindowSystemInterface::handleMouseEvent",
"QTBUG-71263: Native double clicks are not implemented.");
auto localPos = QHighDpi::fromNativeLocalPosition(local, window);
auto globalPos = QHighDpi::fromNativePixels(global, window);
auto globalPos = QHighDpi::fromNativeGlobalPosition(global, window);
QWindowSystemInterfacePrivate::MouseEvent *e =
new QWindowSystemInterfacePrivate::MouseEvent(window, timestamp, localPos, globalPos,
@ -441,7 +441,7 @@ bool QWindowSystemInterface::handleFrameStrutMouseEvent(QWindow *window, ulong t
Qt::MouseEventSource source)
{
auto localPos = QHighDpi::fromNativeLocalPosition(local, window);
auto globalPos = QHighDpi::fromNativePixels(global, window);
auto globalPos = QHighDpi::fromNativeGlobalPosition(global, window);
QWindowSystemInterfacePrivate::MouseEvent *e =
new QWindowSystemInterfacePrivate::MouseEvent(window, timestamp, localPos, globalPos,
@ -578,16 +578,15 @@ bool QWindowSystemInterface::handleWheelEvent(QWindow *window, ulong timestamp,
// Simple case: vertical deltas only:
if (angleDelta.y() != 0 && angleDelta.x() == 0) {
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativePixels(global, window), pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical,
mods, phase, source, invertedScrolling, device);
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativeGlobalPosition(global, window),
pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical, mods, phase, source, invertedScrolling, device);
return QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
}
// Simple case: horizontal deltas only:
if (angleDelta.y() == 0 && angleDelta.x() != 0) {
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativePixels(global, window), pixelDelta, angleDelta, angleDelta.x(), Qt::Horizontal,
mods, phase, source, invertedScrolling, device);
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativeGlobalPosition(global, window),
pixelDelta, angleDelta, angleDelta.x(), Qt::Horizontal, mods, phase, source, invertedScrolling, device);
return QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
}
@ -596,14 +595,14 @@ bool QWindowSystemInterface::handleWheelEvent(QWindow *window, ulong timestamp,
// Both horizontal and vertical deltas: Send two wheel events.
// The first event contains the Qt 5 pixel and angle delta as points,
// and in addition the Qt 4 compatibility vertical angle delta.
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativePixels(global, window), pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical,
mods, phase, source, invertedScrolling, device);
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativeGlobalPosition(global, window),
pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical, mods, phase, source, invertedScrolling, device);
acceptVert = QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
// The second event contains null pixel and angle points and the
// Qt 4 compatibility horizontal angle delta.
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativePixels(global, window), QPoint(), QPoint(), angleDelta.x(), Qt::Horizontal,
mods, phase, source, invertedScrolling, device);
e = new QWindowSystemInterfacePrivate::WheelEvent(window, timestamp, QHighDpi::fromNativeLocalPosition(local, window), QHighDpi::fromNativeGlobalPosition(global, window),
QPoint(), QPoint(), angleDelta.x(), Qt::Horizontal, mods, phase, source, invertedScrolling, device);
acceptHorz = QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
return acceptVert || acceptHorz;
}
@ -915,7 +914,7 @@ bool QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp,
QWindowSystemInterfacePrivate::TabletEvent *e =
new QWindowSystemInterfacePrivate::TabletEvent(window, timestamp,
QHighDpi::fromNativeLocalPosition(local, window),
QHighDpi::fromNativePixels(global, window),
QHighDpi::fromNativeGlobalPosition(global, window),
device, buttons, pressure,
xTilt, yTilt, tangentialPressure, rotation, z, modifiers);
return QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
@ -1192,9 +1191,10 @@ Q_GUI_EXPORT void qt_handleMouseEvent(QWindow *window, const QPointF &local, con
Qt::MouseButtons state, Qt::MouseButton button,
QEvent::Type type, Qt::KeyboardModifiers mods, int timestamp)
{
const qreal factor = QHighDpiScaling::factor(window);
QPointF nativeLocal = QHighDpi::toNativeLocalPosition(local, window);
QPointF nativeGlobal = QHighDpi::toNativeGlobalPosition(global, window);
QWindowSystemInterface::handleMouseEvent<QWindowSystemInterface::SynchronousDelivery>(window,
timestamp, local * factor, global * factor, state, button, type, mods);
timestamp, nativeLocal, nativeGlobal, state, button, type, mods);
}
Q_GUI_EXPORT void qt_handleKeyEvent(QWindow *window, QEvent::Type t, int k, Qt::KeyboardModifiers mods, const QString & text = QString(), bool autorep = false, ushort count = 1)