3
Copyright 2012-2014 Adam Reichold
5
This file is part of qpdfview.
7
qpdfview is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 2 of the License, or
10
(at your option) any later version.
12
qpdfview is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with qpdfview. If not, see <http://www.gnu.org/licenses/>.
28
#include "rendertask.h"
34
Settings* TileItem::s_settings = 0;
36
QCache< TileItem::CacheKey, TileItem::CacheObject > TileItem::s_cache;
38
TileItem::TileItem(QObject* parent) : QObject(parent),
48
s_settings = Settings::instance();
51
s_cache.setMaxCost(s_settings->pageItem().cacheSize());
53
m_renderTask = new RenderTask(parentPage()->m_page, this);
55
connect(m_renderTask, SIGNAL(finished()), SLOT(on_renderTask_finished()));
56
connect(m_renderTask, SIGNAL(imageReady(RenderParam,QRect,bool,QImage,QRectF)), SLOT(on_renderTask_imageReady(RenderParam,QRect,bool,QImage,QRectF)));
61
m_renderTask->cancel(true);
65
void TileItem::setCropRect(const QRectF& cropRect)
67
if(!s_settings->pageItem().trimMargins())
72
if(m_cropRect.isNull() && !cropRect.isNull())
74
m_cropRect = cropRect;
76
parentPage()->updateCropRect();
80
void TileItem::dropCachedPixmaps(PageItem* page)
82
foreach(CacheKey key, s_cache.keys())
91
void TileItem::paint(QPainter* painter, const QPointF& topLeft)
93
const QPixmap& pixmap = takePixmap();
99
painter->drawPixmap(m_rect.topLeft() + topLeft, pixmap);
101
else if(!m_obsoletePixmap.isNull())
105
painter->drawPixmap(QRectF(m_rect).translated(topLeft), m_obsoletePixmap, QRectF());
109
const qreal iconExtent = qMin(0.1 * m_rect.width(), 0.1 * m_rect.height());
110
const QRect iconRect(topLeft.x() + m_rect.left() + 0.01 * m_rect.width(),
111
topLeft.y() + m_rect.top() + 0.01 * m_rect.height(),
112
iconExtent, iconExtent);
118
s_settings->pageItem().progressIcon().paint(painter, iconRect);
124
s_settings->pageItem().errorIcon().paint(painter, iconRect);
129
void TileItem::refresh(bool keepObsoletePixmaps)
131
if(keepObsoletePixmaps && s_settings->pageItem().keepObsoletePixmaps())
133
CacheObject* object = s_cache.object(cacheKey());
137
m_obsoletePixmap = object->first;
142
m_obsoletePixmap = QPixmap();
145
if(!keepObsoletePixmaps)
147
m_cropRect = QRectF();
150
m_renderTask->cancel(true);
152
m_pixmapError = false;
153
m_pixmap = QPixmap();
156
int TileItem::startRender(bool prefetch)
158
if(m_pixmapError || m_renderTask->isRunning() || (prefetch && s_cache.contains(cacheKey())))
163
m_renderTask->start(parentPage()->m_renderParam,
165
s_settings->pageItem().trimMargins(), s_settings->pageItem().paperColor());
170
void TileItem::cancelRender()
172
m_renderTask->cancel();
174
m_pixmap = QPixmap();
175
m_obsoletePixmap = QPixmap();
178
void TileItem::deleteAfterRender()
182
if(!m_renderTask->isRunning())
188
QTimer::singleShot(0, this, SLOT(deleteAfterRender()));
192
void TileItem::on_renderTask_finished()
194
parentPage()->update();
197
void TileItem::on_renderTask_imageReady(const RenderParam& renderParam,
198
const QRect& rect, bool prefetch,
199
QImage image, QRectF cropRect)
201
if(parentPage()->m_renderParam != renderParam || m_rect != rect)
206
m_obsoletePixmap = QPixmap();
210
m_pixmapError = true;
215
if(prefetch && !m_renderTask->wasCanceledForcibly())
217
int cost = image.width() * image.height() * image.depth() / 8;
218
s_cache.insert(cacheKey(), new CacheObject(QPixmap::fromImage(image), cropRect), cost);
220
setCropRect(cropRect);
222
else if(!m_renderTask->wasCanceled())
224
m_pixmap = QPixmap::fromImage(image);
226
setCropRect(cropRect);
230
PageItem* TileItem::parentPage() const
232
return qobject_cast< PageItem* >(parent());
235
TileItem::CacheKey TileItem::cacheKey() const
237
PageItem* page = parentPage();
240
QDataStream(&key, QIODevice::WriteOnly)
241
<< page->m_renderParam.resolution.resolutionX
242
<< page->m_renderParam.resolution.resolutionY
243
<< page->m_renderParam.scaleFactor
244
<< page->m_renderParam.rotation
245
<< page->m_renderParam.invertColors
248
return qMakePair(page, key);
251
QPixmap TileItem::takePixmap()
253
CacheObject* object = s_cache.object(cacheKey());
257
m_obsoletePixmap = QPixmap();
259
setCropRect(object->second);
260
return object->first;
265
if(!m_pixmap.isNull())
267
int cost = m_pixmap.width() * m_pixmap.height() * m_pixmap.depth() / 8;
268
s_cache.insert(cacheKey(), new CacheObject(m_pixmap, m_cropRect), cost);
271
m_pixmap = QPixmap();