1 /*
2 SPDX-FileCopyrightText: 2008 Tobias Koenig <tokoe@kde.org>
3 SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6 #include "generator_fax.h"
7
8 #include <QPainter>
9 #include <QPrinter>
10
11 #include <KAboutData>
12 #include <KLocalizedString>
13
14 #include <core/document.h>
15 #include <core/page.h>
16
17 OKULAR_EXPORT_PLUGIN(FaxGenerator, "libokularGenerator_fax.json")
18
FaxGenerator(QObject * parent,const QVariantList & args)19 FaxGenerator::FaxGenerator(QObject *parent, const QVariantList &args)
20 : Generator(parent, args)
21 {
22 setFeature(Threaded);
23 setFeature(PrintNative);
24 setFeature(PrintToFile);
25 }
26
~FaxGenerator()27 FaxGenerator::~FaxGenerator()
28 {
29 }
30
loadDocument(const QString & fileName,QVector<Okular::Page * > & pagesVector)31 bool FaxGenerator::loadDocument(const QString &fileName, QVector<Okular::Page *> &pagesVector)
32 {
33 if (fileName.endsWith(QLatin1String(".g3"), Qt::CaseInsensitive))
34 m_type = FaxDocument::G3;
35 else
36 m_type = FaxDocument::G4;
37
38 FaxDocument faxDocument(fileName, m_type);
39
40 if (!faxDocument.load()) {
41 emit error(i18n("Unable to load document"), -1);
42 return false;
43 }
44
45 m_img = faxDocument.image();
46
47 pagesVector.resize(1);
48
49 Okular::Page *page = new Okular::Page(0, m_img.width(), m_img.height(), Okular::Rotation0);
50 pagesVector[0] = page;
51
52 return true;
53 }
54
doCloseDocument()55 bool FaxGenerator::doCloseDocument()
56 {
57 m_img = QImage();
58
59 return true;
60 }
61
image(Okular::PixmapRequest * request)62 QImage FaxGenerator::image(Okular::PixmapRequest *request)
63 {
64 // perform a smooth scaled generation
65 int width = request->width();
66 int height = request->height();
67 if (request->page()->rotation() % 2 == 1)
68 qSwap(width, height);
69
70 return m_img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
71 }
72
generateDocumentInfo(const QSet<Okular::DocumentInfo::Key> & keys) const73 Okular::DocumentInfo FaxGenerator::generateDocumentInfo(const QSet<Okular::DocumentInfo::Key> &keys) const
74 {
75 Okular::DocumentInfo docInfo;
76 if (keys.contains(Okular::DocumentInfo::MimeType)) {
77 if (m_type == FaxDocument::G3)
78 docInfo.set(Okular::DocumentInfo::MimeType, QStringLiteral("image/fax-g3"));
79 else
80 docInfo.set(Okular::DocumentInfo::MimeType, QStringLiteral("image/fax-g4"));
81 }
82 return docInfo;
83 }
84
print(QPrinter & printer)85 bool FaxGenerator::print(QPrinter &printer)
86 {
87 QPainter p(&printer);
88
89 QImage image(m_img);
90
91 if ((image.width() > printer.width()) || (image.height() > printer.height()))
92
93 image = image.scaled(printer.width(), printer.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
94
95 p.drawImage(0, 0, image);
96
97 return true;
98 }
99
100 #include "generator_fax.moc"
101