1 #ifndef Header_PDF_RenderManager
2 #define Header_PDF_RenderManager
3 
4 #include "mostQtHeaders.h"
5 
6 #ifndef NO_POPPLER_PREVIEW
7 
8 #include <QCache>
9 #include <QImage>
10 
11 #include "pdfrenderengine.h"
12 
13 class RecInfo
14 {
15 public:
16 	QObject *obj;
17 	const char *slot;
18 	int x, y, w, h;
19 	int pageNr;
20 	qreal xres;
21 	bool cache;
22 	bool priority;
23 };
24 
25 
26 class CachePixmap: public QPixmap
27 {
28 public:
29 	CachePixmap(const QPixmap &pixmap);
30 	CachePixmap();
31 	void setRes(qreal res, int x, int y);
getRes()32 	qreal getRes()
33 	{
34 		return resolution;
35 	}
getCoord()36 	QPoint getCoord()
37 	{
38 		return QPoint(x, y);
39 	}
40 
41 private:
42 	qreal resolution;
43 	int x, y;
44 };
45 
46 
47 class PDFQueue : public QObject
48 {
49 public:
50 	explicit PDFQueue(QObject *parent = nullptr);
51 
ref()52 	inline void ref()
53 	{
54 		m_ref.ref();
55 	}
56 	void deref();
getRef()57 	int getRef()
58 	{
59 		return m_ref.fetchAndAddRelaxed(0);
60 	}
61 
62 	QQueue<RenderCommand> mCommands;
63 	QSemaphore mCommandsAvailable;
64 	QMutex mQueueLock;
65 	bool stopped;
66 
67 	QMutex mPriorityLock;
68 
69 	int num_renderQueues;
70 
71 	QList<PDFRenderEngine *>renderQueues;
72 
73 	QByteArray documentData;
74 
75 private:
76 	QAtomicInt m_ref;
77 };
78 
79 class SetImageForwarder : public QObject
80 {
81     Q_OBJECT
82 
83 public:
84     explicit SetImageForwarder(QObject *parent, QObject *obj, const char *rec, QPixmap img, int pageNr);
85     QObject *obj;
86     const char *rec;
87     QPixmap img;
88     int pageNr;
89 
90     void forward(int delay);
91 
92 public slots:
93     void setImage();
94 };
95 
96 class PDFRenderManager : public QObject
97 {
98 	Q_OBJECT
99 
100 public:
101     explicit PDFRenderManager(QObject *parent, int limitQueues = 0);
102 	~PDFRenderManager();
103 
104 	static const int BufferedLoad = 0;
105 	static const int DirectLoad = 1;
106 	static const int HybridLoad = 2;
107 	enum Error {NoError, FileOpenFailed, PopplerError, PopplerErrorBadAlloc, PopplerErrorException, FileLocked, FileIncomplete };
108 
109     QPixmap renderToImage(int pageNr, QObject *obj, const char *rec, double xres = 72.0, double yres = 72.0, int x = -1, int y = -1, int w = -1, int h = -1, bool cache = true, bool priority = false, int delayTimeout = -1, Poppler::Page::Rotation rotate = Poppler::Page::Rotate0);
110     QSharedPointer<Poppler::Document> loadDocument(const QString &fileName, Error &error, const QString &userPasswordStr,  bool foreceLoad = false);
111 	void stopRendering();
112 	void setCacheSize(int megabyte);
113 	void fillCache(int pg = -1);
114 	qreal getResLimit();
115 	void setLoadStrategy(int strategy);
116 
117 public slots:
118 	void addToCache(QImage img, int pageNr, int ticket);
119 
120 private:
121 	friend class PDFRenderEngine;
122 	bool checkDuplicate(int &ticket, RecInfo &info);
123 	void enqueue(RenderCommand cmd, bool priority);
124 
125 	void reduceCacheFilling(double fraction);
126 
127     QSharedPointer<Poppler::Document> document;
128 	int cachedNumPages;
129 
130 	QCache<int, CachePixmap> renderedPages;
131 	QMultiMap<int, RecInfo> lstOfReceivers;
132 	int currentTicket;
133 
134 	QMap<int, RecInfo> lstForThumbs;
135 	PDFQueue *queueAdministration;
136 	bool mFillCacheMode;
137 
138 	int loadStrategy;
139 };
140 
141 #endif // PDFRENDERMANAGER_H
142 
143 #endif
144