diff options
Diffstat (limited to 'examples/gestures')
-rw-r--r-- | examples/gestures/browser/webview.cpp | 30 | ||||
-rw-r--r-- | examples/gestures/browser/webview.h | 2 |
2 files changed, 29 insertions, 3 deletions
diff --git a/examples/gestures/browser/webview.cpp b/examples/gestures/browser/webview.cpp index 38fb43d..95a96e7 100644 --- a/examples/gestures/browser/webview.cpp +++ b/examples/gestures/browser/webview.cpp @@ -316,10 +316,34 @@ bool WebView::event(QEvent *event) void WebView::gestureEvent(QGestureEvent *event) { if (const QGesture *g = event->gesture(Qt::PanGesture)) { - if (QWebFrame *frame = page()->mainFrame()) { - QPoint offset = g->pos() - g->lastPos(); - frame->setScrollPosition(frame->scrollPosition() - offset); + if (g->state() == Qt::GestureUpdated) { + if (QWebFrame *frame = page()->mainFrame()) { + QPoint offset = g->pos() - g->lastPos(); + frame->setScrollPosition(frame->scrollPosition() - offset); + } + speed = g->pos() - g->lastPos(); + } else if (g->state() == Qt::GestureStarted) { + startTimer(20); + } else { } event->accept(); } } + +static QPoint deaccelerate(const QPoint &speed, int a = 1, int max = 64) +{ + int x = qBound(-max, speed.x(), max); + int y = qBound(-max, speed.y(), max); + x = (x == 0) ? x : (x > 0) ? qMax(0, x - a) : qMin(0, x + a); + y = (y == 0) ? y : (y > 0) ? qMax(0, y - a) : qMin(0, y + a); + return QPoint(x, y); +} + +void WebView::timerEvent(QTimerEvent *event) +{ + speed = deaccelerate(speed); + if (speed.isNull()) + killTimer(event->timerId()); + if (QWebFrame *frame = page()->mainFrame()) + frame->setScrollPosition(frame->scrollPosition() - speed); +} diff --git a/examples/gestures/browser/webview.h b/examples/gestures/browser/webview.h index 037d117..15fb40c 100644 --- a/examples/gestures/browser/webview.h +++ b/examples/gestures/browser/webview.h @@ -99,6 +99,7 @@ public: protected: bool event(QEvent *event); void gestureEvent(QGestureEvent *event); + void timerEvent(QTimerEvent *event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void contextMenuEvent(QContextMenuEvent *event); @@ -116,6 +117,7 @@ private: QUrl m_initialUrl; int m_progress; WebPage *m_page; + QPoint speed; }; #endif |