~patrick-hetu/+junk/evopedia-app

« back to all changes in this revision

Viewing changes to src/flickable.cpp

  • Committer: Patrick Hetu
  • Date: 2013-07-22 02:35:22 UTC
  • Revision ID: patrick.hetu@gmail.com-20130722023522-jrfmj5s6eb3mfdv8
initial test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: Qt Software Information (qt-info@nokia.com)
 
5
**
 
6
** This file is part of the Graphics Dojo project on Qt Labs.
 
7
**
 
8
** This file may be used under the terms of the GNU General Public
 
9
** License version 2.0 or 3.0 as published by the Free Software Foundation
 
10
** and appearing in the file LICENSE.GPL included in the packaging of
 
11
** this file.  Please review the following information to ensure GNU
 
12
** General Public Licensing requirements will be met:
 
13
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 
14
** http://www.gnu.org/copyleft/gpl.html.
 
15
**
 
16
** If you are unsure which license is appropriate for your use, please
 
17
** contact the sales department at qt-sales@nokia.com.
 
18
**
 
19
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
20
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
21
**
 
22
****************************************************************************/
 
23
 
 
24
#include "flickable.h"
 
25
 
 
26
#include <QtCore>
 
27
#include <QtGui>
 
28
#include <QCoreApplication>
 
29
 
 
30
class FlickableTicker: QObject
 
31
{
 
32
public:
 
33
    FlickableTicker(Flickable *scroller) {
 
34
        m_scroller = scroller;
 
35
    }
 
36
 
 
37
    void start(int interval) {
 
38
        if (!m_timer.isActive())
 
39
            m_timer.start(interval, this);
 
40
    }
 
41
 
 
42
    void stop() {
 
43
        m_timer.stop();
 
44
    }
 
45
 
 
46
protected:
 
47
    void timerEvent(QTimerEvent *event) {
 
48
        Q_UNUSED(event);
 
49
        m_scroller->tick();
 
50
    }
 
51
 
 
52
private:
 
53
    Flickable *m_scroller;
 
54
    QBasicTimer m_timer;
 
55
};
 
56
 
 
57
class FlickablePrivate
 
58
{
 
59
public:
 
60
    typedef enum {
 
61
        Steady,
 
62
        Pressed,
 
63
        ManualScroll,
 
64
        AutoScroll,
 
65
        Stop
 
66
    } State;
 
67
 
 
68
    State state;
 
69
    int threshold;
 
70
    QPoint pressPos;
 
71
    QPoint offset;
 
72
    QPoint delta;
 
73
    QPointF speed;
 
74
    FlickableTicker *ticker;
 
75
    QTime timeStamp;
 
76
    QWidget *target;
 
77
    QList<QEvent*> ignoreList;
 
78
};
 
79
 
 
80
Flickable::Flickable()
 
81
{
 
82
    d = new FlickablePrivate;
 
83
    d->state = FlickablePrivate::Steady;
 
84
    d->threshold = 10;
 
85
    d->ticker = new FlickableTicker(this);
 
86
    d->timeStamp = QTime::currentTime();
 
87
    d->target = 0;
 
88
}
 
89
 
 
90
Flickable::~Flickable()
 
91
{
 
92
    delete d;
 
93
}
 
94
 
 
95
void Flickable::setThreshold(int th)
 
96
{
 
97
    if (th >= 0)
 
98
        d->threshold = th;
 
99
}
 
100
 
 
101
int Flickable::threshold() const
 
102
{
 
103
    return d->threshold;
 
104
}
 
105
 
 
106
void Flickable::setAcceptMouseClick(QWidget *target)
 
107
{
 
108
    d->target = target;
 
109
}
 
110
 
 
111
static QPointF deaccelerate(const QPointF &speed, int max = 64)
 
112
{
 
113
    QPointF newSpeed = speed;
 
114
    qreal absSpeed = newSpeed.x() * newSpeed.x() + newSpeed.y() * newSpeed.y();
 
115
    if (absSpeed > max * max) {
 
116
        newSpeed *= max / sqrt(absSpeed);
 
117
    }
 
118
    return newSpeed * .95;
 
119
}
 
120
 
 
121
void Flickable::handleMousePress(QMouseEvent *event)
 
122
{
 
123
    event->ignore();
 
124
 
 
125
    if (event->button() != Qt::LeftButton)
 
126
        return;
 
127
 
 
128
    if (d->ignoreList.removeAll(event))
 
129
        return;
 
130
 
 
131
    switch (d->state) {
 
132
 
 
133
    case FlickablePrivate::Steady:
 
134
        event->accept();
 
135
        d->state = FlickablePrivate::Pressed;
 
136
        d->pressPos = event->pos();
 
137
        break;
 
138
 
 
139
    case FlickablePrivate::AutoScroll:
 
140
        event->accept();
 
141
        d->state = FlickablePrivate::Stop;
 
142
        d->speed = QPointF(0, 0);
 
143
        d->pressPos = event->pos();
 
144
        d->offset = scrollOffset();
 
145
        d->ticker->stop();
 
146
        break;
 
147
 
 
148
    default:
 
149
        break;
 
150
    }
 
151
}
 
152
 
 
153
void Flickable::handleMouseRelease(QMouseEvent *event)
 
154
{
 
155
    event->ignore();
 
156
 
 
157
    if (event->button() != Qt::LeftButton)
 
158
        return;
 
159
 
 
160
    if (d->ignoreList.removeAll(event))
 
161
        return;
 
162
 
 
163
    QPoint delta;
 
164
 
 
165
    switch (d->state) {
 
166
 
 
167
    case FlickablePrivate::Pressed:
 
168
        event->accept();
 
169
        d->state = FlickablePrivate::Steady;
 
170
        if (d->target) {
 
171
            QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
 
172
                                                  d->pressPos, Qt::LeftButton,
 
173
                                                  Qt::LeftButton, Qt::NoModifier);
 
174
            QMouseEvent *event2 = new QMouseEvent(*event);
 
175
            d->ignoreList << event1;
 
176
            d->ignoreList << event2;
 
177
            QCoreApplication::postEvent(d->target, event1);
 
178
            QCoreApplication::postEvent(d->target, event2);
 
179
        }
 
180
        break;
 
181
 
 
182
    case FlickablePrivate::ManualScroll:
 
183
        event->accept();
 
184
        delta = event->pos() - d->pressPos;
 
185
        if (d->timeStamp.elapsed() > 100) {
 
186
            d->timeStamp = QTime::currentTime();
 
187
            d->speed = delta - d->delta;
 
188
            d->delta = delta;
 
189
        }
 
190
        d->offset = scrollOffset();
 
191
        d->pressPos = event->pos();
 
192
        if (d->speed.manhattanLength() < 1) {
 
193
            d->state = FlickablePrivate::Steady;
 
194
        } else {
 
195
            d->speed /= 4;
 
196
            d->state = FlickablePrivate::AutoScroll;
 
197
            d->ticker->start(20);
 
198
        }
 
199
        break;
 
200
 
 
201
    case FlickablePrivate::Stop:
 
202
        event->accept();
 
203
        d->state = FlickablePrivate::Steady;
 
204
        d->offset = scrollOffset();
 
205
        break;
 
206
 
 
207
    default:
 
208
        break;
 
209
    }
 
210
}
 
211
 
 
212
void Flickable::handleMouseMove(QMouseEvent *event)
 
213
{
 
214
    event->ignore();
 
215
 
 
216
    if (!(event->buttons() & Qt::LeftButton))
 
217
        return;
 
218
 
 
219
    if (d->ignoreList.removeAll(event))
 
220
        return;
 
221
 
 
222
    QPoint delta;
 
223
 
 
224
    switch (d->state) {
 
225
 
 
226
    case FlickablePrivate::Pressed:
 
227
    case FlickablePrivate::Stop:
 
228
        delta = event->pos() - d->pressPos;
 
229
        if (delta.x() > d->threshold || delta.x() < -d->threshold ||
 
230
                delta.y() > d->threshold || delta.y() < -d->threshold) {
 
231
            d->timeStamp = QTime::currentTime();
 
232
            d->state = FlickablePrivate::ManualScroll;
 
233
            d->delta = QPoint(0, 0);
 
234
            d->pressPos = event->pos();
 
235
            event->accept();
 
236
        }
 
237
        break;
 
238
 
 
239
    case FlickablePrivate::ManualScroll:
 
240
        event->accept();
 
241
        delta = event->pos() - d->pressPos;
 
242
        setScrollOffset(d->offset - delta);
 
243
        if (d->timeStamp.elapsed() > 100) {
 
244
            d->timeStamp = QTime::currentTime();
 
245
            d->speed = delta - d->delta;
 
246
            d->delta = delta;
 
247
        }
 
248
        break;
 
249
 
 
250
    default:
 
251
        break;
 
252
    }
 
253
}
 
254
 
 
255
void Flickable::externalScrollUpdate()
 
256
{
 
257
    d->state = FlickablePrivate::Steady;
 
258
    d->speed = QPointF(0, 0);
 
259
    d->offset = scrollOffset();
 
260
    d->ticker->stop();
 
261
}
 
262
 
 
263
void Flickable::tick()
 
264
{
 
265
    if (d->state == FlickablePrivate:: AutoScroll) {
 
266
        d->speed = deaccelerate(d->speed);
 
267
        setScrollOffset((d->offset - d->speed).toPoint());
 
268
        d->offset = scrollOffset();
 
269
        if (d->speed.manhattanLength() < 1) {
 
270
            d->speed = QPointF(0, 0);
 
271
            d->state = FlickablePrivate::Steady;
 
272
            d->ticker->stop();
 
273
        }
 
274
    } else {
 
275
        d->ticker->stop();
 
276
    }
 
277
}