/****************************************************************************
** 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