mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Thumbnails - first part
This commit is contained in:
@@ -49,7 +49,7 @@ void PDFDrawSpaceController::setDocument(const PDFDocument* document, const PDFO
|
||||
m_document = document;
|
||||
m_fontCache.setDocument(document);
|
||||
m_optionalContentActivity = optionalContentActivity;
|
||||
connect(m_optionalContentActivity, &PDFOptionalContentActivity::optionalContentGroupStateChanged, this, &PDFDrawSpaceController::repaintNeeded);
|
||||
connect(m_optionalContentActivity, &PDFOptionalContentActivity::optionalContentGroupStateChanged, this, &PDFDrawSpaceController::onOptionalContentGroupStateChanged);
|
||||
recalculate();
|
||||
}
|
||||
}
|
||||
@@ -133,6 +133,12 @@ QSizeF PDFDrawSpaceController::getReferenceBoundingBox() const
|
||||
return rect.size();
|
||||
}
|
||||
|
||||
void PDFDrawSpaceController::onOptionalContentGroupStateChanged()
|
||||
{
|
||||
emit pageImageChanged(true, { });
|
||||
emit repaintNeeded();
|
||||
}
|
||||
|
||||
void PDFDrawSpaceController::recalculate()
|
||||
{
|
||||
if (!m_document)
|
||||
@@ -393,6 +399,7 @@ PDFDrawWidgetProxy::PDFDrawWidgetProxy(QObject* parent) :
|
||||
m_controller = new PDFDrawSpaceController(this);
|
||||
connect(m_controller, &PDFDrawSpaceController::drawSpaceChanged, this, &PDFDrawWidgetProxy::update);
|
||||
connect(m_controller, &PDFDrawSpaceController::repaintNeeded, this, &PDFDrawWidgetProxy::repaintNeeded);
|
||||
connect(m_controller, &PDFDrawSpaceController::pageImageChanged, this, &PDFDrawWidgetProxy::pageImageChanged);
|
||||
}
|
||||
|
||||
PDFDrawWidgetProxy::~PDFDrawWidgetProxy()
|
||||
@@ -603,6 +610,36 @@ void PDFDrawWidgetProxy::draw(QPainter* painter, QRect rect)
|
||||
}
|
||||
}
|
||||
|
||||
QImage PDFDrawWidgetProxy::drawThumbnailImage(PDFInteger pageIndex, int pixelSize) const
|
||||
{
|
||||
QImage image;
|
||||
if (!m_controller->getDocument())
|
||||
{
|
||||
// No thumbnail - return empty image
|
||||
return image;
|
||||
}
|
||||
|
||||
if (const PDFPage* page = m_controller->getDocument()->getCatalog()->getPage(pageIndex))
|
||||
{
|
||||
QRectF pageRect = page->getRotatedMediaBox();
|
||||
QSizeF pageSize = pageRect.size();
|
||||
pageSize.scale(pixelSize, pixelSize, Qt::KeepAspectRatio);
|
||||
QSize imageSize = pageSize.toSize();
|
||||
|
||||
if (imageSize.isValid())
|
||||
{
|
||||
image = QImage(imageSize, QImage::Format_RGBA8888_Premultiplied);
|
||||
image.fill(Qt::white);
|
||||
|
||||
QPainter painter(&image);
|
||||
PDFRenderer renderer(m_controller->getDocument(), m_controller->getFontCache(), m_controller->getOptionalContentActivity(), m_features, m_meshQualitySettings);
|
||||
renderer.render(&painter, QRect(QPoint(0, 0), imageSize), pageIndex);
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
std::vector<PDFInteger> PDFDrawWidgetProxy::getPagesIntersectingRect(QRect rect) const
|
||||
{
|
||||
std::vector<PDFInteger> pages;
|
||||
|
@@ -113,8 +113,11 @@ public:
|
||||
signals:
|
||||
void drawSpaceChanged();
|
||||
void repaintNeeded();
|
||||
void pageImageChanged(bool all, const std::vector<PDFInteger>& pages);
|
||||
|
||||
private:
|
||||
void onOptionalContentGroupStateChanged();
|
||||
|
||||
/// Recalculates the draw space. Preserves setted page rotation.
|
||||
void recalculate();
|
||||
|
||||
@@ -176,6 +179,12 @@ public:
|
||||
/// \param rect Rectangle in which the content is painted
|
||||
void draw(QPainter* painter, QRect rect);
|
||||
|
||||
/// Draws thumbnail image of the given size (so larger of the page size
|
||||
/// width or height equals to pixel size and the latter size is rescaled
|
||||
/// using the aspect ratio)
|
||||
/// \param pixelSize Pixel size
|
||||
QImage drawThumbnailImage(PDFInteger pageIndex, int pixelSize) const;
|
||||
|
||||
enum Operation
|
||||
{
|
||||
ZoomIn,
|
||||
@@ -260,6 +269,7 @@ signals:
|
||||
void pageLayoutChanged();
|
||||
void renderingError(PDFInteger pageIndex, const QList<PDFRenderError>& errors);
|
||||
void repaintNeeded();
|
||||
void pageImageChanged(bool all, const std::vector<PDFInteger>& pages);
|
||||
|
||||
private:
|
||||
struct LayoutItem
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "pdfitemmodels.h"
|
||||
#include "pdfdocument.h"
|
||||
#include "pdfdrawspacecontroller.h"
|
||||
|
||||
#include <QFont>
|
||||
#include <QStyle>
|
||||
@@ -614,4 +615,160 @@ const PDFFileSpecification* PDFAttachmentsTreeItemModel::getFileSpecification(co
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PDFThumbnailsItemModel::PDFThumbnailsItemModel(const PDFDrawWidgetProxy* proxy, QObject* parent) :
|
||||
QAbstractItemModel(parent),
|
||||
m_proxy(proxy),
|
||||
m_thumbnailSize(100),
|
||||
m_extraItemWidthHint(0),
|
||||
m_extraItemHeighHint(0),
|
||||
m_pageCount(0),
|
||||
m_document(nullptr)
|
||||
{
|
||||
connect(proxy, &PDFDrawWidgetProxy::pageImageChanged, this, &PDFThumbnailsItemModel::onPageImageChanged);
|
||||
}
|
||||
|
||||
bool PDFThumbnailsItemModel::isEmpty() const
|
||||
{
|
||||
return rowCount(QModelIndex()) == 0;
|
||||
}
|
||||
|
||||
QModelIndex PDFThumbnailsItemModel::index(int row, int column, const QModelIndex& parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
{
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
return createIndex(row, column, nullptr);
|
||||
}
|
||||
|
||||
QModelIndex PDFThumbnailsItemModel::parent(const QModelIndex& child) const
|
||||
{
|
||||
Q_UNUSED(child);
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
int PDFThumbnailsItemModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
if (parent.isValid())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_pageCount;
|
||||
}
|
||||
|
||||
int PDFThumbnailsItemModel::columnCount(const QModelIndex& parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
QVariant PDFThumbnailsItemModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
if (!index.isValid() || !m_document)
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const int page = index.row();
|
||||
switch (role)
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
return QString::number(page + 1);
|
||||
|
||||
case Qt::DecorationRole:
|
||||
{
|
||||
QString key = getKey(index.row());
|
||||
|
||||
QPixmap pixmap;
|
||||
if (!m_thumbnailCache.find(key, pixmap))
|
||||
{
|
||||
QImage thumbnail = m_proxy->drawThumbnailImage(index.row(), m_thumbnailSize);
|
||||
if (!thumbnail.isNull())
|
||||
{
|
||||
pixmap = QPixmap::fromImage(qMove(thumbnail));
|
||||
m_thumbnailCache.insert(key, pixmap);
|
||||
}
|
||||
}
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
case Qt::TextAlignmentRole:
|
||||
{
|
||||
return int(Qt::AlignHCenter | Qt::AlignBottom);
|
||||
}
|
||||
|
||||
case Qt::SizeHintRole:
|
||||
{
|
||||
const PDFPage* page = m_document->getCatalog()->getPage(index.row());
|
||||
QSizeF pageSize = page->getRotatedMediaBox().size();
|
||||
pageSize.scale(m_thumbnailSize, m_thumbnailSize, Qt::KeepAspectRatio);
|
||||
return pageSize.toSize() + QSize(m_extraItemWidthHint, m_extraItemHeighHint);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
void PDFThumbnailsItemModel::setThumbnailsSize(int size)
|
||||
{
|
||||
if (m_thumbnailSize != size)
|
||||
{
|
||||
emit layoutAboutToBeChanged();
|
||||
m_thumbnailSize = size;
|
||||
m_thumbnailCache.clear();
|
||||
emit layoutChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void PDFThumbnailsItemModel::setDocument(const PDFDocument* document)
|
||||
{
|
||||
if (m_document != document)
|
||||
{
|
||||
m_thumbnailCache.clear();
|
||||
m_document = document;
|
||||
|
||||
m_pageCount = 0;
|
||||
if (m_document)
|
||||
{
|
||||
m_pageCount = static_cast<int>(m_document->getCatalog()->getPageCount());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PDFThumbnailsItemModel::onPageImageChanged(bool all, const std::vector<PDFInteger>& pages)
|
||||
{
|
||||
Q_UNUSED(all);
|
||||
Q_UNUSED(pages);
|
||||
|
||||
if (all)
|
||||
{
|
||||
m_thumbnailCache.clear();
|
||||
emit dataChanged(index(0, 0, QModelIndex()), index(rowCount(QModelIndex()) - 1, 0, QModelIndex()));
|
||||
}
|
||||
else
|
||||
{
|
||||
const int rowCount = this->rowCount(QModelIndex());
|
||||
for (const PDFInteger pageIndex : pages)
|
||||
{
|
||||
if (pageIndex < rowCount)
|
||||
{
|
||||
m_thumbnailCache.remove(getKey(pageIndex));
|
||||
emit dataChanged(index(pageIndex, 0, QModelIndex()), index(pageIndex, 0, QModelIndex()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString PDFThumbnailsItemModel::getKey(int pageIndex) const
|
||||
{
|
||||
return QString("PDF_THUMBNAIL_%1").arg(pageIndex);
|
||||
}
|
||||
|
||||
} // namespace pdf
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include "pdfobject.h"
|
||||
|
||||
#include <QIcon>
|
||||
#include <QPixmapCache>
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
namespace pdf
|
||||
@@ -31,6 +32,7 @@ class PDFDocument;
|
||||
class PDFOutlineItem;
|
||||
class PDFFileSpecification;
|
||||
class PDFOptionalContentActivity;
|
||||
class PDFDrawWidgetProxy;
|
||||
|
||||
/// Represents tree item in the GUI tree
|
||||
class PDFTreeItem
|
||||
@@ -217,6 +219,42 @@ public:
|
||||
const PDFFileSpecification* getFileSpecification(const QModelIndex& index) const;
|
||||
};
|
||||
|
||||
class PDFFORQTLIBSHARED_EXPORT PDFThumbnailsItemModel : public QAbstractItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit inline PDFThumbnailsItemModel(const PDFDrawWidgetProxy* proxy, QObject* parent);
|
||||
|
||||
bool isEmpty() const;
|
||||
|
||||
virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
||||
virtual QModelIndex parent(const QModelIndex& child) const override;
|
||||
virtual int rowCount(const QModelIndex& parent) const override;
|
||||
virtual int columnCount(const QModelIndex& parent) const override;
|
||||
virtual QVariant data(const QModelIndex& index, int role) const override;
|
||||
|
||||
void setThumbnailsSize(int size);
|
||||
void setDocument(const PDFDocument* document);
|
||||
|
||||
/// Sets the extra item width/height for size hint. This space will be added to the size hint (pixmap size)
|
||||
void setExtraItemSizeHint(int width, int height) { m_extraItemWidthHint = width; m_extraItemHeighHint = height; }
|
||||
|
||||
private:
|
||||
void onPageImageChanged(bool all, const std::vector<PDFInteger>& pages);
|
||||
|
||||
/// Returns generated key for page index
|
||||
QString getKey(int pageIndex) const;
|
||||
|
||||
const PDFDrawWidgetProxy* m_proxy;
|
||||
int m_thumbnailSize;
|
||||
int m_extraItemWidthHint;
|
||||
int m_extraItemHeighHint;
|
||||
int m_pageCount;
|
||||
const PDFDocument* m_document;
|
||||
QPixmapCache m_thumbnailCache;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFITEMMODELS_H
|
||||
|
Reference in New Issue
Block a user