1 /****************************************************************************
\r
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
\r
4 ** All rights reserved.
\r
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
\r
7 ** This file is part of the demos of the Qt Toolkit.
\r
9 ** $QT_BEGIN_LICENSE:LGPL$
\r
11 ** Licensees holding valid Qt Commercial licenses may use this file in
\r
12 ** accordance with the Qt Commercial License Agreement provided with the
\r
13 ** Software or, alternatively, in accordance with the terms contained in
\r
14 ** a written agreement between you and Nokia.
\r
16 ** GNU Lesser General Public License Usage
\r
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
\r
18 ** General Public License version 2.1 as published by the Free Software
\r
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
\r
20 ** packaging of this file. Please review the following information to
\r
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
\r
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
\r
24 ** In addition, as a special exception, Nokia gives you certain additional
\r
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
\r
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
\r
28 ** GNU General Public License Usage
\r
29 ** Alternatively, this file may be used under the terms of the GNU
\r
30 ** General Public License version 3.0 as published by the Free Software
\r
31 ** Foundation and appearing in the file LICENSE.GPL included in the
\r
32 ** packaging of this file. Please review the following information to
\r
33 ** ensure the GNU General Public License version 3.0 requirements will be
\r
34 ** met: http://www.gnu.org/copyleft/gpl.html.
\r
36 ** If you have questions regarding the use of this file, please contact
\r
37 ** Nokia at qt-info@nokia.com.
\r
40 ****************************************************************************/
\r
42 #include "flickcharm.h"
\r
44 #include <QAbstractScrollArea>
\r
45 #include <QApplication>
\r
46 #include <QBasicTimer>
\r
50 #include <QMouseEvent>
\r
51 #include <QScrollBar>
\r
53 #include <QWebFrame>
\r
58 const int fingerAccuracyThreshold = 3;
\r
61 const int maxSpeed = 2000;
\r
62 const int maxSpeedAutoScroll = 1250;
\r
64 const int maxSpeed = 4000;
\r
65 const int maxSpeedAutoScroll = 2500;
\r
70 Steady, // Interaction without scrolling
\r
71 ManualScroll, // Scrolling manually with the finger on the screen
\r
72 AutoScroll, // Scrolling automatically
\r
73 AutoScrollAcceleration // Scrolling automatically but a finger is on the screen
\r
81 QList<QEvent*> ignored;
\r
82 QTime accelerationTimer;
\r
83 bool lastPosValid:1;
\r
84 bool waitingAcceleration:1;
\r
87 : lastPosValid(false)
\r
88 , waitingAcceleration(false)
\r
94 lastPosValid = false;
\r
96 void updateSpeed(const QPoint &newPosition)
\r
99 const int timeElapsed = speedTimer.elapsed();
\r
101 const QPoint newPixelDiff = (newPosition - lastPos);
\r
102 const QPoint pixelsPerSecond = newPixelDiff * (1000 / timeElapsed);
\r
103 // fingers are inacurates, we ignore small changes to avoid stopping the autoscroll because
\r
104 // of a small horizontal offset when scrolling vertically
\r
105 const int newSpeedY = (qAbs(pixelsPerSecond.y()) > fingerAccuracyThreshold) ? pixelsPerSecond.y() : 0;
\r
106 const int newSpeedX = (qAbs(pixelsPerSecond.x()) > fingerAccuracyThreshold) ? pixelsPerSecond.x() : 0;
\r
107 if (state == AutoScrollAcceleration) {
\r
108 const int max = maxSpeedAutoScroll; // px by seconds
\r
109 const int oldSpeedY = speed.y();
\r
110 const int oldSpeedX = speed.x();
\r
111 if ((oldSpeedY <= 0 && newSpeedY <= 0) || (oldSpeedY >= 0 && newSpeedY >= 0)
\r
112 && (oldSpeedX <= 0 && newSpeedX <= 0) || (oldSpeedX >= 0 && newSpeedX >= 0)) {
\r
113 speed.setY(qBound(-max, (oldSpeedY + (newSpeedY / 4)), max));
\r
114 speed.setX(qBound(-max, (oldSpeedX + (newSpeedX / 4)), max));
\r
119 const int max = maxSpeed; // px by seconds
\r
120 // we average the speed to avoid strange effects with the last delta
\r
121 if (!speed.isNull()) {
\r
122 speed.setX(qBound(-max, (speed.x() / 4) + (newSpeedX * 3 / 4), max));
\r
123 speed.setY(qBound(-max, (speed.y() / 4) + (newSpeedY * 3 / 4), max));
\r
125 speed = QPoint(newSpeedX, newSpeedY);
\r
130 lastPosValid = true;
\r
132 speedTimer.start();
\r
133 lastPos = newPosition;
\r
136 // scroll by dx, dy
\r
137 // return true if the widget was scrolled
\r
138 bool scrollWidget(const int dx, const int dy)
\r
140 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
\r
142 const int x = scrollArea->horizontalScrollBar()->value();
\r
143 const int y = scrollArea->verticalScrollBar()->value();
\r
144 scrollArea->horizontalScrollBar()->setValue(x - dx);
\r
145 scrollArea->verticalScrollBar()->setValue(y - dy);
\r
146 return (scrollArea->horizontalScrollBar()->value() != x
\r
147 || scrollArea->verticalScrollBar()->value() != y);
\r
150 QWebView *webView = qobject_cast<QWebView*>(widget);
\r
152 QWebFrame *frame = webView->page()->mainFrame();
\r
153 const QPoint position = frame->scrollPosition();
\r
154 frame->setScrollPosition(position - QPoint(dx, dy));
\r
155 return frame->scrollPosition() != position;
\r
160 bool scrollTo(const QPoint &newPosition)
\r
162 const QPoint delta = newPosition - lastPos;
\r
163 updateSpeed(newPosition);
\r
164 return scrollWidget(delta.x(), delta.y());
\r
168 class FlickCharmPrivate
\r
171 QHash<QWidget*, FlickData*> flickData;
\r
172 QBasicTimer ticker;
\r
174 void startTicker(QObject *object)
\r
176 if (!ticker.isActive())
\r
177 ticker.start(15, object);
\r
178 timeCounter.start();
\r
182 FlickCharm::FlickCharm(QObject *parent): QObject(parent)
\r
184 d = new FlickCharmPrivate;
\r
187 FlickCharm::~FlickCharm()
\r
192 void FlickCharm::activateOn(QWidget *widget)
\r
194 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
\r
196 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
\r
197 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
\r
199 QWidget *viewport = scrollArea->viewport();
\r
201 viewport->installEventFilter(this);
\r
202 scrollArea->installEventFilter(this);
\r
204 d->flickData.remove(viewport);
\r
205 d->flickData[viewport] = new FlickData;
\r
206 d->flickData[viewport]->widget = widget;
\r
207 d->flickData[viewport]->state = FlickData::Steady;
\r
212 QWebView *webView = qobject_cast<QWebView*>(widget);
\r
214 QWebFrame *frame = webView->page()->mainFrame();
\r
215 frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
\r
216 frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
\r
218 webView->installEventFilter(this);
\r
220 d->flickData.remove(webView);
\r
221 d->flickData[webView] = new FlickData;
\r
222 d->flickData[webView]->widget = webView;
\r
223 d->flickData[webView]->state = FlickData::Steady;
\r
228 qWarning() << "FlickCharm only works on QAbstractScrollArea (and derived classes)";
\r
229 qWarning() << "or QWebView (and derived classes)";
\r
232 void FlickCharm::deactivateFrom(QWidget *widget)
\r
234 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
\r
236 QWidget *viewport = scrollArea->viewport();
\r
238 viewport->removeEventFilter(this);
\r
239 scrollArea->removeEventFilter(this);
\r
241 delete d->flickData[viewport];
\r
242 d->flickData.remove(viewport);
\r
247 QWebView *webView = qobject_cast<QWebView*>(widget);
\r
249 webView->removeEventFilter(this);
\r
251 delete d->flickData[webView];
\r
252 d->flickData.remove(webView);
\r
258 static QPoint deaccelerate(const QPoint &speed, const int deltatime)
\r
260 const int deltaSpeed = deltatime;
\r
264 x = (x == 0) ? x : (x > 0) ? qMax(0, x - deltaSpeed) : qMin(0, x + deltaSpeed);
\r
265 y = (y == 0) ? y : (y > 0) ? qMax(0, y - deltaSpeed) : qMin(0, y + deltaSpeed);
\r
266 return QPoint(x, y);
\r
269 bool FlickCharm::eventFilter(QObject *object, QEvent *event)
\r
271 if (!object->isWidgetType())
\r
274 const QEvent::Type type = event->type();
\r
277 case QEvent::MouseButtonPress:
\r
278 case QEvent::MouseMove:
\r
279 case QEvent::MouseButtonRelease:
\r
281 case QEvent::MouseButtonDblClick: // skip double click
\r
287 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
\r
288 if (type == QEvent::MouseMove && mouseEvent->buttons() != Qt::LeftButton)
\r
291 if (mouseEvent->modifiers() != Qt::NoModifier)
\r
294 QWidget *viewport = qobject_cast<QWidget*>(object);
\r
295 FlickData *data = d->flickData.value(viewport);
\r
296 if (!viewport || !data || data->ignored.removeAll(event))
\r
299 const QPoint mousePos = mouseEvent->pos();
\r
300 bool consumed = false;
\r
301 switch (data->state) {
\r
303 case FlickData::Steady:
\r
304 if (type == QEvent::MouseButtonPress) {
\r
306 data->pressPos = mousePos;
\r
307 } else if (type == QEvent::MouseButtonRelease) {
\r
309 QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
\r
310 data->pressPos, Qt::LeftButton,
\r
311 Qt::LeftButton, Qt::NoModifier);
\r
312 QMouseEvent *event2 = new QMouseEvent(QEvent::MouseButtonRelease,
\r
313 data->pressPos, Qt::LeftButton,
\r
314 Qt::LeftButton, Qt::NoModifier);
\r
316 data->ignored << event1;
\r
317 data->ignored << event2;
\r
318 QApplication::postEvent(object, event1);
\r
319 QApplication::postEvent(object, event2);
\r
320 } else if (type == QEvent::MouseMove) {
\r
322 data->scrollTo(mousePos);
\r
324 const QPoint delta = mousePos - data->pressPos;
\r
325 if (delta.x() > fingerAccuracyThreshold || delta.y() > fingerAccuracyThreshold)
\r
326 data->state = FlickData::ManualScroll;
\r
330 case FlickData::ManualScroll:
\r
331 if (type == QEvent::MouseMove) {
\r
333 data->scrollTo(mousePos);
\r
334 } else if (type == QEvent::MouseButtonRelease) {
\r
336 data->state = FlickData::AutoScroll;
\r
337 data->lastPosValid = false;
\r
338 d->startTicker(this);
\r
342 case FlickData::AutoScroll:
\r
343 if (type == QEvent::MouseButtonPress) {
\r
345 data->state = FlickData::AutoScrollAcceleration;
\r
346 data->waitingAcceleration = true;
\r
347 data->accelerationTimer.start();
\r
348 data->updateSpeed(mousePos);
\r
349 data->pressPos = mousePos;
\r
350 } else if (type == QEvent::MouseButtonRelease) {
\r
352 data->state = FlickData::Steady;
\r
353 data->resetSpeed();
\r
357 case FlickData::AutoScrollAcceleration:
\r
358 if (type == QEvent::MouseMove) {
\r
360 data->updateSpeed(mousePos);
\r
361 data->accelerationTimer.start();
\r
362 if (data->speed.isNull())
\r
363 data->state = FlickData::ManualScroll;
\r
364 } else if (type == QEvent::MouseButtonRelease) {
\r
366 data->state = FlickData::AutoScroll;
\r
367 data->waitingAcceleration = false;
\r
368 data->lastPosValid = false;
\r
374 data->lastPos = mousePos;
\r
378 void FlickCharm::timerEvent(QTimerEvent *event)
\r
381 QHashIterator<QWidget*, FlickData*> item(d->flickData);
\r
382 while (item.hasNext()) {
\r
384 FlickData *data = item.value();
\r
385 if (data->state == FlickData::AutoScrollAcceleration
\r
386 && data->waitingAcceleration
\r
387 && data->accelerationTimer.elapsed() > 40) {
\r
388 data->state = FlickData::ManualScroll;
\r
389 data->resetSpeed();
\r
391 if (data->state == FlickData::AutoScroll || data->state == FlickData::AutoScrollAcceleration) {
\r
392 const int timeElapsed = d->timeCounter.elapsed();
\r
393 const QPoint delta = (data->speed) * timeElapsed / 1000;
\r
394 bool hasScrolled = data->scrollWidget(delta.x(), delta.y());
\r
396 if (data->speed.isNull() || !hasScrolled)
\r
397 data->state = FlickData::Steady;
\r
400 data->speed = deaccelerate(data->speed, timeElapsed);
\r
407 d->timeCounter.start();
\r
409 QObject::timerEvent(event);
\r