summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael BrĂ¼ning <michael.bruning@qt.io>2017-01-09 16:59:25 +0100
committerMichael BrĂ¼ning <michael.bruning@qt.io>2017-01-10 13:19:19 +0000
commit17f9e58022b181b1a097ec75cf824714778c8946 (patch)
tree45f5b80ca4feb5b9471b0ea2c272b254af274dd2 /src
parenta536eec3d420e7e95dc4708687e14edf683d767a (diff)
Emit a new signal when printing to a PDF file finishes
[ChangeLog][Important Changes] Printing to a PDF file will now emit signal the signal pdfPrintingFinished in both QQuickWebEngineView and QWebEnginePage. The boolean passed with the signal to indicate if the printing and saving of the PDF was successful. The path of the created file is also passed to enable the user to map the signal to a print request. Task-number: QTBUG-56677 Change-Id: Ifab5a20b048f33a8cd872165bd4d453b01708037 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/core/print_view_manager_qt.cpp61
-rw-r--r--src/core/print_view_manager_qt.h11
-rw-r--r--src/core/web_contents_adapter.cpp34
-rw-r--r--src/core/web_contents_adapter_client.h1
-rw-r--r--src/webengine/api/qquickwebengineview.cpp6
-rw-r--r--src/webengine/api/qquickwebengineview_p.h3
-rw-r--r--src/webengine/api/qquickwebengineview_p_p.h1
-rw-r--r--src/webengine/doc/src/webengineview.qdoc24
-rw-r--r--src/webenginewidgets/api/qwebenginepage.cpp31
-rw-r--r--src/webenginewidgets/api/qwebenginepage.h2
-rw-r--r--src/webenginewidgets/api/qwebenginepage_p.h1
11 files changed, 141 insertions, 34 deletions
diff --git a/src/core/print_view_manager_qt.cpp b/src/core/print_view_manager_qt.cpp
index 0231df8bd..6006c3bad 100644
--- a/src/core/print_view_manager_qt.cpp
+++ b/src/core/print_view_manager_qt.cpp
@@ -63,7 +63,8 @@ namespace {
static const qreal kMicronsToMillimeter = 1000.0f;
static std::vector<char>
-GetStdVectorFromHandle(base::SharedMemoryHandle handle, uint32_t data_size) {
+GetStdVectorFromHandle(base::SharedMemoryHandle handle, uint32_t data_size)
+{
std::unique_ptr<base::SharedMemory> shared_buf(
new base::SharedMemory(handle, true));
@@ -76,7 +77,8 @@ GetStdVectorFromHandle(base::SharedMemoryHandle handle, uint32_t data_size) {
}
static scoped_refptr<base::RefCountedBytes>
-GetBytesFromHandle(base::SharedMemoryHandle handle, uint32_t data_size) {
+GetBytesFromHandle(base::SharedMemoryHandle handle, uint32_t data_size)
+{
std::unique_ptr<base::SharedMemory> shared_buf(
new base::SharedMemory(handle, true));
@@ -91,7 +93,10 @@ GetBytesFromHandle(base::SharedMemoryHandle handle, uint32_t data_size) {
// Write the PDF file to disk.
static void SavePdfFile(scoped_refptr<base::RefCountedBytes> data,
- const base::FilePath& path) {
+ const base::FilePath& path,
+ const QtWebEngineCore::PrintViewManagerQt::PrintToPDFFileCallback
+ &saveCallback)
+{
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
DCHECK_GT(data->size(), 0U);
@@ -100,8 +105,10 @@ static void SavePdfFile(scoped_refptr<base::RefCountedBytes> data,
base::File file(path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
- if (file.IsValid())
- metafile.SaveTo(&file);
+ bool success = file.IsValid() && metafile.SaveTo(&file);
+ content::BrowserThread::PostTask(content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(saveCallback, success));
}
static base::DictionaryValue *createPrintSettings()
@@ -172,30 +179,43 @@ PrintViewManagerQt::~PrintViewManagerQt()
}
#if defined(ENABLE_BASIC_PRINTING)
-bool PrintViewManagerQt::PrintToPDF(const QPageLayout &pageLayout, bool printInColor, const QString &filePath)
+void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayout,
+ bool printInColor, const QString &filePath,
+ const PrintToPDFFileCallback& callback)
{
- if (m_printSettings || !filePath.length())
- return false;
+ if (callback.is_null())
+ return;
+
+ if (m_printSettings || !filePath.length()) {
+ content::BrowserThread::PostTask(content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(callback, false));
+ return;
+ }
m_pdfOutputPath = toFilePath(filePath);
+ m_pdfSaveCallback = callback;
if (!PrintToPDFInternal(pageLayout, printInColor)) {
+ content::BrowserThread::PostTask(content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(callback, false));
resetPdfState();
- return false;
}
- return true;
}
-bool PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, bool printInColor, const PrintToPDFCallback& callback)
+void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout,
+ bool printInColor,
+ const PrintToPDFCallback& callback)
{
if (callback.is_null())
- return false;
+ return;
// If there already is a pending print in progress, don't try starting another one.
if (m_printSettings) {
content::BrowserThread::PostTask(content::BrowserThread::UI,
FROM_HERE,
base::Bind(callback, std::vector<char>()));
- return false;
+ return;
}
m_pdfPrintCallback = callback;
@@ -205,9 +225,7 @@ bool PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, b
base::Bind(callback, std::vector<char>()));
resetPdfState();
- return false;
}
- return true;
}
bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, bool printInColor)
@@ -218,7 +236,8 @@ bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, bool
m_printSettings.reset(createPrintSettingsFromQPageLayout(pageLayout));
m_printSettings->SetBoolean(printing::kSettingShouldPrintBackgrounds
, web_contents()->GetRenderViewHost()->GetWebkitPreferences().should_print_backgrounds);
- m_printSettings->SetInteger(printing::kSettingColor, printInColor ? printing::COLOR : printing::GRAYSCALE);
+ m_printSettings->SetInteger(printing::kSettingColor,
+ printInColor ? printing::COLOR : printing::GRAYSCALE);
return Send(new PrintMsg_InitiatePrintPreview(routing_id(), false));
}
@@ -255,6 +274,7 @@ void PrintViewManagerQt::resetPdfState()
{
m_pdfOutputPath.clear();
m_pdfPrintCallback.Reset();
+ m_pdfSaveCallback.Reset();
m_printSettings.reset();
}
@@ -273,20 +293,23 @@ void PrintViewManagerQt::OnMetafileReadyForPrinting(
// Create local copies so we can reset the state and take a new pdf print job.
base::Callback<void(const std::vector<char>&)> pdf_print_callback = m_pdfPrintCallback;
+ base::Callback<void(bool)> pdf_save_callback = m_pdfSaveCallback;
base::FilePath pdfOutputPath = m_pdfOutputPath;
resetPdfState();
if (!pdf_print_callback.is_null()) {
- std::vector<char> data_vector = GetStdVectorFromHandle(params.metafile_data_handle, params.data_size);
+ std::vector<char> data_vector = GetStdVectorFromHandle(params.metafile_data_handle,
+ params.data_size);
content::BrowserThread::PostTask(content::BrowserThread::UI,
FROM_HERE,
base::Bind(pdf_print_callback, data_vector));
} else {
- scoped_refptr<base::RefCountedBytes> data_bytes = GetBytesFromHandle(params.metafile_data_handle, params.data_size);
+ scoped_refptr<base::RefCountedBytes> data_bytes
+ = GetBytesFromHandle(params.metafile_data_handle, params.data_size);
content::BrowserThread::PostTask(content::BrowserThread::FILE,
FROM_HERE,
- base::Bind(&SavePdfFile, data_bytes, pdfOutputPath));
+ base::Bind(&SavePdfFile, data_bytes, pdfOutputPath, pdf_save_callback));
}
}
diff --git a/src/core/print_view_manager_qt.h b/src/core/print_view_manager_qt.h
index 668516096..b2ba73b27 100644
--- a/src/core/print_view_manager_qt.h
+++ b/src/core/print_view_manager_qt.h
@@ -81,10 +81,16 @@ class PrintViewManagerQt
public:
~PrintViewManagerQt() override;
typedef base::Callback<void(const std::vector<char> &result)> PrintToPDFCallback;
+ typedef base::Callback<void(bool success)> PrintToPDFFileCallback;
#if defined(ENABLE_BASIC_PRINTING)
// Method to print a page to a Pdf document with page size \a pageSize in location \a filePath.
- bool PrintToPDF(const QPageLayout &pageLayout, bool printInColor, const QString &filePath);
- bool PrintToPDFWithCallback(const QPageLayout &pageLayout, bool printInColor, const PrintToPDFCallback &callback);
+ void PrintToPDFFileWithCallback(const QPageLayout &pageLayout,
+ bool printInColor,
+ const QString &filePath,
+ const PrintToPDFFileCallback& callback);
+ void PrintToPDFWithCallback(const QPageLayout &pageLayout,
+ bool printInColor,
+ const PrintToPDFCallback &callback);
#endif // ENABLE_BASIC_PRINTING
// PrintedPagesSource implementation.
@@ -114,6 +120,7 @@ protected:
base::FilePath m_pdfOutputPath;
PrintToPDFCallback m_pdfPrintCallback;
+ PrintToPDFFileCallback m_pdfSaveCallback;
private:
friend class content::WebContentsUserData<PrintViewManagerQt>;
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index abf9aa3a3..030d3ea89 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -184,11 +184,20 @@ static void callbackOnEvaluateJS(WebContentsAdapterClient *adapterClient, quint6
}
#if defined(ENABLE_BASIC_PRINTING)
-static void callbackOnPrintingFinished(WebContentsAdapterClient *adapterClient, int requestId, const std::vector<char>& result)
+static void callbackOnPrintingFinished(WebContentsAdapterClient *adapterClient,
+ int requestId,
+ const std::vector<char>& result)
{
if (requestId)
adapterClient->didPrintPage(requestId, QByteArray(result.data(), result.size()));
}
+
+static void callbackOnPdfSavingFinished(WebContentsAdapterClient *adapterClient,
+ const QString& filePath,
+ bool success)
+{
+ adapterClient->didPrintPageToPdf(filePath, success);
+}
#endif
static content::WebContents *createBlankWebContents(WebContentsAdapterClient *adapterClient, content::BrowserContext *browserContext)
@@ -963,19 +972,28 @@ void WebContentsAdapter::wasHidden()
void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString &filePath)
{
#if defined(ENABLE_BASIC_PRINTING)
- PrintViewManagerQt::FromWebContents(webContents())->PrintToPDF(pageLayout, true, filePath);
+ Q_D(WebContentsAdapter);
+ PrintViewManagerQt::PrintToPDFFileCallback callback = base::Bind(&callbackOnPdfSavingFinished,
+ d->adapterClient,
+ filePath);
+ PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFFileWithCallback(pageLayout,
+ true,
+ filePath,
+ callback);
#endif // if defined(ENABLE_BASIC_PRINTING)
}
-quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout, const bool colorMode)
+quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout,
+ const bool colorMode)
{
#if defined(ENABLE_BASIC_PRINTING)
Q_D(WebContentsAdapter);
- PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished
- , d->adapterClient
- , d->nextRequestId);
- PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, colorMode
- , callback);
+ PrintViewManagerQt::PrintToPDFCallback callback = base::Bind(&callbackOnPrintingFinished,
+ d->adapterClient,
+ d->nextRequestId);
+ PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout,
+ colorMode,
+ callback);
return d->nextRequestId++;
#else
return 0;
diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h
index 4e15df753..3112e4484 100644
--- a/src/core/web_contents_adapter_client.h
+++ b/src/core/web_contents_adapter_client.h
@@ -347,6 +347,7 @@ public:
virtual void didFetchDocumentInnerText(quint64 requestId, const QString& result) = 0;
virtual void didFindText(quint64 requestId, int matchCount) = 0;
virtual void didPrintPage(quint64 requestId, const QByteArray &result) = 0;
+ virtual void didPrintPageToPdf(const QString &filePath, bool success) = 0;
virtual void passOnFocus(bool reverse) = 0;
// returns the last QObject (QWidget/QQuickItem) based object in the accessibility
// hierarchy before going into the BrowserAccessibility tree
diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp
index fbd3ef9ed..76d1ca8b5 100644
--- a/src/webengine/api/qquickwebengineview.cpp
+++ b/src/webengine/api/qquickwebengineview.cpp
@@ -1103,6 +1103,12 @@ void QQuickWebEngineViewPrivate::didPrintPage(quint64 requestId, const QByteArra
callback.call(args);
}
+void QQuickWebEngineViewPrivate::didPrintPageToPdf(const QString &filePath, bool success)
+{
+ Q_Q(QQuickWebEngineView);
+ Q_EMIT q->pdfPrintingFinished(filePath, success);
+}
+
void QQuickWebEngineViewPrivate::showValidationMessage(const QRect &anchor, const QString &mainText, const QString &subText)
{
#ifdef ENABLE_QML_TESTSUPPORT_API
diff --git a/src/webengine/api/qquickwebengineview_p.h b/src/webengine/api/qquickwebengineview_p.h
index d83958147..27224fadd 100644
--- a/src/webengine/api/qquickwebengineview_p.h
+++ b/src/webengine/api/qquickwebengineview_p.h
@@ -98,7 +98,7 @@ private:
const bool m_toggleOn;
};
-#define LATEST_WEBENGINEVIEW_REVISION 4
+#define LATEST_WEBENGINEVIEW_REVISION 5
class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineView : public QQuickItem {
Q_OBJECT
@@ -512,6 +512,7 @@ Q_SIGNALS:
Q_REVISION(4) void colorDialogRequested(QQuickWebEngineColorDialogRequest *request);
Q_REVISION(4) void fileDialogRequested(QQuickWebEngineFileDialogRequest *request);
Q_REVISION(4) void formValidationMessageRequested(QQuickWebEngineFormValidationMessageRequest *request);
+ Q_REVISION(5) void pdfPrintingFinished(const QString &filePath, bool success);
#ifdef ENABLE_QML_TESTSUPPORT_API
void testSupportChanged();
diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h
index d692140ef..2ecd70d78 100644
--- a/src/webengine/api/qquickwebengineview_p_p.h
+++ b/src/webengine/api/qquickwebengineview_p_p.h
@@ -123,6 +123,7 @@ public:
virtual void didFetchDocumentInnerText(quint64, const QString&) Q_DECL_OVERRIDE { }
virtual void didFindText(quint64, int) Q_DECL_OVERRIDE;
virtual void didPrintPage(quint64 requestId, const QByteArray &result) Q_DECL_OVERRIDE;
+ virtual void didPrintPageToPdf(const QString &filePath, bool success) Q_DECL_OVERRIDE;
virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE;
virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE;
virtual void authenticationRequired(QSharedPointer<QtWebEngineCore::AuthenticationDialogController>) Q_DECL_OVERRIDE;
diff --git a/src/webengine/doc/src/webengineview.qdoc b/src/webengine/doc/src/webengineview.qdoc
index 418da9f9d..8135cad53 100644
--- a/src/webengine/doc/src/webengineview.qdoc
+++ b/src/webengine/doc/src/webengineview.qdoc
@@ -1109,11 +1109,31 @@
*/
/*!
+ \qmlsignal WebEngineView::pdfPrintingFinished(string filePath, bool success)
+ \since QtWebEngine 1.5
+
+ This signal is emitted when printing the web page into a PDF file has
+ finished.
+ \a filePath will contain the path the file was requested to be created
+ at, and \a success will be \c true if the file was successfully created and
+ \c false otherwise.
+
+ \sa printToPdf()
+*/
+
+/*!
\qmlmethod void WebEngineView::printToPdf(const QString &filePath, PrintedPageSizeId pageSizeId, PrintedPageOrientation orientation)
\since QtWebEngine 1.3
- Prints the WebEngineView's current content to a PDF document and stores it under \a filePath. The document's size will be determined
- by the value of \a pageSizeId and its orientation will be determined using \a orientation.
+ Prints the WebEngineView's current content to a PDF document and stores it
+ under \a filePath. The document's size will be determined by the value of
+ \a pageSizeId and its orientation will be determined using \a orientation.
+
+ This method issues an asynchronous request for printing the web page into a
+ PDF and returns immediately. To be informed about the result of the
+ request, connect to the signal pdfPrintingFinished().
+
+ \sa pdfPrintingFinished()
*/
/*!
diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index 16e9438c9..c22736cab 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -359,6 +359,12 @@ void QWebEnginePagePrivate::loadFinished(bool success, const QUrl &url, bool isE
updateNavigationActions();
}
+void QWebEnginePagePrivate::didPrintPageToPdf(const QString &filePath, bool success)
+{
+ Q_Q(QWebEnginePage);
+ Q_EMIT q->pdfPrintingFinished(filePath, success);
+}
+
void QWebEnginePagePrivate::focusContainer()
{
if (view)
@@ -729,6 +735,19 @@ QWebEnginePage::QWebEnginePage(QObject* parent)
*/
/*!
+ \fn void QWebEnginePage::pdfPrintingFinished(const QString &filePath, bool success)
+ \since 5.9
+
+ This signal is emitted when printing the web page into a PDF file has
+ finished.
+ \a filePath will contain the path the file was requested to be created
+ at, and \a success will be \c true if the file was successfully created and
+ \c false otherwise.
+
+ \sa printToPdf()
+*/
+
+/*!
\property QWebEnginePage::scrollPosition
\since 5.7
@@ -1936,11 +1955,19 @@ QSizeF QWebEnginePage::contentsSize() const
}
/*!
- Renders the current content of the page into a PDF document and saves it in the location specified in \a filePath.
- The page size and orientation of the produced PDF document are taken from the values specified in \a pageLayout.
+ Renders the current content of the page into a PDF document and saves it
+ in the location specified in \a filePath.
+ The page size and orientation of the produced PDF document are taken from
+ the values specified in \a pageLayout.
+
+ This method issues an asynchronous request for printing the web page into
+ a PDF and returns immediately.
+ To be informed about the result of the request, connect to the signal
+ pdfPrintingFinished().
If a file already exists at the provided file path, it will be overwritten.
\since 5.7
+ \sa pdfPrintingFinished()
*/
void QWebEnginePage::printToPdf(const QString &filePath, const QPageLayout &pageLayout)
{
diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h
index c6c7add1b..2ff9ad928 100644
--- a/src/webenginewidgets/api/qwebenginepage.h
+++ b/src/webenginewidgets/api/qwebenginepage.h
@@ -322,6 +322,8 @@ Q_SIGNALS:
void audioMutedChanged(bool muted);
void recentlyAudibleChanged(bool recentlyAudible);
+ void pdfPrintingFinished(const QString &filePath, bool success);
+
protected:
virtual QWebEnginePage *createWindow(WebWindowType type);
virtual QStringList chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes);
diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h
index 0ad077a0e..99caeac86 100644
--- a/src/webenginewidgets/api/qwebenginepage_p.h
+++ b/src/webenginewidgets/api/qwebenginepage_p.h
@@ -119,6 +119,7 @@ public:
virtual void didFetchDocumentInnerText(quint64 requestId, const QString& result) Q_DECL_OVERRIDE;
virtual void didFindText(quint64 requestId, int matchCount) Q_DECL_OVERRIDE;
virtual void didPrintPage(quint64 requestId, const QByteArray &result) Q_DECL_OVERRIDE;
+ virtual void didPrintPageToPdf(const QString &filePath, bool success) Q_DECL_OVERRIDE;
virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE;
virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE;
virtual void authenticationRequired(QSharedPointer<QtWebEngineCore::AuthenticationDialogController>) Q_DECL_OVERRIDE;