"Fossies" - the Fresh Open Source Software Archive

Member "poppler-0.82.0/qt5/tests/test-password-qt5.cpp" (25 Oct 2019, 3959 Bytes) of package /linux/misc/poppler-0.82.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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