Messagelib

webenginepage.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "webenginepage.h"
8 #include "webhittest.h"
9 #include "webhittestresult.h"
10 
11 #include <KLocalizedString>
12 
13 #include <QEventLoop>
14 #include <QFileDialog>
15 #include <QPointer>
16 #include <QPrinter>
17 #include <QTimer>
18 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19 #include <QWebEngineDownloadItem>
20 #else
21 #include <QWebEngineDownloadRequest>
22 #endif
23 #include <QWebEngineProfile>
24 
25 using namespace WebEngineViewer;
26 
28  : QWebEnginePage(new QWebEngineProfile, parent)
29 {
30  // Create a private (off the record) QWebEngineProfile here to isolate the
31  // browsing settings, and adopt it as a child so that it will be deleted
32  // when we are destroyed. The profile must remain active for as long as
33  // any QWebEnginePage's belonging to it exist, see the API documentation
34  // of QWebEnginePage::QWebEnginePage(QWebEngineProfile *, QObject *).
35  // Deleting it as our child on destruction is safe.
36  //
37  // Do not try to save a line of code by setting the parent on construction:
38  //
39  // WebEnginePage::WebEnginePage(QObject *parent)
40  // : QWebEnginePage(new QWebEngineProfile(this), parent)
41  //
42  // because the QWebEngineProfile constructor will call out to the QWebEnginePage
43  // and crash because the QWebEnginePage is not fully constructed yet.
44  profile()->setParent(this);
45 
46  init();
47 }
48 
49 WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject *parent)
50  : QWebEnginePage(profile, parent)
51 {
52  init();
53 }
54 
55 void WebEnginePage::init()
56 {
57  connect(profile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePage::saveHtml);
58 }
59 
60 WebEngineViewer::WebHitTest *WebEnginePage::hitTestContent(const QPoint &pos)
61 {
62  return new WebHitTest(this, mapToViewport(pos), pos);
63 }
64 
65 QPoint WebEnginePage::mapToViewport(const QPoint &pos) const
66 {
67  return QPoint(pos.x() / zoomFactor(), pos.y() / zoomFactor());
68 }
69 
70 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
71 void WebEnginePage::saveHtml(QWebEngineDownloadItem *download)
72 #else
73 void WebEnginePage::saveHtml(QWebEngineDownloadRequest *download)
74 #endif
75 {
76  const QString fileName = QFileDialog::getSaveFileName(view(), i18n("Save HTML Page"));
77  if (!fileName.isEmpty()) {
78 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
79  download->setSavePageFormat(QWebEngineDownloadItem::SingleHtmlSaveFormat);
80 #else
81  download->setSavePageFormat(QWebEngineDownloadRequest::SingleHtmlSaveFormat);
82 #endif
83  download->setDownloadDirectory(QFileInfo(fileName).path());
84  download->setDownloadFileName(QFileInfo(fileName).fileName());
85  download->accept();
86  }
87 }
88 
89 bool WebEnginePage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
90 {
91  if (isMainFrame && type == NavigationTypeLinkClicked) {
92  Q_EMIT urlClicked(url);
93  return false;
94  }
95  return true;
96 }
97 
98 void WebEnginePage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
99  const QString &message,
100  int lineNumber,
101  const QString &sourceID)
102 {
103  Q_UNUSED(level)
104  Q_UNUSED(sourceID)
105  // Don't convert to debug categories
106  qDebug() << "WebEnginePage::javaScriptConsoleMessage lineNumber: " << lineNumber << " message: " << message;
107  Q_EMIT showConsoleMessage(message);
108 }
109 
110 bool WebEnginePage::execPrintPreviewPage(QPrinter *printer, int timeout)
111 {
112  QPointer<QEventLoop> loop = new QEventLoop;
113  bool result = false;
114  QTimer::singleShot(timeout, loop.data(), &QEventLoop::quit);
115 
116  print(printer, [loop, &result](bool res) {
117  if (loop && loop->isRunning()) {
118  result = res;
119  loop->quit();
120  }
121  });
122 
123  loop->exec();
124  delete loop;
125 
126  return result;
127 }
QAction * print(const QObject *recvr, const char *slot, QObject *parent)
The WebHitTest class.
Definition: webhittest.h:22
int x() const const
int y() const const
WebEnginePage(QObject *parent=nullptr)
Constructor.
QString i18n(const char *text, const TYPE &arg...)
void quit()
bool isEmpty() const const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
T * data() const const
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 19 2022 03:53:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.