Messagelib

webenginepage.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 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 <QWebEngineDownloadItem>
15 #include <QPointer>
16 #include <QTimer>
17 #include <QFileDialog>
18 #include <QWebEngineProfile>
19 #include <QPrinter>
20 
21 using 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 
45 WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject *parent)
46  : QWebEnginePage(profile, parent)
47 {
48  init();
49 }
50 
51 void WebEnginePage::init()
52 {
53  connect(profile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePage::saveHtml);
54 }
55 
56 WebEngineViewer::WebHitTest *WebEnginePage::hitTestContent(const QPoint &pos)
57 {
58  return new WebHitTest(this, mapToViewport(pos), pos);
59 }
60 
61 QPoint WebEnginePage::mapToViewport(const QPoint &pos) const
62 {
63  return QPoint(pos.x() / zoomFactor(), pos.y() / zoomFactor());
64 }
65 
66 void WebEnginePage::saveHtml(QWebEngineDownloadItem *download)
67 {
68  const QString fileName = QFileDialog::getSaveFileName(view(), i18n("Save HTML Page"));
69  if (!fileName.isEmpty()) {
70  download->setSavePageFormat(QWebEngineDownloadItem::SingleHtmlSaveFormat);
71  download->setDownloadDirectory(QFileInfo(fileName).path());
72  download->setDownloadFileName(QFileInfo(fileName).fileName());
73  download->accept();
74  }
75 }
76 
77 bool 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 
86 void WebEnginePage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID)
87 {
88  Q_UNUSED(level)
89  Q_UNUSED(sourceID)
90  //Don't convert to debug categories
91  qDebug() << "WebEnginePage::javaScriptConsoleMessage lineNumber: " << lineNumber << " message: " << message;
92  Q_EMIT showConsoleMessage(message);
93 }
94 
95 bool WebEnginePage::execPrintPreviewPage(QPrinter *printer, int timeout)
96 {
98  bool result = false;
99  QTimer::singleShot(timeout, loop.data(), &QEventLoop::quit);
100 
101  print(printer, [loop, &result](bool res) {
102  if (loop && loop->isRunning()) {
103  result = res;
104  loop->quit();
105  }
106  });
107 
108  loop->exec();
109  delete loop;
110 
111  return result;
112 }
void quit()
WebEnginePage(QObject *parent=nullptr)
Constructor.
T * data() const const
The WebHitTest class.
Definition: webhittest.h:21
int x() const const
int y() const const
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jan 14 2021 23:16:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.