From b59336e73aca6915d96bbdc673710693d27e1096 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 7 Mar 2017 10:01:23 +0100 Subject: add PointHandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iad9ba3f8121ff22136e85413ec610bc1ee361e37 Reviewed-by: Jan Arve Sæther --- src/quick/handlers/handlers.pri | 2 + src/quick/handlers/qquickhandlersmodule.cpp | 2 + src/quick/handlers/qquickpointhandler.cpp | 134 ++++++++++++++++++++++++++++ src/quick/handlers/qquickpointhandler_p.h | 81 +++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100644 src/quick/handlers/qquickpointhandler.cpp create mode 100644 src/quick/handlers/qquickpointhandler_p.h diff --git a/src/quick/handlers/handlers.pri b/src/quick/handlers/handlers.pri index 9e32b9278c..8bd74d95da 100644 --- a/src/quick/handlers/handlers.pri +++ b/src/quick/handlers/handlers.pri @@ -5,6 +5,7 @@ HEADERS += \ $$PWD/qquickpinchhandler_p.h \ $$PWD/qquickpointerdevicehandler_p.h \ $$PWD/qquickpointerhandler_p.h \ + $$PWD/qquickpointhandler_p.h \ $$PWD/qquicksinglepointhandler_p.h \ $$PWD/qquicktaphandler_p.h \ @@ -15,6 +16,7 @@ SOURCES += \ $$PWD/qquickpinchhandler.cpp \ $$PWD/qquickpointerdevicehandler.cpp \ $$PWD/qquickpointerhandler.cpp \ + $$PWD/qquickpointhandler.cpp \ $$PWD/qquicksinglepointhandler.cpp \ $$PWD/qquicktaphandler.cpp \ diff --git a/src/quick/handlers/qquickhandlersmodule.cpp b/src/quick/handlers/qquickhandlersmodule.cpp index 8472c6a062..c9e08fe4f2 100644 --- a/src/quick/handlers/qquickhandlersmodule.cpp +++ b/src/quick/handlers/qquickhandlersmodule.cpp @@ -41,6 +41,7 @@ #include "qquickpointerhandler_p.h" #include "qquickdraghandler_p.h" #include "qquickpinchhandler_p.h" +#include "qquickpointhandler_p.h" #include "qquicktaphandler_p.h" static void initResources() @@ -82,6 +83,7 @@ static void qt_quickhandlers_defineModule(const char *uri, int major, int minor) qmlRegisterUncreatableType(uri,major,minor,"PointerHandler", QQuickPointerHandler::tr("PointerHandler is an abstract base class")); + qmlRegisterType(uri,major,minor,"PointHandler"); qmlRegisterType(uri,major,minor,"DragHandler"); qmlRegisterUncreatableType(uri, major, minor, "DragAxis", QQuickDragHandler::tr("DragAxis is only available as a grouped property of DragHandler")); diff --git a/src/quick/handlers/qquickpointhandler.cpp b/src/quick/handlers/qquickpointhandler.cpp new file mode 100644 index 0000000000..d40837c43e --- /dev/null +++ b/src/quick/handlers/qquickpointhandler.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 "qquickpointhandler_p.h" +#include +#include + +QT_BEGIN_NAMESPACE + +/*! + \qmltype PointHandler + \instantiates QQuickPointHandler + \inqmlmodule QtQuick + \ingroup qtquick-handlers + \brief Handler for reacting to a single touchpoint + + PointHandler can be used to show feedback about a touchpoint or the mouse + position, or to otherwise react to pointer events. By being only a passive + grabber, it has the ability to keep oversight of all movements, and its + grab cannot be stolen even when other gestures are detected and exclusive + grabs occur. + + If your goal is orthogonal surveillance of eventpoints, an older + alternative was QObject::installEventFilter(), but that has never been a + built-in QtQuick feature: it requires a custom C++ QQuickItem subclass. + PointHandler is more efficient than that, because only pointer events will + be delivered to it, during the course of normal event delivery in + QQuickWindow, whereas an event filter needs to filter all events of all + types, and thus sets itself up as a potential event delivery bottleneck. + + One possible use case is to add this handler to a transparent Item which is + on top of the rest of the scene, so that when a point is freshly pressed, + it will be delivered to that Item and its handlers first, providing the + opportunity to take the passive grab as early as possible. Then such an + item (like a pane of glass over the whole UI) also becomes a good parent + for other Items which visualize the kind of reactive feedback which must + always be on top; and likewise it can be the parent for popups, popovers, + dialogs and so on. For example, a declared Window can have an Item with a + high Z value so that it stays on top. It can also be helpful for your + main.cpp to use QQmlContext::setContextProperty() to make the "glass pane" + accessible by ID to the entire UI, so that other Items and PointHandlers + can be reparented to it. + + Inside a PointHandler you can declare a \l target Item, but PointHandler + will not automatically manipulate it in any way. The target Item can bind to + properties of the PointHandler. In this way it can follow a point's movements. + + \sa MultiPointTouchArea +*/ + +QQuickPointHandler::QQuickPointHandler(QObject *parent) + : QQuickSinglePointHandler(parent) +{ + setIgnoreAdditionalPoints(); +} + +QQuickPointHandler::~QQuickPointHandler() +{ +} + +bool QQuickPointHandler::wantsEventPoint(QQuickEventPoint *pt) +{ + // On press, we want it unless a sibling of the same type also does. + if (pt->state() == QQuickEventPoint::Pressed && QQuickSinglePointHandler::wantsEventPoint(pt)) { + for (const QQuickPointerHandler *grabber : pt->passiveGrabbers()) { + if (grabber && grabber->parent() == parent() && + grabber->metaObject()->className() == metaObject()->className()) + return false; + } + return true; + } + // If we've already been interested in a point, stay interested, even if it has strayed outside bounds. + return (pt->state() != QQuickEventPoint::Pressed && point().id() == pt->pointId()); +} + +void QQuickPointHandler::handleEventPoint(QQuickEventPoint *point) +{ + switch (point->state()) { + case QQuickEventPoint::Pressed: + setPassiveGrab(point); + setActive(true); + break; + case QQuickEventPoint::Released: + setActive(false); + break; + default: + break; + } + point->setAccepted(false); // Just lurking... don't interfere with propagation + emit translationChanged(); +} + +QVector2D QQuickPointHandler::translation() const +{ + return QVector2D(point().position() - point().pressPosition()); +} + +QT_END_NAMESPACE diff --git a/src/quick/handlers/qquickpointhandler_p.h b/src/quick/handlers/qquickpointhandler_p.h new file mode 100644 index 0000000000..5babab0c4d --- /dev/null +++ b/src/quick/handlers/qquickpointhandler_p.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QQUICKPONTHANDLER_H +#define QQUICKPONTHANDLER_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 "qquicksinglepointhandler_p.h" + +QT_BEGIN_NAMESPACE + +class Q_AUTOTEST_EXPORT QQuickPointHandler : public QQuickSinglePointHandler +{ + Q_OBJECT + Q_PROPERTY(QVector2D translation READ translation NOTIFY translationChanged) + +public: + explicit QQuickPointHandler(QObject *parent = 0); + ~QQuickPointHandler(); + + QVector2D translation() const; + +Q_SIGNALS: + void translationChanged(); + +protected: + bool wantsEventPoint(QQuickEventPoint *pt) override; + void handleEventPoint(QQuickEventPoint *point) override; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPointHandler) + +#endif // QQUICKPONTHANDLER_H -- cgit v1.2.3