File indexing completed on 2024-05-12 16:06:42

0001 /*
0002     SPDX-FileCopyrightText: 2008 Tobias Koenig <tokoe@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "generator_fax.h"
0007 
0008 #include <QPainter>
0009 #include <QPrinter>
0010 
0011 #include <KAboutData>
0012 #include <KLocalizedString>
0013 
0014 #include <core/document.h>
0015 #include <core/page.h>
0016 
0017 OKULAR_EXPORT_PLUGIN(FaxGenerator, "libokularGenerator_fax.json")
0018 
0019 FaxGenerator::FaxGenerator(QObject *parent, const QVariantList &args)
0020     : Generator(parent, args)
0021 {
0022     setFeature(Threaded);
0023     setFeature(PrintNative);
0024     setFeature(PrintToFile);
0025 }
0026 
0027 FaxGenerator::~FaxGenerator()
0028 {
0029 }
0030 
0031 bool FaxGenerator::loadDocument(const QString &fileName, QVector<Okular::Page *> &pagesVector)
0032 {
0033     if (fileName.endsWith(QLatin1String(".g3"), Qt::CaseInsensitive)) {
0034         m_type = FaxDocument::G3;
0035     } else {
0036         m_type = FaxDocument::G4;
0037     }
0038 
0039     FaxDocument faxDocument(fileName, m_type);
0040 
0041     if (!faxDocument.load()) {
0042         Q_EMIT error(i18n("Unable to load document"), -1);
0043         return false;
0044     }
0045 
0046     m_img = faxDocument.image();
0047 
0048     pagesVector.resize(1);
0049 
0050     Okular::Page *page = new Okular::Page(0, m_img.width(), m_img.height(), Okular::Rotation0);
0051     pagesVector[0] = page;
0052 
0053     return true;
0054 }
0055 
0056 bool FaxGenerator::doCloseDocument()
0057 {
0058     m_img = QImage();
0059 
0060     return true;
0061 }
0062 
0063 QImage FaxGenerator::image(Okular::PixmapRequest *request)
0064 {
0065     // perform a smooth scaled generation
0066     int width = request->width();
0067     int height = request->height();
0068     if (request->page()->rotation() % 2 == 1) {
0069         std::swap(width, height);
0070     }
0071 
0072     return m_img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
0073 }
0074 
0075 Okular::DocumentInfo FaxGenerator::generateDocumentInfo(const QSet<Okular::DocumentInfo::Key> &keys) const
0076 {
0077     Okular::DocumentInfo docInfo;
0078     if (keys.contains(Okular::DocumentInfo::MimeType)) {
0079         if (m_type == FaxDocument::G3) {
0080             docInfo.set(Okular::DocumentInfo::MimeType, QStringLiteral("image/fax-g3"));
0081         } else {
0082             docInfo.set(Okular::DocumentInfo::MimeType, QStringLiteral("image/fax-g4"));
0083         }
0084     }
0085     return docInfo;
0086 }
0087 
0088 Okular::Document::PrintError FaxGenerator::print(QPrinter &printer)
0089 {
0090     QPainter p(&printer);
0091 
0092     QImage image(m_img);
0093 
0094     if ((image.width() > printer.width()) || (image.height() > printer.height())) {
0095         image = image.scaled(printer.width(), printer.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
0096     }
0097 
0098     p.drawImage(0, 0, image);
0099 
0100     return Okular::Document::NoPrintError;
0101 }
0102 
0103 #include "generator_fax.moc"