diff options
author | Adriano Rezende <adriano.rezende@openbossa.org> | 2009-11-16 10:52:54 -0300 |
---|---|---|
committer | Adriano Rezende <adriano.rezende@openbossa.org> | 2009-11-16 10:52:54 -0300 |
commit | 2fe6890798baff4fa5b5e324acc3ec1567557b90 (patch) | |
tree | 55484dbcf2690503eea6c03c5def3733bde7fafb /shared | |
parent | 25e43f30060da3973250bf7b94ff5fe61ddead89 (diff) |
Shared: Added KineticScroll class
The KineticScroll can be used as a helper for kinetic widgets to generate
kinetic feedback. It consumes mouse events and emits signals with offset
movements.
Signed-off-by: Adriano Rezende <adriano.rezende@openbossa.org>
Diffstat (limited to 'shared')
-rw-r--r-- | shared/kineticscroll.cpp | 159 | ||||
-rw-r--r-- | shared/kineticscroll.h | 65 |
2 files changed, 224 insertions, 0 deletions
diff --git a/shared/kineticscroll.cpp b/shared/kineticscroll.cpp new file mode 100644 index 0000000..fe937e9 --- /dev/null +++ b/shared/kineticscroll.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: openBossa - INdT (renato.chencarek@openbossa.org) +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** the openBossa stream from INdT (renato.chencarek@openbossa.org). +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QTime> +#include <QTimer> + +#include "kineticscroll.h" + +#define MAX_SPEED 2000 + + +class KineticScrollPrivate +{ +public: + QTimer *animation; + qreal animationSpeed; + QTime animationITime; + qreal animationAccel; + qreal accelConstant; + QTime time; + QTime lastTime; + int value; + int lastValue; +}; + + +KineticScroll::KineticScroll(QObject *parent) + : QObject(parent), d(new KineticScrollPrivate) +{ + d->animation = 0; + d->accelConstant = 0.3; + d->value = d->lastValue = -1; + d->time = d->lastTime = QTime::currentTime(); +} + +KineticScroll::~KineticScroll() +{ + delete d; +} + +void KineticScroll::mouseUp(int value) +{ + if (d->lastValue < 0 || d->animation) + return; + + QTime t = QTime::currentTime(); + qreal dv = value - d->lastValue; + qreal dt = (qreal)d->lastTime.msecsTo(t) / 1000; + + if (dt == 0) + return; + + kineticStart(dv / dt); + mouseCancel(); +} + +bool KineticScroll::mouseDown(int value) +{ + bool r = true; + if (d->animation) { + d->animation->stop(); + d->animation->deleteLater(); + d->animation = 0; + r = false; + } + d->lastValue = d->value = value; + d->lastTime = d->time = QTime::currentTime(); + + return r; +} + +void KineticScroll::mouseMove(int value) +{ + if (d->lastValue < 0) + return; + + int dv = value - d->value; + QTime t = QTime::currentTime(); + + d->lastValue = d->value; + d->lastTime = d->time; + d->value = value; + d->time = t; + + emit signalMoveOffset(dv); +} + +void KineticScroll::mouseCancel() +{ + d->value = -1; + d->lastValue = -1; +} + +void KineticScroll::kineticStop() +{ + if (d->animation) { + d->animation->stop(); + d->animation->deleteLater(); + } + + d->animation = 0; +} + +void KineticScroll::kineticStart(qreal speed) +{ + d->animationSpeed = qBound<qreal>(-MAX_SPEED, speed, MAX_SPEED); + d->animationITime = QTime::currentTime(); + d->animationAccel = -d->animationSpeed * d->accelConstant; + + d->animation = new QTimer(); + connect(d->animation, SIGNAL(timeout()), this, SLOT(animator())); + d->animation->start(10); +} + +void KineticScroll::animator() +{ + QTime now = QTime::currentTime(); + qreal dt = (qreal)d->animationITime.msecsTo(now) / 1000; + qreal speed = d->animationSpeed + d->animationAccel * dt; + qreal value = d->animationSpeed * dt + d->animationAccel * dt * dt / 2; + + if (d->animationAccel * speed > 0) { + // just to notify animation finish + emit signalMoveOffset(0); + kineticStop(); + } else { + emit signalMoveOffset(qRound(value)); + d->animationSpeed = speed; + d->animationITime = now; + } +} diff --git a/shared/kineticscroll.h b/shared/kineticscroll.h new file mode 100644 index 0000000..ffc8efb --- /dev/null +++ b/shared/kineticscroll.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: openBossa - INdT (renato.chencarek@openbossa.org) +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** the openBossa stream from INdT (renato.chencarek@openbossa.org). +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef KINETICSCROLL_H +#define KINETICSCROLL_H + +#include <QObject> + + +class KineticScrollPrivate; + +class KineticScroll : public QObject +{ + Q_OBJECT + +public: + KineticScroll(QObject *parent = 0); + ~KineticScroll(); + + void mouseUp(int value); + bool mouseDown(int value); + void mouseMove(int value); + void mouseCancel(); + void kineticStart(qreal speed); + void kineticStop(); + +public Q_SLOTS: + void animator(); + +Q_SIGNALS: + void signalMoveOffset(int); + +private: + KineticScrollPrivate *d; +}; + +#endif |