From 4f5380f413a8a5e89ee57f301a9d6010eac92ad4 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Fri, 6 Jul 2018 15:11:49 +0200 Subject: Add PrintMe example This example shows how to do simple printing with print preview. Task-number: QTBUG-53745 Change-Id: I4b1adbb9847b72928a988b5687fb13c84bb987cd Reviewed-by: Allan Sandfeld Jensen --- examples/webenginewidgets/printme/data/data.qrc | 7 ++ examples/webenginewidgets/printme/data/icon.svg | 24 +++++ examples/webenginewidgets/printme/data/index.html | 24 +++++ examples/webenginewidgets/printme/data/style.css | 72 +++++++++++++ .../printme/doc/images/printme-example.png | Bin 0 -> 42074 bytes .../webenginewidgets/printme/doc/src/printme.qdoc | 98 ++++++++++++++++++ examples/webenginewidgets/printme/main.cpp | 76 ++++++++++++++ examples/webenginewidgets/printme/printhandler.cpp | 115 +++++++++++++++++++++ examples/webenginewidgets/printme/printhandler.h | 79 ++++++++++++++ examples/webenginewidgets/printme/printme.pro | 9 ++ examples/webenginewidgets/webenginewidgets.pro | 7 +- 11 files changed, 509 insertions(+), 2 deletions(-) create mode 100644 examples/webenginewidgets/printme/data/data.qrc create mode 100644 examples/webenginewidgets/printme/data/icon.svg create mode 100644 examples/webenginewidgets/printme/data/index.html create mode 100644 examples/webenginewidgets/printme/data/style.css create mode 100644 examples/webenginewidgets/printme/doc/images/printme-example.png create mode 100644 examples/webenginewidgets/printme/doc/src/printme.qdoc create mode 100644 examples/webenginewidgets/printme/main.cpp create mode 100644 examples/webenginewidgets/printme/printhandler.cpp create mode 100644 examples/webenginewidgets/printme/printhandler.h create mode 100644 examples/webenginewidgets/printme/printme.pro (limited to 'examples/webenginewidgets') diff --git a/examples/webenginewidgets/printme/data/data.qrc b/examples/webenginewidgets/printme/data/data.qrc new file mode 100644 index 000000000..a9c76cc7e --- /dev/null +++ b/examples/webenginewidgets/printme/data/data.qrc @@ -0,0 +1,7 @@ + + + index.html + style.css + icon.svg + + diff --git a/examples/webenginewidgets/printme/data/icon.svg b/examples/webenginewidgets/printme/data/icon.svg new file mode 100644 index 000000000..b90ff26dd --- /dev/null +++ b/examples/webenginewidgets/printme/data/icon.svg @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + diff --git a/examples/webenginewidgets/printme/data/index.html b/examples/webenginewidgets/printme/data/index.html new file mode 100644 index 000000000..cf286e85a --- /dev/null +++ b/examples/webenginewidgets/printme/data/index.html @@ -0,0 +1,24 @@ + + + + + PrintMe + + + + +
+ +
+

Hello Paper World!

+

Press Ctrl+p to print with print preview

+

Press Ctrl+Shift+p to print without print preview

+

Click the button to print using JavaScript

+

Print Now

+ + + diff --git a/examples/webenginewidgets/printme/data/style.css b/examples/webenginewidgets/printme/data/style.css new file mode 100644 index 000000000..cf6a2b7bf --- /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 000000000..a636972fd 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 000000000..d05d5147b --- /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 + + \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 000000000..e4d6d9dc8 --- /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 +#include +#include + +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 000000000..d8c4fc177 --- /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 +#include +#include +#include +#include +#include + +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 000000000..69b71f4a8 --- /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 + +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 000000000..15b3959d5 --- /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 20c7ead50..0d47aac80 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 { -- cgit v1.2.3