New option Show tile number.

This commit is contained in:
Roman Telezhynskyi 2022-02-10 14:07:05 +02:00
parent d16aa1af64
commit 09cf8257d6
11 changed files with 201 additions and 66 deletions

View File

@ -378,7 +378,7 @@ void VPLayoutSettings::SetShowWatermark(bool newShowWatermark)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VPLayoutSettings::GetPrintTilesScheme() const auto VPLayoutSettings::GetPrintTilesScheme() const -> bool
{ {
return m_printTilesScheme; return m_printTilesScheme;
} }
@ -388,3 +388,15 @@ void VPLayoutSettings::SetPrintTilesScheme(bool newPrintTilesScheme)
{ {
m_printTilesScheme = newPrintTilesScheme; m_printTilesScheme = newPrintTilesScheme;
} }
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutSettings::GetShowTileNumber() const -> bool
{
return m_showTileNumbers;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutSettings::SetShowTileNumber(bool newTileNumbers)
{
m_showTileNumbers = newTileNumbers;
}

View File

@ -37,7 +37,7 @@
class VPLayoutSettings class VPLayoutSettings
{ {
Q_DECLARE_TR_FUNCTIONS(VPLayoutSettings) Q_DECLARE_TR_FUNCTIONS(VPLayoutSettings) // NOLINT
public: public:
VPLayoutSettings() = default; VPLayoutSettings() = default;
@ -312,9 +312,12 @@ public:
auto GetShowWatermark() const -> bool; auto GetShowWatermark() const -> bool;
void SetShowWatermark(bool newShowWatermark); void SetShowWatermark(bool newShowWatermark);
bool GetPrintTilesScheme() const; auto GetPrintTilesScheme() const -> bool;
void SetPrintTilesScheme(bool newPrintTilesScheme); void SetPrintTilesScheme(bool newPrintTilesScheme);
auto GetShowTileNumber() const -> bool;
void SetShowTileNumber(bool newTileNumbers);
private: private:
Unit m_unit{Unit::Cm}; Unit m_unit{Unit::Cm};
@ -371,6 +374,8 @@ private:
QString m_watermarkPath{}; QString m_watermarkPath{};
bool m_printTilesScheme{false}; bool m_printTilesScheme{false};
bool m_showTileNumbers{false};
}; };
#endif // VPLAYOUTSETTINGS_H #endif // VPLAYOUTSETTINGS_H

View File

@ -3,6 +3,7 @@
#include "../vptilefactory.h" #include "../vptilefactory.h"
#include "../layout/vplayout.h" #include "../layout/vplayout.h"
#include "../layout/vpsheet.h" #include "../layout/vpsheet.h"
#include "qnamespace.h"
#include <QFileInfo> #include <QFileInfo>
#include <QImageReader> #include <QImageReader>
@ -12,6 +13,74 @@
namespace namespace
{ {
constexpr qreal penWidth = 1; constexpr qreal penWidth = 1;
//---------------------------------------------------------------------------------------------------------------------
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;
}
error = abs(factor-1);
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;
}
} // namespace } // namespace
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -61,14 +130,9 @@ void VPGraphicsTileGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem
Q_UNUSED(option); Q_UNUSED(option);
VPLayoutPtr layout = m_layout.toStrongRef(); VPLayoutPtr layout = m_layout.toStrongRef();
if(not layout.isNull() && layout->LayoutSettings().GetShowTiles()) if(layout.isNull() || not layout->LayoutSettings().GetShowTiles())
{ {
VPSheetPtr sheet = layout->GetSheet(m_sheetUuid); return;
QMarginsF sheetMargins;
if (not sheet.isNull() && not sheet->IgnoreMargins())
{
sheetMargins = sheet->GetSheetMargins();
} }
QPen pen(QColor(255,0,0,127), penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); QPen pen(QColor(255,0,0,127), penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
@ -84,11 +148,57 @@ void VPGraphicsTileGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem
const qreal width = (layout->TileFactory()->DrawingAreaWidth() - VPTileFactory::tileStripeWidth) / xScale; const qreal width = (layout->TileFactory()->DrawingAreaWidth() - VPTileFactory::tileStripeWidth) / xScale;
const qreal height = (layout->TileFactory()->DrawingAreaHeight() - VPTileFactory::tileStripeWidth) / yScale; 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 nbCol = layout->TileFactory()->ColNb(sheet);
const int nbRow = layout->TileFactory()->RowNb(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(); 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)
{
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);
}
}
};
auto PaintTileNumber = [painter, layout, nbCol, font]
(const QRectF &img, int i, int j)
{
if (layout->LayoutSettings().GetShowTileNumber())
{
painter->save();
painter->setFont(font);
QPen pen = painter->pen();
pen.setColor(Qt::black);
painter->setPen(pen);
painter->drawText(img, Qt::AlignCenter, QString::number(j*nbCol + i+1));
painter->restore();
}
};
for(int j=0;j<=nbRow;++j) for(int j=0;j<=nbRow;++j)
{ {
// horizontal lines // horizontal lines
@ -103,28 +213,10 @@ void VPGraphicsTileGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem
if (j < nbRow && i < nbCol) if (j < nbRow && i < nbCol)
{ {
QRectF img(sheetMargins.left()+i*width, sheetMargins.top()+j*height, QRectF img(sheetMargins.left()+i*width, sheetMargins.top()+j*height, width, height);
width, height);
if (not layout->LayoutSettings().WatermarkPath().isEmpty() && PaintWatermark(img);
layout->LayoutSettings().GetShowWatermark()) PaintTileNumber(img, i, j);
{
if (watermarkData.opacity > 0)
{
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);
}
}
}
}
} }
} }
} }

