Commit 495d8377 authored by Andrea Diamantini's avatar Andrea Diamantini
Browse files

Guess if url is on target blank and eventually show a describing icon

We show "window-new" or blank target links and "mail-message-new"
on mailto ones. The same as Konqueror does :)

Also, based on the hovering guess, shown different contextual menus

BUG:237095
BUG:303911
parent 3b7a1ffa
......@@ -221,6 +221,8 @@ TARGET_LINK_LIBRARIES ( kdeinit_rekonq
${KDE4_KDEUI_LIBS}
${KDE4_KIO_LIBS}
${KDE4_KPARTS_LIBS}
# ---------------------------
${X11_X11_LIB}
)
......
......@@ -57,6 +57,7 @@
#include <QFile>
#include <QTimer>
#include <QBitmap>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QLabel>
......@@ -65,6 +66,11 @@
#include <QWebHistory>
#include <QNetworkRequest>
#ifdef Q_WS_X11
#include <X11/Xlib.h>
#include <fixx11h.h>
#endif
WebView::WebView(QWidget* parent)
: KWebView(parent, false)
......@@ -80,6 +86,7 @@ WebView::WebView(QWidget* parent)
, m_isViewSmoothScrolling(false)
, m_accessKeysPressed(false)
, m_accessKeysActive(false)
, _isExternalLinkHovered(false)
{
// loadUrl signal
connect(this, SIGNAL(loadUrl(KUrl, Rekonq::OpenType)), rApp, SLOT(loadUrl(KUrl, Rekonq::OpenType)));
......@@ -256,15 +263,25 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
sendByMailAction->setData(result.linkUrl());
sendByMailAction->setText(i18n("Share link"));
a = new KAction(KIcon("tab-new"), i18n("Open in New &Tab"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(a);
if (_isExternalLinkHovered)
{
a = new KAction(KIcon("view-close"), i18n("Open &Here"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkHere()));
menu.addAction(a);
}
else
{
a = new KAction(KIcon("tab-new"), i18n("Open in New &Tab"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(a);
a = new KAction(KIcon("window-new"), i18n("Open in New &Window"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
menu.addAction(a);
a = new KAction(KIcon("window-new"), i18n("Open in New &Window"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
menu.addAction(a);
}
menu.addSeparator();
......@@ -347,12 +364,14 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
truncatedUrl.truncate(truncateSize);
truncatedUrl += QL1S("...");
}
//open selected text url in a new tab
// open selected text url in a new tab
QAction * const openInNewTabAction = new KAction(KIcon("tab-new"), i18n("Open '%1' in New Tab", truncatedUrl), this);
openInNewTabAction->setData(QUrl(urlLikeText));
connect(openInNewTabAction, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(openInNewTabAction);
//open selected text url in a new window
// open selected text url in a new window
QAction * const openInNewWindowAction = new KAction(KIcon("window-new"), i18n("Open '%1' in New Window", truncatedUrl), this);
openInNewWindowAction->setData(QUrl(urlLikeText));
connect(openInNewWindowAction, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
......@@ -496,6 +515,8 @@ void WebView::mouseMoveEvent(QMouseEvent *event)
{
QPoint mousePos = event->pos();
guessHoveredLink(mousePos);
if (m_isViewAutoScrolling)
{
QPoint r = mousePos - m_clickPos;
......@@ -628,12 +649,12 @@ void WebView::slotCopyImageLocation()
}
void WebView::openLinkInNewWindow()
void WebView::openLinkHere()
{
KAction *a = qobject_cast<KAction*>(sender());
KUrl url(a->data().toUrl());
emit loadUrl(url, Rekonq::NewWindow);
emit loadUrl(url, Rekonq::CurrentTab);
}
......@@ -646,6 +667,14 @@ void WebView::openLinkInNewTab()
}
void WebView::openLinkInNewWindow()
{
KAction *a = qobject_cast<KAction*>(sender());
KUrl url(a->data().toUrl());
emit loadUrl(url, Rekonq::NewWindow);
}
void WebView::bookmarkLink()
{
KAction *a = qobject_cast<KAction*>(sender());
......@@ -1271,3 +1300,97 @@ void WebView::mouseReleaseEvent(QMouseEvent *event)
QWebView::mouseReleaseEvent(event);
}
void WebView::guessHoveredLink(QPoint p)
{
QWebHitTestResult hitTest = page()->mainFrame()->hitTestContent(p);
const QUrl url = hitTest.linkUrl();
bool emptyUrl = url.isEmpty();
if (!_isExternalLinkHovered && emptyUrl)
return;
// if url is empty, reset
if (emptyUrl)
{
kDebug() << "LINK EMPTY";
//set usual link icon
if (!_iconLabel.isNull())
{
delete _iconLabel.data();
_iconLabel.clear();
}
_isExternalLinkHovered = false;
return;
}
QWebFrame *frame = hitTest.linkTargetFrame();
if (!frame && _isExternalLinkHovered)
return;
if (url.scheme() == QL1S("mailto"))
{
kDebug() << "MAIL TO";
#ifdef Q_WS_X11
if (!_iconLabel.isNull())
{
delete _iconLabel.data();
_iconLabel.clear();
}
_iconLabel = new QLabel(0, Qt::X11BypassWindowManagerHint);
XSetWindowAttributes attr;
attr.save_under = True;
XChangeWindowAttributes(QX11Info::display(), _iconLabel.data()->winId(), CWSaveUnder, &attr);
QPixmap icon_pixmap = KIcon("mail-message-new").pixmap(16);
_iconLabel.data()->resize(icon_pixmap.width(), icon_pixmap.height());
_iconLabel.data()->setMask(icon_pixmap.createMaskFromColor(Qt::transparent));
_iconLabel.data()->setPixmap(icon_pixmap);
_iconLabel.data()->update();
QPoint c = QCursor::pos();
_iconLabel.data()->move(c.x() + 15, c.y() + 15);
XRaiseWindow(QX11Info::display(), _iconLabel.data()->winId());
QApplication::flush();
_iconLabel.data()->show();
#endif
_isExternalLinkHovered = true;
return;
}
if (!frame)
{
kDebug() << "LINK OUT";
// set external link icon
#ifdef Q_WS_X11
if (!_iconLabel.isNull())
{
delete _iconLabel.data();
_iconLabel.clear();
}
_iconLabel = new QLabel(0, Qt::X11BypassWindowManagerHint);
XSetWindowAttributes attr;
attr.save_under = True;
XChangeWindowAttributes(QX11Info::display(), _iconLabel.data()->winId(), CWSaveUnder, &attr);
QPixmap icon_pixmap = KIcon("window-new").pixmap(16);
_iconLabel.data()->resize(icon_pixmap.width(), icon_pixmap.height());
_iconLabel.data()->setMask(icon_pixmap.createMaskFromColor(Qt::transparent));
_iconLabel.data()->setPixmap(icon_pixmap);
_iconLabel.data()->update();
QPoint c = QCursor::pos();
_iconLabel.data()->move(c.x() + 15, c.y() + 15);
XRaiseWindow(QX11Info::display(), _iconLabel.data()->winId());
QApplication::flush();
_iconLabel.data()->show();
#endif
_isExternalLinkHovered = true;
}
}
......@@ -94,8 +94,10 @@ private Q_SLOTS:
void printFrame();
void openLinkInNewWindow();
void openLinkHere();
void openLinkInNewTab();
void openLinkInNewWindow();
void bookmarkLink();
void sendByMail();
......@@ -118,6 +120,7 @@ private Q_SLOTS:
void blockImage();
void guessHoveredLink(QPoint);
Q_SIGNALS:
void loadUrl(const KUrl &, const Rekonq::OpenType &);
......@@ -154,6 +157,9 @@ private:
QHash<QChar, QWebElement> m_accessKeyNodes;
bool m_accessKeysPressed;
bool m_accessKeysActive;
bool _isExternalLinkHovered;
QWeakPointer<QLabel> _iconLabel;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment