diff --git a/examples/webenginewidgets/printme/data/data.qrc b/examples/webenginewidgets/printme/data/data.qrc new file mode 100644 index 0000000000000000000000000000000000000000..a9c76cc7eed4a6f04b25eab672bccda70ddc3ee3 --- /dev/null +++ b/examples/webenginewidgets/printme/data/data.qrc @@ -0,0 +1,7 @@ +<RCC> + <qresource prefix="/"> + <file>index.html</file> + <file>style.css</file> + <file>icon.svg</file> + </qresource> +</RCC> diff --git a/examples/webenginewidgets/printme/data/icon.svg b/examples/webenginewidgets/printme/data/icon.svg new file mode 100644 index 0000000000000000000000000000000000000000..b90ff26ddec6086797b7be5f9993836c0dbc0ea1 --- /dev/null +++ b/examples/webenginewidgets/printme/data/icon.svg @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> +<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" + width="94px" height="94px" viewBox="0 0 94 94" enable-background="new 0 0 94 94" xml:space="preserve"> +<g> + <circle fill="none" cx="47" cy="47" r="47"/> + <g> + <path fill="#46A2DA" d="M47,92.979c-11.779,0-23.559-4.484-32.526-13.451C-3.461,61.591-3.461,32.409,14.472,14.474 + C32.41-3.463,61.592-3.461,79.526,14.473c17.935,17.936,17.935,47.119,0.002,65.054l-0.002,0.001 + C70.559,88.495,58.779,92.979,47,92.979z"/> + </g> + <path fill="#80C342" d="M93,47C93,21.595,72.405,1,47,1C34.297,1,22.797,6.149,14.473,14.473l65.054,65.054 + C87.851,71.203,93,59.703,93,47z"/> + <g> + <path fill="#46A2DA" d="M47,65c-4.808,0-9.328-1.873-12.728-5.272c-7.018-7.019-7.018-18.438,0-25.456 + C37.672,30.873,42.192,29,47,29s9.328,1.873,12.728,5.272c7.018,7.019,7.018,18.438,0,25.456C56.328,63.127,51.808,65,47,65z"/> + <path fill="#FFFFFF" d="M62.248,59.919c6.671-7.858,6.312-19.644-1.105-27.061C57.237,28.953,52.118,27,47,27 + c-5.118,0-10.237,1.953-14.142,5.858c-7.81,7.81-7.81,20.474,0,28.284C36.763,65.047,41.882,67,47,67 + c4.379,0,8.752-1.441,12.372-4.3L77.88,81.209c0.989-0.895,1.935-1.837,2.843-2.814L62.248,59.919z M35.686,58.314 + c-6.238-6.238-6.238-16.389,0-22.627C38.708,32.664,42.726,31,47,31c4.274,0,8.292,1.664,11.314,4.686 + c6.238,6.238,6.238,16.389,0,22.627C55.292,61.336,51.274,63,47,63C42.726,63,38.708,61.336,35.686,58.314z"/> + </g> +</g> +</svg> diff --git a/examples/webenginewidgets/printme/data/index.html b/examples/webenginewidgets/printme/data/index.html new file mode 100644 index 0000000000000000000000000000000000000000..cf286e85a673b8849fea30f0c7b1f75f86676d7e --- /dev/null +++ b/examples/webenginewidgets/printme/data/index.html @@ -0,0 +1,24 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>PrintMe</title> + <link rel="stylesheet" type="text/css" href="style.css"> + <script> + function printNow() { + window.print(); + } + </script> + </head> + <body> + <form class="form"> + <img class="logo" src="icon.svg" alt="qtwebengine"> + <div class="header"> + <h1>Hello Paper World!</h1> + <h2>Press Ctrl+p to print with print preview</h2> + <h2>Press Ctrl+Shift+p to print without print preview</h2> + <h2>Click the button to print using JavaScript</h2> + <p class="button" onclick="printNow()">Print Now</p> + </form> + </body> +</html> diff --git a/examples/webenginewidgets/printme/data/style.css b/examples/webenginewidgets/printme/data/style.css new file mode 100644 index 0000000000000000000000000000000000000000..cf6a2b7bfc125178793dabc038d1ffaa07a585f4 --- /dev/null +++ b/examples/webenginewidgets/printme/data/style.css @@ -0,0 +1,72 @@ +html,body { + height:100%; + width:100%; + margin:0; +} + body { + display:flex; +} + .logo { + width: 75px; + height: 75px; + float: left; + margin: 20px 20px 0px 20px; + -webkit-animation:spin 8s linear infinite; +} + @-webkit-keyframes spin { + 100% { + -webkit-transform: rotate(360deg); + } +} + .header { + display: inline +} + .form { + width: 480px; + height: 170px; + background: -webkit-linear-gradient(bottom, #ddd, #fff); + border: 1px solid #999; + border-radius: 12px; + color: #46a; + font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif; + font-size: 14px; + font-style: italic; + font-weight: bold; + margin: auto; + padding: 10px; + position: relative; + line-height: 26px; + text-decoration: none; + -webkit-box-shadow: 0px 0px 5px #444; +} + h1 { + padding-left:40px; + color: #46a2da; +} + h2 { + color: #80c342; + font-size: 13px; + margin-top: -20px; +} + span { + margin-left: 20px; +} + .button{ + display: inline-block; + background: #46a2da; + width: 100px; + height: 30px; + padding: 0px; + text-align: center; + font-weight: bold; + color: #ffffff; + text-decoration: none; + border: 1px solid #999; + margin-left: 190px; +} + .button:hover { + background-color: #46a200 +} + .button:active { + background-color: #3e8e41; +} diff --git a/examples/webenginewidgets/printme/doc/images/printme-example.png b/examples/webenginewidgets/printme/doc/images/printme-example.png new file mode 100644 index 0000000000000000000000000000000000000000..a636972fd5a1539ec41965305420d1810c92b86c Binary files /dev/null and b/examples/webenginewidgets/printme/doc/images/printme-example.png differ diff --git a/examples/webenginewidgets/printme/doc/src/printme.qdoc b/examples/webenginewidgets/printme/doc/src/printme.qdoc new file mode 100644 index 0000000000000000000000000000000000000000..d05d5147bb5580ba4a68226f18c3b0e9ffdde6c2 --- /dev/null +++ b/examples/webenginewidgets/printme/doc/src/printme.qdoc @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webenginewidgets/printme + \title WebEngine Widgets PrintMe Example + \ingroup webengine-widgetexamples + \brief Demonstrates how to print web pages using Qt WebEngine Widgets. + + \image printme-example.png + + \e PrintMe demonstrates how to use the \l{QWebEnginePage} and + \l{QPrintDialog} classes to print a web page. Further, it shows how + to implement print preview by using the \l{QPrintPreviewDialog} class. + For completeness, it also illustrates how to trigger a printing request + within JavaScript. + + \include examples-run.qdocinc + + \section1 Simple HTML Page + + In this example, we create an internal HTML page that is added as a resource + collection file (.qrc). The page shows only a small HTML message box that + explains how to trigger printing by using keyboard shortcuts or clicking a + button. The button has the JavaScript \c{onclick} event attribute that calls + the JavaScript \c{window.print()} function. + + \quotefromfile webenginewidgets/printme/data/index.html + \skipto <html + \printuntil html> + + \section1 Main Function + + In the \c main function, we first instantiate a QWebEngineView and set the + URL to our internal HTML page. Next, we create a \c PrintHandler instance + and pass the requested page. For convenience, we also create keyboard + shortcuts that can be used to call a print dialog or print preview dialog. + + \quotefromfile webenginewidgets/printme/main.cpp + \skipto QWebEngineView view + \printto return + + \section1 Print Handler + + In the \c{PrintHandler} function, we first implement \c{printPreview()}, + where we instantiate \l{QPrinter} together with \l{QPrintPreviewDialog}. + We need the \l{QPrintPreviewDialog::paintRequested} handle to generate a + set of preview pages. + + \quotefromfile webenginewidgets/printme/printhandler.cpp + \skipto PrintHandler::printPreview( + \printuntil /^\}/ + + Now we can implement the \c{PrintHandler::printDocument()} slot, which is + called in response to the \l{QPrintPreviewDialog::paintRequested} signal. + + \quotefromfile webenginewidgets/printme/printhandler.cpp + \skipto PrintHandler::printDocument( + \printuntil /^\}/ + + To do actual painting on a printer, we call the \l{QWebEnginePage::print()} + function. Because this call blocks the main event loop, we need to create + a local one. We begin the local event loop by calling + \l{QEventLoop::exec()}. When the local event loop terminates, + we check for \c{result} and report any errors that occurred. + + The last function we implement, \c{PrintHandler::print()}, is trivial, + because it simply opens \l{QPrintDialog} and calls the previously + implemented \c{PrintHandler::printDocument()}. + + \quotefromfile webenginewidgets/printme/printhandler.cpp + \skipto PrintHandler::print( + \printuntil /^\}/ +*/ diff --git a/examples/webenginewidgets/printme/main.cpp b/examples/webenginewidgets/printme/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4d6d9dc8ab956b1ca3f6fc9dae245cdf80f481b --- /dev/null +++ b/examples/webenginewidgets/printme/main.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "printhandler.h" +#include <QApplication> +#include <QShortcut> +#include <QWebEngineView> + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication app(argc, argv); + + QWebEngineView view; + view.setUrl(QUrl(QStringLiteral("qrc:/index.html"))); + view.resize(1024, 750); + view.show(); + + PrintHandler handler; + handler.setPage(view.page()); + + auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_P), &view); + auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P), &view); + + QObject::connect(printPreviewShortCut, &QShortcut::activated, &handler, &PrintHandler::printPreview); + QObject::connect(printShortCut, &QShortcut::activated, &handler, &PrintHandler::print); + + return app.exec(); +} diff --git a/examples/webenginewidgets/printme/printhandler.cpp b/examples/webenginewidgets/printme/printhandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8c4fc17762a48b20e27dc15fd6b4f2d90882fdf --- /dev/null +++ b/examples/webenginewidgets/printme/printhandler.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "printhandler.h" +#include <QEventLoop> +#include <QPrintDialog> +#include <QPrinter> +#include <QPainter> +#include <QPrintPreviewDialog> +#include <QWebEnginePage> + +PrintHandler::PrintHandler(QObject *parent) + : QObject(parent) +{ + +} + +void PrintHandler::setPage(QWebEnginePage *page) +{ + Q_ASSERT(!m_page); + m_page = page; + connect(m_page, &QWebEnginePage::printRequested, this, &PrintHandler::printPreview); +} + +void PrintHandler::print() +{ + QPrinter printer(QPrinter::HighResolution); + QPrintDialog dialog(&printer, m_page->view()); + if (dialog.exec() != QDialog::Accepted) + return; + printDocument(&printer); +} + +void PrintHandler::printDocument(QPrinter *printer) +{ + QEventLoop loop; + bool result; + auto printPreview = [&](bool success) { result = success; loop.quit(); }; + m_page->print(printer, std::move(printPreview)); + loop.exec(); + if (!result) { + QPainter painter; + if (painter.begin(printer)) { + QFont font = painter.font(); + font.setPixelSize(20); + painter.setFont(font); + painter.drawText(QPointF(10,25), + QStringLiteral("Could not generate print preview.")); + + painter.end(); + } + } +} + +void PrintHandler::printPreview() +{ + if (!m_page) + return; + if (m_inPrintPreview) + return; + m_inPrintPreview = true; + QPrinter printer; + QPrintPreviewDialog preview(&printer, m_page->view()); + connect(&preview, &QPrintPreviewDialog::paintRequested, + this, &PrintHandler::printDocument); + preview.exec(); + m_inPrintPreview = false; +} diff --git a/examples/webenginewidgets/printme/printhandler.h b/examples/webenginewidgets/printme/printhandler.h new file mode 100644 index 0000000000000000000000000000000000000000..69b71f4a8c4c5bd23f35e1a71e5bacd51fa2d183 --- /dev/null +++ b/examples/webenginewidgets/printme/printhandler.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PRINTHANDLER_H +#define PRINTHANDLER_H + +#include <QObject> + +QT_BEGIN_NAMESPACE +class QPainter; +class QPrinter; +class QWebEnginePage; +QT_END_NAMESPACE + +class PrintHandler : public QObject +{ + Q_OBJECT +public: + PrintHandler(QObject *parent = nullptr); + void setPage(QWebEnginePage *page); + +public slots: + void print(); + void printPreview(); + void printDocument(QPrinter *printer); + +private: + QWebEnginePage *m_page = nullptr; + bool m_inPrintPreview = false; +}; + +#endif // PRINTHANDLER_H diff --git a/examples/webenginewidgets/printme/printme.pro b/examples/webenginewidgets/printme/printme.pro new file mode 100644 index 0000000000000000000000000000000000000000..15b3959d51a3693ec5391d87c3facabf9b7336e0 --- /dev/null +++ b/examples/webenginewidgets/printme/printme.pro @@ -0,0 +1,9 @@ +QT += webenginewidgets printsupport + +HEADERS = printhandler.h +SOURCES = main.cpp \ + printhandler.cpp +RESOURCES = data/data.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/printme +INSTALLS += target diff --git a/examples/webenginewidgets/webenginewidgets.pro b/examples/webenginewidgets/webenginewidgets.pro index 20c7ead50636a2ba23be80e034621dbfd71d259d..0d47aac80ffe9c2eb6f3c8b768dea070a85187d3 100644 --- a/examples/webenginewidgets/webenginewidgets.pro +++ b/examples/webenginewidgets/webenginewidgets.pro @@ -1,5 +1,5 @@ include($$QTWEBENGINE_OUT_ROOT/src/core/qtwebenginecore-config.pri) # workaround for QTBUG-68093 -QT_FOR_CONFIG += webenginecore +QT_FOR_CONFIG += webenginecore webenginecore-private TEMPLATE=subdirs @@ -7,7 +7,6 @@ SUBDIRS += \ minimal \ contentmanipulation \ cookiebrowser \ - html2pdf \ simplebrowser \ stylesheetbrowser \ videoplayer \ @@ -16,6 +15,10 @@ SUBDIRS += \ qtConfig(webengine-geolocation): SUBDIRS += maps qtConfig(webengine-webchannel): SUBDIRS += markdowneditor +qtConfig(webengine-printing-and-pdf) { + SUBDIRS += printme html2pdf +} + qtConfig(webengine-spellchecker):!qtConfig(webengine-native-spellchecker):!cross_compile { SUBDIRS += spellchecker } else {