horizontal kinetic scrolling working right now

svn path=/trunk/KDE/kdelibs/; revision=1025781
This commit is contained in:
Igor Trindade Oliveira 2009-09-19 17:07:56 +00:00
parent e24e0294e3
commit 03ab1a611a
2 changed files with 57 additions and 50 deletions

View File

@ -25,6 +25,8 @@
#include <QTime>
#include <QDebug>
#include <QGraphicsWidget>
#include <QPoint>
#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();
}
}
}

View File

@ -26,6 +26,8 @@ class QGraphicsSceneMouseEvent;
class QGraphicsWidget;
#include <QObject>
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);
};