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 /src/core | |
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>
Diffstat (limited to 'src/core')
-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 |
4 files changed, 39 insertions, 12 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); |