From 03ab1a611a4e14733419fde29b699b499fa1f532 Mon Sep 17 00:00:00 2001 From: Igor Trindade Oliveira Date: Sat, 19 Sep 2009 17:07:56 +0000 Subject: [PATCH] horizontal kinetic scrolling working right now svn path=/trunk/KDE/kdelibs/; revision=1025781 --- private/kineticscroll.cpp | 97 ++++++++++++++++++++------------------- private/kineticscroll_p.h | 10 ++-- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/private/kineticscroll.cpp b/private/kineticscroll.cpp index 947d134d7..499d3a31b 100644 --- a/private/kineticscroll.cpp +++ b/private/kineticscroll.cpp @@ -25,6 +25,8 @@ #include #include #include +#include + #include "kineticscroll_p.h" /* TODO: @@ -35,7 +37,6 @@ * - swap the 'magic numbers' for consts * - consider if direct access to control state/velocity variables should * be done directly or using functions - * - implement horizontal scrolling (simultaneously with vertical) */ namespace Plasma @@ -72,12 +73,13 @@ public: unsigned int timeDelta; qreal scrollVelocity; - int movement; - int kinMovement; + QPoint movement; + QPoint kinMovement; qreal mScrollVelocity; enum { None, Up, Down }; - int timerID, overshoot, cposition, direction, minimalPos, maximumPos; + int timerID, overshoot, direction; + QPoint cposition, minimalPos, maximumPos; char bounceFlag; QGraphicsWidget *widget; @@ -98,17 +100,17 @@ KineticScrolling::~KineticScrolling() delete d; } -int KineticScrolling::movement() const +QPoint KineticScrolling::movement() { return d->movement; } -int KineticScrolling::kinMovement() const +QPoint KineticScrolling::kinMovement() { return d->kinMovement; } -qreal KineticScrolling::duration() const +qreal KineticScrolling::duration() { return d->timeDelta; } @@ -120,14 +122,14 @@ void KineticScrolling::mousePressEvent(QGraphicsSceneMouseEvent *event) Q_UNUSED(event); d->count(); d->scrollVelocity = 0; - d->movement = 0; - d->kinMovement = 0; + d->movement = QPoint(0, 0); + d->kinMovement = QPoint(0, 0); } void KineticScrolling::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - int temp = event->lastPos().y() - event->pos().y(); - if (temp) { + QPoint temp = event->lastPos().toPoint() - event->pos().toPoint(); + if (!temp.isNull()) { d->movement = temp; d->kinMovement += temp; } @@ -138,26 +140,26 @@ void KineticScrolling::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void KineticScrolling::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { d->timeDelta = d->elapsed(); - int temp = event->lastPos().y() - event->pos().y(); - if (temp) + QPoint temp = event->lastPos().toPoint() - event->pos().toPoint(); + if (!temp.isNull()) d->kinMovement += temp; if (d->timeDelta > 600) { - if (d->kinMovement > 0) - d->kinMovement = 3; + if (d->kinMovement.y() > 0) + d->kinMovement.setY(3); else - d->kinMovement = -3; + d->kinMovement.setY(-3); } /* The after */ - if (d->cposition > 0) { + if (d->cposition.y() > 0) { d->direction = KineticScrollingPrivate::Up; - } else if (abs(d->cposition) > + } else if (abs(d->cposition.y()) > abs(d->scrollingWidget->size().height() - d->widget->size().height())) { d->direction = KineticScrollingPrivate::Down; - } else { + } else { d->direction = KineticScrollingPrivate::None; - d->mScrollVelocity = kinMovement(); + d->mScrollVelocity = (d->kinMovement.x() + d->kinMovement.y())>>2; startAnimationTimer(30); return; } @@ -165,7 +167,6 @@ void KineticScrolling::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) d->mScrollVelocity = 5; killTimer(d->timerID); startAnimationTimer(50); - } void KineticScrolling::wheelReleaseEvent(QGraphicsSceneWheelEvent *event) @@ -177,10 +178,10 @@ void KineticScrolling::wheelReleaseEvent(QGraphicsSceneWheelEvent *event) d->timeDelta = d->elapsed(); int temp = event->delta(); temp *= -0.5; - d->kinMovement += temp; + d->kinMovement.setY(kinMovement().y() + temp); /* After */ - d->mScrollVelocity = kinMovement(); + d->mScrollVelocity = kinMovement().y(); startAnimationTimer(30); } @@ -205,32 +206,38 @@ void KineticScrolling::timerEvent(QTimerEvent *event) killTimer(d->timerID); } - setKineticScrollValue(qRound(d->mScrollVelocity)); + setKineticScrollValue( QPoint(d->mScrollVelocity, d->mScrollVelocity)); } else { if ((d->timerID != 0) && (d->direction != KineticScrollingPrivate::None)) { bounceTimer(); } - } + } } -void KineticScrolling::setKineticScrollValue(int value) +void KineticScrolling::setKineticScrollValue(QPoint value) { - d->minimalPos = (-(d->widget->size().height())) + d->scrollingWidget->size().height() - -(d->overshoot); - d->minimalPos = qMin(d->overshoot, d->minimalPos); - d->maximumPos = d->widget->pos().y() - value; + d->minimalPos.setY((-(d->widget->size().height())) + d->scrollingWidget->size().height() + -(d->overshoot)); + d->minimalPos.setX((-(d->widget->size().width())) + d->scrollingWidget->size().width()); - d->cposition = qBound(d->minimalPos, d->maximumPos, d->overshoot); - d->widget->setPos(0, d->cposition); + d->minimalPos.setY(qMin(d->overshoot, d->minimalPos.y())); + d->minimalPos.setX(qMin( 0, d->minimalPos.x())); - if (d->cposition == d->overshoot) { + d->maximumPos = d->widget->pos().toPoint() - value; + + d->cposition.setY(qBound(d->minimalPos.y(), d->maximumPos.y(), d->overshoot)); + d->cposition.setX( qBound(d->minimalPos.x(), d->maximumPos.x(), 0)); + + d->widget->setPos(d->cposition); + + if (d->cposition.y() == d->overshoot) { d->direction = KineticScrollingPrivate::Up; killTimer(d->timerID); d->mScrollVelocity = 5; startAnimationTimer(30); - } else if (d->cposition == d->minimalPos) { + } else if (d->cposition.y() == d->minimalPos.y()) { d->direction = KineticScrollingPrivate::Down; killTimer(d->timerID); d->mScrollVelocity = 5; @@ -238,7 +245,6 @@ void KineticScrolling::setKineticScrollValue(int value) } else d->direction = KineticScrollingPrivate::None; - } void KineticScrolling::bounceTimer() @@ -253,35 +259,34 @@ void KineticScrolling::bounceTimer() else if ((d->direction == KineticScrollingPrivate::Down) && (d->bounceFlag == 1)) delta = 2; - d->cposition += delta; - d->widget->setPos(0, d->cposition); + d->cposition.setY(d->cposition.y() + delta); + d->widget->setPos(d->cposition); - if ((d->direction == KineticScrollingPrivate::Up) && (d->cposition < 0)) { + if ((d->direction == KineticScrollingPrivate::Up) && (d->cposition.y() < 0)) { if (!d->bounceFlag) { - d->cposition = d->overshoot/4; + d->cposition.setY(d->overshoot/4); d->bounceFlag = 1; - d->widget->setPos(0, d->cposition); + d->widget->setPos(d->cposition); return; } else { d->bounceFlag = 0; - d->cposition = 0; + d->cposition = QPoint(d->cposition.x(), 0); doneOvershoot() ; } } else if ((d->direction == KineticScrollingPrivate::Down) && - ((d->cposition - d->overshoot) > d->minimalPos)) { + ((d->cposition.y() - d->overshoot) > d->minimalPos.y())) { if (!d->bounceFlag) { - d->cposition = d->minimalPos + d->overshoot/4; + d->cposition.setY(d->minimalPos.y() + d->overshoot/4); d->bounceFlag = 1; - d->widget->setPos(0, d->cposition); + d->widget->setPos(d->cposition); return; } else { d->bounceFlag = 0; - d->cposition = d->minimalPos + d->overshoot; + d->cposition.setY(d->minimalPos.y() + d->overshoot); doneOvershoot(); } } - } diff --git a/private/kineticscroll_p.h b/private/kineticscroll_p.h index f456e34aa..f02958c50 100644 --- a/private/kineticscroll_p.h +++ b/private/kineticscroll_p.h @@ -26,6 +26,8 @@ class QGraphicsSceneMouseEvent; class QGraphicsWidget; #include +class QPoint; + namespace Plasma { @@ -46,14 +48,14 @@ class KineticScrolling: public QObject KineticScrollingPrivate *d; void timerEvent(QTimerEvent *event); void bounceTimer(); - qreal duration() const; - int movement() const; - int kinMovement() const; + qreal duration(); + QPoint movement(); + QPoint kinMovement(); void startAnimationTimer(int interval); void doneOvershoot(void); public Q_SLOTS: - void setKineticScrollValue(int value); + void setKineticScrollValue(QPoint value); };