Added custom context menu for web browser.
This commit is contained in:
parent
0505e08d70
commit
6055c95b28
@ -1,7 +1,12 @@
|
||||
#include <QStyleOptionFrameV3>
|
||||
#include <QAction>
|
||||
#include <QMenu>
|
||||
#include <QWebFrame>
|
||||
#include <QContextMenuEvent>
|
||||
|
||||
#include "core/basewebpage.h"
|
||||
#include "gui/basewebview.h"
|
||||
#include "gui/themefactory.h"
|
||||
|
||||
|
||||
BaseWebView::BaseWebView(QWidget *parent)
|
||||
@ -30,6 +35,48 @@ void BaseWebView::displayErrorPage() {
|
||||
setHtml("error", url());
|
||||
}
|
||||
|
||||
void BaseWebView::contextMenuEvent(QContextMenuEvent *event) {
|
||||
QMenu context_menu(tr("Web browser"), this);
|
||||
QMenu image_submenu(tr("Image"), &context_menu);
|
||||
QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(event->pos());
|
||||
|
||||
// Obtain needed actions.
|
||||
QAction *action_reload = pageAction(QWebPage::Reload);
|
||||
action_reload->setText(tr("Reload web page"));
|
||||
action_reload->setToolTip(tr("Reload current web page"));
|
||||
context_menu.addAction(action_reload);
|
||||
|
||||
if (hit_result.linkUrl().isValid()) {
|
||||
QAction *action_copylink = pageAction(QWebPage::CopyLinkToClipboard);
|
||||
action_copylink->setText(tr("Copy link url"));
|
||||
action_copylink->setToolTip(tr("Copy link url to clipboard"));
|
||||
action_copylink->setIcon(ThemeFactory::fromTheme("edit-copy"));
|
||||
context_menu.addAction(action_copylink);
|
||||
}
|
||||
|
||||
if (!hit_result.pixmap().isNull()) {
|
||||
context_menu.addMenu(&image_submenu);
|
||||
|
||||
QAction *action_copyimage = pageAction(QWebPage::CopyImageToClipboard);
|
||||
action_copyimage->setText(tr("Copy image"));
|
||||
action_copyimage->setToolTip(tr("Copy image to clipboard"));
|
||||
action_copyimage->setIcon(ThemeFactory::fromTheme("insert-image"));
|
||||
image_submenu.addAction(action_copyimage);
|
||||
}
|
||||
|
||||
if (hit_result.imageUrl().isValid()) {
|
||||
QAction *action_copyimageurl = pageAction(QWebPage::CopyImageUrlToClipboard);
|
||||
action_copyimageurl->setText(tr("Copy image url"));
|
||||
action_copyimageurl->setToolTip(tr("Copy image url to clipboard"));
|
||||
action_copyimageurl->setIcon(ThemeFactory::fromTheme("edit-copy"));
|
||||
image_submenu.addAction(action_copyimageurl);
|
||||
}
|
||||
|
||||
// Display the menu.
|
||||
context_menu.exec(mapToGlobal(event->pos()));
|
||||
context_menu.deleteLater();
|
||||
}
|
||||
|
||||
void BaseWebView::paintEvent(QPaintEvent *event) {
|
||||
QWebView::paintEvent(event);
|
||||
|
||||
|
@ -29,6 +29,9 @@ class BaseWebView : public QWebView {
|
||||
// Does additional painting.
|
||||
void paintEvent(QPaintEvent *event);
|
||||
|
||||
// Provides custom context menu.
|
||||
void contextMenuEvent(QContextMenuEvent *event);
|
||||
|
||||
private:
|
||||
BaseWebPage *m_page;
|
||||
};
|
||||
|
@ -116,6 +116,10 @@ QList<WebBrowser *> WebBrowser::runningWebBrowsers() {
|
||||
return m_runningWebBrowsers;
|
||||
}
|
||||
|
||||
void WebBrowser::setNavigationBarVisible(bool visible) {
|
||||
m_toolBar->setVisible(visible);
|
||||
}
|
||||
|
||||
WebBrowserNetworkAccessManager *WebBrowser::globalNetworkManager() {
|
||||
if (m_networkManager.isNull()) {
|
||||
// TODO: Not sure if qApp is needed here.
|
||||
|
@ -30,6 +30,9 @@ class WebBrowser : public QWidget {
|
||||
// Returns list of all running web browsers.
|
||||
static QList<WebBrowser*> runningWebBrowsers();
|
||||
|
||||
public slots:
|
||||
void setNavigationBarVisible(bool visible);
|
||||
|
||||
protected:
|
||||
// Creates necessary connections.
|
||||
void createConnections();
|
||||
|
Loading…
x
Reference in New Issue
Block a user