1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: Qt Software Information (qt-info@nokia.com)
6
** This file is part of the Graphics Dojo project on Qt Labs.
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.
16
** If you are unsure which license is appropriate for your use, please
17
** contact the sales department at qt-sales@nokia.com.
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.
22
****************************************************************************/
24
#include "flickable.h"
28
#include <QCoreApplication>
30
class FlickableTicker: QObject
33
FlickableTicker(Flickable *scroller) {
34
m_scroller = scroller;
37
void start(int interval) {
38
if (!m_timer.isActive())
39
m_timer.start(interval, this);
47
void timerEvent(QTimerEvent *event) {
53
Flickable *m_scroller;
57
class FlickablePrivate
74
FlickableTicker *ticker;
77
QList<QEvent*> ignoreList;
80
Flickable::Flickable()
82
d = new FlickablePrivate;
83
d->state = FlickablePrivate::Steady;
85
d->ticker = new FlickableTicker(this);
86
d->timeStamp = QTime::currentTime();
90
Flickable::~Flickable()
95
void Flickable::setThreshold(int th)
101
int Flickable::threshold() const
106
void Flickable::setAcceptMouseClick(QWidget *target)
111
static QPointF deaccelerate(const QPointF &speed, int max = 64)
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);
118
return newSpeed * .95;
121
void Flickable::handleMousePress(QMouseEvent *event)
125
if (event->button() != Qt::LeftButton)
128
if (d->ignoreList.removeAll(event))
133
case FlickablePrivate::Steady:
135
d->state = FlickablePrivate::Pressed;
136
d->pressPos = event->pos();
139
case FlickablePrivate::AutoScroll:
141
d->state = FlickablePrivate::Stop;
142
d->speed = QPointF(0, 0);
143
d->pressPos = event->pos();
144
d->offset = scrollOffset();
153
void Flickable::handleMouseRelease(QMouseEvent *event)
157
if (event->button() != Qt::LeftButton)
160
if (d->ignoreList.removeAll(event))
167
case FlickablePrivate::Pressed:
169
d->state = FlickablePrivate::Steady;
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);
182
case FlickablePrivate::ManualScroll:
184
delta = event->pos() - d->pressPos;
185
if (d->timeStamp.elapsed() > 100) {
186
d->timeStamp = QTime::currentTime();
187
d->speed = delta - d->delta;
190
d->offset = scrollOffset();
191
d->pressPos = event->pos();
192
if (d->speed.manhattanLength() < 1) {
193
d->state = FlickablePrivate::Steady;
196
d->state = FlickablePrivate::AutoScroll;
197
d->ticker->start(20);
201
case FlickablePrivate::Stop:
203
d->state = FlickablePrivate::Steady;
204
d->offset = scrollOffset();
212
void Flickable::handleMouseMove(QMouseEvent *event)
216
if (!(event->buttons() & Qt::LeftButton))
219
if (d->ignoreList.removeAll(event))
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();
239
case FlickablePrivate::ManualScroll:
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;
255
void Flickable::externalScrollUpdate()
257
d->state = FlickablePrivate::Steady;
258
d->speed = QPointF(0, 0);
259
d->offset = scrollOffset();
263
void Flickable::tick()
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;