1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*******************************************************************************
**
** 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 <http://www.gnu.org/licenses/>.
**
******************************************************************************/
#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->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<qreal>(value) / 10));
this->setZoomFactor(static_cast<qreal>(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);
}
|