aboutsummaryrefslogtreecommitdiff
path: root/plainqt/style/qtmaemo6kineticscrolling.h
blob: e10a4908ce4bea6bca3eaf8eee2649fabb492fbe (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libdui.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it 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.
**
****************************************************************************/

#ifndef QTMAEMO6KINETICSCROLLING_H
#define QTMAEMO6KINETICSCROLLING_H

#include <QObject>
#include <QBasicTimer>
#include <QHash>
#include <QPoint>

class QWidget;
class QAbstractScrollArea;

class QtMaemo6KineticScrolling: public QObject
{
    Q_OBJECT
    Q_PROPERTY(int scrollStartDelay READ scrollStartDelay WRITE setScrollStartDelay);
    Q_PROPERTY(int scrollStartOffset READ scrollStartOffset WRITE setScrollStartOffset);
    Q_PROPERTY(int deaccelerationInterval READ deaccelerationInterval WRITE setDeaccelerationInterval);
    Q_PROPERTY(int deaccelerationStrength READ deaccelerationStrength WRITE setDeaccelerationStrength);
    Q_PROPERTY(int maxKineticScrollSpeed READ maxKineticScrollSpeed WRITE setMaxKineticScrollSpeed);
public:
    QtMaemo6KineticScrolling(QObject *parent = 0);
    ~QtMaemo6KineticScrolling();

    /*!
     * Enables the kinetic scrolling on the given scrollarea.
     */
    void enableOn(QAbstractScrollArea *scrollArea);

    /*! \reimp */
    bool eventFilter(QObject *object, QEvent *event);
    /*! \reimp_end */

    /*!
     * \brief Returns the current scroll delay in milliseconds.
     * After a press event a move must been made within this time
     * before the scrolling starts. If there is no move within this time
     * the mouse press event is forwarded to the widget under the mouse.
     * Default is 50ms.
     * \see scrollStartOffset
     */
    int scrollStartDelay() const {
        return m_scrollStartDelay;
    };

    /*!
     * \brief Sets the scroll delay in milliseconds.
     * \see scrollStartDelay()
     */
    void setScrollStartDelay(int delay) {
        m_scrollStartDelay = delay;
    };

    /*!
     * \brief Rturns the current scroll offset in milliseconds.
     * After a press event a move must been made with at least this manhattan
     * length before the scrolling starts. If there is no move within this length
     * the mouse press event is forwarded to the widget under the mouse.
     * Default is 5.
     * \see scrollStartDelay()
     */
    int scrollStartOffset() const {
        return m_scrollStartOffset;
    };

    /*!
     * \brief Sets the scroll offset in milliseconds.
     * \see scrollStartOffset()
     */
    void setScrollStartOffset(int offset) {
        m_scrollStartDelay = offset;
    };

    /*!
     * \brief Returns the current intervall in milliseconds the deacceleration methode is called.
     * After a press event a move must been made with at least this manhattan
     * length before the scrolling starts. If there is no move within this length
     * the mouse press event is forwarded to the widget under the mouse.
     * Default is 20ms.
     */
    int deaccelerationInterval() const {
        return m_deaccelerationInterval;
    };

    /*!
     * \brief Sets the deaccelaration interval in milliseconds.
     * \see deaccelerationInterval()
     */
    void setDeaccelerationInterval(int interval) {
        m_deaccelerationInterval = interval;
    };

    /*!
     * \brief Returns the current deaccelaration strength.
     * this is the value of speed in pixels the kinetic scrolling will be decreased
     * every deaccelaration interval.
     * Default is 1.
     */
    int deaccelerationStrength() const {
        return m_deaccelerationStrength;
    };

    /*!
     * \brief Sets the deaccelaration strength.
     * \see deaccelerationStrength()
     */
    void setDeaccelerationStrength(int strength) {
        m_deaccelerationStrength = strength;
    };

    /*!
     * \brief Returns the max value of the kinetic scrolling speed in pixels per interval.
     * Default is 64.
     */
    int maxKineticScrollSpeed() const {
        return m_maxKineticScrollSpeed;
    };
    /*!
     * \brief Sets the maximum speed of kinetic scrolling in pixels per interval.
     * \see maxKineticScrollSpeed()
     */
    void setMaxKineticScrollSpeed(int speed) {
        m_maxKineticScrollSpeed = speed;
    };

    /*!
     * \brief If application has direction right to left, set this.
     */
    void setRightToLeft(bool rightToLeft) {
        m_rightToLeft = rightToLeft;
    }

protected:
    void timerEvent(QTimerEvent *event);
    void installEventFilter(QObjectList list);

private:
    class KineticData {
    public:
        typedef enum { Waiting, Pressed, Panning, KineticScroll, Bounce, BounceBack, Stop } State;
        void setState(State state);
        State state() const { return m_state; };
        QAbstractScrollArea *scrollArea;
        QWidget* pressedWidget;
        QPoint pressPos;
        QPoint offset;
        QPoint dragPos;
        QPoint speed;
        QPoint viewportOrigPos;
        QList<QEvent*> ignored;
    protected:
        State m_state;
    };

    QHash<QWidget*, KineticData*> m_kineticData;
    QBasicTimer m_ticker;

    int m_scrollStartDelay;
    int m_scrollStartOffset;
    int m_deaccelerationInterval;
    int m_deaccelerationStrength;
    int m_maxKineticScrollSpeed;

    bool m_rightToLeft;
};

#endif // QTMAEMO6KINETICSCROLLING_H