diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-07-04 20:05:00 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-09-19 15:37:11 +0200 |
commit | ab5df626bef9365089ce716ce476bccae1d0a04b (patch) | |
tree | 7ca558d24ad391b2cdfbe468a3752270eabd0466 /src/quick/handlers | |
parent | 227be82e4cf4b0fc58d4d50154cee7c45eb03777 (diff) |
Add dragThreshold property to Event Handlers
We need drag threshold to be adjustable on each handler instance instead
of relying only on the system default drag threshold. For example in
some use cases DragHandler needs to work with a threshold of 0 or 1 to
start dragging as soon as the point is pressed or as soon as the point
is moved, with no "jump", to enable fine adjustment of a value on some
control such as a Slider.
This involves moving the dragOverThreshold() functions that handlers are
using from QQuickWindowPrivate to QQuickPointerHandlerPrivate, so that
they can use the adjustable threshold value.
Task-number: QTBUG-68075
Change-Id: Ie720cbbf9f30abb40d1731d92f8e7f1e6534eeb5
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Diffstat (limited to 'src/quick/handlers')
-rw-r--r-- | src/quick/handlers/qquickdraghandler.cpp | 3 | ||||
-rw-r--r-- | src/quick/handlers/qquickpinchhandler.cpp | 7 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler.cpp | 69 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler_p.h | 6 | ||||
-rw-r--r-- | src/quick/handlers/qquickpointerhandler_p_p.h | 7 | ||||
-rw-r--r-- | src/quick/handlers/qquicktaphandler.cpp | 9 |
6 files changed, 89 insertions, 12 deletions
diff --git a/src/quick/handlers/qquickdraghandler.cpp b/src/quick/handlers/qquickdraghandler.cpp index 61b66beff4..492897b68b 100644 --- a/src/quick/handlers/qquickdraghandler.cpp +++ b/src/quick/handlers/qquickdraghandler.cpp @@ -39,6 +39,7 @@ #include "qquickdraghandler_p.h" #include <private/qquickwindow_p.h> +#include <private/qquickmultipointhandler_p_p.h> #include <QDebug> QT_BEGIN_NAMESPACE @@ -228,7 +229,7 @@ void QQuickDragHandler::handlePointerEventImpl(QQuickPointerEvent *event) accumulatedDragDelta.setY(0); } qreal angle = std::atan2(accumulatedDragDelta.y(), accumulatedDragDelta.x()) * 180 / M_PI; - bool overThreshold = QQuickWindowPrivate::dragOverThreshold(accumulatedDragDelta); + bool overThreshold = d_func()->dragOverThreshold(accumulatedDragDelta); qCDebug(lcDragHandler) << "movement" << accumulatedDragDelta << "angle" << angle << "of point" << point << "pressed @" << point->scenePressPosition() << "over threshold?" << overThreshold; minAngle = qMin(angle, minAngle); diff --git a/src/quick/handlers/qquickpinchhandler.cpp b/src/quick/handlers/qquickpinchhandler.cpp index a5a867015c..20ad95b0d7 100644 --- a/src/quick/handlers/qquickpinchhandler.cpp +++ b/src/quick/handlers/qquickpinchhandler.cpp @@ -43,6 +43,7 @@ #include <private/qsgadaptationlayer_p.h> #include <private/qquickitem_p.h> #include <private/qguiapplication_p.h> +#include <private/qquickmultipointhandler_p_p.h> #include <private/qquickwindow_p.h> #include <QEvent> #include <QMouseEvent> @@ -338,8 +339,7 @@ void QQuickPinchHandler::handlePointerEventImpl(QQuickPointerEvent *event) const QVector2D currentCentroid(centroid().scenePosition()); const QVector2D pressCentroid(centroid().scenePressPosition()); - QStyleHints *styleHints = QGuiApplication::styleHints(); - const int dragThreshold = styleHints->startDragDistance(); + const int dragThreshold = QQuickPointerHandler::dragThreshold(); const int dragThresholdSquared = dragThreshold * dragThreshold; double accumulatedCentroidDistance = 0; // Used to detect scale @@ -399,7 +399,8 @@ void QQuickPinchHandler::handlePointerEventImpl(QQuickPointerEvent *event) point->setAccepted(false); // don't stop propagation setPassiveGrab(point); } - if (QQuickWindowPrivate::dragOverThreshold(point)) + Q_D(QQuickMultiPointHandler); + if (d->dragOverThreshold(point)) ++numberOfPointsDraggedOverThreshold; } diff --git a/src/quick/handlers/qquickpointerhandler.cpp b/src/quick/handlers/qquickpointerhandler.cpp index 12c06aa179..9d4fae1a71 100644 --- a/src/quick/handlers/qquickpointerhandler.cpp +++ b/src/quick/handlers/qquickpointerhandler.cpp @@ -113,6 +113,47 @@ void QQuickPointerHandler::setMargin(qreal pointDistanceThreshold) } /*! + \qmlproperty int PointerHandler::dragThreshold + \since 5.15 + + The distance in pixels that the user must drag an event point in order to + have it treated as a drag gesture. + + The default value depends on the platform and screen resolution. + It can be reset back to the default value by setting it to undefined. + The behavior when a drag gesture begins varies in different handlers. +*/ +int QQuickPointerHandler::dragThreshold() const +{ + Q_D(const QQuickPointerHandler); + if (d->dragThreshold < 0) + return qApp->styleHints()->startDragDistance(); + return d->dragThreshold; +} + +void QQuickPointerHandler::setDragThreshold(int t) +{ + Q_D(QQuickPointerHandler); + if (d->dragThreshold == t) + return; + + if (t > std::numeric_limits<qint16>::max()) + qWarning() << "drag threshold cannot exceed" << std::numeric_limits<qint16>::max(); + d->dragThreshold = qint16(t); + emit dragThresholdChanged(); +} + +void QQuickPointerHandler::resetDragThreshold() +{ + Q_D(QQuickPointerHandler); + if (d->dragThreshold < 0) + return; + + d->dragThreshold = -1; + emit dragThresholdChanged(); +} + +/*! Notification that the grab has changed in some way which is relevant to this handler. The \a grabber (subject) will be the Input Handler whose state is changing, or null if the state change regards an Item. @@ -547,4 +588,32 @@ QQuickPointerHandlerPrivate::QQuickPointerHandlerPrivate() { } +template <typename TEventPoint> +bool QQuickPointerHandlerPrivate::dragOverThreshold(qreal d, Qt::Axis axis, const TEventPoint *p) const +{ + Q_Q(const QQuickPointerHandler); + QStyleHints *styleHints = qApp->styleHints(); + bool overThreshold = qAbs(d) > q->dragThreshold(); + const bool dragVelocityLimitAvailable = (styleHints->startDragVelocity() > 0); + if (!overThreshold && dragVelocityLimitAvailable) { + qreal velocity = qreal(axis == Qt::XAxis ? p->velocity().x() : p->velocity().y()); + overThreshold |= qAbs(velocity) > styleHints->startDragVelocity(); + } + return overThreshold; +} + +bool QQuickPointerHandlerPrivate::dragOverThreshold(QVector2D delta) const +{ + Q_Q(const QQuickPointerHandler); + const float threshold = q->dragThreshold(); + return qAbs(delta.x()) > threshold || qAbs(delta.y()) > threshold; +} + +bool QQuickPointerHandlerPrivate::dragOverThreshold(const QQuickEventPoint *point) const +{ + QPointF delta = point->scenePosition() - point->scenePressPosition(); + return (dragOverThreshold(delta.x(), Qt::XAxis, point) || + dragOverThreshold(delta.y(), Qt::YAxis, point)); +} + QT_END_NAMESPACE diff --git a/src/quick/handlers/qquickpointerhandler_p.h b/src/quick/handlers/qquickpointerhandler_p.h index c600e42491..995db9c1dc 100644 --- a/src/quick/handlers/qquickpointerhandler_p.h +++ b/src/quick/handlers/qquickpointerhandler_p.h @@ -71,6 +71,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPointerHandler : public QObject, public QQmlP Q_PROPERTY(QQuickItem * parent READ parentItem CONSTANT) Q_PROPERTY(GrabPermissions grabPermissions READ grabPermissions WRITE setGrabPermissions NOTIFY grabPermissionChanged) Q_PROPERTY(qreal margin READ margin WRITE setMargin NOTIFY marginChanged) + Q_PROPERTY(int dragThreshold READ dragThreshold WRITE setDragThreshold RESET resetDragThreshold NOTIFY dragThresholdChanged REVISION 15) public: explicit QQuickPointerHandler(QQuickItem *parent = nullptr); @@ -110,11 +111,16 @@ public: qreal margin() const; void setMargin(qreal pointDistanceThreshold); + int dragThreshold() const; + void setDragThreshold(int t); + void resetDragThreshold(); + Q_SIGNALS: void enabledChanged(); void activeChanged(); void targetChanged(); void marginChanged(); + Q_REVISION(15) void dragThresholdChanged(); void grabChanged(QQuickEventPoint::GrabTransition transition, QQuickEventPoint *point); void grabPermissionChanged(); void canceled(QQuickEventPoint *point); diff --git a/src/quick/handlers/qquickpointerhandler_p_p.h b/src/quick/handlers/qquickpointerhandler_p_p.h index 2ea4905643..5727b1ef55 100644 --- a/src/quick/handlers/qquickpointerhandler_p_p.h +++ b/src/quick/handlers/qquickpointerhandler_p_p.h @@ -68,9 +68,16 @@ public: QQuickPointerHandlerPrivate(); + template<typename TEventPoint> + bool dragOverThreshold(qreal d, Qt::Axis axis, const TEventPoint *p) const; + + bool dragOverThreshold(QVector2D delta) const; + bool dragOverThreshold(const QQuickEventPoint *point) const; + QQuickPointerEvent *currentEvent = nullptr; QQuickItem *target = nullptr; qreal m_margin = 0; + qint16 dragThreshold = -1; // -1 means use the platform default uint8_t grabPermissions : 8; bool enabled : 1; bool active : 1; diff --git a/src/quick/handlers/qquicktaphandler.cpp b/src/quick/handlers/qquicktaphandler.cpp index a10064a665..5f9d2151d7 100644 --- a/src/quick/handlers/qquicktaphandler.cpp +++ b/src/quick/handlers/qquicktaphandler.cpp @@ -97,13 +97,6 @@ QQuickTapHandler::QQuickTapHandler(QQuickItem *parent) } } -static bool dragOverThreshold(const QQuickEventPoint *point) -{ - QPointF delta = point->scenePosition() - point->scenePressPosition(); - return (QQuickWindowPrivate::dragOverThreshold(delta.x(), Qt::XAxis, point) || - QQuickWindowPrivate::dragOverThreshold(delta.y(), Qt::YAxis, point)); -} - bool QQuickTapHandler::wantsEventPoint(QQuickEventPoint *point) { if (!point->pointerEvent()->asPointerMouseEvent() && @@ -115,7 +108,7 @@ bool QQuickTapHandler::wantsEventPoint(QQuickEventPoint *point) // (e.g. DragHandler) gets a chance to take over. // Don't forget to emit released in case of a cancel. bool ret = false; - bool overThreshold = dragOverThreshold(point); + bool overThreshold = d_func()->dragOverThreshold(point); if (overThreshold) { m_longPressTimer.stop(); m_holdTimer.invalidate(); |