diff options
author | Robert Griebl <robert.griebl@nokia.com> | 2010-12-07 13:29:59 (GMT) |
---|---|---|
committer | Robert Griebl <robert.griebl@nokia.com> | 2010-12-15 17:47:34 (GMT) |
commit | 3e0df49f978933b1e4e6b48c695bf813ec9a2828 (patch) | |
tree | f221060111eae2702fbcee06d20b2bf773d5d0cf | |
parent | 32d200da9cc7a4dfb3f302f22ef5718a286845c9 (diff) | |
download | Qt-3e0df49f978933b1e4e6b48c695bf813ec9a2828.zip Qt-3e0df49f978933b1e4e6b48c695bf813ec9a2828.tar.gz Qt-3e0df49f978933b1e4e6b48c695bf813ec9a2828.tar.bz2 |
Fix drag velocity smoothing in QScroller.
The algorithm used for smoothing the drag velocity was wrong in many ways.
Reviewed-by: Ralf Engels
-rw-r--r-- | examples/scroller/plot/settingswidget.cpp | 2 | ||||
-rw-r--r-- | src/gui/util/qscroller.cpp | 16 | ||||
-rw-r--r-- | src/gui/util/qscrollerproperties.cpp | 9 |
3 files changed, 16 insertions, 11 deletions
diff --git a/examples/scroller/plot/settingswidget.cpp b/examples/scroller/plot/settingswidget.cpp index af1e621..840e3fc 100644 --- a/examples/scroller/plot/settingswidget.cpp +++ b/examples/scroller/plot/settingswidget.cpp @@ -348,7 +348,7 @@ private: MetricItem items[] = { { METRIC(MousePressEventDelay), 1000, "ms", qreal(0), qreal(2000), qreal(10) }, { METRIC(DragStartDistance), 1000, "mm", qreal(1), qreal(20), qreal(0.1) }, - { METRIC(DragVelocitySmoothingFactor), 1, "", qreal(0), qreal(1), qreal(0.01) }, + { METRIC(DragVelocitySmoothingFactor), 1, "", qreal(0), qreal(1), qreal(0.1) }, { METRIC(AxisLockThreshold), 1, "", qreal(0), qreal(1), qreal(0.01) }, { METRIC(ScrollingCurve), 1, "", QEasingCurve(), 0, 0 }, diff --git a/src/gui/util/qscroller.cpp b/src/gui/util/qscroller.cpp index b7b5903..0955f58 100644 --- a/src/gui/util/qscroller.cpp +++ b/src/gui/util/qscroller.cpp @@ -1046,6 +1046,9 @@ void QScrollerPrivate::setDpiFromWidget(QWidget *widget) */ void QScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 deltaTime) { + if (deltaTime <= 0) + return; + Q_Q(QScroller); QPointF ppm = q->pixelPerMeter(); const QScrollerPropertiesPrivate *sp = properties.d.data(); @@ -1057,14 +1060,12 @@ void QScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 delta if (((deltaPixelRaw / qreal(deltaTime)).manhattanLength() / ((ppm.x() + ppm.y()) / 2) * 1000) > qreal(2.5)) deltaPixel = deltaPixelRaw * qreal(2.5) * ppm / 1000 / (deltaPixelRaw / qreal(deltaTime)).manhattanLength(); - qreal inversSmoothingFactor = ((qreal(1) - sp->dragVelocitySmoothingFactor) * qreal(deltaTime) / qreal(1000)); QPointF newv = -deltaPixel / qreal(deltaTime) * qreal(1000) / ppm; - newv = newv * (qreal(1) - inversSmoothingFactor) + releaseVelocity * inversSmoothingFactor; + if (releaseVelocity != QPointF(0, 0)) + newv = newv * sp->dragVelocitySmoothingFactor + releaseVelocity * (qreal(1) - sp->dragVelocitySmoothingFactor); - if (deltaPixel.x()) - releaseVelocity.setX(qBound(-sp->maximumVelocity, newv.x(), sp->maximumVelocity)); - if (deltaPixel.y()) - releaseVelocity.setY(qBound(-sp->maximumVelocity, newv.y(), sp->maximumVelocity)); + releaseVelocity.setX(qBound(-sp->maximumVelocity, newv.x(), sp->maximumVelocity)); + releaseVelocity.setY(qBound(-sp->maximumVelocity, newv.y(), sp->maximumVelocity)); qScrollerDebug() << " --> new velocity:" << releaseVelocity; } @@ -1569,6 +1570,9 @@ bool QScrollerPrivate::releaseWhileDragging(const QPointF &position, qint64 time Q_Q(QScroller); const QScrollerPropertiesPrivate *sp = properties.d.data(); + // handleDrag updates lastPosition, lastTimestamp and velocity + handleDrag(position, timestamp); + // check if we moved at all - this can happen if you stop a running // scroller with a press and release shortly afterwards QPointF deltaPixel = position - pressPosition; diff --git a/src/gui/util/qscrollerproperties.cpp b/src/gui/util/qscrollerproperties.cpp index d21f131..4a1f085 100644 --- a/src/gui/util/qscrollerproperties.cpp +++ b/src/gui/util/qscrollerproperties.cpp @@ -61,7 +61,7 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() #ifdef Q_WS_MAEMO_5 spp.mousePressEventDelay = qreal(0); spp.dragStartDistance = qreal(2.5 / 1000); - spp.dragVelocitySmoothingFactor = qreal(0.15); + spp.dragVelocitySmoothingFactor = qreal(10); spp.axisLockThreshold = qreal(0); spp.scrollingCurve.setType(QEasingCurve::OutQuad); spp.decelerationFactor = 1.0; @@ -82,7 +82,7 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() #else spp.mousePressEventDelay = qreal(0.25); spp.dragStartDistance = qreal(5.0 / 1000); - spp.dragVelocitySmoothingFactor = qreal(0.02); + spp.dragVelocitySmoothingFactor = qreal(0.8); spp.axisLockThreshold = qreal(0); spp.scrollingCurve.setType(QEasingCurve::OutQuad); spp.decelerationFactor = qreal(0.125); @@ -342,8 +342,9 @@ void QScrollerProperties::setScrollMetric(ScrollMetric metric, const QVariant &v moved before the flick gesture is triggered in \c m. \value DragVelocitySmoothingFactor A value that describes how much new drag velocities are - included in the final scrolling velocity. This value should be in the range between \c 0 and \c - 1. Low values meaning that the last dragging velocity is not very important. + included in the final scrolling velocity. This value should be in the range between \c 0 and + \c 1. The lower the value, the more smoothing will be applied to the dragging velocity. The + default value is \c 0.8. \value AxisLockThreshold If greater than zero a scroll movement will be restricted to one axis only if the movement is inside an angle about the axis. The threshold must be in the range \c 0 |