diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-03-24 07:44:14 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2018-06-28 09:44:50 +0000 |
commit | ca9e2a0d7488cbbb94d8343c43eb49c6ee5f6519 (patch) | |
tree | 79a0ba3e2a54af085f9efc187f36bfc8f100a4cd /src/quick | |
parent | 3cda7df8ac8fc6f9a5562e89a4b047449dc3b2bc (diff) |
Add HoverHandler to detect a hovering mouse pointer
Detect whether the handler's parent contains the mouse, while the
point property tracks the event point (position etc.)
Task-number: QTBUG-68072
Change-Id: Ica99332596eab3e344852a11f1ceb7aaf6348c86
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Diffstat (limited to 'src/quick')
-rw-r--r-- | src/quick/handlers/handlers.pri | 2 | ||||
-rw-r--r-- | src/quick/handlers/qquickhandlersmodule.cpp | 2 | ||||
-rw-r--r-- | src/quick/handlers/qquickhoverhandler.cpp | 107 | ||||
-rw-r--r-- | src/quick/handlers/qquickhoverhandler_p.h | 91 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler.cpp | 12 | ||||
-rw-r--r-- | src/quick/handlers/qquicksinglepointhandler.cpp | 7 | ||||
-rw-r--r-- | src/quick/handlers/qquicksinglepointhandler_p.h | 2 | ||||
-rw-r--r-- | src/quick/items/qquickitem.cpp | 13 | ||||
-rw-r--r-- | src/quick/items/qquickitem_p.h | 1 | ||||
-rw-r--r-- | src/quick/items/qquickwindow.cpp | 25 |
10 files changed, 257 insertions, 5 deletions
diff --git a/src/quick/handlers/handlers.pri b/src/quick/handlers/handlers.pri index a47d144fbb..111a526ce2 100644 --- a/src/quick/handlers/handlers.pri +++ b/src/quick/handlers/handlers.pri @@ -2,6 +2,7 @@ HEADERS += \ $$PWD/qquickdraghandler_p.h \ $$PWD/qquickhandlerpoint_p.h \ $$PWD/qquickhandlersmodule_p.h \ + $$PWD/qquickhoverhandler_p.h \ $$PWD/qquickmultipointhandler_p.h \ $$PWD/qquickpinchhandler_p.h \ $$PWD/qquickpointerdevicehandler_p.h \ @@ -17,6 +18,7 @@ SOURCES += \ $$PWD/qquickdraghandler.cpp \ $$PWD/qquickhandlerpoint.cpp \ $$PWD/qquickhandlersmodule.cpp \ + $$PWD/qquickhoverhandler.cpp \ $$PWD/qquickmultipointhandler.cpp \ $$PWD/qquickpinchhandler.cpp \ $$PWD/qquickpointerdevicehandler.cpp \ diff --git a/src/quick/handlers/qquickhandlersmodule.cpp b/src/quick/handlers/qquickhandlersmodule.cpp index c9e08fe4f2..814602c0bd 100644 --- a/src/quick/handlers/qquickhandlersmodule.cpp +++ b/src/quick/handlers/qquickhandlersmodule.cpp @@ -40,6 +40,7 @@ #include "qquickhandlersmodule_p.h" #include "qquickpointerhandler_p.h" #include "qquickdraghandler_p.h" +#include "qquickhoverhandler_p.h" #include "qquickpinchhandler_p.h" #include "qquickpointhandler_p.h" #include "qquicktaphandler_p.h" @@ -87,6 +88,7 @@ static void qt_quickhandlers_defineModule(const char *uri, int major, int minor) qmlRegisterType<QQuickDragHandler>(uri,major,minor,"DragHandler"); qmlRegisterUncreatableType<QQuickDragAxis>(uri, major, minor, "DragAxis", QQuickDragHandler::tr("DragAxis is only available as a grouped property of DragHandler")); + qmlRegisterType<QQuickHoverHandler>(uri,major,minor,"HoverHandler"); qmlRegisterType<QQuickPinchHandler>(uri,major,minor,"PinchHandler"); qmlRegisterType<QQuickTapHandler>(uri,major,minor,"TapHandler"); qRegisterMetaType<QQuickHandlerPoint>(); diff --git a/src/quick/handlers/qquickhoverhandler.cpp b/src/quick/handlers/qquickhoverhandler.cpp new file mode 100644 index 0000000000..430722bd93 --- /dev/null +++ b/src/quick/handlers/qquickhoverhandler.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickhoverhandler_p.h" + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(lcHoverHandler, "qt.quick.handler.hover") + +/*! + \qmltype HoverHandler + \instantiates QQuickHoverHandler + \inqmlmodule QtQuick + \ingroup qtquick-handlers + \brief Handler for mouse and tablet hover + + HoverHandler detects a hovering cursor. Since touchscreens don't generally + offer hover events, in practice it detects a hovering mouse or tablet stylus. + + \sa MouseArea +*/ + +QQuickHoverHandler::QQuickHoverHandler(QObject *parent) + : QQuickSinglePointHandler(parent) + , m_hovered(false) +{ + // Rule out the touchscreen for now (can be overridden in QML in case a hover-detecting touchscreen exists) + setAcceptedDevices(static_cast<QQuickPointerDevice::DeviceType>( + static_cast<int>(QQuickPointerDevice::AllDevices) ^ static_cast<int>(QQuickPointerDevice::TouchScreen))); +} + +QQuickHoverHandler::~QQuickHoverHandler() +{ + QQuickItemPrivate *parItemPrivate = QQuickItemPrivate::get(parentItem()); + parItemPrivate->setHasHoverInChild(false); +} + +void QQuickHoverHandler::componentComplete() +{ + parentItem()->setAcceptHoverEvents(true); + QQuickItemPrivate::get(parentItem())->setHasHoverInChild(true); +} + +bool QQuickHoverHandler::wantsPointerEvent(QQuickPointerEvent *event) +{ + QQuickEventPoint *point = event->point(0); + if (QQuickPointerDeviceHandler::wantsPointerEvent(event) && wantsEventPoint(point)) { + // assume this is a mouse event, so there's only one point + setPointId(point->pointId()); + return true; + } + setHovered(false); + return false; +} + +void QQuickHoverHandler::handleEventPoint(QQuickEventPoint *point) +{ + setHovered(true); + setPassiveGrab(point); +} + +void QQuickHoverHandler::setHovered(bool hovered) +{ + if (m_hovered != hovered) { + qCDebug(lcHoverHandler) << objectName() << "hovered" << m_hovered << "->" << hovered; + m_hovered = hovered; + emit hoveredChanged(); + } +} + +QT_END_NAMESPACE diff --git a/src/quick/handlers/qquickhoverhandler_p.h b/src/quick/handlers/qquickhoverhandler_p.h new file mode 100644 index 0000000000..876d01761b --- /dev/null +++ b/src/quick/handlers/qquickhoverhandler_p.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKHOVERHANDLER_H +#define QQUICKHOVERHANDLER_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qquickitem.h" +#include "qevent.h" +#include "qquicksinglepointhandler_p.h" +#include <QtCore/qbasictimer.h> + +QT_BEGIN_NAMESPACE + +class Q_AUTOTEST_EXPORT QQuickHoverHandler : public QQuickSinglePointHandler +{ + Q_OBJECT + Q_PROPERTY(bool hovered READ isHovered NOTIFY hoveredChanged) + +public: + explicit QQuickHoverHandler(QObject *parent = 0); + ~QQuickHoverHandler(); + + bool isHovered() const { return m_hovered; } + +Q_SIGNALS: + void hoveredChanged(); + +protected: + void componentComplete() override; + bool wantsPointerEvent(QQuickPointerEvent *event) override; + void handleEventPoint(QQuickEventPoint *point) override; + +private: + void setHovered(bool hovered); + +private: + bool m_hovered; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickHoverHandler) + +#endif // QQUICKHOVERHANDLER_H diff --git a/src/quick/handlers/qquickpointerhandler.cpp b/src/quick/handlers/qquickpointerhandler.cpp index 4539aa221c..33c2acebe2 100644 --- a/src/quick/handlers/qquickpointerhandler.cpp +++ b/src/quick/handlers/qquickpointerhandler.cpp @@ -310,9 +310,15 @@ QPointF QQuickPointerHandler::eventPos(const QQuickEventPoint *point) const bool QQuickPointerHandler::parentContains(const QQuickEventPoint *point) const { - if (point) { - if (QQuickItem *par = parentItem()) - return par->contains(par->mapFromScene(point->scenePosition())); + if (!point) + return false; + if (QQuickItem *par = parentItem()) { + if (par->window()) { + QPoint screenPosition = par->window()->mapToGlobal(point->scenePosition().toPoint()); + if (!par->window()->geometry().contains(screenPosition)) + return false; + } + return par->contains(par->mapFromScene(point->scenePosition())); } return false; } diff --git a/src/quick/handlers/qquicksinglepointhandler.cpp b/src/quick/handlers/qquicksinglepointhandler.cpp index 04b4214bab..84f8349a53 100644 --- a/src/quick/handlers/qquicksinglepointhandler.cpp +++ b/src/quick/handlers/qquicksinglepointhandler.cpp @@ -120,7 +120,7 @@ bool QQuickSinglePointHandler::wantsPointerEvent(QQuickPointerEvent *event) } } if (chosen && candidatePointCount == 1) { - m_pointInfo.m_id = chosen->pointId(); + setPointId(chosen->pointId()); chosen->setAccepted(); } } @@ -185,6 +185,11 @@ void QQuickSinglePointHandler::moveTarget(QPointF pos, QQuickEventPoint *point) m_pointInfo.m_position = target()->mapFromScene(m_pointInfo.m_scenePosition); } +void QQuickSinglePointHandler::setPointId(int id) +{ + m_pointInfo.m_id = id; +} + /*! \qmlproperty int QtQuick::SinglePointHandler::acceptedButtons diff --git a/src/quick/handlers/qquicksinglepointhandler_p.h b/src/quick/handlers/qquicksinglepointhandler_p.h index 66ef1dbeb2..79bf74b222 100644 --- a/src/quick/handlers/qquicksinglepointhandler_p.h +++ b/src/quick/handlers/qquicksinglepointhandler_p.h @@ -87,6 +87,8 @@ protected: void moveTarget(QPointF pos, QQuickEventPoint *point); + void setPointId(int id); + private: void reset(); diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index 01413385d9..3b1402fe16 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -67,6 +67,7 @@ #include <QtQuick/private/qquickstate_p.h> #include <private/qquickitem_p.h> #include <QtQuick/private/qquickaccessibleattached_p.h> +#include <QtQuick/private/qquickhoverhandler_p.h> #include <QtQuick/private/qquickpointerhandler_p.h> #include <private/qv4engine_p.h> @@ -7342,6 +7343,8 @@ void QQuickItemPrivate::setHasHoverInChild(bool hasHover) QQuickItemPrivate *otherChildPrivate = QQuickItemPrivate::get(otherChild); if (otherChildPrivate->subtreeHoverEnabled || otherChildPrivate->hoverEnabled) return; // nope! sorry, something else wants it kept on. + if (otherChildPrivate->hasHoverHandlers()) + return; // nope! sorry, we have pointer handlers which are interested. } } @@ -8095,6 +8098,16 @@ bool QQuickItemPrivate::hasPointerHandlers() const return extra.isAllocated() && !extra->pointerHandlers.isEmpty(); } +bool QQuickItemPrivate::hasHoverHandlers() const +{ + if (!hasPointerHandlers()) + return false; + for (QQuickPointerHandler *h : extra->pointerHandlers) + if (qmlobject_cast<QQuickHoverHandler *>(h)) + return true; + return false; +} + #if QT_CONFIG(quick_shadereffect) QQuickItemLayer::QQuickItemLayer(QQuickItem *item) : m_item(item) diff --git a/src/quick/items/qquickitem_p.h b/src/quick/items/qquickitem_p.h index 93287cf0aa..f43f04ed39 100644 --- a/src/quick/items/qquickitem_p.h +++ b/src/quick/items/qquickitem_p.h @@ -280,6 +280,7 @@ public: QQuickItemLayer *layer() const; bool hasPointerHandlers() const; + bool hasHoverHandlers() const; // data property static void data_append(QQmlListProperty<QObject> *, QObject *); diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index c80e052dad..e53ca7f8de 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -46,6 +46,7 @@ #include "qquickevents_p_p.h" #include <private/qquickdrag_p.h> +#include <private/qquickhoverhandler_p.h> #include <private/qquickpointerhandler_p.h> #include <QtQuick/private/qsgrenderer_p.h> @@ -1546,8 +1547,20 @@ bool QQuickWindowPrivate::clearHover(ulong timestamp) QPointF pos = q->mapFromGlobal(QGuiApplicationPrivate::lastCursorPosition.toPoint()); bool accepted = false; - for (QQuickItem* item : qAsConst(hoverItems)) + for (QQuickItem* item : qAsConst(hoverItems)) { accepted = sendHoverEvent(QEvent::HoverLeave, item, pos, pos, QGuiApplication::keyboardModifiers(), timestamp, true) || accepted; + QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); + if (itemPrivate->hasPointerHandlers()) { + pos = q->mapFromGlobal(QCursor::pos()); + QQuickPointerEvent *pointerEvent = pointerEventInstance(QQuickPointerDevice::genericMouseDevice(), QEvent::MouseMove); + pointerEvent->point(0)->reset(Qt::TouchPointMoved, pos, quint64(1) << 24 /* mouse has device ID 1 */, timestamp, QVector2D()); + pointerEvent->point(0)->setAccepted(true); + pointerEvent->localize(item); + for (QQuickPointerHandler *h : itemPrivate->extra->pointerHandlers) + if (QQuickHoverHandler *hh = qmlobject_cast<QQuickHoverHandler *>(h)) + hh->handlePointerEvent(pointerEvent); + } + } hoverItems.clear(); return accepted; } @@ -1843,6 +1856,16 @@ bool QQuickWindowPrivate::deliverHoverEvent(QQuickItem *item, const QPointF &sce } } + if (itemPrivate->hasPointerHandlers()) { + QQuickPointerEvent *pointerEvent = pointerEventInstance(QQuickPointerDevice::genericMouseDevice(), QEvent::MouseMove); + pointerEvent->point(0)->reset(Qt::TouchPointMoved, scenePos, quint64(1) << 24 /* mouse has device ID 1 */, timestamp, QVector2D()); + pointerEvent->point(0)->setAccepted(true); + pointerEvent->localize(item); + for (QQuickPointerHandler *h : itemPrivate->extra->pointerHandlers) + if (QQuickHoverHandler *hh = qmlobject_cast<QQuickHoverHandler *>(h)) + hh->handlePointerEvent(pointerEvent); + } + if (itemPrivate->hoverEnabled) { QPointF p = item->mapFromScene(scenePos); if (item->contains(p)) { |