/******************************************************************************* ** ** smolbote: yet another qute browser ** Copyright (C) 2017 Xian Nox ** ** This program is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 3 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program. If not, see . ** ******************************************************************************/ #include "webview.h" #include // copy page URL #include #include // zoom widget #include #include #include #include #include #include #include // printer support #include #include #include #include WebView::WebView(QWidget *parent) : QWebEngineView(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("TODO: 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(value) / 10)); this->setZoomFactor(static_cast(value) / 10); }); } m_pageMenu->addAction(zoomWidgetAction); m_pageMenu->addSeparator(); 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() { delete m_pageMenu; } QMenu *WebView::pageMenu() { Q_CHECK_PTR(m_pageMenu); return m_pageMenu; } void WebView::setPage(QWebEnginePage *page) { disconnect(this->page(), SIGNAL(linkHovered(QString)), this, SLOT(handleLinkHovered(QString))); connect(page, SIGNAL(linkHovered(QString)), this, SLOT(handleLinkHovered(QString))); QWebEngineView::setPage(page); } void WebView::handleLinkHovered(const QString &url) { emit linkHovered(url); }