Debian Codegraph @

qt5/tests/test-password-qt5.cpp

raw
1 #include <QtCore/QDebug>
2 #include <QtWidgets/QApplication>
3 #include <QtGui/QImage>
4 #include <QtGui/QPainter>
5 #include <QtGui/QPaintEvent>
6 #include <QtWidgets/QWidget>
7
8 #include <poppler-qt5.h>
9
10 class PDFDisplay : public QWidget // picture display widget
11 {
12 Q_OBJECT
13 public:
14 explicit PDFDisplay(Poppler::Document *d, QWidget *parent = nullptr);
15 ~PDFDisplay() override;
16
17 protected:
18 void paintEvent(QPaintEvent *) override;
19 void keyPressEvent(QKeyEvent *) override;
20
21 private:
22 void display();
23 int m_currentPage;
24 QImage image;
25 Poppler::Document *doc;
26 };
27
28 PDFDisplay::PDFDisplay(Poppler::Document *d, QWidget *parent) : QWidget(parent)
29 {
30 doc = d;
31 m_currentPage = 0;
32 display();
33 }
34
35 void PDFDisplay::display()
36 {
37 if (doc) {
38 Poppler::Page *page = doc->page(m_currentPage);
39 if (page) {
40 qDebug() << "Displaying page: " << m_currentPage;
41 image = page->renderToImage();
42 update();
43 delete page;
44 }
45 } else {
46 qWarning() << "doc not loaded";
47 }
48 }
49
50 PDFDisplay::~PDFDisplay()
51 {
52 delete doc;
53 }
54
55 void PDFDisplay::paintEvent(QPaintEvent * /*e*/)
56 {
57 QPainter paint(this); // paint widget
58 if (!image.isNull()) {
59 paint.drawImage(0, 0, image);
60 } else {
61 qWarning() << "null image";
62 }
63 }
64
65 void PDFDisplay::keyPressEvent(QKeyEvent *e)
66 {
67 if (e->key() == Qt::Key_Down) {
68 if (m_currentPage + 1 < doc->numPages()) {
69 m_currentPage++;
70 display();
71 }
72 } else if (e->key() == Qt::Key_Up) {
73 if (m_currentPage > 0) {
74 m_currentPage--;
75 display();
76 }
77 } else if (e->key() == Qt::Key_Q) {
78 exit(0);
79 }
80 }
81
82 int main(int argc, char **argv)
83 {
84 QApplication a(argc, argv); // QApplication required!
85
86 if (argc != 3) {
87 qWarning() << "usage: test-password-qt5 owner-password filename";
88 exit(1);
89 }
90
91 Poppler::Document *doc = Poppler::Document::load(QString::fromLocal8Bit(argv[2]), argv[1]);
92 if (!doc) {
93 qWarning() << "doc not loaded";
94 exit(1);
95 }
96
97 // output some meta-data
98 auto pdfVersion = doc->getPdfVersion();
99 qDebug() << " PDF Version: " << qPrintable(QStringLiteral("%1.%2").arg(pdfVersion.major).arg(pdfVersion.minor));
100 qDebug() << " Title: " << doc->info(QStringLiteral("Title"));
101 qDebug() << " Subject: " << doc->info(QStringLiteral("Subject"));
102 qDebug() << " Author: " << doc->info(QStringLiteral("Author"));
103 qDebug() << " Key words: " << doc->info(QStringLiteral("Keywords"));
104 qDebug() << " Creator: " << doc->info(QStringLiteral("Creator"));
105 qDebug() << " Producer: " << doc->info(QStringLiteral("Producer"));
106 qDebug() << " Date created: " << doc->date(QStringLiteral("CreationDate")).toString();
107 qDebug() << " Date modified: " << doc->date(QStringLiteral("ModDate")).toString();
108 qDebug() << "Number of pages: " << doc->numPages();
109 qDebug() << " Linearised: " << doc->isLinearized();
110 qDebug() << " Encrypted: " << doc->isEncrypted();
111 qDebug() << " OK to print: " << doc->okToPrint();
112 qDebug() << " OK to copy: " << doc->okToCopy();
113 qDebug() << " OK to change: " << doc->okToChange();
114 qDebug() << "OK to add notes: " << doc->okToAddNotes();
115 qDebug() << " Page mode: " << doc->pageMode();
116 QStringList fontNameList;
117 Q_FOREACH (const Poppler::FontInfo &font, doc->fonts())
118 fontNameList += font.name();
119 qDebug() << " Fonts: " << fontNameList.join(QStringLiteral(", "));
120
121 Poppler::Page *page = doc->page(0);
122 qDebug() << " Page 1 size: " << page->pageSize().width() / 72 << "inches x " << page->pageSize().height() / 72 << "inches";
123
124 PDFDisplay test(doc); // create picture display
125 test.setWindowTitle(QStringLiteral("Poppler-Qt5 Test"));
126 test.show(); // show it
127
128 return QApplication::exec(); // start event loop
129 }
130
131 #include "test-password-qt5.moc"