[go: up one dir, main page]

Menu

[332e47]: / src / pdfpager.h  Maximize  Restore  History

Download this file

183 lines (152 with data), 4.6 kB

  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/****************************************************************************
** This file is part of the taborca project hosted at
** sf.net/projects/taborca
** Copyright (C) 2013 Shawn Rutledge
** Contact: s@ecloud.org
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called LICENSE; if not, write to the
** Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
****************************************************************************/
#ifndef PDFPAGER_H
#define PDFPAGER_H
#include <poppler-qt6.h>
#include <QObject>
#include <QImage>
#include <QIntValidator>
#include <QMutex>
#include <QRunnable>
#include "page.h"
/**
\brief wrapper for a Poppler::Document, used to
select and render pages and thumbnails.
*/
class PDFPager : public QObject, QRunnable
{
Q_OBJECT
public:
PDFPager();
int page() { return m_pageNum; }
QString path() { return m_path; }
/**
Render thumbnails, and pre-render page images, memory permitting.
*/
void run();
public slots:
/**
Open the PDF at the given path,
emit opened() and numPages(), then iterate through the pages
and emit thumbnail() for each. Finally, go back to page 0
since it will be shown by default.
*/
void openPDF(QString fpath);
/**
Reset to original state: no file is open.
*/
void closePDF();
/**
Go to (and render) the given page.
*/
void page(int pnum);
/**
Convenience version of page(int) taking a number in string form.
*/
void page(QString pnum) { page(pnum.toInt()); }
void nextPage(bool forward = true);
/**
Re-render the current page at the given size.
*/
virtual void renderSize(QSize s, bool reRender = true);
/**
Re-render the current page at the same size as the given rectangle.
*/
virtual void renderSize(const QRectF& s) { renderSize(s.size().toSize()); }
signals:
void opened(QString fpath, int numPages);
void closed(QString fpath);
void numPages(int n);
void rendering(bool t);
void rendered(int pnum, QImage pm);
void pageText(QStringList);
void pageChanged(int p);
void pageChanged(QString p);
void thumbnail(int pnum, QImage pm, QString label);
void statusMessage(QString msg, int timeout = 0);
void statusClear();
public:
/**
The Poppler implementation.
*/
std::unique_ptr<Poppler::Document> pdf;
/**
Validator for the page number entry field on the toolbar.
*/
QIntValidator pageRange;
/**
Get the concatenated text of this page, without formatting.
*/
QString plainText() { return m_text; }
protected:
/**
Rendering implementation.
*/
virtual void render(int pnum, QSize renderSize = QSize(), bool emitImage = true);
/**
\brief Handler for completion of rendering; emits
rendering(false) and rendered().
This exists because of an (unimplemented) plan to render
higher-resolution thumbnails, and same-resolution page images,
in a background thread, in a subclass of this one called PDFPageCache.
The completion of rendering would trigger rendering of the
next page, in that case.
*/
virtual void renderDone(int pnum, QImage pm);
protected:
/**
Current page number, the last one rendered and presumably showing
in the main PDF view.
*/
int m_pageNum;
/**
Size at which we will render the next page.
*/
QSize m_renderSize;
QSize m_thumbnailRenderSize;
/**
Path to the pdf as given in openPDF.
*/
QString m_path;
/**
Concatenated text from the page, e.g. existing OCR results
from the PDF file.
*/
QString m_text;
QMutex m_renderMutex;
// Cannot be a QPixmap cache because a thread is involved
// "QPixmap: It is not safe to use pixmaps outside the GUI thread"
/**
Cache for the "main" page images
@todo maybe: store cached images at more than one size,
in case the user is zooming in and out a lot
*/
QVector<Page> m_cache;
/**
Which pages do not have valid thumbnails yet?
(Usually an entire document will be like that, but
doesn't have to be.)
*/
QList<int> m_nullThumbnails;
//friend class PDFPageCache;
};
#endif // PDFPAGER_H