Messagelib

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

KDE's Doxygen guidelines are available online.