summaryrefslogtreecommitdiffstats
path: root/demos/embedded/anomaly/src/flickcharm.cpp
blob: b7e215107b604c783b0dc96adbf3c0a9f67b3b05 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demos of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used 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.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "flickcharm.h"

#include <QAbstractScrollArea>
#include <QApplication>
#include <QBasicTimer>
#include <QEvent>
#include <QHash>
#include <QList>
#include <QMouseEvent>
#include <QScrollBar>
#include <QTime>
#include <QWebFrame>
#include <QWebView>

#include <QDebug>

const int fingerAccuracyThreshold = 3;

struct FlickData {
    typedef enum {
        Steady, // Interaction without scrolling
        ManualScroll, // Scrolling manually with the finger on the screen
        AutoScroll, // Scrolling automatically
        AutoScrollAcceleration // Scrolling automatically but a finger is on the screen
    } State;
    State state;
    QWidget *widget;
    QPoint pressPos;
    QPoint lastPos;
    QPoint speed;
    QTime speedTimer;
    QList<QEvent*> ignored;
    QTime accelerationTimer;
    bool lastPosValid:1;
    bool waitingAcceleration:1;

    FlickData()
        : lastPosValid(false)
        , waitingAcceleration(false)
    {}

    void resetSpeed()
    {
        speed = QPoint();
        lastPosValid = false;
    }
    void updateSpeed(const QPoint &newPosition)
    {
        if (lastPosValid) {
            const int timeElapsed = speedTimer.elapsed();
            if (timeElapsed) {
                const QPoint newPixelDiff = (newPosition - lastPos);
                const QPoint pixelsPerSecond = newPixelDiff * (1000 / timeElapsed);
                // fingers are inacurates, we ignore small changes to avoid stopping the autoscroll because
                // of a small horizontal offset when scrolling vertically
                const int newSpeedY = (qAbs(pixelsPerSecond.y()) > fingerAccuracyThreshold) ? pixelsPerSecond.y() : 0;
                const int newSpeedX = (qAbs(pixelsPerSecond.x()) > fingerAccuracyThreshold) ? pixelsPerSecond.x() : 0;
                if (state == AutoScrollAcceleration) {
                    const int max = 4000; // px by seconds
                    const int oldSpeedY = speed.y();
                    const int oldSpeedX = speed.x();
                    if ((oldSpeedY <= 0 && newSpeedY <= 0) ||  (oldSpeedY >= 0 && newSpeedY >= 0)
                        && (oldSpeedX <= 0 && newSpeedX <= 0) ||  (oldSpeedX >= 0 && newSpeedX >= 0)) {
                        speed.setY(qBound(-max, (oldSpeedY + (newSpeedY / 4)), max));
                        speed.setX(qBound(-max, (oldSpeedX + (newSpeedX / 4)), max));
                    } else {
                        speed = QPoint();
                    }
                } else {
                    const int max = 2500; // px by seconds
                    // we average the speed to avoid strange effects with the last delta
                    if (!speed.isNull()) {
                        speed.setX(qBound(-max, (speed.x() / 4) + (newSpeedX * 3 / 4), max));
                        speed.setY(qBound(-max, (speed.y() / 4) + (newSpeedY * 3 / 4), max));
                    } else {
                        speed = QPoint(newSpeedX, newSpeedY);
                    }
                }
            }
        } else {
            lastPosValid = true;
        }
        speedTimer.start();
        lastPos = newPosition;
    }

    // scroll by dx, dy
    // return true if the widget was scrolled
    bool scrollWidget(const int dx, const int dy)
    {
        QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
        if (scrollArea) {
            const int x = scrollArea->horizontalScrollBar()->value();
            const int y = scrollArea->verticalScrollBar()->value();
            scrollArea->horizontalScrollBar()->setValue(x - dx);
            scrollArea->verticalScrollBar()->setValue(y - dy);
            return (scrollArea->horizontalScrollBar()->value() != x
                    || scrollArea->verticalScrollBar()->value() != y);
        }

        QWebView *webView = qobject_cast<QWebView*>(widget);
        if (webView) {
            QWebFrame *frame = webView->page()->mainFrame();
            const QPoint position = frame->scrollPosition();
            frame->setScrollPosition(position - QPoint(dx, dy));
            return frame->scrollPosition() != position;
        }
        return false;
    }

    bool scrollTo(const QPoint &newPosition)
    {
        const QPoint delta = newPosition - lastPos;
        updateSpeed(newPosition);
        return scrollWidget(delta.x(), delta.y());
    }
};

class FlickCharmPrivate
{
public:
    QHash<QWidget*, FlickData*> flickData;
    QBasicTimer ticker;
    QTime timeCounter;
    void startTicker(QObject *object)
    {
        if (!ticker.isActive())
            ticker.start(15, object);
        timeCounter.start();
    }
};

FlickCharm::FlickCharm(QObject *parent): QObject(parent)
{
    d = new FlickCharmPrivate;
}

FlickCharm::~FlickCharm()
{
    delete d;
}

void FlickCharm::activateOn(QWidget *widget)
{
    QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
    if (scrollArea) {
        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

        QWidget *viewport = scrollArea->viewport();

        viewport->installEventFilter(this);
        scrollArea->installEventFilter(this);

        d->flickData.remove(viewport);
        d->flickData[viewport] = new FlickData;
        d->flickData[viewport]->widget = widget;
        d->flickData[viewport]->state = FlickData::Steady;

        return;
    }

    QWebView *webView = qobject_cast<QWebView*>(widget);
    if (webView) {
        QWebFrame *frame = webView->page()->mainFrame();
        frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
        frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);

        webView->installEventFilter(this);

        d->flickData.remove(webView);
        d->flickData[webView] = new FlickData;
        d->flickData[webView]->widget = webView;
        d->flickData[webView]->state = FlickData::Steady;

        return;
    }

    qWarning() << "FlickCharm only works on QAbstractScrollArea (and derived classes)";
    qWarning() << "or QWebView (and derived classes)";
}

