summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMorten Johan Sorvig <morten.sorvig@nokia.com>2012-01-09 11:25:40 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-24 14:55:06 +0100
commit5a2efb490bc3549ef42420a0dafcf22072785e0d (patch)
tree5e0dc4396301e791fda198e9a11448532bc1aeec /src
parente8952aba2914722b0ad0e6f07e2193ead7f2e39d (diff)
QWheelEvent high-resolution delta support.
Support pixel-based deltas as well as sending dx and dy values in the same event. Keep source and behavior compatibility with Qt 4. New API: QPoint pixelDelta() const QPoint angleDelta() const Deprecate delta() and orientation(). Both pixel-based deltas and combined updates are necessary for smooth trackpad-based scrolling on OS X. Qt 4 compatible behavior is achieved by sending an extra wheel event in cases where the initial event has a combined dx and dy update. This extra event sends dx in delta() and orientation(), with pixelDelta() and angleDelta() set to null. Modify the Cocoa implementation to provide pixel deltas. It is expected that not all platforms can provide these. Angle deltas will always be available. Change-Id: I20c10f0df338ddcd6a3f7a4d40949ed5ae3b4795 Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/gui/kernel/qevent.cpp70
-rw-r--r--src/gui/kernel/qevent.h24
-rw-r--r--src/gui/kernel/qguiapplication.cpp3
-rw-r--r--src/gui/kernel/qwindowsysteminterface_qpa.cpp49
-rw-r--r--src/gui/kernel/qwindowsysteminterface_qpa.h3
-rw-r--r--src/gui/kernel/qwindowsysteminterface_qpa_p.h12
-rw-r--r--src/plugins/platforms/cocoa/qnsview.mm48
-rw-r--r--src/widgets/kernel/qapplication.cpp4
-rw-r--r--src/widgets/kernel/qwidgetwindow_qpa.cpp2
9 files changed, 168 insertions, 47 deletions
diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp
index f99f3ddec3..281d4f61bc 100644
--- a/src/gui/kernel/qevent.cpp
+++ b/src/gui/kernel/qevent.cpp
@@ -453,7 +453,13 @@ QHoverEvent::~QHoverEvent()
Wheel events are sent to the widget under the mouse cursor, but
if that widget does not handle the event they are sent to the
- focus widget. The rotation distance is provided by delta().
+ focus widget. Wheel events are generated for both mouse wheels
+ and trackpad scroll gestures. There are two ways to read the
+ wheel event delta: angleDelta() returns the delta in wheel
+ degrees. This value is always provided. pixelDelta() returns
+ the delta in screen pixels and is available on platforms that
+ have high-resolution trackpads, such as Mac OS X.
+
The functions pos() and globalPos() return the mouse cursor's
location at the time of the event.
@@ -483,8 +489,11 @@ QHoverEvent::~QHoverEvent()
*/
/*!
+ \obsolete
Constructs a wheel event object.
+ Use the QPoint-based constructor instead.
+
The position, \a pos, is the location of the mouse cursor within
the widget. The globalPos() is initialized to QCursor::pos()
which is usually, but not always, correct.
@@ -496,13 +505,13 @@ QHoverEvent::~QHoverEvent()
\a modifiers holds the keyboard modifier flags at the time of the
event, and \a orient holds the wheel's orientation.
- \sa pos() delta() state()
+ \sa pos() pixelDelta() angleDelta() state()
*/
#ifndef QT_NO_WHEELEVENT
QWheelEvent::QWheelEvent(const QPointF &pos, int delta,
Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers,
Qt::Orientation orient)
- : QInputEvent(Wheel, modifiers), p(pos), d(delta), mouseState(buttons), o(orient)
+ : QInputEvent(Wheel, modifiers), p(pos), qt4D(delta), qt4O(orient), mouseState(buttons)
{
g = QCursor::pos();
}
@@ -515,26 +524,69 @@ QWheelEvent::~QWheelEvent()
}
/*!
+ \obsolete
Constructs a wheel event object.
+ Use the QPoint-based constructor instead.
+
The \a pos provides the location of the mouse cursor
within the widget. The position in global coordinates is specified
by \a globalPos. \a delta contains the rotation distance, \a modifiers
holds the keyboard modifier flags at the time of the event, and
\a orient holds the wheel's orientation.
- \sa pos() globalPos() delta() state()
+
+ \sa pos() pixelDelta() angleDelta() state()
*/
QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, int delta,
Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers,
Qt::Orientation orient)
- : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), d(delta), mouseState(buttons), o(orient)
+ : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), qt4D(delta), qt4O(orient), mouseState(buttons)
{}
+/*!
+ Constructs a wheel event object.
+
+ The \a pos provides the location of the mouse cursor
+ within the window. The position in global coordinates is specified
+ by \a globalPos. \pixelDelta contains the scrolling distance
+ in pixels on screen, \a angleDelta contains the wheel rotation distance.
+ \pixelDelta is optional and can be null.
+
+ \a modifiers holds the keyboard modifier flags at the time of the event.
+
+ \a pixelDelta contains the scrolling delta in pixels,
+ \a angleDelta contains the rotation distance, and
+ \a orient holds the wheel's orientation.
+
+ \sa pos() globalPos() delta() state()
+*/
+
+QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos,
+ QPoint pixelDelta, QPoint angleDelta, int qt4Delta, Qt::Orientation qt4Orientation,
+ Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
+ : QInputEvent(Wheel, modifiers), p(pos), g(globalPos), pixelD(pixelDelta),
+ angleD(angleDelta), qt4D(qt4Delta), qt4O(qt4Orientation), mouseState(buttons)
+{}
+
+
#endif // QT_NO_WHEELEVENT
/*!
- \fn int QWheelEvent::delta() const
+ \fn QPoint QWheelEvent::pixelDelta() const
+
+ Returns the scrolling distance in pixels on screen. This value is
+ provided on platforms that support high-resolution pixel-based
+ delta values, such as Mac OS X. The value should be used directly
+ to scroll content on screen.
+
+ Example:
+
+ \snippet doc/src/snippets/code/src_gui_kernel_qevent.cpp 0
+*/
+
+/*!
+ \fn QPoint QWheelEvent::angleDelta() const
Returns the distance that the wheel is rotated, in eighths of a
degree. A positive value indicates that the wheel was rotated
@@ -556,6 +608,12 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, int delta
*/
/*!
+ \fn int QWheelEvent::delta() const
+
+ This function has been deprecated, use pixelDelta() or angleDelta() instead.
+*/
+
+/*!
\fn const QPoint &QWheelEvent::pos() const
Returns the position of the mouse cursor relative to the widget
diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h
index 93dea41de9..ca93095a94 100644
--- a/src/gui/kernel/qevent.h
+++ b/src/gui/kernel/qevent.h
@@ -148,9 +148,21 @@ public:
QWheelEvent(const QPointF &pos, const QPointF& globalPos, int delta,
Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers,
Qt::Orientation orient = Qt::Vertical);
+ QWheelEvent(const QPointF &pos, const QPointF& globalPos,
+ QPoint pixelDelta, QPoint angleDelta, int qt4Delta, Qt::Orientation qt4Orientation,
+ Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
+
~QWheelEvent();
- inline int delta() const { return d; }
+
+ inline QPoint pixelDelta() const { return pixelD; }
+ inline QPoint angleDelta() const { return angleD; }
+
+#if QT_DEPRECATED_SINCE(5, 0)
+ inline QT_DEPRECATED int delta() const { return qt4D; }
+ inline QT_DEPRECATED Qt::Orientation orientation() const { return qt4O; }
+#endif
+
#ifndef QT_NO_INTEGER_EVENT_COORDINATES
inline QPoint pos() const { return p.toPoint(); }
inline QPoint globalPos() const { return g.toPoint(); }
@@ -163,15 +175,15 @@ public:
inline const QPointF &globalPosF() const { return g; }
inline Qt::MouseButtons buttons() const { return mouseState; }
- Qt::Orientation orientation() const { return o; }
-
-
protected:
QPointF p;
QPointF g;
- int d;
+ QPoint pixelD;
+ QPoint angleD;
+ int qt4D;
+ Qt::Orientation qt4O;
Qt::MouseButtons mouseState;
- Qt::Orientation o;
+ int reserved;
};
#endif
diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp
index 2e7441d62c..1cd448a6bb 100644
--- a/src/gui/kernel/qguiapplication.cpp
+++ b/src/gui/kernel/qguiapplication.cpp
@@ -1032,8 +1032,7 @@ void QGuiApplicationPrivate::processWheelEvent(QWindowSystemInterfacePrivate::Wh
QWindow *window = e->window.data();
if (window) {
- QWheelEvent ev(e->localPos, e->globalPos, e->delta, buttons, e->modifiers,
- e->orient);
+ QWheelEvent ev(e->localPos, e->globalPos, e->pixelDelta, e->angleDelta, e->qt4Delta, e->qt4Orientation, buttons, e->modifiers);
ev.setTimestamp(e->timestamp);
QGuiApplication::sendSpontaneousEvent(window, &ev);
return;
diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.cpp b/src/gui/kernel/qwindowsysteminterface_qpa.cpp
index e6c4454104..40a4ec07a6 100644
--- a/src/gui/kernel/qwindowsysteminterface_qpa.cpp
+++ b/src/gui/kernel/qwindowsysteminterface_qpa.cpp
@@ -189,8 +189,53 @@ void QWindowSystemInterface::handleWheelEvent(QWindow *w, const QPointF & local,
void QWindowSystemInterface::handleWheelEvent(QWindow *tlw, ulong timestamp, const QPointF & local, const QPointF & global, int d, Qt::Orientation o, Qt::KeyboardModifiers mods)
{
- QWindowSystemInterfacePrivate::WheelEvent *e =
- new QWindowSystemInterfacePrivate::WheelEvent(tlw, timestamp, local, global, d, o, mods);
+ QPoint point = (o == Qt::Vertical) ? QPoint(0, d) : QPoint(d, 0);
+ handleWheelEvent(tlw, timestamp, local, global, point, point, mods);
+}
+
+void QWindowSystemInterface::handleWheelEvent(QWindow *w, const QPointF & local, const QPointF & global, QPoint pixelDelta, QPoint angleDelta, Qt::KeyboardModifiers mods)
+{
+ unsigned long time = QWindowSystemInterfacePrivate::eventTime.elapsed();
+ handleWheelEvent(w, time, local, global, pixelDelta, angleDelta, mods);
+}
+
+void QWindowSystemInterface::handleWheelEvent(QWindow *tlw, ulong timestamp, const QPointF & local, const QPointF & global, QPoint pixelDelta, QPoint angleDelta, Qt::KeyboardModifiers mods)
+{
+ // Qt 4 sends two separate wheel events for horizontal and vertical
+ // deltas. For Qt 5 we want to send the deltas in one event, but at the
+ // same time preserve source and behavior compatibility with Qt 4.
+ //
+ // In addition high-resolution pixel-based deltas are also supported.
+ // Platforms that does not support these may pass a null point here.
+ // Angle deltas must always be sent in addition to pixel deltas.
+ QWindowSystemInterfacePrivate::WheelEvent *e;
+
+ if (angleDelta.isNull())
+ return;
+
+ // Simple case: vertical deltas only:
+ if (angleDelta.y() != 0 && angleDelta.x() == 0) {
+ e = new QWindowSystemInterfacePrivate::WheelEvent(tlw, timestamp, local, global, pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical, mods);
+ QWindowSystemInterfacePrivate::queueWindowSystemEvent(e);
+ return;
+ }
+
+ // Simple case: horizontal deltas only:
+ if (angleDelta.y() == 0 && angleDelta.x() != 0) {
+ e = new QWindowSystemInterfacePrivate::WheelEvent(tlw, timestamp, local, global, pixelDelta, angleDelta, angleDelta.x(), Qt::Horizontal, mods);
+ QWindowSystemInterfacePrivate::queueWindowSystemEvent(e);
+ return;
+ }
+
+ // 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(tlw, timestamp, local, global, pixelDelta, angleDelta, angleDelta.y(), Qt::Vertical, mods);
+ QWindowSystemInterfacePrivate::queueWindowSystemEvent(e);
+
+ // The second event contains null pixel and angle points and the
+ // Qt 4 compatibility horizontal angle delta.
+ e = new QWindowSystemInterfacePrivate::WheelEvent(tlw, timestamp, local, global, QPoint(), QPoint(), angleDelta.x(), Qt::Horizontal, mods);
QWindowSystemInterfacePrivate::queueWindowSystemEvent(e);
}
diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.h b/src/gui/kernel/qwindowsysteminterface_qpa.h
index 78152a1178..836fb40bd7 100644
--- a/src/gui/kernel/qwindowsysteminterface_qpa.h
+++ b/src/gui/kernel/qwindowsysteminterface_qpa.h
@@ -80,7 +80,10 @@ public:
quint32 nativeModifiers,
const QString& text = QString(), bool autorep = false,
ushort count = 1);
+ static void handleWheelEvent(QWindow *w, const QPointF & local, const QPointF & global, QPoint pixelDelta, QPoint angleDelta, Qt::KeyboardModifiers mods = Qt::NoModifier);
+ static void handleWheelEvent(QWindow *w, ulong timestamp, const QPointF & local, const QPointF & global, QPoint pixelDelta, QPoint angleDelta, Qt::KeyboardModifiers mods = Qt::NoModifier);
+ // Wheel event compatibility functions. Will be removed: do not use.
static void handleWheelEvent(QWindow *w, const QPointF & local, const QPointF & global, int d, Qt::Orientation o, Qt::KeyboardModifiers mods = Qt::NoModifier);
static void handleWheelEvent(QWindow *w, ulong timestamp, const QPointF & local, const QPointF & global, int d, Qt::Orientation o, Qt::KeyboardModifiers mods = Qt::NoModifier);
diff --git a/src/gui/kernel/qwindowsysteminterface_qpa_p.h b/src/gui/kernel/qwindowsysteminterface_qpa_p.h
index 661b39da88..f5c141b4c9 100644
--- a/src/gui/kernel/qwindowsysteminterface_qpa_p.h
+++ b/src/gui/kernel/qwindowsysteminterface_qpa_p.h
@@ -157,13 +157,15 @@ public:
class WheelEvent : public InputEvent {
public:
- WheelEvent(QWindow *w, ulong time, const QPointF & local, const QPointF & global, int d,
- Qt::Orientation o, Qt::KeyboardModifiers mods)
- : InputEvent(w, time, Wheel, mods), delta(d), localPos(local), globalPos(global), orient(o) { }
- int delta;
+ WheelEvent(QWindow *w, ulong time, const QPointF & local, const QPointF & global, QPoint pixelD, QPoint angleD, int qt4D, Qt::Orientation qt4O,
+ Qt::KeyboardModifiers mods)
+ : InputEvent(w, time, Wheel, mods), pixelDelta(pixelD), angleDelta(angleD), qt4Delta(qt4D), qt4Orientation(qt4O), localPos(local), globalPos(global) { }
+ QPoint pixelDelta;
+ QPoint angleDelta;
+ int qt4Delta;
+ Qt::Orientation qt4Orientation;
QPointF localPos;
QPointF globalPos;
- Qt::Orientation orient;
};
class KeyEvent : public InputEvent {
diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm
index 577e0751c6..a43b3fe893 100644
--- a/src/plugins/platforms/cocoa/qnsview.mm
+++ b/src/plugins/platforms/cocoa/qnsview.mm
@@ -371,14 +371,11 @@ static QTouchDevice *touchDevice = 0;
#ifndef QT_NO_WHEELEVENT
- (void)scrollWheel:(NSEvent *)theEvent
{
- int deltaX = 0;
- int deltaY = 0;
- int deltaZ = 0;
-
const EventRef carbonEvent = (EventRef)[theEvent eventRef];
const UInt32 carbonEventKind = carbonEvent ? ::GetEventKind(carbonEvent) : 0;
const bool scrollEvent = carbonEventKind == kEventMouseScroll;
+ QPoint angleDelta;
if (scrollEvent) {
// The mouse device contains pixel scroll wheel support (Mighty Mouse, Trackpad).
// Since deviceDelta is delivered as pixels rather than degrees, we need to
@@ -389,40 +386,45 @@ static QTouchDevice *touchDevice = 0;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if ([theEvent respondsToSelector:@selector(scrollingDeltaX)]) {
- deltaX = [theEvent scrollingDeltaX] * pixelsToDegrees;
- deltaY = [theEvent scrollingDeltaY] * pixelsToDegrees;
- // scrollingDeltaZ API is missing.
+ angleDelta.setX([theEvent scrollingDeltaX] * pixelsToDegrees);
+ angleDelta.setY([theEvent scrollingDeltaY] * pixelsToDegrees);
} else
#endif
{
- deltaX = [theEvent deviceDeltaX] * pixelsToDegrees;
- deltaY = [theEvent deviceDeltaY] * pixelsToDegrees;
- deltaZ = [theEvent deviceDeltaZ] * pixelsToDegrees;
+ angleDelta.setX([theEvent deviceDeltaX] * pixelsToDegrees);
+ angleDelta.setY([theEvent deviceDeltaY] * pixelsToDegrees);
}
} else {
// carbonEventKind == kEventMouseWheelMoved
// Remove acceleration, and use either -120 or 120 as delta:
- deltaX = qBound(-120, int([theEvent deltaX] * 10000), 120);
- deltaY = qBound(-120, int([theEvent deltaY] * 10000), 120);
- deltaZ = qBound(-120, int([theEvent deltaZ] * 10000), 120);
+ angleDelta.setX(qBound(-120, int([theEvent deltaX] * 10000), 120));
+ angleDelta.setY(qBound(-120, int([theEvent deltaY] * 10000), 120));
}
+ QPoint pixelDelta;
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+ if ([theEvent respondsToSelector:@selector(scrollingDeltaX)]) {
+ if ([theEvent hasPreciseScrollingDeltas]) {
+ pixelDelta.setX([theEvent scrollingDeltaX]);
+ pixelDelta.setY([theEvent scrollingDeltaY]);
+ } else {
+ // docs: "In the case of !hasPreciseScrollingDeltas, multiply the delta with the line width."
+ // scrollingDeltaX seems to return a minimum value of 0.1 in this case, map that to two pixels.
+ const CGFloat lineWithEstimate = 20.0;
+ pixelDelta.setX([theEvent scrollingDeltaX] * lineWithEstimate);
+ pixelDelta.setY([theEvent scrollingDeltaY] * lineWithEstimate);
+ }
+ }
+#endif
+
+
NSPoint windowPoint = [self convertPoint: [theEvent locationInWindow] fromView: nil];
QPoint qt_windowPoint(windowPoint.x, windowPoint.y);
NSTimeInterval timestamp = [theEvent timestamp];
ulong qt_timestamp = timestamp * 1000;
- if (deltaX != 0)
- QWindowSystemInterface::handleWheelEvent(m_window, qt_timestamp, qt_windowPoint, qt_windowPoint, deltaX, Qt::Horizontal);
-
- if (deltaY != 0)
- QWindowSystemInterface::handleWheelEvent(m_window, qt_timestamp, qt_windowPoint, qt_windowPoint, deltaY, Qt::Vertical);
-
- if (deltaZ != 0)
- // Qt doesn't explicitly support wheels with a Z component. In a misguided attempt to
- // try to be ahead of the pack, I'm adding this extra value.
- QWindowSystemInterface::handleWheelEvent(m_window, qt_timestamp, qt_windowPoint, qt_windowPoint, deltaY, (Qt::Orientation)3);
+ QWindowSystemInterface::handleWheelEvent(m_window, qt_timestamp, qt_windowPoint, qt_windowPoint, pixelDelta, angleDelta);
}
#endif //QT_NO_WHEELEVENT
diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp
index 650d4af38b..c350223278 100644
--- a/src/widgets/kernel/qapplication.cpp
+++ b/src/widgets/kernel/qapplication.cpp
@@ -3357,8 +3357,8 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
}
while (w) {
- QWheelEvent we(relpos, wheel->globalPos(), wheel->delta(), wheel->buttons(),
- wheel->modifiers(), wheel->orientation());
+ QWheelEvent we(relpos, wheel->globalPos(), wheel->pixelDelta(), wheel->angleDelta(), wheel->delta(), wheel->orientation(), wheel->buttons(),
+ wheel->modifiers());
we.spont = wheel->spontaneous();
res = d->notify_helper(w, w == receiver ? wheel : &we);
eventAccepted = ((w == receiver) ? wheel : &we)->isAccepted();
diff --git a/src/widgets/kernel/qwidgetwindow_qpa.cpp b/src/widgets/kernel/qwidgetwindow_qpa.cpp
index c04d8a9dc3..4d5e9d5774 100644
--- a/src/widgets/kernel/qwidgetwindow_qpa.cpp
+++ b/src/widgets/kernel/qwidgetwindow_qpa.cpp
@@ -364,7 +364,7 @@ void QWidgetWindow::handleWheelEvent(QWheelEvent *event)
QPoint mapped = widget->mapFrom(m_widget, event->pos());
- QWheelEvent translated(mapped, event->globalPos(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
+ QWheelEvent translated(mapped, event->globalPos(), event->pixelDelta(), event->pixelDelta(), event->delta(), event->orientation(), event->buttons(), event->modifiers());
QGuiApplication::sendSpontaneousEvent(widget, &translated);
}