summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2009-04-23 14:36:16 (GMT)
committerDenis Dzyubenko <denis.dzyubenko@nokia.com>2009-05-11 14:51:55 (GMT)
commite8f57e06eb21f51a6283f7a18d66aa8ca2c6b38a (patch)
tree387647a90b8f31afa4444d5603065eb93e3bc52c
parent01da5e343223fc973a8a88c3b825f9fc9b5fde42 (diff)
downloadQt-e8f57e06eb21f51a6283f7a18d66aa8ca2c6b38a.zip
Qt-e8f57e06eb21f51a6283f7a18d66aa8ca2c6b38a.tar.gz
Qt-e8f57e06eb21f51a6283f7a18d66aa8ca2c6b38a.tar.bz2
Added inertial to the browser demo.
-rw-r--r--examples/gestures/browser/webview.cpp30
-rw-r--r--examples/gestures/browser/webview.h2
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