diff options
author | Ralf Engels <ralf.engels@nokia.com> | 2010-04-16 17:54:23 +0200 |
---|---|---|
committer | Ralf Engels <ralf.engels@nokia.com> | 2010-04-16 17:54:23 +0200 |
commit | e5e41d0c981777236ce407e2c0131715e83d498b (patch) | |
tree | dba44c045d1f189687162a16eee4dfea2f22960e | |
parent | 730bb4fba2248cc37dfe05cd6450dc2752e845ba (diff) |
Cleanup involving scrollto and dragresistance factor
-rw-r--r-- | qkineticscroller.cpp | 120 |
1 files changed, 57 insertions, 63 deletions
diff --git a/qkineticscroller.cpp b/qkineticscroller.cpp index cace269..951b9f7 100644 --- a/qkineticscroller.cpp +++ b/qkineticscroller.cpp @@ -722,6 +722,7 @@ void QKineticScrollerPrivate::timerEventWhileDragging() { if (!dragDistance.isNull()) { qDebug() << "timerEventWhileDragging" << dragDistance; + setContentPositionHelper(-dragDistance); dragDistance = QPointF(0, 0); } @@ -851,8 +852,6 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) } } - overshootPosition /= overshootDragResistanceFactor; - break; case QKineticScroller::StateScrolling: @@ -863,8 +862,7 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) scrollAbsoluteTimer.start(); if (state == QKineticScroller::StateDragging) { - overshootPosition *= overshootDragResistanceFactor; - + // TODO: better calculate StartTime using the current releaseVelocity overshootStartTimeX = overshootStartTimeY = qreal(scrollAbsoluteTimer.elapsed()) / 1000 - M_PI / (overshootSpringConstantRoot * 2); overshootVelocity = overshootPosition / pixelPerMeter * overshootSpringConstantRoot; } @@ -881,7 +879,7 @@ void QKineticScrollerPrivate::setState(QKineticScroller::State newstate) the viewport. If the specified point cannot be reached, the contents are scrolled to the - nearest valid position. + nearest valid position (in this case the scroller might or might not overshoot). The scrolling speed will be calculated so that the given position will be reached after a platform-defined time span (1 second for Maemo 5). @@ -1008,10 +1006,16 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) { Q_Q(QKineticScroller); + if (state == QKineticScroller::StateDragging && overshootDragResistanceFactor) + overshootPosition /= overshootDragResistanceFactor; + QPointF oldPos = q->contentPosition() + overshootPosition; QPointF newPos = oldPos + deltaPos; QPointF maxPos = q->maximumContentPosition(); + QPointF oldScrollToDist = scrollToPosition - oldPos; + QPointF newScrollToDist = scrollToPosition - newPos; + qDebug() << "setContentPositionHelper overshoot:"<<overshootPosition<<"delta:"<<deltaPos<<"old:" << oldPos << " new:" << newPos; QPointF oldClampedPos; @@ -1034,113 +1038,103 @@ void QKineticScrollerPrivate::setContentPositionHelper(const QPointF &deltaPos) qreal oldOvershootX = (canOvershootX) ? oldPos.x() - oldClampedPos.x() : 0; qreal oldOvershootY = (canOvershootY) ? oldPos.y() - oldClampedPos.y() : 0; - if (state == QKineticScroller::StateDragging && overshootDragResistanceFactor) { - oldOvershootX /= overshootDragResistanceFactor; - oldOvershootY /= overshootDragResistanceFactor; - } qreal newOvershootX = (canOvershootX) ? newPos.x() - newClampedPos.x() : 0; qreal newOvershootY = (canOvershootY) ? newPos.y() - newClampedPos.y() : 0; + if (state == QKineticScroller::StateDragging && overshootDragResistanceFactor) { + oldOvershootX *= overshootDragResistanceFactor; + oldOvershootY *= overshootDragResistanceFactor; + newOvershootX *= overshootDragResistanceFactor; + newOvershootY *= overshootDragResistanceFactor; + } + + // -- stop at the maximum overshoot distance (if set) + if (!overshootMaximumDistance.isNull()) { + newOvershootX = qBound(-overshootMaximumDistance.x() * pixelPerMeter, newOvershootX, overshootMaximumDistance.x() * pixelPerMeter); + + newOvershootY = qBound(-overshootMaximumDistance.y() * pixelPerMeter, newOvershootY, overshootMaximumDistance.y() * pixelPerMeter); + } + // --- sanity check for scrollTo in case we can't even scroll that direction if (!(maxPos.x() || alwaysOvershootX)) scrollToX = false; if (!(maxPos.y() || alwaysOvershootY)) scrollToY = false; - QPointF oldScrollToDist = scrollToPosition - oldPos; - QPointF newScrollToDist = scrollToPosition - newPos; - - QPointF realOvershootDistance; - + // --- handle crossing over borders (scrollTo and overshoot) qDebug() << "oldOver: " << oldOvershootY << " newOver: " << newOvershootY; // -- x axis if (scrollToX && qSign(oldScrollToDist.x()) != qSign(newScrollToDist.x())) { newClampedPos.setX(scrollToPosition.x()); + newOvershootX = 0; releaseVelocity.setX(0); scrollToX = false; } else if (oldOvershootX && (qSign(oldOvershootX) != qSign(newOvershootX))) { newClampedPos.setX((oldOvershootX < 0) ? 0 : maxPos.x()); - realOvershootDistance.setX(0); + newOvershootX = 0; releaseVelocity.setX(0); overshootVelocity.setX(0); overshootX = false; - } else if (newOvershootX) { - if (!oldOvershootX) { - overshootStartTimeX = qreal(scrollAbsoluteTimer.elapsed()) / 1000; - overshootVelocity.setX(calculateVelocity(overshootStartTimeX).x()); + } else if (!oldOvershootX && newOvershootX) { + overshootStartTimeX = qreal(scrollAbsoluteTimer.elapsed()) / 1000; + overshootVelocity.setX(calculateVelocity(overshootStartTimeX).x()); - // restrict the overshoot to overshootMaximumDistance - qreal maxOvershootVelocity = overshootMaximumDistance.x() * overshootSpringConstantRoot; - if (overshootMaximumDistance.x() && qAbs(overshootVelocity.x()) > maxOvershootVelocity) - overshootVelocity.setX(maxOvershootVelocity * qSign(newOvershootX)); + // restrict the overshoot to overshootMaximumDistance + qreal maxOvershootVelocity = overshootMaximumDistance.x() * overshootSpringConstantRoot; + if (overshootMaximumDistance.x() && qAbs(overshootVelocity.x()) > maxOvershootVelocity) + overshootVelocity.setX(maxOvershootVelocity * qSign(newOvershootX)); - // -- prevent going into overshoot too far - if (state != QKineticScroller::StateDragging && overshootDragResistanceFactor) - newOvershootX = qSign(newOvershootX) * qreal(0.0001); + // -- prevent going into overshoot too far + if (state != QKineticScroller::StateDragging) + newOvershootX = qSign(newOvershootX) * qreal(0.0001); - scrollToX = false; - overshootX = true; - } - realOvershootDistance.setX(newOvershootX); + scrollToX = false; + overshootX = true; } // -- y axis if (scrollToY && qSign(oldScrollToDist.y()) != qSign(newScrollToDist.y())) { newClampedPos.setY(scrollToPosition.y()); + newOvershootY = 0; releaseVelocity.setY(0); scrollToY = false; } else if (oldOvershootY && (qSign(oldOvershootY) != qSign(newOvershootY))) { newClampedPos.setY((oldOvershootY < 0) ? 0 : maxPos.y()); - realOvershootDistance.setY(0); + newOvershootY = 0; releaseVelocity.setY(0); overshootVelocity.setY(0); overshootY = false; - } else if (newOvershootY) { - if (!oldOvershootY) { - overshootStartTimeY = qreal(scrollAbsoluteTimer.elapsed()) / 1000; - overshootVelocity.setY(calculateVelocity(overshootStartTimeY).y()); + } else if (!oldOvershootY && newOvershootY) { + overshootStartTimeY = qreal(scrollAbsoluteTimer.elapsed()) / 1000; + overshootVelocity.setY(calculateVelocity(overshootStartTimeY).y()); - // -- restrict the overshoot to overshootMaximumDistance - qreal maxOvershootVelocity = overshootMaximumDistance.y() * overshootSpringConstantRoot; - if (overshootMaximumDistance.y() && (qAbs(overshootVelocity.y()) > maxOvershootVelocity)) - overshootVelocity.setY(maxOvershootVelocity * qSign(newOvershootY)); + // -- restrict the overshoot to overshootMaximumDistance + qreal maxOvershootVelocity = overshootMaximumDistance.y() * overshootSpringConstantRoot; + if (overshootMaximumDistance.y() && (qAbs(overshootVelocity.y()) > maxOvershootVelocity)) + overshootVelocity.setY(maxOvershootVelocity * qSign(newOvershootY)); - // -- prevent going into overshoot too far - if (state != QKineticScroller::StateDragging && overshootDragResistanceFactor) - newOvershootY = qSign(newOvershootY) * qreal(0.0001); + // -- prevent going into overshoot too far + if (state != QKineticScroller::StateDragging) + newOvershootY = qSign(newOvershootY) * qreal(0.0001); - scrollToY = false; - overshootY = true; - } - realOvershootDistance.setY(newOvershootY); + scrollToY = false; + overshootY = true; } - overshootPosition = realOvershootDistance; - - // -- finishing overshoot distance - if (!realOvershootDistance.isNull()) { - if (state == QKineticScroller::StateDragging) - realOvershootDistance *= overshootDragResistanceFactor; - - // -- stop at the maximum overshoot distance (if set) - if (!overshootMaximumDistance.isNull()) { - realOvershootDistance.setX(qBound(-overshootMaximumDistance.x() * pixelPerMeter, realOvershootDistance.x(), overshootMaximumDistance.x() * pixelPerMeter)); - - realOvershootDistance.setY(qBound(-overshootMaximumDistance.y() * pixelPerMeter, realOvershootDistance.y(), overshootMaximumDistance.y() * pixelPerMeter)); - } - } + overshootPosition.setX(newOvershootX); + overshootPosition.setY(newOvershootY); - q->setContentPosition(newClampedPos, realOvershootDistance); + q->setContentPosition(newClampedPos, overshootPosition); if (debugHook) - debugHook(debugHookUser, calculateVelocity(qreal(scrollAbsoluteTimer.elapsed()) / 1000), newClampedPos, realOvershootDistance); + debugHook(debugHookUser, calculateVelocity(qreal(scrollAbsoluteTimer.elapsed()) / 1000), newClampedPos, overshootPosition); - qDebug() << "setContentPositionHelper" << newClampedPos << " overshoot:" << realOvershootDistance << + qDebug() << "setContentPositionHelper" << newClampedPos << " overshoot:" << overshootPosition<< "Overshoot: "<<overshootX<<","<<overshootY<<"ScrollTo:"<<scrollToX<<","<<scrollToY; } |