summaryrefslogtreecommitdiffstats
path: root/testapp/qwebviewkineticscroller.cpp
blob: 3d3790bfc7480dacdee96aa7a3a49a293346af31 (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module 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 <qwebviewkineticscroller.h>
#include <QApplication>
#include <QGraphicsView>
#include <QWebView>
#include <QWebFrame>
#include <QScrollBar>
#include <QPointF>
#include <QPointer>
#include <QMouseEvent>
#include <QTouchEvent>
#if QT_VERSION < 0x040700
#  include <QTime>
#else
#  include <QElapsedTimer>
#endif

#include <QtDebug>

QT_BEGIN_NAMESPACE

bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event);

class QWebViewKineticScrollerPrivate
{
public:
    QWebViewKineticScroller *q_ptr;

    QWebView *web;
    bool ignoreEvents;
    bool touchActive;
    bool pressed;
    QPointF fractionalPosition;
    QPointer<QWebFrame> scrollFrame;
    Qt::ScrollBarPolicy oldHorizontalSBP;
    Qt::ScrollBarPolicy oldVerticalSBP;

#if QT_VERSION < 0x040700
    QTime timer;
#else
    QElapsedTimer timer;
#endif

    QWebViewKineticScrollerPrivate()
        : q_ptr(0)
        , web(0)
        , ignoreEvents(false)
        , touchActive(false)
        , pressed(false)
    {}

    void init()
    {
        timer.start();
    }

    void sendEvent(QWidget *w, QEvent *e)
    {
        ignoreEvents = true;
        qt_sendSpontaneousEvent(w, e);
        ignoreEvents = false;
    }

    QWebFrame *currentFrame() const;
    QWebFrame *scrollingFrameAt(const QPointF &pos) const;
};

/*!
 * The QWebViewKineticScroller class implements the QKineticScroller for the QWebView
 */
QWebViewKineticScroller::QWebViewKineticScroller()
    : QObject()
    , QKineticScroller()
    , d_ptr(new QWebViewKineticScrollerPrivate())
{
    Q_D(QWebViewKineticScroller);
    d->q_ptr = this;
    d->init();
}

/*!
    Destroys the scroller.
*/
QWebViewKineticScroller::~QWebViewKineticScroller()
{ }

void QWebViewKineticScroller::setWidget(QWebView *widget)
{
    Q_D(QWebViewKineticScroller);

    if (d->web) {
        d->web->removeEventFilter(this);
        d->web->setAttribute(Qt::WA_AcceptTouchEvents, false);

        QWebFrame *mainFrame = d->web->page()->mainFrame();
        mainFrame->setScrollBarPolicy(Qt::Vertical, d->oldVerticalSBP);
        mainFrame->setScrollBarPolicy(Qt::Horizontal, d->oldHorizontalSBP);
    }

    d->scrollFrame = 0;
    reset();
    d->web = widget;
    d->fractionalPosition = QPointF();
    setParent(d->web);

    if (d->web) {
        QWebFrame *mainFrame = d->web->page()->mainFrame();
        d->oldVerticalSBP = mainFrame->scrollBarPolicy(Qt::Vertical);
        d->oldHorizontalSBP = mainFrame->scrollBarPolicy(Qt::Horizontal);
        mainFrame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
        mainFrame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
        d->web->setAttribute(Qt::WA_AcceptTouchEvents, true);
        d->web->installEventFilter(this);
    }
}

