summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoss Heim <moss.heim@qt.io>2024-02-12 16:05:00 +0100
committerMoss Heim <moss.heim@qt.io>2024-05-31 16:02:35 +0200
commitd825fca2d3c88117aa50269bbe09f5cd49d96ac7 (patch)
tree3a07474b1dc81496374c8a836cec7dd2b2c612ff
parent2df6e75cd1ea7f68ae8b91982bc6b378d64cf2fc (diff)
Add QWebEnginePage::printRequestedByFrame signal
Emitted whenever a frame excluding the main frame calls JS print() function. `printRequested` is now only emitted when the main frame requests printing, instead of any frame. Change-Id: I4b65e5a164b513cc9a9692c1285470847b7a26e3 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r--src/core/api/qwebengineframe.h2
-rw-r--r--src/core/api/qwebenginepage.cpp28
-rw-r--r--src/core/api/qwebenginepage.h1
-rw-r--r--src/core/api/qwebenginepage_p.h2
-rw-r--r--src/core/printing/print_view_manager_qt.cpp5
-rw-r--r--src/core/web_contents_adapter_client.h1
-rw-r--r--src/webenginequick/api/qquickwebengineview.cpp8
-rw-r--r--src/webenginequick/api/qquickwebengineview_p.h1
-rw-r--r--src/webenginequick/api/qquickwebengineview_p_p.h1
-rw-r--r--src/webenginequick/doc/src/webengineview_lgpl.qdoc17
-rw-r--r--src/webenginewidgets/api/qwebengineview.cpp21
-rw-r--r--src/webenginewidgets/api/qwebengineview.h1
-rw-r--r--src/webenginewidgets/api/qwebengineview_p.h1
-rw-r--r--tests/auto/core/qwebengineframe/tst_qwebengineframe.cpp19
-rw-r--r--tests/auto/quick/publicapi/tst_publicapi.cpp1
15 files changed, 102 insertions, 7 deletions
diff --git a/src/core/api/qwebengineframe.h b/src/core/api/qwebengineframe.h
index 988d50d8e..3042fbae0 100644
--- a/src/core/api/qwebengineframe.h
+++ b/src/core/api/qwebengineframe.h
@@ -59,7 +59,9 @@ public:
private:
friend class QWebEnginePage;
+ friend class QWebEnginePagePrivate;
friend class QQuickWebEngineView;
+ friend class QQuickWebEngineViewPrivate;
QWebEngineFrame(QtWebEngineCore::WebContentsAdapterClient *page, quint64 id);
diff --git a/src/core/api/qwebenginepage.cpp b/src/core/api/qwebenginepage.cpp
index 439214235..c393c014b 100644
--- a/src/core/api/qwebenginepage.cpp
+++ b/src/core/api/qwebenginepage.cpp
@@ -1662,11 +1662,14 @@ void QWebEnginePagePrivate::setToolTip(const QString &toolTipText)
\fn void QWebEnginePage::printRequested()
\since 5.12
- This signal is emitted when the JavaScript \c{window.print()} method is called or the user pressed the print
- button of PDF viewer plugin.
+ This signal is emitted when the JavaScript \c{window.print()} method is called on the main
+ frame, or the user pressed the print button of PDF viewer plugin.
Typically, the signal handler can simply call printToPdf().
- \sa printToPdf()
+ Since 6.8, this signal is only emitted for the main frame, instead of being emitted for any
+ frame that requests printing.
+
+ \sa printToPdf(), printRequestedByFrame()
*/
void QWebEnginePagePrivate::printRequested()
@@ -1679,6 +1682,25 @@ void QWebEnginePagePrivate::printRequested()
view->printRequested();
}
+/*!
+ \fn void QWebEnginePage::printRequestedByFrame(QWebEngineFrame frame)
+ \since 6.8
+
+ This signal is emitted when the JavaScript \c{window.print()} method is called on \a frame.
+ If the frame is the main frame, \c{printRequested} is emitted instead.
+
+ \sa printRequested(), printToPdf()
+*/
+
+void QWebEnginePagePrivate::printRequestedByFrame(quint64 frameId)
+{
+ Q_Q(QWebEnginePage);
+ QWebEngineFrame frame(this, frameId);
+ QTimer::singleShot(0, q, [q, frame]() { Q_EMIT q->printRequestedByFrame(frame); });
+ if (view)
+ view->printRequestedByFrame(frame);
+}
+
QtWebEngineCore::TouchHandleDrawableDelegate *
QWebEnginePagePrivate::createTouchHandleDelegate(const QMap<int, QImage> &images)
{
diff --git a/src/core/api/qwebenginepage.h b/src/core/api/qwebenginepage.h
index 13c582931..59eed2b9b 100644
--- a/src/core/api/qwebenginepage.h
+++ b/src/core/api/qwebenginepage.h
@@ -353,6 +353,7 @@ Q_SIGNALS:
void pdfPrintingFinished(const QString &filePath, bool success);
void printRequested();
+ void printRequestedByFrame(QWebEngineFrame frame);
void visibleChanged(bool visible);
diff --git a/src/core/api/qwebenginepage_p.h b/src/core/api/qwebenginepage_p.h
index f0e179ea8..ef6ce7f2b 100644
--- a/src/core/api/qwebenginepage_p.h
+++ b/src/core/api/qwebenginepage_p.h
@@ -74,6 +74,7 @@ public:
virtual void didPrintPage(QPrinter *&printer, QSharedPointer<QByteArray> result) = 0;
virtual void didPrintPageToPdf(const QString &filePath, bool success) = 0;
virtual void printRequested() = 0;
+ virtual void printRequestedByFrame(QWebEngineFrame frame) = 0;
virtual void showAutofillPopup(QtWebEngineCore::AutofillPopupController *controller,
const QRect &bounds, bool autoselectFirstSuggestion) = 0;
virtual void hideAutofillPopup() = 0;
@@ -163,6 +164,7 @@ public:
bool isEnabled() const override;
void setToolTip(const QString &toolTipText) override;
void printRequested() override;
+ void printRequestedByFrame(quint64 frameId) override;
QtWebEngineCore::TouchHandleDrawableDelegate *
createTouchHandleDelegate(const QMap<int, QImage> &) override;
void showTouchSelectionMenu(QtWebEngineCore::TouchSelectionMenuController *, const QRect &,
diff --git a/src/core/printing/print_view_manager_qt.cpp b/src/core/printing/print_view_manager_qt.cpp
index 1d21c2fb9..db9dc8743 100644
--- a/src/core/printing/print_view_manager_qt.cpp
+++ b/src/core/printing/print_view_manager_qt.cpp
@@ -333,7 +333,10 @@ void PrintViewManagerQt::SetupScriptedPrintPreview(SetupScriptedPrintPreviewCall
if (rfh)
GetPrintRenderFrame(rfh)->OnPrintPreviewDialogClosed();
- client->printRequested();
+ if (web_contents()->GetPrimaryMainFrame() == rfh)
+ client->printRequested();
+ else
+ client->printRequestedByFrame(static_cast<quint64>(rfh->GetFrameTreeNodeId()));
}
void PrintViewManagerQt::ShowScriptedPrintPreview(bool /*source_is_modifiable*/)
diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h
index 4c664fd21..78cb8e793 100644
--- a/src/core/web_contents_adapter_client.h
+++ b/src/core/web_contents_adapter_client.h
@@ -209,6 +209,7 @@ public:
virtual void setToolTip(const QString& toolTipText) = 0;
virtual ClientType clientType() = 0;
virtual void printRequested() = 0;
+ virtual void printRequestedByFrame(quint64 frameId) = 0;
virtual TouchHandleDrawableDelegate * createTouchHandleDelegate(const QMap<int, QImage> &images) = 0;
virtual void showTouchSelectionMenu(TouchSelectionMenuController *menuController, const QRect &bounds, const QSize &handleSize) = 0;
virtual void hideTouchSelectionMenu() = 0;
diff --git a/src/webenginequick/api/qquickwebengineview.cpp b/src/webenginequick/api/qquickwebengineview.cpp
index 7c77f22de..411caa0a4 100644
--- a/src/webenginequick/api/qquickwebengineview.cpp
+++ b/src/webenginequick/api/qquickwebengineview.cpp
@@ -842,6 +842,14 @@ void QQuickWebEngineViewPrivate::printRequested()
});
}
+void QQuickWebEngineViewPrivate::printRequestedByFrame(quint64 frameId)
+{
+ Q_Q(QQuickWebEngineView);
+ QTimer::singleShot(0, q, [this, q, frameId]() {
+ Q_EMIT q->printRequestedByFrame(QWebEngineFrame(this, frameId));
+ });
+}
+
void QQuickWebEngineViewPrivate::findTextFinished(const QWebEngineFindTextResult &result)
{
Q_Q(QQuickWebEngineView);
diff --git a/src/webenginequick/api/qquickwebengineview_p.h b/src/webenginequick/api/qquickwebengineview_p.h
index 37e39dfed..a310e0934 100644
--- a/src/webenginequick/api/qquickwebengineview_p.h
+++ b/src/webenginequick/api/qquickwebengineview_p.h
@@ -561,6 +561,7 @@ Q_SIGNALS:
Q_REVISION(6,4) void fileSystemAccessRequested(const QWebEngineFileSystemAccessRequest &request);
Q_REVISION(6, 7) void webAuthUxRequested(QWebEngineWebAuthUxRequest *request);
Q_REVISION(6,7) void desktopMediaRequested(const QWebEngineDesktopMediaRequest &request);
+ Q_REVISION(6, 8) void printRequestedByFrame(QWebEngineFrame frame);
protected:
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
diff --git a/src/webenginequick/api/qquickwebengineview_p_p.h b/src/webenginequick/api/qquickwebengineview_p_p.h
index 50667dda0..6d0257532 100644
--- a/src/webenginequick/api/qquickwebengineview_p_p.h
+++ b/src/webenginequick/api/qquickwebengineview_p_p.h
@@ -130,6 +130,7 @@ public:
QtWebEngineCore::ProfileAdapter *profileAdapter() override;
QtWebEngineCore::WebContentsAdapter *webContentsAdapter() override;
void printRequested() override;
+ void printRequestedByFrame(quint64 frameId) override;
void findTextFinished(const QWebEngineFindTextResult &result) override;
void showAutofillPopup(QtWebEngineCore::AutofillPopupController *controller,
const QRect &bounds, bool autoselectFirstSuggestion) override;
diff --git a/src/webenginequick/doc/src/webengineview_lgpl.qdoc b/src/webenginequick/doc/src/webengineview_lgpl.qdoc
index bbefcd2bc..82bf605ca 100644
--- a/src/webenginequick/doc/src/webengineview_lgpl.qdoc
+++ b/src/webenginequick/doc/src/webengineview_lgpl.qdoc
@@ -1347,14 +1347,27 @@
\qmlsignal WebEngineView::printRequested
\since QtWebEngine 1.8
- This signal is emitted when the JavaScript \c{window.print()} method is called or the user pressed the print
- button of PDF viewer plugin.
+ This signal is emitted when the JavaScript \c{window.print()} method is called on the main
+ frame, or the user pressed the print button of PDF viewer plugin.
Typically, the signal handler can simply call printToPdf().
+ Since QtWebEngine 6.8, this signal is only emitted for the main frame, instead of being emitted
+ for any frame that requests printing.
+
\sa printToPdf
*/
/*!
+ \qmlsignal WebEngineView::printRequestedByFrame(webEngineFrame frame)
+ \since QtWebEngine 6.8
+
+ This signal is emitted when the JavaScript \c{window.print()} method is called on \a frame.
+ If the frame is the main frame, \c{printRequested} is emitted instead.
+
+ \sa printRequested
+*/
+
+/*!
\qmlsignal WebEngineView::selectClientCertificate(WebEngineClientCertificateSelection clientCertificateSelection)
\since QtWebEngine 1.9
diff --git a/src/webenginewidgets/api/qwebengineview.cpp b/src/webenginewidgets/api/qwebengineview.cpp
index d99261adc..50a850750 100644
--- a/src/webenginewidgets/api/qwebengineview.cpp
+++ b/src/webenginewidgets/api/qwebengineview.cpp
@@ -887,6 +887,12 @@ void QWebEngineViewPrivate::printRequested()
});
}
+void QWebEngineViewPrivate::printRequestedByFrame(QWebEngineFrame frame)
+{
+ Q_Q(QWebEngineView);
+ QTimer::singleShot(0, q, [q, frame]() { Q_EMIT q->printRequestedByFrame(frame); });
+}
+
bool QWebEngineViewPrivate::isVisible() const
{
Q_Q(const QWebEngineView);
@@ -1430,7 +1436,20 @@ void QWebEngineView::printToPdf(const std::function<void(const QByteArray&)> &re
button of PDF viewer plugin.
Typically, the signal handler can simply call print().
- \sa print()
+ Since 6.8, this signal is only emitted for the main frame, instead of being emitted
+ for any frame that requests printing.
+
+ \sa printRequestedByFrame(), print()
+*/
+
+/*!
+ \fn void QWebEngineView::printRequestedByFrame(QWebEngineFrame frame)
+ \since 6.8
+
+ This signal is emitted when the JavaScript \c{window.print()} method is called on \a frame.
+ If the frame is the main frame, \c{printRequested} is emitted instead.
+
+ \sa printRequested(), print()
*/
/*!
diff --git a/src/webenginewidgets/api/qwebengineview.h b/src/webenginewidgets/api/qwebengineview.h
index f93d61b12..008aaa032 100644
--- a/src/webenginewidgets/api/qwebengineview.h
+++ b/src/webenginewidgets/api/qwebengineview.h
@@ -114,6 +114,7 @@ Q_SIGNALS:
int exitCode);
void pdfPrintingFinished(const QString &filePath, bool success);
void printRequested();
+ void printRequestedByFrame(QWebEngineFrame frame);
void printFinished(bool success);
protected:
diff --git a/src/webenginewidgets/api/qwebengineview_p.h b/src/webenginewidgets/api/qwebengineview_p.h
index aa330ac23..389bc4a66 100644
--- a/src/webenginewidgets/api/qwebengineview_p.h
+++ b/src/webenginewidgets/api/qwebengineview_p.h
@@ -73,6 +73,7 @@ public:
void didPrintPage(QPrinter *&printer, QSharedPointer<QByteArray> result) override;
void didPrintPageToPdf(const QString &filePath, bool success) override;
void printRequested() override;
+ void printRequestedByFrame(QWebEngineFrame frame) override;
void showAutofillPopup(QtWebEngineCore::AutofillPopupController *controller,
const QRect &bounds, bool autoselectFirstSuggestion) override;
void hideAutofillPopup() override;
diff --git a/tests/auto/core/qwebengineframe/tst_qwebengineframe.cpp b/tests/auto/core/qwebengineframe/tst_qwebengineframe.cpp
index 7cd075443..ce0b61ee2 100644
--- a/tests/auto/core/qwebengineframe/tst_qwebengineframe.cpp
+++ b/tests/auto/core/qwebengineframe/tst_qwebengineframe.cpp
@@ -38,6 +38,7 @@ private Q_SLOTS:
void url();
void size();
void runJavaScript();
+ void printRequestedByFrame();
private:
};
@@ -189,6 +190,24 @@ void tst_QWebEngineFrame::runJavaScript()
QCOMPARE(result, QString("test-subframe0"));
}
+void tst_QWebEngineFrame::printRequestedByFrame()
+{
+ QWebEnginePage page;
+ QSignalSpy loadSpy{ &page, SIGNAL(loadFinished(bool)) };
+ QSignalSpy printRequestedSpy{ &page, SIGNAL(printRequestedByFrame(QWebEngineFrame)) };
+
+ page.load(QUrl("qrc:/resources/nesting-iframe.html"));
+ QTRY_COMPARE(loadSpy.size(), 1);
+ auto oFrame2 = page.findFrameByName("test-main-frame");
+ QVERIFY(oFrame2.has_value());
+ CallbackSpy<QVariant> spy;
+ oFrame2->runJavaScript("window.print()", spy.ref());
+ spy.waitForResult();
+ QCOMPARE(printRequestedSpy.size(), 1);
+ auto *framePtr = get_if<QWebEngineFrame>(&printRequestedSpy[0][0]);
+ QCOMPARE(*framePtr, *oFrame2);
+}
+
QTEST_MAIN(tst_QWebEngineFrame)
#include "tst_qwebengineframe.moc"
diff --git a/tests/auto/quick/publicapi/tst_publicapi.cpp b/tests/auto/quick/publicapi/tst_publicapi.cpp
index 990b1de4f..ee247ec71 100644
--- a/tests/auto/quick/publicapi/tst_publicapi.cpp
+++ b/tests/auto/quick/publicapi/tst_publicapi.cpp
@@ -766,6 +766,7 @@ static const QStringList expectedAPI = QStringList()
<< "QQuickWebEngineView.NewViewInWindow --> NewViewDestination"
<< "QQuickWebEngineView.pdfPrintingFinished(QString,bool) --> void"
<< "QQuickWebEngineView.printRequested() --> void"
+ << "QQuickWebEngineView.printRequestedByFrame(QWebEngineFrame) --> void"
<< "QQuickWebEngineView.printToPdf(QJSValue) --> void"
<< "QQuickWebEngineView.printToPdf(QJSValue,PrintedPageSizeId) --> void"
<< "QQuickWebEngineView.printToPdf(QJSValue,PrintedPageSizeId,PrintedPageOrientation) --> void"