diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-12-15 21:21:01 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-12-15 21:21:01 +0100 |
commit | 930c972169249251ef238a454d0126f2b7fec2c5 (patch) | |
tree | c94de07dee48b089a04a412cbd1b0a76b7990ab3 /src/webengine | |
parent | Using Q_CHECK_PTR instead of Q_ASSERT on pointers (diff) | |
download | smolbote-930c972169249251ef238a454d0126f2b7fec2c5.tar.xz |
WebView pageMenu implementation
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/webview.cpp | 98 | ||||
-rw-r--r-- | src/webengine/webview.h | 3 |
2 files changed, 93 insertions, 8 deletions
diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 50041b8..d3e987a 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -21,18 +21,102 @@ #include "webview.h" #include <QWebEnginePage> +// copy page URL +#include <QApplication> +#include <QClipboard> + +// zoom widget +#include <QWidgetAction> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QLabel> +#include <QToolButton> +#include <QSlider> +#include <QStyle> + +// printer support +#include <QFileDialog> +#include <QPrinter> +#include <QPrinterInfo> +#include <QPrintDialog> + WebView::WebView(QWidget *parent) : QWebEngineView(parent) { m_pageMenu = new QMenu(); - m_pageMenu->addAction(tr("TODO: Copy page URL")); - m_pageMenu->addAction(tr("TODO: Bookmark page")); + 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("TODO: Bookmark page")); + connect(bookmarkAction, &QAction::triggered, this, [this]() { + emit newBookmark(this->title(), this->url()); + }); + m_pageMenu->addSeparator(); - m_pageMenu->addAction(tr("TODO: Zoom level: 100%")); + + 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(); - m_pageMenu->addAction(tr("TODO: Save Page")); - m_pageMenu->addAction(tr("TODO: Print page")); - m_pageMenu->addAction(tr("TODO: Print to PDF")); + + QAction *savePageAction = m_pageMenu->addAction(tr("TODO: Save Page")); + connect(savePageAction, &QAction::triggered, this, [this]() { + this->triggerPageAction(QWebEnginePage::SavePage); + }); + + 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; + }); + + 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); + }); } WebView::~WebView() @@ -40,7 +124,7 @@ WebView::~WebView() delete m_pageMenu; } -QMenu *WebView::menu() +QMenu *WebView::pageMenu() { Q_CHECK_PTR(m_pageMenu); return m_pageMenu; diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 95d12d9..deecd35 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -31,12 +31,13 @@ public: explicit WebView(QWidget *parent = nullptr); ~WebView(); - QMenu *menu(); + QMenu *pageMenu(); void setPage(QWebEnginePage *page); signals: void linkHovered(const QString &url); + void newBookmark(const QString &title, const QUrl &url); private slots: void handleLinkHovered(const QString &url); |