diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-06-08 13:11:55 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-06-22 14:23:30 +0000 |
commit | a693cae520455ece9bf860ab2804f03d465d51af (patch) | |
tree | 530c06f750c0554af3e46f368be96f7fe80cadb2 | |
parent | 3752a3cbcd0cc0742a5b955e925df18395afdb75 (diff) |
Add QPageRanges to PDF printing
Task-number: QTBUG-73497
Change-Id: I0a66c4f1767c54b0bcc9f9a3b61e29c43ec20177
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
(cherry picked from commit 0887e880ced988704559505816b520972e6876c6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/core/printing/print_view_manager_qt.cpp | 23 | ||||
-rw-r--r-- | src/core/printing/print_view_manager_qt.h | 6 | ||||
-rw-r--r-- | src/core/web_contents_adapter.cpp | 17 | ||||
-rw-r--r-- | src/core/web_contents_adapter.h | 5 | ||||
-rw-r--r-- | src/webenginequick/api/qquickwebengineview.cpp | 7 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebengineview.cpp | 15 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebengineview.h | 9 |
7 files changed, 60 insertions, 22 deletions
diff --git a/src/core/printing/print_view_manager_qt.cpp b/src/core/printing/print_view_manager_qt.cpp index de1b81fb9..32e636e6d 100644 --- a/src/core/printing/print_view_manager_qt.cpp +++ b/src/core/printing/print_view_manager_qt.cpp @@ -50,6 +50,7 @@ #include "web_engine_context.h" #include <QtGui/qpagelayout.h> +#include <QtGui/qpageranges.h> #include <QtGui/qpagesize.h> #include "base/values.h" @@ -185,6 +186,18 @@ static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayo return printSettings; } +static base::ListValue *createPageRangeSettings(const QList<QPageRanges::Range> &ranges) +{ + base::ListValue *pageRangeArray = new base::ListValue; + for (int i = 0; i < ranges.count(); i++) { + std::unique_ptr<base::DictionaryValue> pageRange(new base::DictionaryValue); + pageRange->SetInteger(printing::kSettingPageRangeFrom, ranges.at(i).from); + pageRange->SetInteger(printing::kSettingPageRangeTo, ranges.at(i).to); + pageRangeArray->Append(std::move(pageRange)); + } + return pageRangeArray; +} + } // namespace namespace QtWebEngineCore { @@ -207,6 +220,7 @@ PrintViewManagerQt::~PrintViewManagerQt() } void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, bool printInColor, const QString &filePath, const PrintToPDFFileCallback& callback) @@ -222,7 +236,7 @@ void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayou m_pdfOutputPath = toFilePath(filePath); m_pdfSaveCallback = callback; - if (!PrintToPDFInternal(pageLayout, printInColor)) { + if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(callback, false)); resetPdfState(); @@ -230,6 +244,7 @@ void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayou } void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, bool printInColor, bool useCustomMargins, const PrintToPDFCallback& callback) @@ -245,7 +260,7 @@ void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, } m_pdfPrintCallback = callback; - if (!PrintToPDFInternal(pageLayout, printInColor, useCustomMargins)) { + if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor, useCustomMargins)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(callback, QSharedPointer<QByteArray>())); @@ -254,6 +269,7 @@ void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, } bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, const bool printInColor, const bool useCustomMargins) { @@ -265,6 +281,9 @@ bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, web_contents()->GetOrCreateWebPreferences().should_print_backgrounds); m_printSettings->SetInteger(printing::kSettingColor, int(printInColor ? printing::mojom::ColorModel::kColor : printing::mojom::ColorModel::kGrayscale)); + if (!pageRanges.isEmpty()) + m_printSettings->Set(printing::kSettingPageRange, + std::unique_ptr<base::ListValue>(createPageRangeSettings(pageRanges.toRangeList()))); if (web_contents()->IsCrashed()) return false; diff --git a/src/core/printing/print_view_manager_qt.h b/src/core/printing/print_view_manager_qt.h index ecb3d6053..7405d95df 100644 --- a/src/core/printing/print_view_manager_qt.h +++ b/src/core/printing/print_view_manager_qt.h @@ -60,6 +60,7 @@ QT_BEGIN_NAMESPACE class QPageLayout; +class QPageRanges; class QString; QT_END_NAMESPACE @@ -75,10 +76,12 @@ public: // Method to print a page to a Pdf document with page size \a pageSize in location \a filePath. void PrintToPDFFileWithCallback(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, bool printInColor, const QString &filePath, const PrintToPDFFileCallback& callback); void PrintToPDFWithCallback(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, bool printInColor, bool useCustomMargins, const PrintToPDFCallback &callback); @@ -110,7 +113,8 @@ protected: const printing::mojom::PreviewIds& ids); void OnShowScriptedPrintPreview(content::RenderFrameHost* rfh, bool source_is_modifiable); - bool PrintToPDFInternal(const QPageLayout &, bool printInColor, bool useCustomMargins = true); + bool PrintToPDFInternal(const QPageLayout &, const QPageRanges &, + bool printInColor, bool useCustomMargins = true); private: void resetPdfState(); diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index dd506f1e1..70b5e42aa 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -1326,7 +1326,7 @@ void WebContentsAdapter::wasHidden() m_webContents->WasHidden(); } -void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString &filePath) +void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QPageRanges &pageRanges, const QString &filePath) { #if QT_CONFIG(webengine_printing_and_pdf) CHECK_INITIALIZED(); @@ -1334,13 +1334,15 @@ void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString m_adapterClient, filePath); PrintViewManagerQt::FromWebContents(m_webContents.get())->PrintToPDFFileWithCallback(pageLayout, - true, - filePath, - callback); + pageRanges, + true, + filePath, + callback); #endif // QT_CONFIG(webengine_printing_and_pdf) } quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout, + const QPageRanges &pageRanges, bool colorMode, bool useCustomMargins) { @@ -1350,9 +1352,10 @@ quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayo m_adapterClient, m_nextRequestId); PrintViewManagerQt::FromWebContents(m_webContents.get())->PrintToPDFWithCallback(pageLayout, - colorMode, - useCustomMargins, - callback); + pageRanges, + colorMode, + useCustomMargins, + callback); return m_nextRequestId++; #else Q_UNUSED(pageLayout); diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index e9ee6f104..26cfa1b64 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -82,6 +82,7 @@ class QDragMoveEvent; class QDropEvent; class QMimeData; class QPageLayout; +class QPageRanges; class QTemporaryDir; class QWebChannel; class QWebEngineUrlRequestInterceptor; @@ -222,8 +223,8 @@ public: void endDragging(QDropEvent *e, const QPointF &screenPos); void leaveDrag(); #endif // QT_CONFIG(draganddrop) - void printToPDF(const QPageLayout&, const QString&); - quint64 printToPDFCallbackResult(const QPageLayout &, + void printToPDF(const QPageLayout&, const QPageRanges &, const QString&); + quint64 printToPDFCallbackResult(const QPageLayout &, const QPageRanges &, bool colorMode = true, bool useCustomMargins = true); diff --git a/src/webenginequick/api/qquickwebengineview.cpp b/src/webenginequick/api/qquickwebengineview.cpp index abfd0b79f..f62ff361a 100644 --- a/src/webenginequick/api/qquickwebengineview.cpp +++ b/src/webenginequick/api/qquickwebengineview.cpp @@ -82,6 +82,7 @@ #include <QMarginsF> #include <QMimeData> #include <QPageLayout> +#include <QPageRanges> #include <QPageSize> #include <QQmlComponent> #include <QQmlContext> @@ -1434,8 +1435,9 @@ void QQuickWebEngineView::printToPdf(const QString& filePath, PrintedPageSizeId QPageSize layoutSize(static_cast<QPageSize::PageSizeId>(pageSizeId)); QPageLayout::Orientation layoutOrientation = static_cast<QPageLayout::Orientation>(orientation); QPageLayout pageLayout(layoutSize, layoutOrientation, QMarginsF(0.0, 0.0, 0.0, 0.0)); + QPageRanges ranges; d->ensureContentsAdapter(); - d->adapter->printToPDF(pageLayout, filePath); + d->adapter->printToPDF(pageLayout, ranges, filePath); #else Q_UNUSED(filePath); Q_UNUSED(pageSizeId); @@ -1450,12 +1452,13 @@ void QQuickWebEngineView::printToPdf(const QJSValue &callback, PrintedPageSizeId QPageSize layoutSize(static_cast<QPageSize::PageSizeId>(pageSizeId)); QPageLayout::Orientation layoutOrientation = static_cast<QPageLayout::Orientation>(orientation); QPageLayout pageLayout(layoutSize, layoutOrientation, QMarginsF(0.0, 0.0, 0.0, 0.0)); + QPageRanges ranges; if (callback.isUndefined()) return; d->ensureContentsAdapter(); - quint64 requestId = d->adapter->printToPDFCallbackResult(pageLayout); + quint64 requestId = d->adapter->printToPDFCallbackResult(pageLayout, ranges); d->m_callbacks.insert(requestId, callback); #else Q_UNUSED(pageSizeId); diff --git a/src/webenginewidgets/api/qwebengineview.cpp b/src/webenginewidgets/api/qwebengineview.cpp index b2eb17ec7..8a3ce81a0 100644 --- a/src/webenginewidgets/api/qwebengineview.cpp +++ b/src/webenginewidgets/api/qwebengineview.cpp @@ -963,7 +963,8 @@ QWebEngineContextMenuRequest *QWebEngineView::lastContextMenuRequest() 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. + the values specified in \a pageLayout, while the range of pages printed is + taken from \a ranges with the default being printing all pages. This method issues an asynchronous request for printing the web page into a PDF and returns immediately. @@ -974,7 +975,7 @@ QWebEngineContextMenuRequest *QWebEngineView::lastContextMenuRequest() const \since 6.2 \sa pdfPrintingFinished() */ -void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layout) +void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layout, const QPageRanges &ranges) { #if QT_CONFIG(webengine_printing_and_pdf) Q_D(QWebEngineView); @@ -983,7 +984,7 @@ void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layo return; } page()->d_ptr->ensureInitialized(); - page()->d_ptr->adapter->printToPDF(layout, filePath); + page()->d_ptr->adapter->printToPDF(layout, ranges, filePath); #else Q_UNUSED(filePath); Q_UNUSED(layout); @@ -993,7 +994,8 @@ void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layo /*! Renders the current content of the page into a PDF document and returns a byte array containing the PDF data as parameter to \a resultCallback. - The page size and orientation of the produced PDF document are taken from the values specified in \a pageLayout. + The page size and orientation of the produced PDF document are taken from the values specified in \a pageLayout, + while the range of pages printed is taken from \a ranges with the default being printing all pages. The \a resultCallback must take a const reference to a QByteArray as parameter. If printing was successful, this byte array will contain the PDF data, otherwise, the byte array will be empty. @@ -1004,7 +1006,7 @@ void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layo \since 6.2 */ -void QWebEngineView::printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout) +void QWebEngineView::printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout, const QPageRanges &ranges) { Q_D(QWebEngineView); #if QT_CONFIG(webengine_printing_and_pdf) @@ -1014,7 +1016,7 @@ void QWebEngineView::printToPdf(const QWebEngineCallback<const QByteArray&> &res return; } page()->d_ptr->ensureInitialized(); - quint64 requestId = page()->d_ptr->adapter->printToPDFCallbackResult(layout); + quint64 requestId = page()->d_ptr->adapter->printToPDFCallbackResult(layout, ranges); d->m_callbacks.registerCallback(requestId, resultCallback); #else Q_UNUSED(layout); @@ -1072,6 +1074,7 @@ void QWebEngineView::print(QPrinter *printer) d->currentPrinter = printer; page()->d_ptr->ensureInitialized(); page()->d_ptr->adapter->printToPDFCallbackResult(printer->pageLayout(), + printer->pageRanges(), printer->colorMode() == QPrinter::Color, false); #else diff --git a/src/webenginewidgets/api/qwebengineview.h b/src/webenginewidgets/api/qwebengineview.h index 3faa07a6b..b43552211 100644 --- a/src/webenginewidgets/api/qwebengineview.h +++ b/src/webenginewidgets/api/qwebengineview.h @@ -41,6 +41,7 @@ #define QWEBENGINEVIEW_H #include <QtGui/qpainter.h> +#include <QtGui/qpageranges.h> #include <QtNetwork/qnetworkaccessmanager.h> #include <QtWidgets/qwidget.h> @@ -109,8 +110,12 @@ public: #endif QWebEngineContextMenuRequest *lastContextMenuRequest() const; - void printToPdf(const QString &filePath, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF())); - void printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF())); + void printToPdf(const QString &filePath, + const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()), + const QPageRanges &ranges = {}); + void printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, + const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()), + const QPageRanges &ranges = {}); void print(QPrinter *printer); public Q_SLOTS: |