2021-07-07 18:30:03 +02:00
|
|
|
// Copyright (C) 2021 Jakub Melka
|
|
|
|
//
|
|
|
|
// This file is part of Pdf4Qt.
|
|
|
|
//
|
|
|
|
// Pdf4Qt is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// with the written consent of the copyright owner, any later version.
|
|
|
|
//
|
|
|
|
// Pdf4Qt 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "pageitemdelegate.h"
|
|
|
|
#include "pageitemmodel.h"
|
|
|
|
#include "pdfwidgetutils.h"
|
2021-07-08 17:55:34 +02:00
|
|
|
#include "pdfpainterutils.h"
|
2021-07-07 18:30:03 +02:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
namespace pdfdocpage
|
|
|
|
{
|
|
|
|
|
|
|
|
PageItemDelegate::PageItemDelegate(PageItemModel* model, QObject* parent) :
|
|
|
|
BaseClass(parent),
|
|
|
|
m_model(model)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PageItemDelegate::~PageItemDelegate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
const PageGroupItem* item = m_model->getItem(index);
|
|
|
|
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect rect = option.rect;
|
|
|
|
|
|
|
|
QSize scaledSize = pdf::PDFWidgetUtils::scaleDPI(option.widget, m_pageImageSize);
|
|
|
|
int verticalSpacing = pdf::PDFWidgetUtils::scaleDPI_y(option.widget, getVerticalSpacing());
|
2021-07-08 17:55:34 +02:00
|
|
|
int horizontalSpacing = pdf::PDFWidgetUtils::scaleDPI_x(option.widget, getHorizontalSpacing());
|
2021-07-07 18:30:03 +02:00
|
|
|
|
|
|
|
QRect pageBoundingRect = QRect(QPoint(rect.left() + (rect.width() - scaledSize.width()) / 2, rect.top() + verticalSpacing), scaledSize);
|
|
|
|
|
|
|
|
// Draw page preview
|
|
|
|
if (!item->groups.empty())
|
|
|
|
{
|
|
|
|
const PageGroupItem::GroupItem& groupItem = item->groups.front();
|
2021-07-15 19:44:44 +02:00
|
|
|
QSizeF rotatedPageSize = pdf::PDFPage::getRotatedBox(QRectF(QPointF(0, 0), groupItem.rotatedPageDimensionsMM), groupItem.pageAdditionalRotation).size();
|
|
|
|
QSize pageImageSize = rotatedPageSize.scaled(pageBoundingRect.size(), Qt::KeepAspectRatio).toSize();
|
2021-07-07 18:30:03 +02:00
|
|
|
QRect pageImageRect(pageBoundingRect.topLeft() + QPoint((pageBoundingRect.width() - pageImageSize.width()) / 2, (pageBoundingRect.height() - pageImageSize.height()) / 2), pageImageSize);
|
|
|
|
|
|
|
|
painter->setPen(QPen(Qt::black));
|
|
|
|
painter->setBrush(Qt::white);
|
|
|
|
painter->drawRect(pageImageRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int textOffset = pageBoundingRect.bottom() + verticalSpacing;
|
|
|
|
QRect textRect = option.rect;
|
|
|
|
textRect.setTop(textOffset);
|
|
|
|
textRect.setHeight(option.fontMetrics.lineSpacing());
|
|
|
|
painter->drawText(textRect, Qt::AlignCenter | Qt::TextSingleLine, item->groupName);
|
|
|
|
textRect.translate(0, textRect.height());
|
|
|
|
painter->drawText(textRect, Qt::AlignCenter | Qt::TextSingleLine, item->pagesCaption);
|
|
|
|
|
|
|
|
if (option.state.testFlag(QStyle::State_Selected))
|
|
|
|
{
|
|
|
|
QColor selectedColor = option.palette.color(QPalette::Active, QPalette::Highlight);
|
|
|
|
selectedColor.setAlphaF(0.3);
|
|
|
|
painter->fillRect(rect, selectedColor);
|
|
|
|
}
|
2021-07-08 17:55:34 +02:00
|
|
|
|
|
|
|
QPoint tagPoint = rect.topRight() + QPoint(-horizontalSpacing, verticalSpacing);
|
|
|
|
for (const QString& tag : item->tags)
|
|
|
|
{
|
|
|
|
QStringList splitted = tag.split('@', Qt::KeepEmptyParts);
|
|
|
|
if (splitted.size() != 2 || splitted.back().isEmpty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor color;
|
|
|
|
color.setNamedColor(splitted.front());
|
|
|
|
QRect bubbleRect = pdf::PDFPainterHelper::drawBubble(painter, tagPoint, color, splitted.back(), Qt::AlignLeft | Qt::AlignBottom);
|
|
|
|
tagPoint.ry() += bubbleRect.height() + verticalSpacing;
|
|
|
|
}
|
2021-07-07 18:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize PageItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
|
|
|
|
|
|
|
QSize scaledSize = pdf::PDFWidgetUtils::scaleDPI(option.widget, m_pageImageSize);
|
|
|
|
int height = scaledSize.height() + option.fontMetrics.lineSpacing() * 2 + 2 * pdf::PDFWidgetUtils::scaleDPI_y(option.widget, getVerticalSpacing());
|
|
|
|
int width = qMax(pdf::PDFWidgetUtils::scaleDPI_x(option.widget, 40), scaledSize.width() + 2 * pdf::PDFWidgetUtils::scaleDPI_x(option.widget, getHorizontalSpacing()));
|
2021-07-08 13:48:27 +02:00
|
|
|
return QSize(width, height);
|
2021-07-07 18:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize PageItemDelegate::getPageImageSize() const
|
|
|
|
{
|
|
|
|
return m_pageImageSize;
|
|
|
|
}
|
|
|
|
|
2021-07-08 17:55:34 +02:00
|
|
|
void PageItemDelegate::setPageImageSize(QSize pageImageSize)
|
2021-07-07 18:30:03 +02:00
|
|
|
{
|
2021-07-08 17:55:34 +02:00
|
|
|
if (m_pageImageSize != pageImageSize)
|
|
|
|
{
|
|
|
|
m_pageImageSize = pageImageSize;
|
|
|
|
emit sizeHintChanged(QModelIndex());
|
|
|
|
}
|
2021-07-07 18:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pdfdocpage
|