Skip to content

Commit

Permalink
Support Ctrl/Shift+click or middle click for opening links in new tabs
Browse files Browse the repository at this point in the history
Fixes #403. No 'open in background' support for now.
  • Loading branch information
trollixx committed Sep 19, 2015
1 parent 65600e7 commit 98f7642
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/ui/widgets/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <QApplication>
#include <QWheelEvent>

#ifndef USE_WEBENGINE
#include <QWebFrame>
#endif

WebView::WebView(QWidget *parent) :
QWebView(parent)
{
Expand Down Expand Up @@ -64,13 +68,44 @@ void WebView::mousePressEvent(QMouseEvent *event)
forward();
event->accept();
break;
#ifndef USE_WEBENGINE
case Qt::LeftButton:
if (!(event->modifiers() & Qt::ControlModifier || event->modifiers() & Qt::ShiftModifier))
break;
case Qt::MiddleButton:
m_clickedLink = clickedLink(event->pos());
if (m_clickedLink.isValid())
event->accept();
break;
#endif
default:
break;
}

QWebView::mousePressEvent(event);
}

#ifndef USE_WEBENGINE
void WebView::mouseReleaseEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton:
if (!(event->modifiers() & Qt::ControlModifier || event->modifiers() & Qt::ShiftModifier))
break;
case Qt::MiddleButton:
if (m_clickedLink == clickedLink(event->pos()) && m_clickedLink.isValid()) {
QWebView *webView = createWindow(QWebPage::WebBrowserWindow);
webView->setUrl(m_clickedLink);
event->accept();
}
break;
default:
break;
}
QWebView::mouseReleaseEvent(event);
}
#endif

void WebView::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() & Qt::ControlModifier) {
Expand All @@ -81,6 +116,17 @@ void WebView::wheelEvent(QWheelEvent *event)
}
}

#ifndef USE_WEBENGINE
QUrl WebView::clickedLink(const QPoint &pos) const
{
QWebFrame *frame = page()->frameAt(pos);
if (!frame)
return QUrl();

return frame->hitTestContent(pos).linkUrl();
}
#endif

void WebView::updateZoomFactor()
{
setZoomFactor(1 + m_zoomFactor / 10.);
Expand Down
7 changes: 7 additions & 0 deletions src/ui/widgets/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ class WebView : public QWebView
protected:
QWebView *createWindow(QWebPage::WebWindowType type) override;
void mousePressEvent(QMouseEvent *event) override;
#ifndef USE_WEBENGINE
void mouseReleaseEvent(QMouseEvent *event) override;
#endif
void wheelEvent(QWheelEvent *event) override;

private:
#ifndef USE_WEBENGINE
QUrl clickedLink(const QPoint &pos) const;
QUrl m_clickedLink;
#endif
void updateZoomFactor();

int m_zoomFactor = 0;
Expand Down

0 comments on commit 98f7642

Please sign in to comment.