Warning, file /utilities/skanpage/src/DocumentPrinter.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "DocumentPrinter.h"
0008 
0009 #include <QPainter>
0010 #include <QPrinter>
0011 #include <QPrintDialog>
0012 
0013 #include <QTransform>
0014 #include <QtConcurrent>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include "skanpage_debug.h"
0019 
0020 DocumentPrinter::DocumentPrinter(QObject *parent)
0021     : QObject(parent)
0022     , m_printer(std::make_unique<QPrinter>())
0023 {
0024 }
0025 
0026 DocumentPrinter::~DocumentPrinter()
0027 {
0028 }
0029 
0030 void DocumentPrinter::printDocument(const SkanpageUtils::DocumentPages &document)
0031 {
0032     if (document.isEmpty()) {
0033         Q_EMIT showUserMessage(SkanpageUtils::ErrorMessage, i18n("Nothing to print."));
0034         return;
0035     }
0036     if (m_future.isRunning()) {
0037         Q_EMIT showUserMessage(SkanpageUtils::ErrorMessage, i18n("Previous printing operation still in progress."));
0038         return;
0039     }
0040 
0041     QPrintDialog printDialog(m_printer.get());
0042 
0043     if (printDialog.exec() == QDialog::Accepted) {
0044 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0045         m_future = QtConcurrent::run(this, &DocumentPrinter::print, document);
0046 #else
0047         m_future = QtConcurrent::run(&DocumentPrinter::print, this, document);
0048 #endif
0049     }
0050 }
0051 
0052 void DocumentPrinter::print(const SkanpageUtils::DocumentPages &document)
0053 {
0054     int rotationAngle;
0055     QPainter painter(m_printer.get());
0056     QRect rect = painter.viewport();
0057     for (int i = 0; i < document.count(); ++i) {
0058         if (i != 0) {
0059             m_printer->newPage();
0060         }
0061         rotationAngle = document.at(i).rotationAngle;
0062         QImage pageImage(document.at(i).temporaryFile->fileName());
0063         if (rotationAngle != 0) {
0064             pageImage = pageImage.transformed(QTransform().rotate(rotationAngle));
0065         }
0066 
0067         QSize size = pageImage.size();
0068         size.scale(rect.size(), Qt::KeepAspectRatio);
0069         painter.setViewport(rect.x(), rect.y(),size.width(), size.height());
0070         painter.setWindow(pageImage.rect());
0071         painter.drawImage(0, 0, pageImage);
0072     }
0073 
0074     Q_EMIT showUserMessage(SkanpageUtils::InformationMessage, i18n("Document sent to printer."));
0075 }