summaryrefslogtreecommitdiffstats
path: root/shared/kineticscroll.cpp
blob: a2c1748da4fe070d3cb5460bf5bb972e7f413b9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: qt-info@nokia.com
**
** This software, including documentation, is protected by copyright
** controlled by Nokia Corporation.  You may use this software in
** accordance with the terms and conditions contained in the Qt Phone
** Demo License Agreement.
**
****************************************************************************/

#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;
    }
}