diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-01 13:19:07 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-01 13:19:07 +0200 |
commit | 74361f5b05dc0a255f489256bd25065ef20f5dee (patch) | |
tree | 279f45e5f2bd093cd6fbf1500fe476f91108bd1f /src/webengine | |
parent | Add keyboard shortcuts for address bar menus (diff) | |
download | smolbote-74361f5b05dc0a255f489256bd25065ef20f5dee.tar.xz |
Page menus refactoring
- Split off menus into their own classes
Page tools menu
- Shows injected scripts
- Shows dev tools page in another dialog
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/webview.cpp | 135 | ||||
-rw-r--r-- | src/webengine/webview.h | 22 | ||||
-rw-r--r-- | src/webengine/widgets/pagemenu.cpp | 99 | ||||
-rw-r--r-- | src/webengine/widgets/pagemenu.h | 22 | ||||
-rw-r--r-- | src/webengine/widgets/pagetoolsmenu.cpp | 37 | ||||
-rw-r--r-- | src/webengine/widgets/pagetoolsmenu.h | 28 |
6 files changed, 237 insertions, 106 deletions
diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 0b0a9e3..7ca3280 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -7,31 +7,12 @@ */ #include "webview.h" -#include <QMenu> - -// copy page URL -#include <QApplication> -#include <QClipboard> - -// zoom widget -#include <QLabel> -#include <QSlider> -#include <QStyle> -#include <QToolButton> -#include <QVBoxLayout> -#include <QWidgetAction> - -// printer support -#include <QFileDialog> -#include <QPrintDialog> -#include <QPrinter> -#include <QPrinterInfo> - #include "mainwindow/mainwindow.h" +#include "widgets/pagemenu.h" +#include "widgets/pagetoolsmenu.h" +#include <QDialog> #include <QStatusBar> - -// ssl errors -#include "addressbar/urllineedit.h" +#include <QVBoxLayout> WebView::WebView(MainWindow *parentMainWindow, QWidget *parent) : QWebEngineView(parent) @@ -54,82 +35,9 @@ WebView::WebView(MainWindow *parentMainWindow, QWidget *parent) } }); - m_pageMenu = new QMenu(); - m_pageMenu->setMinimumWidth(240); - - QAction *copyUrlAction = m_pageMenu->addAction(tr("Copy page URL")); - connect(copyUrlAction, &QAction::triggered, [this]() { - qApp->clipboard()->setText(this->url().toString()); - }); - - QAction *bookmarkAction = m_pageMenu->addAction(tr("Bookmark page")); - connect(bookmarkAction, &QAction::triggered, this, [this]() { - emit newBookmark(this->title(), this->url()); - }); - - m_pageMenu->addSeparator(); - - QWidgetAction *zoomWidgetAction = new QWidgetAction(m_pageMenu); - { - QWidget *widget = new QWidget(m_pageMenu); - zoomWidgetAction->setDefaultWidget(widget); - - QVBoxLayout *layout = new QVBoxLayout(widget); - widget->setLayout(layout); - - QLabel *zoomLabel = new QLabel(tr("Zoom: 1x")); - layout->addWidget(zoomLabel); - - QHBoxLayout *zoomLayout = new QHBoxLayout(); - layout->addLayout(zoomLayout); - - QSlider *zoomSlider = new QSlider(Qt::Horizontal); - zoomSlider->setMinimum(5); - zoomSlider->setMaximum(50); - zoomSlider->setValue(10); - zoomLayout->addWidget(zoomSlider); - - QToolButton *zoomResetButton = new QToolButton(widget); - zoomResetButton->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload)); - zoomLayout->addWidget(zoomResetButton); - - connect(zoomResetButton, &QToolButton::clicked, [zoomSlider]() { - zoomSlider->setValue(10); - }); - connect(zoomSlider, &QSlider::valueChanged, this, [this, zoomLabel](int value) { - zoomLabel->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10)); - this->setZoomFactor(static_cast<qreal>(value) / 10); - }); - } - m_pageMenu->addAction(zoomWidgetAction); - - m_pageMenu->addSeparator(); - - QAction *savePageAction = m_pageMenu->addAction(tr("Save Page")); - connect(savePageAction, &QAction::triggered, this, [this]() { - this->triggerPageAction(QWebEnginePage::SavePage); - }); - -#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - QAction *printAction = m_pageMenu->addAction(tr("Print page")); - connect(printAction, &QAction::triggered, [this]() { - QPrinter *printer = new QPrinter(QPrinterInfo::defaultPrinter()); - QPrintDialog *dlg = new QPrintDialog(printer, nullptr); - if(dlg->exec() == QDialog::Accepted) { - this->page()->print(printer, [printer](bool success) { - qDebug("print %s", success ? "ok" : "failed"); - delete printer; - }); - } - delete dlg; - }); -#endif - - QAction *printPdfAction = m_pageMenu->addAction(tr("Print to PDF")); - connect(printPdfAction, &QAction::triggered, [this]() { - const QString path = QFileDialog::getSaveFileName(this, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); - this->page()->printToPdf(path); - }); + m_pageMenu = new PageMenu(this); + m_toolsMenu = new PageToolsMenu(this); + m_devToolsPage = new QWebEnginePage(this); } WebView::~WebView() @@ -143,15 +51,29 @@ QMenu *WebView::pageMenu() return m_pageMenu; } -void WebView::setPage(WebPage *page) +void WebView::setPage(QWebEnginePage *page) { Q_CHECK_PTR(page); // make sure the page gets cleaned up if we replace it by taking ownership page->setParent(this); - connect(page, &WebPage::linkHovered, this, &WebView::handleLinkHovered); + page->setDevToolsPage(m_devToolsPage); + connect(page, &QWebEnginePage::linkHovered, this, &WebView::handleLinkHovered); QWebEngineView::setPage(page); } +void WebView::popupPage(QWebEnginePage *page) +{ + Q_CHECK_PTR(page); + auto *popup = new QDialog(this); + auto *view = new QWebEngineView(popup); + view->setPage(page); + + auto *l = new QVBoxLayout(popup); + l->addWidget(view); + + popup->show(); +} + bool WebView::isLoaded() const { return m_loaded; @@ -193,6 +115,15 @@ WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) void WebView::handleLinkHovered(const QString &url) { if(isVisible()) { - m_parent->statusBar()->showMessage(url); + m_parent->statusBar()->showMessage(url, 3000); + } +} + +void WebView::triggerViewAction(WebView::ViewAction action) +{ + switch(action) { + case BookmarkPage: + emit newBookmark(this->title(), this->url()); + break; } } diff --git a/src/webengine/webview.h b/src/webengine/webview.h index a274535..1fab1db 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -6,8 +6,8 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef WEBVIEW_H -#define WEBVIEW_H +#ifndef SMOLBOTE_WEBVIEW_H +#define SMOLBOTE_WEBVIEW_H #include "webpage.h" #include <QWebEngineView> @@ -18,15 +18,26 @@ class WebView : public QWebEngineView { Q_OBJECT public: + enum ViewAction { + BookmarkPage + }; + explicit WebView(MainWindow *parentMainWindow, QWidget *parent = nullptr); ~WebView() override; QMenu *pageMenu(); + QMenu *toolsMenu() { + Q_CHECK_PTR(m_toolsMenu); + return m_toolsMenu; + } - void setPage(WebPage *page); + void setPage(QWebEnginePage *page); + void popupPage(QWebEnginePage *page); bool isLoaded() const; int loadProgress() const; + void triggerViewAction(ViewAction action); + signals: // loadStarted is always emitted, be it page load or in-page request, // but loadFinished is only emitted when it's a page load @@ -43,9 +54,12 @@ private slots: private: MainWindow *m_parent = nullptr; QMenu *m_pageMenu = nullptr; + QMenu *m_toolsMenu = nullptr; bool m_loaded; int m_loadProgress; + + QWebEnginePage *m_devToolsPage; }; -#endif // WEBVIEW_H +#endif // SMOLBOTE_WEBVIEW_H diff --git a/src/webengine/widgets/pagemenu.cpp b/src/webengine/widgets/pagemenu.cpp new file mode 100644 index 0000000..a90d223 --- /dev/null +++ b/src/webengine/widgets/pagemenu.cpp @@ -0,0 +1,99 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "pagemenu.h" +#include "../webview.h" +#include <QApplication> +#include <QClipboard> +#include <QFileDialog> +#include <QLabel> +#include <QPrintDialog> +#include <QPrinterInfo> +#include <QSlider> +#include <QStyle> +#include <QToolButton> +#include <QVBoxLayout> +#include <QWidgetAction> + +PageMenu::PageMenu(WebView *parent) + : QMenu(parent) +{ + setMinimumWidth(240); + + auto *copyUrlAction = addAction(tr("Copy page URL")); + connect(copyUrlAction, &QAction::triggered, parent, [parent]() { + qApp->clipboard()->setText(parent->url().toString()); + }); + + auto *bookmarkAction = addAction(tr("Bookmark page")); + connect(bookmarkAction, &QAction::triggered, parent, [parent]() { + parent->triggerViewAction(WebView::BookmarkPage); + }); + + addSeparator(); + + auto *zoomWidgetAction = new QWidgetAction(this); + { + auto *widget = new QWidget(this); + zoomWidgetAction->setDefaultWidget(widget); + + auto *layout = new QVBoxLayout(widget); + widget->setLayout(layout); + + QLabel *zoomLabel = new QLabel(tr("Zoom: 1x")); + layout->addWidget(zoomLabel); + + auto *zoomLayout = new QHBoxLayout(); + layout->addLayout(zoomLayout); + + auto *zoomSlider = new QSlider(Qt::Horizontal); + zoomSlider->setMinimum(5); + zoomSlider->setMaximum(50); + zoomSlider->setValue(10); + zoomLayout->addWidget(zoomSlider); + + auto *zoomResetButton = new QToolButton(widget); + zoomResetButton->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload)); + zoomLayout->addWidget(zoomResetButton); + + connect(zoomResetButton, &QToolButton::clicked, [zoomSlider]() { + zoomSlider->setValue(10); + }); + connect(zoomSlider, &QSlider::valueChanged, parent, [parent, zoomLabel](int value) { + zoomLabel->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10)); + parent->setZoomFactor(static_cast<qreal>(value) / 10); + }); + } + addAction(zoomWidgetAction); + + addSeparator(); + + auto *savePageAction = addAction(tr("Save Page")); + connect(savePageAction, &QAction::triggered, parent, [parent]() { + parent->triggerPageAction(QWebEnginePage::SavePage); + }); + + auto *printAction = addAction(tr("Print Page")); + connect(printAction, &QAction::triggered, parent, [parent]() { + auto *printer = new QPrinter(QPrinterInfo::defaultPrinter()); + auto *dlg = new QPrintDialog(printer, nullptr); + if(dlg->exec() == QDialog::Accepted) { + parent->page()->print(printer, [printer](bool success) { + qDebug("print %s", success ? "ok" : "failed"); + delete printer; + }); + } + delete dlg; + }); + + auto *printPdfAction = addAction(tr("Print to PDF")); + connect(printPdfAction, &QAction::triggered, parent, [parent]() { + const QString path = QFileDialog::getSaveFileName(parent, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); + parent->page()->printToPdf(path); + }); +} diff --git a/src/webengine/widgets/pagemenu.h b/src/webengine/widgets/pagemenu.h new file mode 100644 index 0000000..91f35e0 --- /dev/null +++ b/src/webengine/widgets/pagemenu.h @@ -0,0 +1,22 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_PAGEMENU_H +#define SMOLBOTE_PAGEMENU_H + +#include <QMenu> + +class WebView; +class PageMenu : public QMenu +{ + Q_OBJECT +public: + explicit PageMenu(WebView *parent = nullptr); +}; + +#endif //SMOLBOTE_PAGEMENU_H diff --git a/src/webengine/widgets/pagetoolsmenu.cpp b/src/webengine/widgets/pagetoolsmenu.cpp new file mode 100644 index 0000000..9a85f69 --- /dev/null +++ b/src/webengine/widgets/pagetoolsmenu.cpp @@ -0,0 +1,37 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "pagetoolsmenu.h" +#include "../webview.h" +#include <QWebEngineProfile> +#include <QWebEngineScriptCollection> + +PageToolsMenu::PageToolsMenu(WebView *parent) + : QMenu(parent) +{ + Q_CHECK_PTR(parent); + parentView = parent; + + connect(this, &QMenu::aboutToShow, this, &PageToolsMenu::createEntries); +} + +void PageToolsMenu::createEntries() +{ + clear(); + + auto *scriptsMenu = new QMenu(tr("Injected scripts"), this); + for(const auto &script : parentView->page()->scripts().toList()) { + scriptsMenu->addAction(script.name())->setEnabled(false); + } + addMenu(scriptsMenu); + + auto *devToolsAction = addAction(tr("Dev tools page")); + connect(devToolsAction, &QAction::triggered, this, [this]() { + parentView->popupPage(parentView->page()->devToolsPage()); + }); +} diff --git a/src/webengine/widgets/pagetoolsmenu.h b/src/webengine/widgets/pagetoolsmenu.h new file mode 100644 index 0000000..010bb43 --- /dev/null +++ b/src/webengine/widgets/pagetoolsmenu.h @@ -0,0 +1,28 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_PAGETOOLSMENU_H +#define SMOLBOTE_PAGETOOLSMENU_H + +#include <QMenu> + +class WebView; +class PageToolsMenu : public QMenu +{ + Q_OBJECT +public: + explicit PageToolsMenu(WebView *parent = nullptr); + +public slots: + void createEntries(); + +private: + WebView *parentView; +}; + +#endif //SMOLBOTE_PAGETOOLSMENU_H |