valentina/src/app/puzzle/scene/vpgraphicstilegrid.cpp

224 lines
7.4 KiB
C++
Raw Normal View History

#include "vpgraphicstilegrid.h"
2021-08-09 14:09:10 +02:00
#include "../vptilefactory.h"
#include "../layout/vplayout.h"
2021-09-07 19:26:35 +02:00
#include "../layout/vpsheet.h"
2022-02-10 13:07:05 +01:00
#include "qnamespace.h"
2021-09-11 18:39:38 +02:00
#include <QFileInfo>
#include <QImageReader>
#include <QPixmapCache>
#include <QSvgRenderer>
2021-09-06 14:31:19 +02:00
namespace
{
constexpr qreal penWidth = 1;
2022-02-10 13:07:05 +01:00
//---------------------------------------------------------------------------------------------------------------------
auto SheetMargins(const VPSheetPtr &sheet) -> QMarginsF
{
if (not sheet.isNull() && not sheet->IgnoreMargins())
{
return sheet->GetSheetMargins();
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto OptimizeFontSizeToFitTextInRect(QPainter *painter, const QRectF &drawRect, const QString &text,
int flags = Qt::TextDontClip|Qt::TextWordWrap, double goalError = 0.01,
int maxIterationNumber=10) -> QFont
{
painter->save();
QRect fontBoundRect;
QFont font;
double minError = std::numeric_limits<double>::max();
double error = std::numeric_limits<double>::max();
int iterationNumber=0;
while((error > goalError) && (iterationNumber<maxIterationNumber))
{
iterationNumber++;
fontBoundRect = painter->fontMetrics().boundingRect(drawRect.toRect(), flags, text);
double xFactor = drawRect.width() / fontBoundRect.width();
double yFactor = drawRect.height() / fontBoundRect.height();
double factor;
if (xFactor<1 && yFactor<1)
{
factor = std::min(xFactor, yFactor);
}
else if (xFactor>1 && yFactor>1)
{
factor = std::max(xFactor, yFactor);
}
else if (xFactor<1 && yFactor>1)
{
factor = xFactor;
}
else
{
factor = yFactor;
}
2022-02-17 12:20:42 +01:00
error = abs(factor-1.0);
2022-02-10 13:07:05 +01:00
if (factor > 1 )
{
if (error < minError)
{
minError = error;
}
else
{
break;
}
}
font = painter->font();
font.setPointSizeF(font.pointSizeF()*factor);
painter->setFont(font);
}
painter->restore();
return font;
}
2021-09-11 18:39:38 +02:00
} // namespace
2021-09-06 14:31:19 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-09-06 14:31:19 +02:00
VPGraphicsTileGrid::VPGraphicsTileGrid(const VPLayoutPtr &layout, const QUuid &sheetUuid, QGraphicsItem *parent):
QGraphicsItem(parent),
2021-09-06 14:31:19 +02:00
m_layout(layout),
m_sheetUuid(sheetUuid)
{
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-25 15:58:50 +02:00
auto VPGraphicsTileGrid::boundingRect() const -> QRectF
{
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if(not layout.isNull() && layout->LayoutSettings().GetShowTiles())
{
2021-09-06 14:31:19 +02:00
VPSheetPtr sheet = layout->GetSheet(m_sheetUuid);
2021-09-07 19:26:35 +02:00
QMarginsF sheetMargins;
if (not sheet.isNull() && not sheet->IgnoreMargins())
{
sheetMargins = sheet->GetSheetMargins();
}
2021-09-06 15:56:56 +02:00
qreal xScale = layout->LayoutSettings().HorizontalScale();
qreal yScale = layout->LayoutSettings().VerticalScale();
2021-09-07 19:26:35 +02:00
qreal width = layout->TileFactory()->DrawingAreaWidth() - VPTileFactory::tileStripeWidth;
qreal height = layout->TileFactory()->DrawingAreaHeight() - VPTileFactory::tileStripeWidth;
QRectF rect(sheetMargins.left(), sheetMargins.top(),
layout->TileFactory()->ColNb(sheet) * (width / xScale),
layout->TileFactory()->RowNb(sheet) * (height / yScale));
2021-09-06 14:31:19 +02:00
constexpr qreal halfPenWidth = penWidth/2.;
return rect.adjusted(-halfPenWidth, -halfPenWidth, halfPenWidth, halfPenWidth);
}
2021-07-29 16:11:18 +02:00
return {};
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTileGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
2022-02-10 13:07:05 +01:00
if(layout.isNull() || not layout->LayoutSettings().GetShowTiles())
{
2022-02-10 13:07:05 +01:00
return;
}
2021-09-06 14:31:19 +02:00
2022-02-10 13:07:05 +01:00
QPen pen(QColor(255,0,0,127), penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
pen.setCosmetic(true);
pen.setStyle(Qt::DashLine);
QBrush noBrush(Qt::NoBrush);
painter->setPen(pen);
painter->setBrush(noBrush);
qreal xScale = layout->LayoutSettings().HorizontalScale();
qreal yScale = layout->LayoutSettings().VerticalScale();
const qreal width = (layout->TileFactory()->DrawingAreaWidth() - VPTileFactory::tileStripeWidth) / xScale;
const qreal height = (layout->TileFactory()->DrawingAreaHeight() - VPTileFactory::tileStripeWidth) / yScale;
VPSheetPtr sheet = layout->GetSheet(m_sheetUuid);
QMarginsF sheetMargins = SheetMargins(sheet);
const int nbCol = layout->TileFactory()->ColNb(sheet);
const int nbRow = layout->TileFactory()->RowNb(sheet);
QFont font = OptimizeFontSizeToFitTextInRect(painter,
QRectF(sheetMargins.left(), sheetMargins.top(), width/3., height/3.),
QString::number(nbRow * nbCol));
VWatermarkData watermarkData = layout->TileFactory()->WatermarkData();
auto PaintWatermark = [painter, layout, xScale, yScale, watermarkData]
(const QRectF &img)
{
if (not layout->LayoutSettings().WatermarkPath().isEmpty() &&
layout->LayoutSettings().GetShowWatermark() && watermarkData.opacity > 0)
2021-09-07 19:26:35 +02:00
{
2022-02-10 13:07:05 +01:00
if (watermarkData.showImage && not watermarkData.path.isEmpty())
{
VPTileFactory::PaintWatermarkImage(painter, img, watermarkData,
layout->LayoutSettings().WatermarkPath(),
xScale, yScale);
}
if (watermarkData.showText && not watermarkData.text.isEmpty())
{
VPTileFactory::PaintWatermarkText(painter, img, watermarkData);
}
2021-09-07 19:26:35 +02:00
}
2022-02-10 13:07:05 +01:00
};
auto PaintTileNumber = [painter, layout, nbCol, font]
(const QRectF &img, int i, int j)
{
if (layout->LayoutSettings().GetShowTileNumber())
{
painter->save();
2021-09-07 19:26:35 +02:00
2022-02-10 13:07:05 +01:00
painter->setFont(font);
2022-02-10 13:07:05 +01:00
QPen pen = painter->pen();
pen.setColor(Qt::black);
painter->setPen(pen);
2021-09-06 15:56:56 +02:00
2022-02-10 13:07:05 +01:00
painter->drawText(img, Qt::AlignCenter, QString::number(j*nbCol + i+1));
2021-09-07 19:26:35 +02:00
2022-02-10 13:07:05 +01:00
painter->restore();
}
};
2021-09-06 15:56:56 +02:00
2022-02-10 13:07:05 +01:00
for(int j=0;j<=nbRow;++j)
{
// horizontal lines
painter->drawLine(QPointF(sheetMargins.left(), sheetMargins.top()+j*height),
QPointF(sheetMargins.left()+nbCol*width, sheetMargins.top()+j*height));
2022-02-10 13:07:05 +01:00
for(int i=0;i<=nbCol;++i)
{
2022-02-10 13:07:05 +01:00
// vertical lines
painter->drawLine(QPointF(sheetMargins.left()+i*width, sheetMargins.top()),
QPointF(sheetMargins.left()+i*width, sheetMargins.top() + nbRow*height));
2021-09-11 18:39:38 +02:00
2022-02-10 13:07:05 +01:00
if (j < nbRow && i < nbCol)
2021-09-11 18:39:38 +02:00
{
2022-02-10 13:07:05 +01:00
QRectF img(sheetMargins.left()+i*width, sheetMargins.top()+j*height, width, height);
PaintWatermark(img);
PaintTileNumber(img, i, j);
2021-09-11 18:39:38 +02:00
}
}
}
}