Debian Codegraph @

qt5/tests/test-poppler-qt5.cpp

raw
1 #include <QtCore/QDebug>
2 #include <QtCore/QFile>
3 #include <QtWidgets/QApplication>
4 #include <QtGui/QImage>
5 #include <QtWidgets/QLabel>
6 #include <QtGui/QMouseEvent>
7 #include <QtGui/QPainter>
8 #include <QtGui/QPaintEvent>
9 #include <QtWidgets/QToolTip>
10 #include <QtWidgets/QWidget>
11
12 #include <poppler-qt5.h>
13
14 class PDFDisplay : public QWidget // picture display widget
15 {
16 Q_OBJECT
17 public:
18 PDFDisplay(Poppler::Document *d, bool qpainter, QWidget *parent = nullptr);
19 ~PDFDisplay() override;
20 void setShowTextRects(bool show);
21 void display();
22
23 protected:
24 void paintEvent(QPaintEvent *) override;
25 void keyPressEvent(QKeyEvent *) override;
26 void mousePressEvent(QMouseEvent *) override;
27
28 private:
29 int m_currentPage;
30 QImage image;
31 Poppler::Document *doc;
32 QString backendString;
33 bool showTextRects;
34 QList<Poppler::TextBox *> textRects;
35 };
36
37 PDFDisplay::PDFDisplay(Poppler::Document *d, bool qpainter, QWidget *parent) : QWidget(parent)
38 {
39 showTextRects = false;
40 doc = d;
41 m_currentPage = 0;
42 if (qpainter) {
43 backendString = QStringLiteral("QPainter");
44 doc->setRenderBackend(Poppler::Document::QPainterBackend);
45 } else {
46 backendString = QStringLiteral("Splash");
47 doc->setRenderBackend(Poppler::Document::SplashBackend);
48 }
49 doc->setRenderHint(Poppler::Document::Antialiasing, true);
50 doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
51 }
52
53 void PDFDisplay::setShowTextRects(bool show)
54 {
55 showTextRects = show;
56 }
57
58 void PDFDisplay::display()
59 {
60 if (doc) {
61 Poppler::Page *page = doc->page(m_currentPage);
62 if (page) {
63 qDebug() << "Displaying page using" << backendString << "backend: " << m_currentPage;
64 QTime t = QTime::currentTime();
65 image = page->renderToImage();
66 qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs";
67 qDeleteAll(textRects);
68 if (showTextRects) {
69 QPainter painter(&image);
70 painter.setPen(Qt::red);
71 textRects = page->textList();
72 Q_FOREACH (Poppler::TextBox *tb, textRects) {
73 painter.drawRect(tb->boundingBox());
74 }
75 } else {
76 textRects.clear();
77 }
78 update();
79 delete page;
80 }
81 } else {
82 qWarning() << "doc not loaded";
83 }
84 }
85
86 PDFDisplay::~PDFDisplay()
87 {
88 qDeleteAll(textRects);
89 delete doc;
90 }
91
92 void PDFDisplay::paintEvent(QPaintEvent * /*e*/)
93 {
94 QPainter paint(this); // paint widget
95 if (!image.isNull()) {
96 paint.drawImage(0, 0, image);
97 } else {
98 qWarning() << "null image";
99 }
100 }
101
102 void PDFDisplay::keyPressEvent(QKeyEvent *e)
103 {
104 if (e->key() == Qt::Key_Down) {
105 if (m_currentPage + 1 < doc->numPages()) {
106 m_currentPage++;
107 display();
108 }
109 } else if (e->key() == Qt::Key_Up) {
110 if (m_currentPage > 0) {
111 m_currentPage--;
112 display();
113 }
114 } else if (e->key() == Qt::Key_Q) {
115 exit(0);
116 }
117 }
118
119 void PDFDisplay::mousePressEvent(QMouseEvent *e)
120 {
121 int i = 0;
122 Q_FOREACH (Poppler::TextBox *tb, textRects) {
123 if (tb->boundingBox().contains(e->pos())) {
124 const QString tt = QStringLiteral("Text: \"%1\"\nIndex in text list: %2").arg(tb->text()).arg(i);
125 QToolTip::showText(e->globalPos(), tt, this);
126 break;
127 }
128 ++i;
129 }
130 }
131
132 int main(int argc, char **argv)
133 {
134 QApplication a(argc, argv); // QApplication required!
135
136 if (argc < 2 || (argc == 3 && strcmp(argv[2], "-extract") != 0 && strcmp(argv[2], "-qpainter") != 0 && strcmp(argv[2], "-textRects") != 0) || argc > 3) {
137 // use argument as file name
138 qWarning() << "usage: test-poppler-qt5 filename [-extract|-qpainter|-textRects]";
139 exit(1);
140 }
141
142 Poppler::Document *doc = Poppler::Document::load(QFile::decodeName(argv[1]));
143 if (!doc) {
144 qWarning() << "doc not loaded";
145 exit(1);
146 }
147
148 if (doc->isLocked()) {
149 qWarning() << "document locked (needs password)";
150 exit(0);
151 }
152
153 // output some meta-data
154 Poppler::Document::PdfVersion pdfVersion = doc->getPdfVersion();
155 qDebug() << " PDF Version: " << qPrintable(QStringLiteral("%1.%2").arg(pdfVersion.major).arg(pdfVersion.minor));
156 qDebug() << " Title: " << doc->info(QStringLiteral("Title"));
157 qDebug() << " Subject: " << doc->info(QStringLiteral("Subject"));
158 qDebug() << " Author: " << doc->info(QStringLiteral("Author"));
159 qDebug() << " Key words: " << doc->info(QStringLiteral("Keywords"));
160 qDebug() << " Creator: " << doc->info(QStringLiteral("Creator"));
161 qDebug() << " Producer: " << doc->info(QStringLiteral("Producer"));
162 qDebug() << " Date created: " << doc->date(QStringLiteral("CreationDate")).toString();
163 qDebug() << " Date modified: " << doc->date(QStringLiteral("ModDate")).toString();
164 qDebug() << "Number of pages: " << doc->numPages();
165 qDebug() << " Linearised: " << doc->isLinearized();
166 qDebug() << " Encrypted: " << doc->isEncrypted();
167 qDebug() << " OK to print: " << doc->okToPrint();
168 qDebug() << " OK to copy: " << doc->okToCopy();
169 qDebug() << " OK to change: " << doc->okToChange();
170 qDebug() << "OK to add notes: " << doc->okToAddNotes();
171 qDebug() << " Page mode: " << doc->pageMode();
172 qDebug() << " Metadata: " << doc->metadata();
173
174 if (doc->hasEmbeddedFiles()) {
175 qDebug() << "Embedded files:";
176 Q_FOREACH (Poppler::EmbeddedFile *file, doc->embeddedFiles()) {
177 qDebug() << " " << file->name();
178 }
179 qDebug();
180 } else {
181 qDebug() << "No embedded files";
182 }
183
184 if (doc->numPages() <= 0) {
185 delete doc;
186 qDebug() << "Doc has no pages";
187 return 0;
188 }
189
190 {
191 Poppler::Page *page = doc->page(0);
192 if (page) {
193 qDebug() << "Page 1 size: " << page->pageSize().width() / 72 << "inches x " << page->pageSize().height() / 72 << "inches";
194 delete page;
195 }
196 }
197
198 if (argc == 2 || (argc == 3 && strcmp(argv[2], "-qpainter") == 0) || (argc == 3 && strcmp(argv[2], "-textRects") == 0)) {
199 bool useQPainter = (argc == 3 && strcmp(argv[2], "-qpainter") == 0);
200 PDFDisplay test(doc, useQPainter); // create picture display
201 test.setWindowTitle(QStringLiteral("Poppler-Qt5 Test"));
202 test.setShowTextRects(argc == 3 && strcmp(argv[2], "-textRects") == 0);
203 test.display();
204 test.show(); // show it
205
206 return QApplication::exec(); // start event loop
207 } else {
208 Poppler::Page *page = doc->page(0);
209
210 QLabel *l = new QLabel(page->text(QRectF()), nullptr);
211 l->show();
212 delete page;
213 delete doc;
214 return QApplication::exec();
215 }
216 }
217
218 #include "test-poppler-qt5.moc"