summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/pdf/poppler-qt/test-poppler-qt.cpp
blob: d616290d9dfad7bd3e95b3e21149dd3cfcfdd7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <tqapplication.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqwidget.h>
#include <tqlabel.h>
#include <tqmessagebox.h>
#include <tqfile.h>
#include <ctype.h>
#include <poppler-qt.h>
#include <stdlib.h>

class PDFDisplay : public TQWidget           // picture display widget
{
public:
    PDFDisplay( Poppler::Document *d );
   ~PDFDisplay();
protected:
    void        paintEvent( TQPaintEvent * );
    void        keyPressEvent( TQKeyEvent * );
private:
    void display();
    
    int currentPage;
    TQPixmap	*pixmap;
    Poppler::Document *doc;
};

PDFDisplay::PDFDisplay( Poppler::Document *d )
{
  doc = d;
  pixmap = 0;
  currentPage = 0;
  display();
}

PDFDisplay::~PDFDisplay()
{
  delete doc;
  delete pixmap;
}

void PDFDisplay::paintEvent( TQPaintEvent *e )
{
  TQPainter paint( this );                     // paint widget
  if (pixmap)
    paint.drawPixmap(0, 0, *pixmap);
}

void PDFDisplay::keyPressEvent( TQKeyEvent *e )
{
  if (e->key() == TQt::Key_Down)
  {
    if (currentPage + 1 < doc->getNumPages())
    {
      currentPage++;
      display();
    }
  }
  else if (e->key() == TQt::Key_Up)
  {
    if (currentPage > 0)
    {
      currentPage--;
      display();
    }
  }
}

void PDFDisplay::display()
{
  if (doc) {
    Poppler::Page *page = doc->getPage(currentPage);
    if (page) {
      delete pixmap;
      page->renderToPixmap(&pixmap, -1, -1, -1, -1);
      delete page;
      update();
    }
  } else {
    printf("doc not loaded\n");
  }
}

int main( int argc, char **argv )
{
  TQApplication a( argc, argv );               // TQApplication required!

  if ( argc < 2  || (argc == 3 && strcmp(argv[2], "-extract") != 0) || argc > 3)
  {
    // use argument as file name
    printf("usage: test-poppler-qt filename [-extract]\n");
    exit(1);
  }
  
  Poppler::Document *doc = Poppler::Document::load(argv[1]);
  if (!doc)
  {
    printf("doc not loaded\n");
    exit(1);
  }
  
  if (argc == 2)
  {  
    PDFDisplay test( doc );        // create picture display
    a.setMainWidget( &test);                // set main widget
    test.setCaption("Poppler-TQt Test");
    test.show();                            // show it

    return a.exec();                        // start event loop
  }
  else
  {
    Poppler::Page *page = doc->getPage(0);

    TQLabel *l = new TQLabel(page->getText(Poppler::Rectangle()), 0);
    l->show();
    a.setMainWidget(l);                // set main widget
    delete page;
    delete doc;
    return a.exec();
  }
}