diff options
Diffstat (limited to 'src/webview.cpp')
-rw-r--r-- | src/webview.cpp | 128 |
1 files changed, 122 insertions, 6 deletions
diff --git a/src/webview.cpp b/src/webview.cpp index 9e06c9da..2fe3775b 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -52,18 +52,27 @@ #include <QtGui/QClipboard> #include <QtGui/QKeyEvent> #include <QtGui/QAction> +#include <QtCore/QTimer> WebView::WebView(QWidget* parent) : QWebView(parent) , m_page(new WebPage(this)) , m_progress(0) + , m_scrollTimer(new QTimer(this)) + , m_scrollDirection(WebView::NoScroll) + , m_scrollSpeedVertical(0) + , m_scrollSpeedHorizontal(0) + { setPage(m_page); connect(page(), SIGNAL(statusBarMessage(const QString&)), this, SLOT(setStatusBarText(const QString&))); connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotUpdateProgress(int))); connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool))); + + connect(m_scrollTimer, SIGNAL(timeout()), this, SLOT(scrollFrameChanged())); + m_scrollTimer->setInterval(50); } @@ -106,6 +115,7 @@ void WebView::setStatusBarText(const QString &string) m_statusBarText = string; } + void WebView::contextMenuEvent(QContextMenuEvent *event) { QWebHitTestResult result = page()->mainFrame()->hitTestContent(event->pos()); @@ -135,6 +145,8 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) a = pageAction(QWebPage::CopyLinkToClipboard); a->setIcon(KIcon("edit-copy")); menu.addAction(a); + + menu.addSeparator(); } // is content editable && selected? Add CUT @@ -153,6 +165,10 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) a = pageAction(QWebPage::Copy); a->setIcon(KIcon("edit-copy")); a->setShortcut(KStandardShortcut::copy().primary()); + if(!result.isContentEditable()) // "Cut" "Copy Text" "Paste" is ugly. Don't add "text" with cut/paste + a->setText(i18n("Copy Text")); + else + a->setText(i18n("Copy")); menu.addAction(a); } @@ -168,7 +184,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) // is content selected? Add SEARCH actions if(result.isContentSelected()) { - KActionMenu *searchMenu = new KActionMenu(i18n("Search with"), this); + KActionMenu *searchMenu = new KActionMenu(KIcon("edit-find"), i18n("Search with"), this); KConfig config("kuriikwsfilterrc"); //Share with konqueror KConfigGroup cg = config.group("General"); @@ -196,12 +212,12 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) } } } - + if (!searchMenu->menu()->isEmpty()) { menu.addAction(searchMenu); } - + menu.addSeparator(); // TODO Add translate, show translation } @@ -233,7 +249,10 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) { // page action QString text = selectedText(); - if (text.startsWith( QLatin1String("http://") ) || text.startsWith( QLatin1String("https://") ) || text.startsWith( QLatin1String("www.") ) ) + if (text.startsWith( QLatin1String("http://") ) + || text.startsWith( QLatin1String("https://") ) + || text.startsWith( QLatin1String("www.") ) + ) { QString truncatedURL = text; if (text.length() > 18) @@ -322,8 +341,82 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) } +void WebView::stopScrollAnimation() +{ + m_scrollTimer->stop(); + m_scrollSpeedVertical = 0; + m_scrollSpeedHorizontal = 0; + m_scrollDirection = WebView::NoScroll; +} + + +void WebView::startScrollAnimation(ScrollDirection direction) +{ + // if no scrollspeed, set the requested direction, otherwise it's just a slowdown or speedup + if (m_scrollSpeedVertical == 0 && (direction == WebView::Up || direction == WebView::Down)) + m_scrollDirection |= direction; + if (m_scrollSpeedHorizontal == 0 && (direction == WebView::Left || direction == WebView::Right)) + m_scrollDirection |= direction; + + // update scrollspeed + switch (direction) + { + case WebView::Up: + --m_scrollSpeedVertical; + break; + case WebView::Down: + ++m_scrollSpeedVertical; + break; + case WebView::Left: + --m_scrollSpeedHorizontal; + break; + case WebView::Right: + ++m_scrollSpeedHorizontal; + break; + default: + break; + } + + if (!m_scrollTimer->isActive()) + m_scrollTimer->start(); + + return; +} + + +void WebView::scrollFrameChanged() +{ + // clear finished scrolling + if (m_scrollSpeedVertical == 0) + m_scrollDirection &= ~WebView::Up | ~WebView::Down; + if (m_scrollSpeedHorizontal == 0) + m_scrollDirection &= ~WebView::Left | ~WebView::Right; + + // all scrolling finished + if (m_scrollDirection == WebView::NoScroll) + { + m_scrollTimer->stop(); + return; + } + + // do the scrolling + page()->currentFrame()->scroll(m_scrollSpeedHorizontal, m_scrollSpeedVertical); + + // check if we reached the end + int y = page()->currentFrame()->scrollPosition().y(); + int x = page()->currentFrame()->scrollPosition().x(); + + if (y == 0 || y == page()->currentFrame()->scrollBarMaximum(Qt::Vertical)) + m_scrollSpeedVertical = 0; + if (x == 0 || x == page()->currentFrame()->scrollBarMaximum(Qt::Horizontal)) + m_scrollSpeedHorizontal = 0; +} + + void WebView::mousePressEvent(QMouseEvent *event) { + stopScrollAnimation(); + m_page->m_pressedButtons = event->buttons(); m_page->m_keyboardModifiers = event->modifiers(); @@ -352,6 +445,8 @@ void WebView::mouseMoveEvent(QMouseEvent *event) void WebView::wheelEvent(QWheelEvent *event) { + stopScrollAnimation(); + if (QApplication::keyboardModifiers() & Qt::ControlModifier) { int numDegrees = event->delta() / 8; @@ -395,7 +490,7 @@ void WebView::viewImage(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifier { KAction *a = qobject_cast<KAction*>(sender()); KUrl url(a->data().toUrl()); - + if (modifiers & Qt::ControlModifier || buttons == Qt::MidButton) { Application::instance()->loadUrl(url, Rekonq::SettingOpenTab); @@ -423,7 +518,6 @@ void WebView::openLinkInNewTab() } -// HACK short term hack: remove this function, unuseful in kdewebkit porting void WebView::keyPressEvent(QKeyEvent *event) { if ((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_C)) @@ -431,5 +525,27 @@ void WebView::keyPressEvent(QKeyEvent *event) triggerPageAction(QWebPage::Copy); return; } + + if (event->modifiers() == Qt::ShiftModifier) + { + switch (event->key()) + { + case Qt::Key_Down: + startScrollAnimation(WebView::Down); + return; + case Qt::Key_Up: + startScrollAnimation(WebView::Up); + return; + case Qt::Key_Left: + startScrollAnimation(WebView::Left); + return; + case Qt::Key_Right: + startScrollAnimation(WebView::Right); + return; + default: + break; + } + } + QWebView::keyPressEvent(event); } |