bool QWebViewKineticScroller::eventFilter(QObject *o, QEvent *e)
{
    Q_D(QWebViewKineticScroller);

    qint64 timestamp = d->timer.elapsed();
    bool res = false;

    if (d->web && (o == d->web) && !d->ignoreEvents && d->web->isEnabled() && isEnabled()) {
        switch (e->type()) {
        case QEvent::TouchBegin:
        case QEvent::TouchUpdate:
        case QEvent::TouchEnd: {
                qWarning("TOUCH%s", e->type() == QEvent::TouchBegin ? "BEGIN" : (e->type() == QEvent::TouchEnd ? "END" : "UPDATE"));
            Input type = (e->type() == QEvent::TouchBegin) ? InputPress :
                        ((e->type() == QEvent::TouchEnd) ? InputRelease : InputMove);
            QTouchEvent *te = static_cast<QTouchEvent *>(e);
            int idx = (te->deviceType() == QTouchEvent::TouchScreen) ? 0 : 1;

            if (te->touchPoints().count() == (idx + 1)) {
                QPointF p = te->touchPoints().at(idx).pos();
                if (type == InputPress) {
                    // remember the frame where the button was pressed
                    QWebFrame *hitFrame = d->scrollingFrameAt(p);
                    if (hitFrame)
                        d->scrollFrame = hitFrame;
                }

                res = handleInput(type, p, timestamp);
                if (type == InputPress) {
                    d->touchActive = true;
                    res = true; // we need to swallow this event, since WebKit would reject it otherwise
                } else if (type == InputRelease) {
                    d->touchActive = false;
                }
                qWarning("   TOUCH%s (%d): %d", type == InputPress ? "BEGIN" : (type == InputRelease ? "END" : "UPDATE"), d->touchActive, res);
            }
            break;
        }
        case QEvent::Wheel:
            // the two-finger gesture on the Mac is mapped to wheel events by default
            qWarning("WHEEL");
            res = d->touchActive;
            break;

        case QEvent::MouseButtonPress:
            // re-install the event filter so that we get the mouse release before all other filters.
            // this is an evil hack, but hard to work around without prioritized event filters.
            d->web->removeEventFilter(this);
            d->web->installEventFilter(this);            
            // fall through

        case QEvent::MouseButtonDblClick:
        case QEvent::MouseMove:
        case QEvent::MouseButtonRelease:
            if (!d->touchActive) {
                Input type = (e->type() == QEvent::MouseButtonRelease) ? InputRelease :
                            ((e->type() == QEvent::MouseMove) ? InputMove : InputPress);
                QPointF p = static_cast<QMouseEvent *>(e)->posF();
                
                if (type == InputPress) {
                    // remember the frame where the button was pressed
                    QWebFrame *hitFrame = d->scrollingFrameAt(p);
                    if (hitFrame)
                        d->scrollFrame = hitFrame;
                    d->pressed = true;
                } else if (type == InputRelease) {
                    d->pressed = false;
                }

                if (type != InputMove || d->pressed)
                    res = handleInput(type, p, timestamp);
            }
            break;

        default:
            break;
        }
    }

    if (res)
        e->accept();
    return res;
}


bool QWebViewKineticScroller::canStartScrollingAt(const QPointF & /*pos*/) const
{
    return true;
}

void QWebViewKineticScroller::cancelPress(const QPointF & /*pressPos*/)
{
}

QWebFrame *QWebViewKineticScrollerPrivate::currentFrame() const
{
    if (web && scrollFrame)
        return scrollFrame;
    else if (web)
        return web->page()->mainFrame();
    else
        return 0;
}

// returns the innermost frame at the given position that can be scrolled
QWebFrame *QWebViewKineticScrollerPrivate::scrollingFrameAt(const QPointF &pos) const
{
    QWebFrame *hitFrame = 0;
    if (web) {
        QWebFrame *mainFrame = web->page()->mainFrame();
        hitFrame = mainFrame->hitTestContent(pos.toPoint()).frame();
        QSize range = hitFrame->contentsSize() - hitFrame->geometry().size();
        
        while (hitFrame && range.width() <= 1 && range.height() <= 1)
            hitFrame = hitFrame->parentFrame();
    }
    return hitFrame;
}

QSizeF QWebViewKineticScroller::viewportSize() const
{
    Q_D(const QWebViewKineticScroller);

    return d->web ? d->web->page()->viewportSize() : QSize();
}

QPointF QWebViewKineticScroller::maximumContentPosition() const
{
    Q_D(const QWebViewKineticScroller);
    
    QWebFrame *frame = d->currentFrame();
    QSize s = frame ? frame->contentsSize() - frame->geometry().size() : QSize();
    return QPointF(qMax(0, s.width()), qMax(0, s.height()));
}

QPointF QWebViewKineticScroller::contentPosition() const
{
    Q_D(const QWebViewKineticScroller);

    QWebFrame *frame = d->currentFrame();
    return frame ? QPointF(frame->scrollPosition()) + d->fractionalPosition : QPointF();
}

void QWebViewKineticScroller::setContentPosition(const QPointF &p, const QPointF & /*overshoot*/)
{
    Q_D(QWebViewKineticScroller);
    
    QWebFrame *frame = d->currentFrame();
    if (frame) {
        QPoint pint(int(p.x()), int(p.y()));
        frame->setScrollPosition(pint);
        d->fractionalPosition = p - QPointF(pint);
    }
}

QT_END_NAMESPACE