Messagelib

webenginepage.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "webenginepage.h"
8#include "webhittest.h"
9
10#include <KLocalizedString>
11
12#include <QEventLoop>
13#include <QFileDialog>
14#include <QPointer>
15#include <QPrinter>
16#include <QTimer>
17#include <QWebEngineDownloadRequest>
18#include <QWebEngineProfile>
19#include <QWebEngineView>
20
21using namespace WebEngineViewer;
22
24 : QWebEnginePage(new QWebEngineProfile, parent)
25{
26 // Create a private (off the record) QWebEngineProfile here to isolate the
27 // browsing settings, and adopt it as a child so that it will be deleted
28 // when we are destroyed. The profile must remain active for as long as
29 // any QWebEnginePage's belonging to it exist, see the API documentation
30 // of QWebEnginePage::QWebEnginePage(QWebEngineProfile *, QObject *).
31 // Deleting it as our child on destruction is safe.
32 //
33 // Do not try to save a line of code by setting the parent on construction:
34 //
35 // WebEnginePage::WebEnginePage(QObject *parent)
36 // : QWebEnginePage(new QWebEngineProfile(this), parent)
37 //
38 // because the QWebEngineProfile constructor will call out to the QWebEnginePage
39 // and crash because the QWebEnginePage is not fully constructed yet.
40 profile()->setParent(this);
41
42 init();
43}
44
45WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject *parent)
46 : QWebEnginePage(profile, parent)
47{
48 init();
49}
50
51void WebEnginePage::init()
52{
53 connect(profile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePage::saveHtml);
54}
55
56WebEngineViewer::WebHitTest *WebEnginePage::hitTestContent(const QPoint &pos)
57{
58 return new WebHitTest(this, mapToViewport(pos), pos);
59}
60
61QPoint WebEnginePage::mapToViewport(const QPoint &pos) const
62{
63 return QPoint(pos.x() / zoomFactor(), pos.y() / zoomFactor());
64}
65
66void WebEnginePage::saveHtml(QWebEngineDownloadRequest *download)
67{
68 const QString fileName = QFileDialog::getSaveFileName(QWebEngineView::forPage(this), i18n("Save HTML Page"));
69 if (!fileName.isEmpty()) {
70 download->setSavePageFormat(QWebEngineDownloadRequest::SingleHtmlSaveFormat);
71 download->setDownloadDirectory(QFileInfo(fileName).path());
72 download->setDownloadFileName(QFileInfo(fileName).fileName());
73 download->accept();
74 }
75}
76
77bool WebEnginePage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
78{
79 if (isMainFrame && type == NavigationTypeLinkClicked) {
80 Q_EMIT urlClicked(url);
81 return false;
82 }
83 return true;
84}
85
86void WebEnginePage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
87 const QString &message,
88 int lineNumber,
89 const QString &sourceID)
90{
91 Q_UNUSED(level)
92 Q_UNUSED(sourceID)
93 // Don't convert to debug categories
94 qDebug() << "WebEnginePage::javaScriptConsoleMessage lineNumber: " << lineNumber << " message: " << message;
95 Q_EMIT showConsoleMessage(message);
96}
97
98#include "moc_webenginepage.cpp"
WebEnginePage(QObject *parent=nullptr)
Constructor.
The WebHitTest class.
Definition webhittest.h:23
QString i18n(const char *text, const TYPE &arg...)
QString path(const QString &relativePath)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
int x() const const
int y() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:49:08 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.