1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "flickable.h"
47 class FlickableTicker: QObject
50 FlickableTicker(Flickable *scroller) {
51 m_scroller = scroller;
54 void start(int interval) {
55 if (!m_timer.isActive())
56 m_timer.start(interval, this);
64 void timerEvent(QTimerEvent *event) {
70 Flickable *m_scroller;
74 class FlickablePrivate
91 FlickableTicker *ticker;
94 QList<QEvent*> ignoreList;
97 Flickable::Flickable()
99 d = new FlickablePrivate;
100 d->state = FlickablePrivate::Steady;
102 d->ticker = new FlickableTicker(this);
103 d->timeStamp = QTime::currentTime();
107 Flickable::~Flickable()
112 void Flickable::setThreshold(int th)
118 int Flickable::threshold() const
123 void Flickable::setAcceptMouseClick(QWidget *target)
128 static QPoint deaccelerate(const QPoint &speed, int a = 1, int max = 64)
130 int x = qBound(-max, speed.x(), max);
131 int y = qBound(-max, speed.y(), max);
132 x = (x == 0) ? x : (x > 0) ? qMax(0, x - a) : qMin(0, x + a);
133 y = (y == 0) ? y : (y > 0) ? qMax(0, y - a) : qMin(0, y + a);
137 void Flickable::handleMousePress(QMouseEvent *event)
141 if (event->button() != Qt::LeftButton)
144 if (d->ignoreList.removeAll(event))
149 case FlickablePrivate::Steady:
151 d->state = FlickablePrivate::Pressed;
152 d->pressPos = event->pos();
155 case FlickablePrivate::AutoScroll:
157 d->state = FlickablePrivate::Stop;
158 d->speed = QPoint(0, 0);
159 d->pressPos = event->pos();
160 d->offset = scrollOffset();
169 void Flickable::handleMouseRelease(QMouseEvent *event)
173 if (event->button() != Qt::LeftButton)
176 if (d->ignoreList.removeAll(event))
183 case FlickablePrivate::Pressed:
185 d->state = FlickablePrivate::Steady;
187 QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
188 d->pressPos, Qt::LeftButton,
189 Qt::LeftButton, Qt::NoModifier);
190 QMouseEvent *event2 = new QMouseEvent(*event);
191 d->ignoreList << event1;
192 d->ignoreList << event2;
193 QApplication::postEvent(d->target, event1);
194 QApplication::postEvent(d->target, event2);
198 case FlickablePrivate::ManualScroll:
200 delta = event->pos() - d->pressPos;
201 if (d->timeStamp.elapsed() > 100) {
202 d->timeStamp = QTime::currentTime();
203 d->speed = delta - d->delta;
206 d->offset = scrollOffset();
207 d->pressPos = event->pos();
208 if (d->speed == QPoint(0, 0)) {
209 d->state = FlickablePrivate::Steady;
212 d->state = FlickablePrivate::AutoScroll;
213 d->ticker->start(20);
217 case FlickablePrivate::Stop:
219 d->state = FlickablePrivate::Steady;
220 d->offset = scrollOffset();
228 void Flickable::handleMouseMove(QMouseEvent *event)
232 if (!(event->buttons() & Qt::LeftButton))
235 if (d->ignoreList.removeAll(event))
242 case FlickablePrivate::Pressed:
243 case FlickablePrivate::Stop:
244 delta = event->pos() - d->pressPos;
245 if (delta.x() > d->threshold || delta.x() < -d->threshold ||
246 delta.y() > d->threshold || delta.y() < -d->threshold) {
247 d->timeStamp = QTime::currentTime();
248 d->state = FlickablePrivate::ManualScroll;
249 d->delta = QPoint(0, 0);
250 d->pressPos = event->pos();
255 case FlickablePrivate::ManualScroll:
257 delta = event->pos() - d->pressPos;
258 setScrollOffset(d->offset - delta);
259 if (d->timeStamp.elapsed() > 100) {
260 d->timeStamp = QTime::currentTime();
261 d->speed = delta - d->delta;
271 void Flickable::tick()
273 if (d->state == FlickablePrivate:: AutoScroll) {
274 d->speed = deaccelerate(d->speed);
275 setScrollOffset(d->offset - d->speed);
276 d->offset = scrollOffset();
277 if (d->speed == QPoint(0, 0)) {
278 d->state = FlickablePrivate::Steady;