valentina/src/app/puzzle/xml/vplayoutfilereader.cpp

977 lines
31 KiB
C++
Raw Normal View History

2020-04-18 16:32:54 +02:00
/************************************************************************
**
2020-05-23 14:04:39 +02:00
** @file vplayoutfilereader.cpp
2020-04-18 16:32:54 +02:00
** @author Ronan Le Tiec
** @date 18 4, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
** *************************************************************************/
2021-08-14 14:19:28 +02:00
#include <Qt>
#include <QFont>
2020-04-18 20:24:25 +02:00
#include <QXmlStreamAttributes>
2020-05-23 14:01:03 +02:00
#include "vplayoutfilereader.h"
2020-05-23 14:02:39 +02:00
#include "vplayoutfilewriter.h"
2020-05-23 13:51:57 +02:00
#include "vplayoutliterals.h"
2021-08-09 14:09:10 +02:00
#include "../layout/vpsheet.h"
2021-08-14 14:19:28 +02:00
#include "../vlayout/vlayoutpiecepath.h"
#include "../vlayout/vtextmanager.h"
#include "../ifc/exception/vexception.h"
#include "../ifc/exception/vexceptionconversionerror.h"
2020-04-18 16:32:54 +02:00
2021-05-22 19:29:33 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(MLReader, "mlReader")
QT_WARNING_POP
2021-08-14 14:19:28 +02:00
namespace
{
//---------------------------------------------------------------------------------------------------------------------
auto StringToTransfrom(const QString &matrix) -> QTransform
{
QStringList elements = matrix.split(ML::groupSep);
if (elements.count() == 9)
{
qreal m11 = elements.at(0).toDouble();
qreal m12 = elements.at(1).toDouble();
qreal m13 = elements.at(2).toDouble();
qreal m21 = elements.at(3).toDouble();
qreal m22 = elements.at(4).toDouble();
qreal m23 = elements.at(5).toDouble();
qreal m31 = elements.at(6).toDouble();
qreal m32 = elements.at(7).toDouble();
qreal m33 = elements.at(8).toDouble();
return {m11, m12, m13, m21, m22, m23, m31, m32, m33};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToPoint(const QString &point) -> QPointF
{
QStringList coordinates = point.split(ML::coordintatesSep);
if (coordinates.count() == 2)
{
return {coordinates.at(0).toDouble(), coordinates.at(1).toDouble()};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToPath(const QString &path) -> QVector<QPointF>
{
QVector<QPointF> p;
QStringList points = path.split(ML::pointsSep);
for (const auto& point : points)
{
p.append(StringToPoint(point));
}
return p;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToGrainlineArrowDirrection(const QString &dirrection) -> GrainlineArrowDirection
{
const QStringList arrows
{
ML::atFrontStr, // 0
ML::atRearStr, // 1
ML::atBothStr // 2
};
GrainlineArrowDirection arrowDirection = GrainlineArrowDirection::atBoth;
switch (arrows.indexOf(dirrection))
{
case 0:// at front
arrowDirection = GrainlineArrowDirection::atFront;
break;
case 1:// at rear
arrowDirection = GrainlineArrowDirection::atRear;
break;
case 2:// at both
default:
arrowDirection = GrainlineArrowDirection::atBoth;
break;
}
return arrowDirection;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToLine(const QString &string) -> QLineF
{
QStringList points = string.split(ML::groupSep);
if (points.count() == 2)
{
return {StringToPoint(points.at(0)), StringToPoint(points.at(1))};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToLines(const QString &string) -> QVector<QLineF>
{
QStringList lines = string.split(ML::itemsSep);
QVector<QLineF> path;
for (const auto& line : lines)
{
QLineF l = StringToLine(line);
if (not l.isNull())
{
path.append(StringToLine(line));
}
}
return path;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToRect(const QString &string) -> QRectF
{
QStringList points = string.split(ML::groupSep);
if (points.count() == 4)
{
return {points.at(0).toDouble(), points.at(1).toDouble(), points.at(2).toDouble(), points.at(3).toDouble()};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToMarkerShape(const QString &string) -> PlaceLabelImg
{
PlaceLabelImg shape;
QStringList paths = string.split(ML::itemsSep);
for (const auto& path : paths)
{
shape.append(StringToPath(path));
}
return shape;
}
} // namespace
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayoutFileReader::ReadFile(const VPLayoutPtr &layout, QFile *file) -> bool
2020-04-18 20:24:25 +02:00
{
setDevice(file);
2021-05-22 19:29:33 +02:00
try
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
if (readNextStartElement())
{
ReadLayout(layout);
}
}
catch(const VException &e)
{
raiseError(e.ErrorMessage());
2020-04-18 20:24:25 +02:00
}
2021-05-21 17:08:37 +02:00
return hasError();
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadLayout(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagLayout);
2021-07-29 16:11:18 +02:00
const QStringList tags
{
ML::TagProperties, // 0
ML::TagUnplacedPieces, // 1
2021-08-14 14:19:28 +02:00
ML::TagSheets // 2
2021-07-29 16:11:18 +02:00
};
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-05-22 19:29:33 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0: // ML::TagProperties
ReadProperties(layout);
break;
case 1: // ML::TagUnplacedPieces
ReadUnplacedPieces(layout);
break;
case 2: // ML::TagSheets
ReadSheets(layout);
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadProperties(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagProperties);
const QStringList tags
{
ML::TagUnit, // 0
ML::TagTitle, // 1
ML::TagDescription, // 2
2021-09-01 08:20:58 +02:00
ML::TagControl, // 3
ML::TagTiles // 4
2021-05-22 19:29:33 +02:00
};
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2020-04-23 14:49:35 +02:00
qDebug() << name().toString();
2020-04-19 16:01:46 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0:// unit
qDebug("read unit");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetUnit(StrToUnits(readElementText()));
2021-05-22 19:29:33 +02:00
break;
case 1:// title
qDebug("read title");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetTitle(readElementText());
2021-05-22 19:29:33 +02:00
break;
case 2:// description
qDebug("read description");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetDescription(readElementText());
2021-05-22 19:29:33 +02:00
break;
2021-09-01 08:20:58 +02:00
case 3: // control
2021-05-22 19:29:33 +02:00
qDebug("read control");
ReadControl(layout);
break;
2021-09-01 08:20:58 +02:00
case 4: // tiles
2021-05-22 19:29:33 +02:00
qDebug("read tiles");
ReadTiles(layout);
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
}
2021-05-22 19:29:33 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadControl(const VPLayoutPtr &layout)
2021-05-22 19:29:33 +02:00
{
AssertRootTag(ML::TagControl);
QXmlStreamAttributes attribs = attributes();
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetWarningSuperpositionOfPieces(
ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
layout->LayoutSettings().SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
2021-08-14 14:19:28 +02:00
layout->LayoutSettings().SetStickyEdges(ReadAttributeBool(attribs, ML::AttrStickyEdges, trueStr));
2021-08-30 17:45:27 +02:00
layout->LayoutSettings().SetPiecesGap(qMax(ReadAttributeDouble(attribs, ML::AttrPiecesGap, QChar('0')), 0.0));
2021-08-26 18:04:24 +02:00
layout->LayoutSettings().SetFollowGrainline(ReadAttributeBool(attribs, ML::AttrFollowGrainline, falseStr));
2021-05-22 19:29:33 +02:00
readElementText();
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadUnplacedPieces(const VPLayoutPtr &layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagUnplacedPieces);
2021-07-29 16:11:18 +02:00
ReadPieces(layout);
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadTiles(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagTiles);
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
QXmlStreamAttributes attribs = attributes();
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetShowTiles(ReadAttributeBool(attribs, ML::AttrVisible, falseStr));
2021-05-22 19:29:33 +02:00
// attribs.value(ML::AttrMatchingMarks); // TODO
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
const QStringList tags
{
ML::TagSize, // 0
ML::TagMargin // 1
};
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-05-22 19:29:33 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0: // size
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetTilesSize(ReadSize());
2021-05-22 19:29:33 +02:00
break;
case 1: // margin
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetTilesMargins(ReadMargins());
2021-05-22 19:29:33 +02:00
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSheets(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagSheets);
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagSheet)
2020-04-18 20:24:25 +02:00
{
ReadSheet(layout);
2020-04-18 20:24:25 +02:00
}
else
{
2021-05-22 19:29:33 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSheet(const VPLayoutPtr &layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagSheet);
2021-09-01 08:20:58 +02:00
VPSheetPtr sheet(new VPSheet(layout));
QXmlStreamAttributes attribs = attributes();
sheet->SetGrainlineType(StrToGrainlineType(ReadAttributeEmptyString(attribs, ML::AttrGrainlineType)));
2021-05-22 19:29:33 +02:00
const QStringList tags
{
ML::TagName, // 0
2021-09-01 08:20:58 +02:00
ML::TagSize, // 1
ML::TagMargin, // 2
ML::TagPieces // 3
2021-05-22 19:29:33 +02:00
};
while (readNextStartElement())
{
switch (tags.indexOf(name().toString()))
{
case 0: // name
sheet->SetName(readElementText());
break;
2021-09-01 08:20:58 +02:00
case 1: // size
sheet->SetSheetSize(ReadSize());
break;
case 2: // margin
sheet->SetSheetMargins(ReadMargins());
break;
case 3: // pieces
2021-08-18 19:33:47 +02:00
ReadPieces(layout, sheet);
2021-05-22 19:29:33 +02:00
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
}
}
readElementText();
2021-08-18 19:33:47 +02:00
layout->AddSheet(sheet);
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadPieces(const VPLayoutPtr &layout, const VPSheetPtr &sheet)
2020-04-18 20:24:25 +02:00
{
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagPiece)
2020-04-18 20:24:25 +02:00
{
2021-08-18 19:33:47 +02:00
VPPiecePtr piece(new VPPiece());
ReadPiece(piece);
if (not piece->IsValid())
{
throw VException(tr("Piece %1 invalid.").arg(piece->GetName()));
}
2021-07-29 16:11:18 +02:00
piece->SetSheet(sheet);
2021-08-18 19:33:47 +02:00
VPLayout::AddPiece(layout, piece);
2020-04-18 20:24:25 +02:00
}
else
{
2021-05-22 19:29:33 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadPiece(const VPPiecePtr &piece)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagPiece);
2020-04-18 20:24:25 +02:00
QXmlStreamAttributes attribs = attributes();
piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));
2020-05-01 19:08:48 +02:00
2021-05-22 19:29:33 +02:00
QString uuidStr = ReadAttributeString(attribs, ML::AttrID, QUuid::createUuid().toString());
2020-06-25 14:17:31 +02:00
piece->SetUUID(QUuid(uuidStr));
2021-08-31 12:08:59 +02:00
piece->SetName(ReadAttributeEmptyString(attribs, ML::AttrGradationLabel));
piece->SetCopyNumber(static_cast<quint16>(ReadAttributeUInt(attribs, ML::AttrCopyNumber, QChar('1'))));
2021-08-31 13:12:46 +02:00
piece->SetHideMainPath(not ReadAttributeBool(attribs, ML::AttrShowSeamline, trueStr));
2020-05-01 19:08:48 +02:00
bool pieceMirrored = ReadAttributeBool(attribs, ML::AttrMirrored, falseStr);
2021-08-09 14:09:10 +02:00
piece->SetMirror(pieceMirrored);
2021-08-14 14:19:28 +02:00
QString matrix = ReadAttributeEmptyString(attribs, ML::AttrTransform);
piece->SetMatrix(StringToTransfrom(matrix));
const QStringList tags
{
ML::TagSeamLine, // 0
ML::TagSeamAllowance, // 1
ML::TagGrainline, // 2
ML::TagNotches, // 3
ML::TagInternalPaths, // 4
ML::TagMarkers, // 5
ML::TagLabels // 6
};
while (readNextStartElement())
{
switch (tags.indexOf(name().toString()))
{
case 0: // seam line
piece->SetCountourPoints(StringToPath(readElementText()));
break;
case 1: // seam allowance
ReadSeamAllowance(piece);
break;
case 2: // grainline
ReadGrainline(piece);
break;
case 3: // notches
ReadNotches(piece);
break;
case 4: // internal paths
ReadInternalPaths(piece);
break;
case 5: // markers
ReadMarkers(piece);
break;
case 6: // labels
ReadLabels(piece);
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSeamAllowance(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagSeamAllowance);
QXmlStreamAttributes attribs = attributes();
bool enabled = ReadAttributeBool(attribs, ML::AttrEnabled, falseStr);
piece->SetSeamAllowance(enabled);
bool builtIn = ReadAttributeBool(attribs, ML::AttrBuiltIn, falseStr);
QVector<QPointF> path = StringToPath(readElementText());
if (enabled)
{
if (not builtIn)
{
2021-09-04 14:57:31 +02:00
if (path.isEmpty())
{
throw VException(tr("Error in line %1. Seam allowance is empty.").arg(lineNumber()));
}
2021-08-14 14:19:28 +02:00
piece->SetSeamAllowancePoints(path);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadGrainline(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagGrainline);
QXmlStreamAttributes attribs = attributes();
bool enabled = ReadAttributeBool(attribs, ML::AttrEnabled, falseStr);
piece->SetGrainlineEnabled(enabled);
QVector<QPointF> path = StringToPath(readElementText());
if (enabled)
{
piece->SetGrainlineAngle(ReadAttributeDouble(attribs, ML::AttrAngle, QChar('0')));
QString arrowDirection = ReadAttributeEmptyString(attribs, ML::AttrArrowDirection);
piece->SetGrainlineArrowType(StringToGrainlineArrowDirrection(arrowDirection));
2021-09-04 14:57:31 +02:00
if (path.isEmpty())
{
throw VException(tr("Error in line %1. Grainline is empty.").arg(lineNumber()));
}
2021-08-14 14:19:28 +02:00
piece->SetGrainlinePoints(path);
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadNotches(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagNotches);
QVector<VLayoutPassmark> passmarks;
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-08-14 14:19:28 +02:00
if (name() == ML::TagNotch)
2020-04-18 20:24:25 +02:00
{
2021-08-14 14:19:28 +02:00
passmarks.append(ReadNotch());
2020-04-18 20:24:25 +02:00
}
else
{
2021-08-14 14:19:28 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
2021-08-14 14:19:28 +02:00
piece->SetPassmarks(passmarks);
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadNotch() -> VLayoutPassmark
{
AssertRootTag(ML::TagNotch);
QXmlStreamAttributes attribs = attributes();
VLayoutPassmark passmark;
passmark.isBuiltIn = ReadAttributeBool(attribs, ML::AttrBuiltIn, falseStr);
passmark.baseLine = StringToLine(ReadAttributeEmptyString(attribs, ML::AttrBaseLine));
passmark.lines = StringToLines(ReadAttributeEmptyString(attribs, ML::AttrPath));
QString defaultType = QString::number(static_cast<int>(PassmarkLineType::OneLine));
passmark.type = static_cast<PassmarkLineType>(ReadAttributeUInt(attribs, ML::AttrType, defaultType));
readElementText();
return passmark;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadInternalPaths(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagInternalPaths);
QVector<VLayoutPiecePath> internalPaths;
while (readNextStartElement())
{
if (name() == ML::TagInternalPath)
{
internalPaths.append(ReadInternalPath());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
piece->SetInternalPaths(internalPaths);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadInternalPath() -> VLayoutPiecePath
{
AssertRootTag(ML::TagInternalPath);
VLayoutPiecePath path;
QXmlStreamAttributes attribs = attributes();
path.SetCutPath(ReadAttributeBool(attribs, ML::AttrCut, falseStr));
path.SetPenStyle(LineStyleToPenStyle(ReadAttributeString(attribs, ML::AttrPenStyle, TypeLineLine)));
2021-09-04 14:57:31 +02:00
QVector<QPointF> shape = StringToPath(readElementText());
if (shape.isEmpty())
{
throw VException(tr("Error in line %1. Internal path shape is empty.").arg(lineNumber()));
}
path.SetPoints(shape);
2021-08-14 14:19:28 +02:00
return path;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadMarkers(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagMarkers);
QVector<VLayoutPlaceLabel> markers;
while (readNextStartElement())
{
if (name() == ML::TagMarker)
{
markers.append(ReadMarker());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
piece->SetPlaceLabels(markers);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadMarker() -> VLayoutPlaceLabel
{
AssertRootTag(ML::TagMarker);
VLayoutPlaceLabel marker;
QXmlStreamAttributes attribs = attributes();
QString matrix = ReadAttributeEmptyString(attribs, ML::AttrTransform);
marker.rotationMatrix = StringToTransfrom(matrix);
marker.type = static_cast<PlaceLabelType>(ReadAttributeUInt(attribs, ML::AttrType, QChar('0')));
marker.center = StringToPoint(ReadAttributeEmptyString(attribs, ML::AttrCenter));
marker.box = StringToRect(ReadAttributeEmptyString(attribs, ML::AttrBox));
2021-09-04 14:57:31 +02:00
PlaceLabelImg shape = StringToMarkerShape(readElementText());
if (shape.isEmpty())
{
throw VException(tr("Error in line %1. Marker shape is empty.").arg(lineNumber()));
}
marker.shape = shape;
2021-08-14 14:19:28 +02:00
return marker;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadLabels(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagLabels);
while (readNextStartElement())
{
if (name() == ML::TagPieceLabel)
{
2021-09-04 15:01:26 +02:00
ReadPieceLabel(piece);
}
else if (name() == ML::TagPatternLabel)
{
ReadPatternLabel(piece);
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadPieceLabel(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagPieceLabel);
2021-08-14 14:19:28 +02:00
2021-09-04 15:01:26 +02:00
QXmlStreamAttributes attribs = attributes();
piece->SetPieceLabelRect(StringToPath(ReadAttributeEmptyString(attribs, ML::AttrShape)));
while (readNextStartElement())
{
if (name() == ML::TagLines)
{
2021-08-14 14:19:28 +02:00
piece->SetPieceLabelData(ReadLabelLines());
}
2021-09-04 15:01:26 +02:00
else
2021-08-14 14:19:28 +02:00
{
2021-09-04 15:01:26 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
2021-08-14 14:19:28 +02:00
2021-09-04 15:01:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadPatternLabel(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagPatternLabel);
QXmlStreamAttributes attribs = attributes();
piece->SetPatternLabelRect(StringToPath(ReadAttributeEmptyString(attribs, ML::AttrShape)));
while (readNextStartElement())
{
if (name() == ML::TagLines)
{
2021-08-14 14:19:28 +02:00
piece->SetPatternLabelData(ReadLabelLines());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadLabelLines() -> VTextManager
{
2021-09-04 15:01:26 +02:00
AssertRootTag(ML::TagLines);
2021-08-14 14:19:28 +02:00
VTextManager text;
QVector<TextLine> lines;
QXmlStreamAttributes attribs = attributes();
QFont f;
f.fromString(ReadAttributeEmptyString(attribs, ML::AttrFont));
text.SetFont(f);
while (readNextStartElement())
{
if (name() == ML::TagLine)
{
lines.append(ReadLabelLine());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
text.SetAllSourceLines(lines);
return text;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadLabelLine() -> TextLine
{
AssertRootTag(ML::TagLine);
TextLine line;
QXmlStreamAttributes attribs = attributes();
line.m_iFontSize = ReadAttributeInt(attribs, ML::AttrFontSize, QString::number(MIN_FONT_SIZE));
line.m_bold = ReadAttributeBool(attribs, ML::AttrBold, falseStr);
line.m_italic = ReadAttributeBool(attribs, ML::AttrItalic, falseStr);
int alignment = ReadAttributeInt(attribs, ML::AttrAlignment, QString::number(static_cast<int>(Qt::AlignCenter)));
line.m_eAlign = static_cast<Qt::Alignment>(alignment);
line.m_qsText = readElementText();
return line;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadMargins() -> QMarginsF
2020-04-18 20:24:25 +02:00
{
QMarginsF margins = QMarginsF();
QXmlStreamAttributes attribs = attributes();
margins.setLeft(ReadAttributeDouble(attribs, ML::AttrLeft, QChar('0')));
margins.setTop(ReadAttributeDouble(attribs, ML::AttrTop, QChar('0')));
margins.setRight(ReadAttributeDouble(attribs, ML::AttrRight, QChar('0')));
margins.setBottom(ReadAttributeDouble(attribs, ML::AttrBottom, QChar('0')));
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
return margins;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadSize() -> QSizeF
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
QSizeF size;
2020-04-18 20:24:25 +02:00
QXmlStreamAttributes attribs = attributes();
size.setWidth(ReadAttributeDouble(attribs, ML::AttrWidth, QChar('0')));
size.setHeight(ReadAttributeDouble(attribs, ML::AttrLength, QChar('0')));
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
return size;
}
2021-05-22 19:29:33 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::AssertRootTag(const QString &tag) const
{
if (not (isStartElement() && name() == tag))
{
2021-06-04 09:00:14 +02:00
throw VException(tr("Unexpected tag %1 in line %2").arg(name().toString()).arg(lineNumber()));
2021-05-22 19:29:33 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> QString
{
const QString parameter = attribs.value(name).toString();
if (parameter.isEmpty())
{
if (defValue.isEmpty())
{
throw VException(tr("Got empty attribute '%1'").arg(name));
}
2021-05-22 19:29:33 +02:00
return defValue;
}
return parameter;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name) -> QString
{
return attribs.value(name).toString();
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> bool
{
QString parametr;
bool val = true;
const QString message = QObject::tr("Can't convert toBool parameter");
try
{
parametr = ReadAttributeString(attribs, name, defValue);
const QStringList bools {trueStr, falseStr, QChar('1'), QChar('0')};
switch (bools.indexOf(parametr))
{
case 0: // true
case 2: // 1
val = true;
break;
case 1: // false
case 3: // 0
val = false;
break;
default:// others
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return val;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeDouble(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> qreal
{
bool ok = false;
qreal param = 0;
const QString message = QObject::tr("Can't convert toDouble parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(QChar(','), QChar('.')).toDouble(&ok);
2021-05-22 19:29:33 +02:00
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return param;
}
2021-08-14 14:19:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadAttributeUInt(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> quint32
{
bool ok = false;
quint32 param = 0;
const QString message = QObject::tr("Can't convert toUInt parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(QChar(','), QChar('.')).toUInt(&ok);
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return param;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadAttributeInt(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> int
{
bool ok = false;
int param = 0;
const QString message = QObject::tr("Can't convert toInt parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(QChar(','), QChar('.')).toInt(&ok);
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return param;
}