diff options
author | Jan Arve Saether <jan-arve.saether@qt.io> | 2016-12-23 15:30:33 +0100 |
---|---|---|
committer | Jan Arve Sæther <jan-arve.saether@qt.io> | 2016-12-29 10:27:49 +0000 |
commit | 70113ef66b245e58ce12e2a9d26268e2eaeb3a42 (patch) | |
tree | c3882090db4e76aac1e5c5e7c8da13b41db4806c /src/quick/handlers | |
parent | 953b5070160a3a1bed7aaf986a1bb1c3b33b0dca (diff) |
Don't assume that target is the parent item
To summarize: A pointer handler always gets its pointer events from its
parent item. It applies its effect (drag, pinch, ...) on the item
referenced to by the target property. By default, target refers to the
parent, but that is not always the case. In addition to this we also
have to handle the case when the target is null
Change-Id: If62108abf0aeb713906bf88472ad9a32a74efff6
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/quick/handlers')
-rw-r--r-- | src/quick/handlers/qquickdraghandler.cpp | 3 | ||||
-rw-r--r-- | src/quick/handlers/qquickhandlersmodule.cpp | 6 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler.cpp | 42 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler_p.h | 7 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointersinglehandler.cpp | 2 |
5 files changed, 33 insertions, 27 deletions
diff --git a/src/quick/handlers/qquickdraghandler.cpp b/src/quick/handlers/qquickdraghandler.cpp index f2a1b74d6c..a1c3486bd0 100644 --- a/src/quick/handlers/qquickdraghandler.cpp +++ b/src/quick/handlers/qquickdraghandler.cpp @@ -76,9 +76,6 @@ bool QQuickDragHandler::wantsEventPoint(QQuickEventPoint *point) void QQuickDragHandler::handleEventPoint(QQuickEventPoint *point) { - // If there's no target or the target has no parent, we shouldn't be dragging - if (!target() || !target()->parentItem()) - return; point->setAccepted(); switch (point->state()) { case QQuickEventPoint::Pressed: diff --git a/src/quick/handlers/qquickhandlersmodule.cpp b/src/quick/handlers/qquickhandlersmodule.cpp index 0daa1841f8..697a198e67 100644 --- a/src/quick/handlers/qquickhandlersmodule.cpp +++ b/src/quick/handlers/qquickhandlersmodule.cpp @@ -53,10 +53,10 @@ QT_BEGIN_NAMESPACE static QQmlPrivate::AutoParentResult handler_autoParent(QObject *obj, QObject *parent) { - if (QQuickItem *parentItem = qmlobject_cast<QQuickItem *>(parent)) { + if (qmlobject_cast<QQuickItem *>(parent)) { QQuickPointerHandler *handler = qmlobject_cast<QQuickPointerHandler *>(obj); - if (handler && !handler->target()) { - handler->setTarget(parentItem); + if (handler) { + handler->setParent(parent); return QQmlPrivate::Parented; } } diff --git a/src/quick/handlers/qquickpointerhandler.cpp b/src/quick/handlers/qquickpointerhandler.cpp index 6225d2a24a..d51835a24b 100644 --- a/src/quick/handlers/qquickpointerhandler.cpp +++ b/src/quick/handlers/qquickpointerhandler.cpp @@ -60,9 +60,19 @@ QQuickPointerHandler::QQuickPointerHandler(QObject *parent) , m_target(nullptr) , m_enabled(true) , m_active(false) + , m_targetExplicitlySet(false) { } +QQuickPointerHandler::~QQuickPointerHandler() +{ + QQuickItem *parItem = parentItem(); + if (parItem) { + QQuickItemPrivate *p = QQuickItemPrivate::get(parItem); + p->extra.value().pointerHandlers.removeOne(this); + } +} + void QQuickPointerHandler::setGrab(QQuickEventPoint *point, bool grab) { if (grab) { @@ -81,14 +91,16 @@ void QQuickPointerHandler::setGrab(QQuickEventPoint *point, bool grab) QPointF QQuickPointerHandler::eventPos(const QQuickEventPoint *point) const { - return (m_target ? m_target->mapFromScene(point->scenePos()) : point->scenePos()); + return (target() ? target()->mapFromScene(point->scenePos()) : point->scenePos()); } -bool QQuickPointerHandler::targetContains(const QQuickEventPoint *point) const +bool QQuickPointerHandler::parentContains(const QQuickEventPoint *point) const { - if (!m_target || !point) - return false; - return m_target->contains(m_target->mapFromScene(point->scenePos())); + if (point) { + if (QQuickItem *par = parentItem()) + return par->contains(par->mapFromScene(point->scenePos())); + } + return false; } /*! @@ -111,25 +123,21 @@ void QQuickPointerHandler::setEnabled(bool enabled) void QQuickPointerHandler::setTarget(QQuickItem *target) { + m_targetExplicitlySet = true; if (m_target == target) return; - if (m_target) { - QQuickItemPrivate *p = QQuickItemPrivate::get(m_target); - p->extra.value().pointerHandlers.removeOne(this); - } - m_target = target; - if (m_target) { - QQuickItemPrivate *p = QQuickItemPrivate::get(m_target); - p->extra.value().pointerHandlers.append(this); - // Accept all buttons, and leave filtering to pointerEvent() and/or user JS, - // because there can be multiple handlers... - m_target->setAcceptedMouseButtons(Qt::AllButtons); - } emit targetChanged(); } +QQuickItem *QQuickPointerHandler::target() const +{ + if (!m_targetExplicitlySet) + return parentItem(); + return m_target; +} + void QQuickPointerHandler::handlePointerEvent(QQuickPointerEvent *event) { const bool wants = wantsPointerEvent(event); diff --git a/src/quick/handlers/qquickpointerhandler_p.h b/src/quick/handlers/qquickpointerhandler_p.h index 9595897d72..85f1096410 100644 --- a/src/quick/handlers/qquickpointerhandler_p.h +++ b/src/quick/handlers/qquickpointerhandler_p.h @@ -70,7 +70,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPointerHandler : public QObject public: QQuickPointerHandler(QObject *parent = 0); - virtual ~QQuickPointerHandler() { } + virtual ~QQuickPointerHandler(); public: bool enabled() const { return m_enabled; } @@ -78,7 +78,7 @@ public: bool active() const { return m_active; } - QQuickItem *target() const { return m_target; } + QQuickItem *target() const; void setTarget(QQuickItem *target); QQuickItem * parentItem() const { return static_cast<QQuickItem *>(QObject::parent()); } @@ -102,13 +102,14 @@ protected: void setGrab(QQuickEventPoint *point, bool grab); virtual void handleGrabCancel(QQuickEventPoint *point); QPointF eventPos(const QQuickEventPoint *point) const; - bool targetContains(const QQuickEventPoint *point) const; + bool parentContains(const QQuickEventPoint *point) const; private: QQuickPointerEvent *m_currentEvent; QQuickItem *m_target; bool m_enabled : 1; bool m_active : 1; + bool m_targetExplicitlySet : 1; friend class QQuickEventPoint; }; diff --git a/src/quick/handlers/qquickpointersinglehandler.cpp b/src/quick/handlers/qquickpointersinglehandler.cpp index d97348ea22..5b2d6b0801 100644 --- a/src/quick/handlers/qquickpointersinglehandler.cpp +++ b/src/quick/handlers/qquickpointersinglehandler.cpp @@ -110,7 +110,7 @@ void QQuickPointerSingleHandler::handlePointerEventImpl(QQuickPointerEvent *even bool QQuickPointerSingleHandler::wantsEventPoint(QQuickEventPoint *point) { - return targetContains(point); + return parentContains(point); } void QQuickPointerSingleHandler::handleGrabCancel(QQuickEventPoint *point) |