aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-07-11 14:15:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-07-11 14:15:39 +0200
commitfddfe065fe1ed91a4883ee93d742f3340a716160 (patch)
tree214bd2a67455b1263856b3a36a3ac5d2f0f6655d
parentAdBlock rule testing (diff)
downloadsmolbote-fddfe065fe1ed91a4883ee93d742f3340a716160.tar.xz
Add custom WebView context menu
-rw-r--r--src/webengine/webview.cpp141
1 files changed, 115 insertions, 26 deletions
diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp
index 9380b13..a234f3a 100644
--- a/src/webengine/webview.cpp
+++ b/src/webengine/webview.cpp
@@ -11,12 +11,30 @@
#include "webpage.h"
#include "webprofile.h"
#include "widgets/pagetoolsmenu.h"
+#include <QContextMenuEvent>
#include <QDialog>
+#include <QSlider>
#include <QStatusBar>
+#include <QToolButton>
#include <QVBoxLayout>
-#include <QContextMenuEvent>
+#include <QWebEngineContextMenuData>
+#include <QWebEngineHistoryItem>
#include <QWidgetAction>
-#include <QSlider>
+#include "profilemanager.h"
+
+inline QAction *historyAction(QWebEngineView *view, const QWebEngineHistoryItem &item)
+{
+ QAction *action = new QAction(view);
+ if(item.title().isEmpty())
+ action->setText(item.url().toString());
+ else
+ action->setText(QObject::tr("%1 (%2)").arg(item.title(), item.url().toString()));
+
+ QObject::connect(action, &QAction::triggered, view, [view, item]() {
+ view->history()->goToItem(item);
+ });
+ return action;
+}
WebView::WebView(WebProfile *profile, QWidget *parent)
: QWebEngineView(parent)
@@ -99,32 +117,103 @@ WebView *WebView::createWindow(QWebEnginePage::WebWindowType type)
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
- QMenu *menu = page()->createStandardContextMenu();
-
- // zoom widget
- menu->addSeparator();
- {
- auto *zoomSlider = new QSlider(Qt::Horizontal);
- zoomSlider->setMinimum(5);
- zoomSlider->setMaximum(50);
- zoomSlider->setValue(10);
-
- auto *zoomAction = menu->addAction(tr("Zoom: %1x").arg(zoomFactor()));
- connect(zoomAction, &QAction::triggered, this, [zoomSlider]() {
- zoomSlider->setValue(10);
- });
-
- connect(zoomSlider, &QSlider::valueChanged, this, [this, zoomAction](int value) {
- zoomAction->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10));
- setZoomFactor(static_cast<qreal>(value) / 10);
- });
-
- auto *zoomWidgetAction = new QWidgetAction(this);
- zoomWidgetAction->setDefaultWidget(zoomSlider);
-
- menu->addAction(zoomWidgetAction);
+ QMenu *menu = nullptr;
+ const auto ctxdata = page()->contextMenuData();
+
+ if(ctxdata.mediaType() == QWebEngineContextMenuData::MediaTypeNone) {
+ menu = new QMenu(this);
+
+ // navigation
+ {
+ auto *backMenu = new QMenu(tr("Back"), this);
+ if(!history()->canGoBack()) {
+ backMenu->setEnabled(false);
+ } else {
+ connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
+ backMenu->clear();
+ const auto backItems = history()->backItems(10);
+ for(const QWebEngineHistoryItem &item : backItems) {
+ backMenu->addAction(historyAction(this, item));
+ }
+ });
+ }
+ menu->addMenu(backMenu);
+
+ auto *forwardMenu = new QMenu(tr("Forward"), this);
+ if(!history()->canGoForward()) {
+ forwardMenu->setEnabled(false);
+ } else {
+ connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
+ forwardMenu->clear();
+ const auto forwardItems = history()->forwardItems(10);
+ for(const QWebEngineHistoryItem &item : forwardItems) {
+ forwardMenu->addAction(historyAction(this, item));
+ }
+ });
+ }
+ menu->addMenu(forwardMenu);
+
+ connect(menu->addAction(tr("Reload")), &QAction::triggered, this, [this]() {
+ page()->triggerAction(QWebEnginePage::Reload);
+ });
+
+ connect(menu->addAction(tr("Reload and bypass Cache")), &QAction::triggered, this, [this]() {
+ page()->triggerAction(QWebEnginePage::ReloadAndBypassCache);
+ });
+ }
+
+ if(!ctxdata.linkUrl().isEmpty()) {
+ menu->addSeparator();
+ connect(menu->addAction(tr("Open link in new tab")), &QAction::triggered, this, [this, ctxdata]() {
+ createWindow(QWebEnginePage::WebBrowserTab)->load(ctxdata.linkUrl());
+ });
+
+ auto *newTabMenu = new QMenu(tr("Open link in new tab with profile"), this);
+ ProfileIterator it(ProfileManager::profileList());
+ while(it.hasNext()) {
+ it.next();
+ connect(newTabMenu->addAction(it.key()), &QAction::triggered, this, [this, ctxdata, it]() {
+ auto *view = createWindow(QWebEnginePage::WebBrowserTab);
+ view->setProfile(it.value());
+ view->load(ctxdata.linkUrl());
+ });
+ }
+ menu->addMenu(newTabMenu);
+
+ connect(menu->addAction(tr("Open link in new window")), &QAction::triggered, this, [this, ctxdata]() {
+ createWindow(QWebEnginePage::WebBrowserWindow)->load(ctxdata.linkUrl());
+ });
+ }
+
+ // zoom widget
+ {
+ menu->addSeparator();
+
+ auto *zoomSlider = new QSlider(Qt::Horizontal);
+ zoomSlider->setMinimum(5);
+ zoomSlider->setMaximum(50);
+ zoomSlider->setValue(zoomFactor() * 10);
+
+ auto *zoomAction = menu->addAction(tr("Zoom: %1x").arg(zoomFactor()));
+ connect(zoomAction, &QAction::triggered, this, [zoomSlider]() {
+ zoomSlider->setValue(10);
+ });
+
+ connect(zoomSlider, &QSlider::valueChanged, this, [this, zoomAction](int value) {
+ zoomAction->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10));
+ setZoomFactor(static_cast<qreal>(value) / 10);
+ });
+
+ auto *zoomWidgetAction = new QWidgetAction(this);
+ zoomWidgetAction->setDefaultWidget(zoomSlider);
+
+ menu->addAction(zoomWidgetAction);
+ }
+ } else {
+ menu = page()->createStandardContextMenu();
}
+ menu->setMinimumWidth(250);
menu->popup(event->globalPos());
}