diff options
author | Yoann Laissus <yoann.laissus@gmail.com> | 2010-06-30 18:36:49 +0200 |
---|---|---|
committer | Yoann Laissus <yoann.laissus@gmail.com> | 2010-08-08 21:08:45 +0200 |
commit | b94c146ef02f7e966251bb78cd124367c73e7d1d (patch) | |
tree | 669a6317a430dc6ae3f33cca1bc14109361ef88a /src | |
parent | Merge branch 'm155' (diff) | |
download | rekonq-b94c146ef02f7e966251bb78cd124367c73e7d1d.tar.xz |
Implement KHTML smooth scrolling for rekonq
Diffstat (limited to 'src')
-rw-r--r-- | src/rekonq.kcfg | 3 | ||||
-rw-r--r-- | src/settings/settings_appearance.ui | 17 | ||||
-rw-r--r-- | src/webview.cpp | 124 | ||||
-rw-r--r-- | src/webview.h | 17 |
4 files changed, 158 insertions, 3 deletions
diff --git a/src/rekonq.kcfg b/src/rekonq.kcfg index 1d6139ac..33a07751 100644 --- a/src/rekonq.kcfg +++ b/src/rekonq.kcfg @@ -73,6 +73,9 @@ <entry name="autoScroll" type="Bool"> <default>true</default> </entry> + <entry name="smoothScrolling" type="Bool"> + <default>true</default> + </entry> </group> diff --git a/src/settings/settings_appearance.ui b/src/settings/settings_appearance.ui index 7ec23484..f7dd4747 100644 --- a/src/settings/settings_appearance.ui +++ b/src/settings/settings_appearance.ui @@ -6,8 +6,8 @@ <rect> <x>0</x> <y>0</y> - <width>351</width> - <height>455</height> + <width>364</width> + <height>457</height> </rect> </property> <property name="windowTitle"> @@ -241,6 +241,19 @@ </property> </widget> </item> + <item> + <widget class="QCheckBox" name="kcfg_smoothScrolling"> + <property name="toolTip"> + <string>Scroll pages with an eye candy effect</string> + </property> + <property name="text"> + <string>Enable smooth scrolling</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> </layout> </widget> </item> diff --git a/src/webview.cpp b/src/webview.cpp index 188216f8..31ab70c8 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -32,6 +32,8 @@ // Auto Includes #include "rekonq.h" +#include <math.h> + // Local Includes #include "mainwindow.h" #include "mainview.h" @@ -73,6 +75,13 @@ WebView::WebView(QWidget* parent) , _HScrollSpeed(0) , _canEnableAutoScroll(true) , _isAutoScrollEnabled(false) + , smoothScroller(0) + , nbSteps(0) + , timer(new QTimer(this)) + , nbTicks(0) + , time(new QTime()) + , smoothScrolling(false) + , dy(0) { WebPage *page = new WebPage(this); setPage(page); @@ -105,6 +114,8 @@ WebView::WebView(QWidget* parent) // scrolling timer connect(_scrollTimer, SIGNAL(timeout()), this, SLOT(scrollFrameChanged())); _scrollTimer->setInterval(100); + timer->setInterval(16); + connect(timer, SIGNAL(timeout()), this, SLOT(scrollTick())); } @@ -119,6 +130,12 @@ WebView::~WebView() QString path = WebSnap::imagePathFromUrl(p->mainFrame()->url().toString()); QFile::remove(path); preview.save(path); + + if (smoothScrolling) + stopScrolling(); + + delete timer; + delete time; } @@ -525,9 +542,13 @@ void WebView::keyPressEvent(QKeyEvent *event) } + + + void WebView::wheelEvent(QWheelEvent *event) { - KWebView::wheelEvent(event); + if (!ReKonfig::smoothScrolling()) + KWebView::wheelEvent(event); // Sync with the zoom slider if (event->modifiers() == Qt::ControlModifier) @@ -545,6 +566,23 @@ void WebView::wheelEvent(QWheelEvent *event) emit zoomChanged((qreal)newFactor / 10); } + else if ( ReKonfig::smoothScrolling() ) + { + int numDegrees = event->delta() / 8; + int numSteps = numDegrees / 15; + + if ((numSteps > 0) != !bas) + stopScrolling(); + + if (numSteps > 0) + bas = false; + else + bas = true; + + setupSmoothScrolling( 100); + + return; + } } @@ -577,3 +615,87 @@ void WebView::scrollFrameChanged() if (x == 0 || x == page()->currentFrame()->scrollBarMaximum(Qt::Horizontal)) _HScrollSpeed = 0; } + +// Scroll chat to bottom +void WebView::setupSmoothScrolling(int posY) +{ + int ddy = qMax(steps ? abs(dy)/steps : 0,3); + + dy += posY; + + if (dy <= 0) + { + stopScrolling(); + return; + } + + steps = 8; + + if (dy / steps < ddy) + { + // Don't move slower than average 4px/step in minimum one direction + // This means fewer than normal steps + steps = (abs(dy)+ddy-1)/ddy; + if (steps < 1) + steps = 1; + } + + time->start(); + if (!smoothScrolling) + { + smoothScrolling = true; + timer->start(); + scrollTick(); + } +} + + +void WebView::scrollTick() +{ + if (dy == 0) + { + stopScrolling(); + return; + } + + if (steps < 1) + steps = 1; + + int takesteps = time->restart() / 16; + int scroll_y = 0; + + if (takesteps < 1) + takesteps = 1; + + if (takesteps > steps) + takesteps = steps; + + for(int i = 0; i < takesteps; i++) + { + int ddy = (dy / (steps+1)) * 2; + + // limit step to requested scrolling distance + if (abs(ddy) > abs(dy)) ddy = dy; + + // update remaining scroll + dy -= ddy; + scroll_y += ddy; + steps--; + } + + + + //page()->mainFrame()->setScrollPosition( QPoint( 0, frame ) ); + if (bas) + page()->mainFrame()->setScrollPosition( QPoint( 0, page()->mainFrame()->scrollPosition().y() + scroll_y ) ); + else + page()->mainFrame()->setScrollPosition( QPoint( 0, page()->mainFrame()->scrollPosition().y() - scroll_y ) ); +} + + +void WebView::stopScrolling() +{ + timer->stop(); + dy = 0; + smoothScrolling = false; +} diff --git a/src/webview.h b/src/webview.h index a4ba676c..cc5508cb 100644 --- a/src/webview.h +++ b/src/webview.h @@ -32,6 +32,8 @@ // Rekonq Includes #include "rekonq_defines.h" +#include <QTimeLine> + // KDE Includes #include <KWebView> @@ -71,6 +73,11 @@ private slots: void inspect(); void scrollFrameChanged(); + void scrollTick(); + + void setupSmoothScrolling(int posY); + + void stopScrolling(); signals: void loadUrl(const KUrl &, const Rekonq::OpenType &); @@ -85,6 +92,16 @@ private: int _HScrollSpeed; bool _canEnableAutoScroll; bool _isAutoScrollEnabled; + + QTimeLine *smoothScroller; + QTimer *timer; + int nbSteps; + int nbTicks; + bool bas; + QTime *time; + bool smoothScrolling; + int dy; + int steps; }; #endif |