void FlickCharm::deactivateFrom(QWidget *widget)
{
    QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
    if (scrollArea) {
        QWidget *viewport = scrollArea->viewport();

        viewport->removeEventFilter(this);
        scrollArea->removeEventFilter(this);

        delete d->flickData[viewport];
        d->flickData.remove(viewport);

        return;
    }

    QWebView *webView = qobject_cast<QWebView*>(widget);
    if (webView) {
        webView->removeEventFilter(this);

        delete d->flickData[webView];
        d->flickData.remove(webView);

        return;
    }
}

static QPoint deaccelerate(const QPoint &speed, const int deltatime)
{
    const int deltaSpeed = deltatime;

    int x = speed.x();
    int y = speed.y();
    x = (x == 0) ? x : (x > 0) ? qMax(0, x - deltaSpeed) : qMin(0, x + deltaSpeed);
    y = (y == 0) ? y : (y > 0) ? qMax(0, y - deltaSpeed) : qMin(0, y + deltaSpeed);
    return QPoint(x, y);
}

bool FlickCharm::eventFilter(QObject *object, QEvent *event)
{
    if (!object->isWidgetType())
        return false;

    const QEvent::Type type = event->type();

    switch (type) {
    case QEvent::MouseButtonPress:
    case QEvent::MouseMove:
    case QEvent::MouseButtonRelease:
        break;
    case QEvent::MouseButtonDblClick: // skip double click
        return true;
    default:
        return false;
    }

    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    if (type == QEvent::MouseMove && mouseEvent->buttons() != Qt::LeftButton)
        return false;

    if (mouseEvent->modifiers() != Qt::NoModifier)
        return false;

    QWidget *viewport = qobject_cast<QWidget*>(object);
    FlickData *data = d->flickData.value(viewport);
    if (!viewport || !data || data->ignored.removeAll(event))
        return false;

    const QPoint mousePos = mouseEvent->pos();
    bool consumed = false;
    switch (data->state) {

    case FlickData::Steady:
        if (type == QEvent::MouseButtonPress) {
            consumed = true;
            data->pressPos = mousePos;
        } else if (type == QEvent::MouseButtonRelease) {
            consumed = true;
            QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
                                                  data->pressPos, Qt::LeftButton,
                                                  Qt::LeftButton, Qt::NoModifier);
            QMouseEvent *event2 = new QMouseEvent(QEvent::MouseButtonRelease,
                                                  data->pressPos, Qt::LeftButton,
                                                  Qt::LeftButton, Qt::NoModifier);

            data->ignored << event1;
            data->ignored << event2;
            QApplication::postEvent(object, event1);
            QApplication::postEvent(object, event2);
        } else if (type == QEvent::MouseMove) {
            consumed = true;
            data->scrollTo(mousePos);

            const QPoint delta = mousePos - data->pressPos;
            if (delta.x() > fingerAccuracyThreshold || delta.y() > fingerAccuracyThreshold)
                data->state = FlickData::ManualScroll;
        }
        break;

    case FlickData::ManualScroll:
        if (type == QEvent::MouseMove) {
            consumed = true;
            data->scrollTo(mousePos);
        } else if (type == QEvent::MouseButtonRelease) {
            consumed = true;
            data->state = FlickData::AutoScroll;
            data->lastPosValid = false;
            d->startTicker(this);
        }
        break;

    case FlickData::AutoScroll:
        if (type == QEvent::MouseButtonPress) {
            consumed = true;
            data->state = FlickData::AutoScrollAcceleration;
            data->waitingAcceleration = true;
            data->accelerationTimer.start();
            data->updateSpeed(mousePos);
            data->pressPos = mousePos;
        } else if (type == QEvent::MouseButtonRelease) {
            consumed = true;
            data->state = FlickData::Steady;
            data->resetSpeed();
        }
        break;

    case FlickData::AutoScrollAcceleration:
        if (type == QEvent::MouseMove) {
            consumed = true;
            data->updateSpeed(mousePos);
            data->accelerationTimer.start();
            if (data->speed.isNull())
                data->state = FlickData::ManualScroll;
        } else if (type == QEvent::MouseButtonRelease) {
            consumed = true;
            data->state = FlickData::AutoScroll;
            data->waitingAcceleration = false;
            data->lastPosValid = false;
        }
        break;
    default:
        break;
    }
    data->lastPos = mousePos;
    return true;
}

void FlickCharm::timerEvent(QTimerEvent *event)
{
    int count = 0;
    QHashIterator<QWidget*, FlickData*> item(d->flickData);
    while (item.hasNext()) {
        item.next();
        FlickData *data = item.value();
        if (data->state == FlickData::AutoScrollAcceleration
            && data->waitingAcceleration
            && data->accelerationTimer.elapsed() > 40) {
            data->state = FlickData::ManualScroll;
            data->resetSpeed();
        }
        if (data->state == FlickData::AutoScroll || data->state == FlickData::AutoScrollAcceleration) {
            const int timeElapsed = d->timeCounter.elapsed();
            const QPoint delta = (data->speed) * timeElapsed / 1000;
            bool hasScrolled = data->scrollWidget(delta.x(), delta.y());

            if (data->speed.isNull() || !hasScrolled)
                data->state = FlickData::Steady;
            else
                count++;
            data->speed = deaccelerate(data->speed, timeElapsed);
        }
    }

    if (!count)
        d->ticker.stop();
    else
        d->timeCounter.start();

    QObject::timerEvent(event);
}