From 507efe5a8a2390813fb620a91b0b3b6b383f599d Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 14 Feb 2017 10:04:49 +0100 Subject: unify handler grab state handling into onGrabChanged MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit onGrabChanged and handleGrab looked redundant. It was also not clear how important it is for handlers to react to passive ungrabs, overrides or cancellations. Rather than debating about when to call one of these and when not to, let's centralize the responsibility in QQuickEventPoint (because the grabber pointers are stored there, so it's the ultimate destination of any grab change), and let's notify all the relevant handlers about all changes, with enough information that each handler can decide for itself what's important and what isn't. But so far most handlers don't need to override this virtual. The base class QQuickPointerHandler takes care of setting the active property to false, rejecting the eventpoint, and unsetting keepMouseGrab and keepTouchGrab whenever grab is lost; and emitting grabChanged or canceled as appropriate to notify any QML code which needs to know. Subclasses mainly care about the change of active state: they must initiate active state themselves, and may react when it reverts to false. Change-Id: I6c7f29472d12564d74ae091b0c81fa08fe131ce7 Reviewed-by: Jan Arve Sæther --- src/quick/items/qquickevents.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src/quick/items/qquickevents.cpp') diff --git a/src/quick/items/qquickevents.cpp b/src/quick/items/qquickevents.cpp index 1843d2b656..cddcf02955 100644 --- a/src/quick/items/qquickevents.cpp +++ b/src/quick/items/qquickevents.cpp @@ -651,11 +651,18 @@ void QQuickEventPoint::setGrabberPointerHandler(QQuickPointerHandler *grabber, b } if (exclusive) { if (grabber != m_exclusiveGrabber.data()) { + if (grabber) { + grabber->onGrabChanged(grabber, GrabExclusive, this); + for (QPointer passiveGrabber : m_passiveGrabbers) { + if (passiveGrabber != grabber) + passiveGrabber->onGrabChanged(grabber, OverrideGrabPassive, this); + } + } else if (QQuickPointerHandler *oldGrabberPointerHandler = qmlobject_cast(m_exclusiveGrabber.data())) { + oldGrabberPointerHandler->onGrabChanged(oldGrabberPointerHandler, UngrabExclusive, this); + } m_exclusiveGrabber = QPointer(grabber); m_grabberIsHandler = true; m_sceneGrabPos = m_scenePos; - for (QPointer passiveGrabber : m_passiveGrabbers) - passiveGrabber->handleGrab(this, grabber, true); } } else { if (!grabber) { @@ -663,8 +670,10 @@ void QQuickEventPoint::setGrabberPointerHandler(QQuickPointerHandler *grabber, b return; } auto ptr = QPointer(grabber); - if (!m_passiveGrabbers.contains(ptr)) + if (!m_passiveGrabbers.contains(ptr)) { m_passiveGrabbers.append(ptr); + grabber->onGrabChanged(grabber, GrabPassive, this); + } } } @@ -684,7 +693,7 @@ void QQuickEventPoint::cancelExclusiveGrab() << ": grab (exclusive)" << m_exclusiveGrabber << "-> nullptr"; } if (auto handler = grabberPointerHandler()) - handler->handleGrabCancel(this); + handler->onGrabChanged(handler, CancelGrabExclusive, this); m_exclusiveGrabber.clear(); } @@ -695,15 +704,24 @@ void QQuickEventPoint::cancelExclusiveGrab() */ void QQuickEventPoint::cancelPassiveGrab(QQuickPointerHandler *handler) { - if (m_passiveGrabbers.removeOne(QPointer(handler))) { + if (removePassiveGrabber(handler)) { if (Q_UNLIKELY(lcPointerGrab().isDebugEnabled())) { qCDebug(lcPointerGrab) << pointDeviceName(this) << "point" << hex << m_pointId << pointStateString(this) << ": grab (passive)" << handler << "removed"; } - handler->handleGrabCancel(this); + handler->onGrabChanged(handler, CancelGrabPassive, this); } } +/*! + If this point has the given \a handler as a passive grabber, remove it as grabber. + Returns true if it was removed, false if it wasn't a grabber. +*/ +bool QQuickEventPoint::removePassiveGrabber(QQuickPointerHandler *handler) +{ + return m_passiveGrabbers.removeOne(handler); +} + /*! If the given \a handler is grabbing this point passively, exclusively or both, cancel the grab and remove it as grabber. @@ -718,7 +736,7 @@ void QQuickEventPoint::cancelPassiveGrab(QQuickPointerHandler *handler) void QQuickEventPoint::cancelAllGrabs(QQuickPointerHandler *handler) { if (m_exclusiveGrabber == handler) { - handler->handleGrabCancel(this); + handler->onGrabChanged(handler, CancelGrabExclusive, this); m_exclusiveGrabber.clear(); } cancelPassiveGrab(handler); -- cgit v1.2.3