View File

@ -36,6 +36,10 @@
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../layout/layoutdef.h" #include "../layout/layoutdef.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
#include "../vmisc/defglobal.h"
#endif // QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
class VPTileFactory; class VPTileFactory;
class VPLayout; class VPLayout;
struct VWatermarkData; struct VWatermarkData;
@ -44,13 +48,13 @@ class VPGraphicsTileGrid : public QGraphicsItem
{ {
public: public:
explicit VPGraphicsTileGrid(const VPLayoutPtr &layout, const QUuid &sheetUuid, QGraphicsItem *parent = nullptr); explicit VPGraphicsTileGrid(const VPLayoutPtr &layout, const QUuid &sheetUuid, QGraphicsItem *parent = nullptr);
~VPGraphicsTileGrid()=default; ~VPGraphicsTileGrid() override =default;
QRectF boundingRect() const override; auto boundingRect() const -> QRectF override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private: private:
Q_DISABLE_COPY(VPGraphicsTileGrid) Q_DISABLE_COPY_MOVE(VPGraphicsTileGrid) // NOLINT
VPLayoutWeakPtr m_layout; VPLayoutWeakPtr m_layout;
QUuid m_sheetUuid; QUuid m_sheetUuid;

View File

@ -1082,6 +1082,16 @@ void VPMainWindow::InitPropertyTabTiles()
LayoutWasSaved(false); LayoutWasSaved(false);
} }
}); });
connect(ui->checkBoxShowTileNumber, &QCheckBox::toggled, this, [this](bool checked)
{
if (not m_layout.isNull())
{
m_layout->LayoutSettings().SetShowTileNumber(checked);
LayoutWasSaved(false);
m_graphicsView->RefreshLayout();
}
});
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -1486,6 +1496,7 @@ void VPMainWindow::SetPropertyTabTilesData()
SetCheckBoxValue(ui->checkBoxTilesShowTiles, m_layout->LayoutSettings().GetShowTiles()); SetCheckBoxValue(ui->checkBoxTilesShowTiles, m_layout->LayoutSettings().GetShowTiles());
SetCheckBoxValue(ui->checkBoxTilesShowWatermark, m_layout->LayoutSettings().GetShowWatermark()); SetCheckBoxValue(ui->checkBoxTilesShowWatermark, m_layout->LayoutSettings().GetShowWatermark());
SetCheckBoxValue(ui->checkBoxPrintTilesScheme, m_layout->LayoutSettings().GetPrintTilesScheme()); SetCheckBoxValue(ui->checkBoxPrintTilesScheme, m_layout->LayoutSettings().GetPrintTilesScheme());
SetCheckBoxValue(ui->checkBoxShowTileNumber, m_layout->LayoutSettings().GetShowTileNumber());
} }
else else
{ {

View File

@ -219,7 +219,7 @@
<enum>QTabWidget::Rounded</enum> <enum>QTabWidget::Rounded</enum>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>2</number>
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
@ -1551,6 +1551,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="checkBoxShowTileNumber">
<property name="text">
<string>Show tile number</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View File

@ -322,6 +322,7 @@ void VPLayoutFileReader::ReadTiles(const VPLayoutPtr &layout)
QXmlStreamAttributes attribs = attributes(); QXmlStreamAttributes attribs = attributes();
layout->LayoutSettings().SetShowTiles(ReadAttributeBool(attribs, ML::AttrVisible, falseStr)); layout->LayoutSettings().SetShowTiles(ReadAttributeBool(attribs, ML::AttrVisible, falseStr));
layout->LayoutSettings().SetPrintTilesScheme(ReadAttributeBool(attribs, ML::AttrPrintScheme, falseStr)); layout->LayoutSettings().SetPrintTilesScheme(ReadAttributeBool(attribs, ML::AttrPrintScheme, falseStr));
layout->LayoutSettings().SetShowTileNumber(ReadAttributeBool(attribs, ML::AttrTileNumber, falseStr));
// attribs.value(ML::AttrMatchingMarks); // TODO // attribs.value(ML::AttrMatchingMarks); // TODO
const QStringList tags const QStringList tags

View File

@ -229,13 +229,13 @@ void VPLayoutFileWriter::WriteSheet(const VPSheetPtr &sheet)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileWriter::WriteTiles(const VPLayoutPtr &layout) void VPLayoutFileWriter::WriteTiles(const VPLayoutPtr &layout)
{ {
Q_UNUSED(layout); // to be removed
writeStartElement(ML::TagTiles); writeStartElement(ML::TagTiles);
SetAttribute(ML::AttrVisible, layout->LayoutSettings().GetShowTiles()); SetAttribute(ML::AttrVisible, layout->LayoutSettings().GetShowTiles());
SetAttribute(ML::AttrMatchingMarks, "standard"); // TODO / Fixme get the right value SetAttribute(ML::AttrMatchingMarks, "standard"); // TODO / Fixme get the right value
SetAttributeOrRemoveIf<bool>(ML::AttrPrintScheme, layout->LayoutSettings().GetPrintTilesScheme(), SetAttributeOrRemoveIf<bool>(ML::AttrPrintScheme, layout->LayoutSettings().GetPrintTilesScheme(),
[](bool print) noexcept {return not print;}); [](bool print) noexcept {return not print;});
SetAttributeOrRemoveIf<bool>(ML::AttrTileNumber, layout->LayoutSettings().GetShowTileNumber(),
[](bool show) noexcept {return not show;});
WriteSize(layout->LayoutSettings().GetTilesSize()); WriteSize(layout->LayoutSettings().GetTilesSize());
WriteMargins(layout->LayoutSettings().GetTilesMargins(), layout->LayoutSettings().IgnoreTilesMargins()); WriteMargins(layout->LayoutSettings().GetTilesMargins(), layout->LayoutSettings().IgnoreTilesMargins());

View File

@ -105,6 +105,7 @@ const QString AttrYScale = QStringLiteral("yScale");
const QString AttrIgnoreMargins = QStringLiteral("ignoreMargins"); const QString AttrIgnoreMargins = QStringLiteral("ignoreMargins");
const QString AttrShowPreview = QStringLiteral("showPreview"); const QString AttrShowPreview = QStringLiteral("showPreview");
const QString AttrPrintScheme = QStringLiteral("printScheme"); const QString AttrPrintScheme = QStringLiteral("printScheme");
const QString AttrTileNumber = QStringLiteral("tileNumber");
const QString atFrontStr = QStringLiteral("atFront"); const QString atFrontStr = QStringLiteral("atFront");
const QString atRearStr = QStringLiteral("atRear"); const QString atRearStr = QStringLiteral("atRear");

View File

@ -110,6 +110,7 @@ extern const QString AttrYScale;
extern const QString AttrIgnoreMargins; extern const QString AttrIgnoreMargins;
extern const QString AttrShowPreview; extern const QString AttrShowPreview;
extern const QString AttrPrintScheme; extern const QString AttrPrintScheme;
extern const QString AttrTileNumber;
extern const QString atFrontStr; extern const QString atFrontStr;
extern const QString atRearStr; extern const QString atRearStr;

View File

@ -39,6 +39,7 @@
<xs:attribute type="xs:boolean" name="visible"/> <xs:attribute type="xs:boolean" name="visible"/>
<xs:attribute type="xs:string" name="matchingMarks"/> <xs:attribute type="xs:string" name="matchingMarks"/>
<xs:attribute type="xs:boolean" name="printScheme"/> <xs:attribute type="xs:boolean" name="printScheme"/>
<xs:attribute type="xs:boolean" name="tileNumber"/>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
<xs:element name="scale"> <xs:element name="scale">