diff options
Diffstat (limited to 'examples/webenginewidgets/demobrowser/browsermainwindow.cpp')
-rw-r--r-- | examples/webenginewidgets/demobrowser/browsermainwindow.cpp | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp index 946810cef..0f24de707 100644 --- a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp +++ b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp @@ -1,12 +1,22 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Contact: https://www.qt.io/licensing/ ** -** This file is part of the examples of the Qt Toolkit. +** This file is part of the demonstration applications of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 @@ -302,8 +312,12 @@ void BrowserMainWindow::setupMenu() #if defined(QWEBENGINEPAGE_PRINT) fileMenu->addAction(tr("P&rint Preview..."), this, SLOT(slotFilePrintPreview())); fileMenu->addAction(tr("&Print..."), this, SLOT(slotFilePrint()), QKeySequence::Print); - fileMenu->addSeparator(); #endif +#ifndef QT_NO_PRINTER + fileMenu->addAction(tr("&Print to PDF..."), this, SLOT(slotFilePrintToPDF())); +#endif // ifndef QT_NO_PRINTER + fileMenu->addSeparator(); + QAction *action = fileMenu->addAction(tr("Private &Browsing..."), this, SLOT(slotPrivateBrowsing())); action->setCheckable(true); action->setChecked(BrowserApplication::instance()->privateBrowsing()); @@ -687,6 +701,39 @@ void BrowserMainWindow::slotFilePrint() #endif } +void BrowserMainWindow::slotHandlePdfPrinted(const QByteArray& result) +{ + if (!result.size()) + return; + + QFile file(m_printerOutputFileName); + + m_printerOutputFileName.clear(); + if (!file.open(QFile::WriteOnly)) + return; + + file.write(result.data(), result.size()); + file.close(); +} + +void BrowserMainWindow::slotFilePrintToPDF() +{ +#ifndef QT_NO_PRINTER + if (!currentTab()) + return; + QPrinter printer; + QPrintDialog *dialog = new QPrintDialog(&printer, this); + dialog->setWindowTitle(tr("Print Document")); + if (dialog->exec() != QDialog::Accepted || printer.outputFileName().isEmpty() || !m_printerOutputFileName.isEmpty()) + return; + + m_printerOutputFileName = printer.outputFileName(); + + currentTab()->page()->printToPdf(invoke(this, &BrowserMainWindow::slotHandlePdfPrinted), printer.pageLayout()); + +#endif // QT_NO_PRINTER +} + #if defined(QWEBENGINEPAGE_PRINT) void BrowserMainWindow::printRequested(QWebEngineFrame *frame) { |