2009-12-27 23:16:11 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>
|
|
|
|
Copyright (C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.org.br>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kineticscroll_p.h"
|
|
|
|
|
2009-09-17 18:24:19 +02:00
|
|
|
#include <QtCore/qglobal.h>
|
|
|
|
#include <QtCore/qmetatype.h>
|
2009-10-15 21:11:34 +02:00
|
|
|
#include <QGraphicsScene>
|
2009-09-17 18:24:19 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2009-10-13 14:36:48 +02:00
|
|
|
#include <QGraphicsSceneWheelEvent>
|
2009-09-17 18:24:19 +02:00
|
|
|
#include <QTime>
|
|
|
|
#include <QGraphicsWidget>
|
2009-09-19 19:07:56 +02:00
|
|
|
#include <QPoint>
|
2009-11-18 18:35:50 +01:00
|
|
|
#include <QPropertyAnimation>
|
2009-11-17 18:34:27 +01:00
|
|
|
#include <QCursor>
|
2009-09-19 19:07:56 +02:00
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
#include <kdebug.h>
|
|
|
|
|
2009-09-17 18:24:19 +02:00
|
|
|
/* TODO:
|
2009-11-18 18:35:50 +01:00
|
|
|
* - clean up the code(remove duplicated code, constify)
|
|
|
|
* - port to Plasma::Animator
|
2009-09-17 18:24:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class KineticScrollingPrivate
|
|
|
|
{
|
|
|
|
public:
|
2009-10-08 18:04:56 +02:00
|
|
|
enum Direction {
|
|
|
|
None,
|
|
|
|
Up,
|
|
|
|
Down,
|
|
|
|
Left,
|
|
|
|
Right
|
|
|
|
};
|
|
|
|
|
2009-11-17 18:34:27 +01:00
|
|
|
enum Gesture {
|
|
|
|
GestureNone = 0,
|
2009-11-18 18:35:50 +01:00
|
|
|
GestureUndefined,
|
|
|
|
GestureScroll,
|
|
|
|
GestureZoom
|
2009-11-17 18:34:27 +01:00
|
|
|
};
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
KineticScrollingPrivate()
|
2009-11-18 18:35:50 +01:00
|
|
|
: overshoot(20),
|
2009-10-08 18:04:56 +02:00
|
|
|
hasOvershoot(true),
|
2009-10-15 21:11:34 +02:00
|
|
|
parent(0),
|
2009-11-17 18:34:27 +01:00
|
|
|
forwardingEvent(false),
|
|
|
|
multitouchGesture(GestureNone)
|
2009-10-01 18:47:19 +02:00
|
|
|
{
|
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
|
|
|
|
void count()
|
|
|
|
{
|
|
|
|
t = QTime::currentTime();
|
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
void syncViewportRect()
|
|
|
|
{
|
|
|
|
contentsSize = parent->property("contentsSize").toSizeF();
|
|
|
|
viewportGeometry = parent->property("viewportGeometry").toRectF();
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
2009-11-18 20:30:56 +01:00
|
|
|
|
|
|
|
bool canScroll(Direction direction, bool hasOvershoot = false) const
|
|
|
|
{
|
|
|
|
QPointF scrollPosition = -parent->property("scrollPosition").value<QPointF>();
|
|
|
|
int offset = (hasOvershoot?overshoot*2:0);
|
|
|
|
|
|
|
|
switch (direction) {
|
|
|
|
case Up:
|
|
|
|
return (scrollPosition.y() < offset);
|
|
|
|
case Down:
|
|
|
|
return (scrollPosition.y() + contentsSize.height() + offset >= viewportGeometry.bottom());
|
|
|
|
case Left:
|
|
|
|
return (scrollPosition.x() < offset);
|
|
|
|
case Right:
|
|
|
|
return (scrollPosition.x() + contentsSize.width() + offset >= viewportGeometry.right());
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-10-01 22:44:40 +02:00
|
|
|
QPointF kinMovement;
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
enum BounceStatus {
|
|
|
|
Running,
|
|
|
|
Finished
|
|
|
|
};
|
|
|
|
|
|
|
|
BounceStatus bounceStatus;
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
QPropertyAnimation *scrollAnimation;
|
|
|
|
|
|
|
|
int overshoot;
|
2009-10-01 22:44:40 +02:00
|
|
|
QPointF cposition;
|
2009-09-20 20:21:41 +02:00
|
|
|
bool hasOvershoot;
|
2009-10-15 21:11:34 +02:00
|
|
|
QGraphicsWidget *parent;
|
2009-10-08 18:04:56 +02:00
|
|
|
QRectF viewportGeometry;
|
|
|
|
QSizeF contentsSize;
|
2009-11-18 18:35:50 +01:00
|
|
|
QPointF maximum, minimum;
|
2009-10-15 21:11:34 +02:00
|
|
|
bool forwardingEvent;
|
2009-11-17 18:34:27 +01:00
|
|
|
Gesture multitouchGesture;
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
unsigned int timeDelta;
|
2009-09-17 18:24:19 +02:00
|
|
|
QTime t;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-20 00:10:25 +02:00
|
|
|
KineticScrolling::KineticScrolling(QGraphicsWidget *parent)
|
|
|
|
: d(new KineticScrollingPrivate)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-10-20 00:10:25 +02:00
|
|
|
setWidget(parent);
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KineticScrolling::~KineticScrolling()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::duration( )
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-10-02 06:05:21 +02:00
|
|
|
d->timeDelta = d->t.msecsTo(QTime::currentTime());
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::overshoot()
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:35:50 +01:00
|
|
|
QPointF scrollPosition = -d->parent->property("scrollPosition").value<QPointF>();
|
2009-10-27 19:04:02 +01:00
|
|
|
|
2009-11-19 20:55:54 +01:00
|
|
|
if (!d->canScroll(KineticScrollingPrivate::Down) &&
|
|
|
|
!d->canScroll(KineticScrollingPrivate::Up)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if (d->bounceStatus != KineticScrollingPrivate::Running) {
|
|
|
|
if ((d->cposition.y() > 0 ) || (d->cposition.y() <= d->minimum.y() + d->overshoot)) {
|
|
|
|
QPointF finalPosition;
|
|
|
|
d->scrollAnimation->setEasingCurve( QEasingCurve::OutBounce );
|
2009-10-27 19:04:02 +01:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if (d->cposition.y() > 0) {
|
|
|
|
finalPosition = QPointF(scrollPosition.x(), 0);
|
|
|
|
} else {
|
|
|
|
finalPosition = QPointF(d->cposition.x(),
|
|
|
|
-d->contentsSize.height( ) + d->parent->size().height());
|
|
|
|
}
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
resetAnimation(-finalPosition, 900);
|
|
|
|
d->bounceStatus = KineticScrollingPrivate::Running;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d->bounceStatus = KineticScrollingPrivate::Finished;
|
|
|
|
d->scrollAnimation->setEasingCurve(QEasingCurve::OutCirc);
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::setScrollValue(QPointF value)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:35:50 +01:00
|
|
|
const QPointF pos = thresholdPosition(value);
|
|
|
|
QPointF posf(-pos);
|
|
|
|
d->parent->setProperty("scrollPosition", posf);
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if ((pos.y() == d->overshoot) || (pos.y() == d->minimum.y())) {
|
|
|
|
overshoot();
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 18:59:56 +01:00
|
|
|
QPointF KineticScrolling::thresholdPosition(QPointF value) const
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:35:50 +01:00
|
|
|
d->minimum.setX(-d->contentsSize.width() + d->viewportGeometry.width());
|
|
|
|
d->minimum.setY(-d->contentsSize.height() + d->viewportGeometry.height()
|
|
|
|
-d->overshoot);
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
d->minimum.setY(qMin((qreal)d->overshoot, d->minimum.y()));
|
|
|
|
d->maximum = value;
|
2009-10-06 18:58:42 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if(d->minimum.x() >= 0) {
|
|
|
|
d->cposition.setX(value.x());
|
|
|
|
} else {
|
2009-12-02 15:17:34 +01:00
|
|
|
d->cposition.setX(qBound(d->minimum.x(), d->maximum.x(), qreal(0)));
|
2009-09-21 20:38:53 +02:00
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if((-d->contentsSize.height() + d->viewportGeometry.height() - d->overshoot) >= 0) {
|
|
|
|
d->cposition.setY(value.y());
|
|
|
|
} else {
|
2009-12-02 15:17:34 +01:00
|
|
|
d->cposition.setY(qBound(d->minimum.y(), d->maximum.y(), qreal(d->overshoot)));
|
2009-11-12 21:44:02 +01:00
|
|
|
}
|
2009-11-18 18:35:50 +01:00
|
|
|
|
|
|
|
return d->cposition;
|
2009-11-12 21:44:02 +01:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::resetAnimation(QPointF finalPosition, int duration)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:35:50 +01:00
|
|
|
if (d->scrollAnimation->state() != QAbstractAnimation::Stopped) {
|
|
|
|
d->scrollAnimation->stop();
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
d->cposition = -finalPosition;
|
|
|
|
QPointF tmpPosition = d->parent->property("scrollPosition").value<QPointF>();
|
|
|
|
d->scrollAnimation->setStartValue(tmpPosition);
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
tmpPosition = finalPosition;
|
2009-10-01 18:47:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
d->scrollAnimation->setEndValue(tmpPosition);
|
|
|
|
d->scrollAnimation->setDuration(duration);
|
|
|
|
d->scrollAnimation->start();
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
}
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (d->scrollAnimation->state() != QAbstractAnimation::Stopped) {
|
|
|
|
d->scrollAnimation->stop();
|
|
|
|
}
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
d->syncViewportRect();
|
|
|
|
d->cposition = -d->parent->property("scrollPosition").value<QPointF>();
|
2009-10-08 18:04:56 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
d->count();
|
|
|
|
d->kinMovement = QPointF(0,0);
|
|
|
|
d->scrollAnimation->setEasingCurve(QEasingCurve::OutCirc);
|
|
|
|
}
|
2009-10-01 18:47:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
QPointF movement = event->lastPos().toPoint() - event->pos().toPoint();
|
|
|
|
const QPointF scrollPosition = -d->parent->property("scrollPosition").value<QPointF>();
|
|
|
|
|
|
|
|
if (!movement.isNull()) {
|
|
|
|
if ((d->contentsSize.width() < d->viewportGeometry.width()) &&
|
|
|
|
(d->contentsSize.height() < d->viewportGeometry.height())) {
|
|
|
|
d->kinMovement = QPointF(0, 0);
|
|
|
|
movement = QPointF(0, 0);
|
2009-11-18 20:55:59 +01:00
|
|
|
} else if (d->contentsSize.height() < d->viewportGeometry.height()) {
|
2009-11-18 18:35:50 +01:00
|
|
|
d->kinMovement += QPointF(movement.x(), 0);
|
|
|
|
movement.setY(0);
|
|
|
|
} else if (d->contentsSize.width() < d->viewportGeometry.width()) {
|
|
|
|
d->kinMovement = QPointF(0, movement.y());
|
|
|
|
movement.setX(0);
|
2009-10-08 18:04:56 +02:00
|
|
|
} else {
|
2009-11-18 18:35:50 +01:00
|
|
|
d->kinMovement += movement;
|
2009-10-08 18:04:56 +02:00
|
|
|
}
|
2009-11-18 18:35:50 +01:00
|
|
|
setScrollValue(scrollPosition - movement);
|
2009-09-21 20:38:53 +02:00
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:59:56 +01:00
|
|
|
Q_UNUSED(event);
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
if (d->scrollAnimation->state() != QAbstractAnimation::Running) {
|
|
|
|
duration();
|
2009-09-17 18:24:19 +02:00
|
|
|
|
2009-11-18 18:59:56 +01:00
|
|
|
if (d->kinMovement != QPointF(0, 0)) {
|
2009-11-18 18:35:50 +01:00
|
|
|
const QPointF scrollPosition = -d->parent->property("scrollPosition").toPointF();
|
|
|
|
d->kinMovement = QPointF(d->kinMovement.x()*3, d->kinMovement.y()*3);
|
|
|
|
const QPointF finalPos = thresholdPosition(scrollPosition - d->kinMovement);
|
|
|
|
resetAnimation( -finalPos, d->timeDelta*8 );
|
|
|
|
}
|
2009-10-21 23:04:03 +02:00
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::wheelReleaseEvent(QGraphicsSceneWheelEvent *event)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:35:50 +01:00
|
|
|
Q_UNUSED(event);
|
|
|
|
d->syncViewportRect();
|
|
|
|
d->kinMovement = QPointF(0,0);
|
2009-10-01 18:47:19 +02:00
|
|
|
|
2009-11-18 20:30:56 +01:00
|
|
|
if((event->orientation() == Qt::Vertical) &&
|
|
|
|
((event->delta() < 0) && d->canScroll(KineticScrollingPrivate::Down) ||
|
|
|
|
(event->delta() > 0) && d->canScroll(KineticScrollingPrivate::Up))) {
|
2009-11-18 18:35:50 +01:00
|
|
|
d->kinMovement.setY(d->kinMovement.y() - event->delta());
|
2009-11-18 20:30:56 +01:00
|
|
|
} else if ((event->orientation() == Qt::Vertical) ||
|
|
|
|
(!d->canScroll(KineticScrollingPrivate::Down) &&
|
|
|
|
!d->canScroll(KineticScrollingPrivate::Up))) {
|
|
|
|
if (((event->delta() < 0) &&
|
|
|
|
d->canScroll(KineticScrollingPrivate::Right)) ||
|
|
|
|
(event->delta() > 0 && d->canScroll(KineticScrollingPrivate::Left))) {
|
|
|
|
d->kinMovement.setX(d->kinMovement.x() - event->delta());
|
|
|
|
} else {
|
|
|
|
event->ignore( );
|
|
|
|
}
|
2009-10-01 18:47:19 +02:00
|
|
|
} else {
|
2009-11-18 20:30:56 +01:00
|
|
|
event->ignore( );
|
|
|
|
return;
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
2009-10-01 18:47:19 +02:00
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
const QPointF scrollPosition = -d->parent->property("scrollPosition").value<QPointF>();
|
|
|
|
const QPointF pos = scrollPosition - d->kinMovement*2;
|
|
|
|
const QPointF finalPos = thresholdPosition(pos);
|
|
|
|
|
|
|
|
d->scrollAnimation->setEasingCurve(QEasingCurve::OutCirc);
|
|
|
|
resetAnimation(-finalPos, 900);
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-11-18 18:35:50 +01:00
|
|
|
void KineticScrolling::keyPressEvent(QKeyEvent *event)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-11-18 18:59:56 +01:00
|
|
|
const int movement = 30;
|
|
|
|
const int duration = 900;
|
2009-11-18 18:35:50 +01:00
|
|
|
|
|
|
|
QPointF scrollPosition = -d->parent->property("scrollPosition").value<QPointF>();
|
|
|
|
QPointF finalPos;
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Left:
|
2009-11-18 18:59:56 +01:00
|
|
|
scrollPosition.setX(scrollPosition.x() + movement);
|
2009-11-18 18:35:50 +01:00
|
|
|
finalPos = thresholdPosition(scrollPosition);
|
2009-11-18 18:59:56 +01:00
|
|
|
resetAnimation(-finalPos, duration);
|
2009-11-18 18:35:50 +01:00
|
|
|
break;
|
|
|
|
case Qt::Key_Right:
|
2009-11-18 18:59:56 +01:00
|
|
|
scrollPosition.setX(scrollPosition.x() - movement);
|
2009-11-18 18:35:50 +01:00
|
|
|
finalPos = thresholdPosition(scrollPosition);
|
2009-11-18 18:59:56 +01:00
|
|
|
resetAnimation(-finalPos, duration);
|
2009-11-18 18:35:50 +01:00
|
|
|
break;
|
|
|
|
case Qt::Key_Up:
|
2009-11-18 18:59:56 +01:00
|
|
|
scrollPosition.setY(scrollPosition.y() + movement);
|
2009-11-18 18:35:50 +01:00
|
|
|
finalPos = thresholdPosition(scrollPosition);
|
2009-11-18 18:59:56 +01:00
|
|
|
resetAnimation(-finalPos, duration);
|
2009-11-18 18:35:50 +01:00
|
|
|
break;
|
|
|
|
case Qt::Key_Down:
|
2009-11-18 18:59:56 +01:00
|
|
|
scrollPosition.setY(scrollPosition.y() - movement);
|
2009-11-18 18:35:50 +01:00
|
|
|
finalPos = thresholdPosition(scrollPosition);
|
2009-11-18 18:59:56 +01:00
|
|
|
resetAnimation(-finalPos, duration);
|
2009-11-18 18:35:50 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-01 18:47:19 +02:00
|
|
|
void KineticScrolling::setWidget(QGraphicsWidget *parent)
|
2009-09-17 18:24:19 +02:00
|
|
|
{
|
2009-10-15 21:11:34 +02:00
|
|
|
if (d->parent) {
|
|
|
|
d->parent->removeEventFilter(this);
|
2009-11-19 16:25:25 +01:00
|
|
|
disconnect(d->scrollAnimation, SIGNAL(finished()), this, SLOT(overshoot()));
|
2009-12-21 19:36:39 +01:00
|
|
|
disconnect(d->scrollAnimation,
|
|
|
|
SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), this,
|
|
|
|
SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)));
|
2009-11-18 18:35:50 +01:00
|
|
|
delete d->scrollAnimation;
|
2009-10-15 21:11:34 +02:00
|
|
|
}
|
|
|
|
|
2009-10-20 00:10:25 +02:00
|
|
|
setParent(parent);
|
|
|
|
|
2009-10-01 18:47:19 +02:00
|
|
|
d->parent = parent;
|
2009-10-15 21:11:34 +02:00
|
|
|
|
2009-11-19 19:18:55 +01:00
|
|
|
d->scrollAnimation = new QPropertyAnimation(parent, "scrollPosition", parent);
|
2009-11-18 18:35:50 +01:00
|
|
|
connect(d->scrollAnimation, SIGNAL(finished()), this, SLOT(overshoot()));
|
2009-12-21 19:36:39 +01:00
|
|
|
connect(d->scrollAnimation,
|
|
|
|
SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), this,
|
|
|
|
SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)));
|
2009-11-18 18:35:50 +01:00
|
|
|
d->scrollAnimation->setEasingCurve(QEasingCurve::OutCirc);
|
|
|
|
|
2009-10-15 21:11:34 +02:00
|
|
|
if (parent) {
|
|
|
|
d->parent->installEventFilter(this);
|
|
|
|
}
|
2009-10-01 18:47:19 +02:00
|
|
|
/* TODO: add a new property in plasma::ScrollWidget 'hasOvershoot' */
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 21:11:34 +02:00
|
|
|
bool KineticScrolling::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
2009-11-18 18:59:56 +01:00
|
|
|
Q_UNUSED(watched);
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2009-10-15 21:11:34 +02:00
|
|
|
if (d->forwardingEvent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool notBlocked = true;
|
2009-11-18 18:35:50 +01:00
|
|
|
if (d->multitouchGesture == KineticScrollingPrivate::GestureNone &&
|
|
|
|
d->parent && d->parent->scene()) {
|
2009-10-15 21:11:34 +02:00
|
|
|
d->forwardingEvent = true;
|
|
|
|
notBlocked = d->parent->scene()->sendEvent(d->parent, event);
|
|
|
|
d->forwardingEvent = false;
|
|
|
|
}
|
|
|
|
|
2009-11-17 18:34:27 +01:00
|
|
|
if (event->type() != QEvent::TouchBegin &&
|
|
|
|
event->type() != QEvent::TouchUpdate &&
|
|
|
|
event->type() != QEvent::TouchEnd &&
|
|
|
|
(!notBlocked ||
|
2009-10-23 14:07:48 +02:00
|
|
|
((event->type() != QEvent::GraphicsSceneMousePress && event->isAccepted()) &&
|
2009-11-17 18:34:27 +01:00
|
|
|
(event->type() != QEvent::GraphicsSceneWheel && event->isAccepted())))) {
|
2009-10-15 21:11:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
|
|
|
|
QGraphicsSceneWheelEvent *we = static_cast<QGraphicsSceneWheelEvent *>(event);
|
|
|
|
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
mousePressEvent(me);
|
|
|
|
break;
|
|
|
|
case QEvent::GraphicsSceneMouseRelease:
|
|
|
|
mouseReleaseEvent(me);
|
|
|
|
break;
|
|
|
|
case QEvent::GraphicsSceneMouseMove:
|
|
|
|
mouseMoveEvent(me);
|
|
|
|
break;
|
2009-11-17 18:34:27 +01:00
|
|
|
case QEvent::TouchBegin:
|
|
|
|
mousePressEvent(0);
|
|
|
|
break;
|
|
|
|
case QEvent::TouchUpdate: {
|
|
|
|
QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
|
|
|
|
if (touchPoints.count() == 2) {
|
|
|
|
const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
|
|
|
|
const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
|
|
|
|
const QLineF line0(touchPoint0.lastPos(), touchPoint1.lastPos());
|
|
|
|
const QLineF line1(touchPoint0.pos(), touchPoint1.pos());
|
|
|
|
const QLineF startLine(touchPoint0.startPos(), touchPoint1.startPos());
|
|
|
|
const QPointF point = line1.pointAt(0.5);
|
|
|
|
const QPointF lastPoint = line0.pointAt(0.5);
|
|
|
|
|
|
|
|
if (d->multitouchGesture == KineticScrollingPrivate::GestureNone) {
|
|
|
|
d->multitouchGesture = KineticScrollingPrivate::GestureUndefined;
|
|
|
|
}
|
|
|
|
if (d->multitouchGesture == KineticScrollingPrivate::GestureUndefined) {
|
|
|
|
const int zoomDistance = qAbs(line1.length() - startLine.length());
|
|
|
|
const int dragDistance = (startLine.pointAt(0.5) - point).manhattanLength();
|
|
|
|
|
|
|
|
if (zoomDistance - dragDistance > 30) {
|
|
|
|
d->multitouchGesture = KineticScrollingPrivate::GestureZoom;
|
|
|
|
} else if (dragDistance - zoomDistance > 30) {
|
|
|
|
d->multitouchGesture = KineticScrollingPrivate::GestureScroll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->multitouchGesture == KineticScrollingPrivate::GestureScroll) {
|
|
|
|
QGraphicsSceneMouseEvent fakeEvent;
|
|
|
|
fakeEvent.setPos(point);
|
|
|
|
fakeEvent.setLastPos(lastPoint);
|
|
|
|
mouseMoveEvent(&fakeEvent);
|
|
|
|
} else if (d->multitouchGesture == KineticScrollingPrivate::GestureZoom) {
|
|
|
|
qreal scaleFactor = 1;
|
|
|
|
if (line0.length() > 0) {
|
|
|
|
scaleFactor = line1.length() / line0.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal zoom = d->parent->property("zoomFactor").toReal();
|
|
|
|
d->parent->setProperty("zoomFactor", zoom * scaleFactor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QEvent::TouchEnd:
|
|
|
|
mouseReleaseEvent(0);
|
|
|
|
d->multitouchGesture = KineticScrollingPrivate::GestureNone;
|
|
|
|
break;
|
2009-11-18 18:35:50 +01:00
|
|
|
|
2009-10-15 21:11:34 +02:00
|
|
|
case QEvent::GraphicsSceneWheel:
|
|
|
|
wheelReleaseEvent(we);
|
|
|
|
break;
|
2009-11-12 21:44:02 +01:00
|
|
|
case QEvent::KeyPress: {
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
|
|
|
|
keyPressEvent(ke);
|
|
|
|
break;
|
|
|
|
}
|
2009-10-15 21:11:34 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-17 18:24:19 +02:00
|
|
|
} // namespace Plasma
|