Fix manual layout format conversion and reading.

This commit is contained in:
Roman Telezhynskyi 2022-11-21 18:52:11 +02:00
parent 1925300ff3
commit 0f2c922ead
12 changed files with 53 additions and 22 deletions

View file

@ -492,25 +492,29 @@ auto VPPiece::PathsSuperposition(const QVector<QPointF> &path1, const QVector<QP
}
//---------------------------------------------------------------------------------------------------------------------
auto VPPiece::IsValid() const -> bool
auto VPPiece::IsValid(QString &error) const -> bool
{
if (not IsHideMainPath() && GetContourPoints().isEmpty())
{
error = tr("Seam line is empty");
return false;
}
if (IsSeamAllowance() && IsSeamAllowanceBuiltIn() && GetContourPoints().isEmpty())
{
error = tr("Seam line is empty");
return false;
}
if (IsSeamAllowance() && not IsSeamAllowanceBuiltIn() && GetSeamAllowancePoints().isEmpty())
{
error = tr("Seam allowance line is empty");
return false;
}
if (IsGrainlineEnabled() && GetGrainline().isEmpty())
{
error = tr("Grainline is empty");
return false;
}

View file

@ -123,7 +123,7 @@ public:
static auto PathsSuperposition(const QVector<QPointF> &path1, const QVector<QPointF> &path2) -> bool;
auto IsValid() const -> bool;
auto IsValid(QString &error) const -> bool;
auto CopyNumber() const -> quint16;
void SetCopyNumber(quint16 newCopyNumber);

View file

@ -3203,9 +3203,10 @@ auto VPMainWindow::AddLayoutPieces(const QVector<VLayoutPiece> &pieces) -> bool
VPPiecePtr piece(new VPPiece(rawPiece));
piece->SetCopyNumber(i);
if (not piece->IsValid())
QString error;
if (not piece->IsValid(error))
{
qCCritical(pWindow) << qPrintable(tr("Piece %1 invalid.").arg(piece->GetName()));
qCCritical(pWindow) << qPrintable(tr("Piece %1 invalid. %2").arg(piece->GetName(), error));
if (m_cmd != nullptr && not m_cmd->IsGuiEnabled())
{

View file

@ -437,9 +437,10 @@ void VPLayoutFileReader::ReadPieces(const VPLayoutPtr &layout, const VPSheetPtr
VPPiecePtr piece(new VPPiece());
ReadPiece(piece);
if (not piece->IsValid())
QString error;
if (not piece->IsValid(error))
{
throw VException(tr("Piece %1 invalid.").arg(piece->GetName()));
throw VException(tr("Piece %1 invalid. %2").arg(piece->GetName(), error));
}
piece->SetSheet(sheet);
@ -461,7 +462,7 @@ void VPLayoutFileReader::ReadPiece(const VPPiecePtr &piece)
QXmlStreamAttributes attribs = attributes();
piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));
QString uuidStr = ReadAttributeString(attribs, ML::AttrID, QUuid::createUuid().toString());
QString uuidStr = ReadAttributeString(attribs, ML::AttrUID, QUuid::createUuid().toString());
piece->SetUUID(QUuid(uuidStr));
piece->SetGradationId(ReadAttributeEmptyString(attribs, ML::AttrGradationLabel));
@ -576,6 +577,7 @@ void VPLayoutFileReader::ReadSeamAllowance(const VPPiecePtr &piece)
piece->SetSeamAllowance(enabled);
bool builtIn = ReadAttributeBool(attribs, ML::AttrBuiltIn, falseStr);
piece->SetSeamAllowanceBuiltIn(builtIn);
QVector<VLayoutPoint> path = ReadLayoutPoints();

View file

@ -251,7 +251,7 @@ void VPLayoutFileWriter::WritePieceList(const QList<VPPiecePtr> &list, const QSt
void VPLayoutFileWriter::WritePiece(const VPPiecePtr &piece)
{
writeStartElement(ML::TagPiece);
SetAttribute(ML::AttrID, piece->GetUUID().toString());
SetAttribute(ML::AttrUID, piece->GetUUID().toString());
SetAttribute(ML::AttrName, piece->GetName());
SetAttributeOrRemoveIf<bool>(ML::AttrMirrored, piece->IsMirror(),
[](bool mirrored) noexcept {return not mirrored;});

View file

@ -76,7 +76,7 @@ const QString AttrBottom = QStringLiteral("bottom"); // NOLINT(cer
const QString AttrWidth = QStringLiteral("width"); // NOLINT(cert-err58-cpp)
const QString AttrLength = QStringLiteral("length"); // NOLINT(cert-err58-cpp)
const QString AttrFollowGrainline = QStringLiteral("followGrainline"); // NOLINT(cert-err58-cpp)
const QString AttrID = QStringLiteral("id"); // NOLINT(cert-err58-cpp)
const QString AttrUID = QStringLiteral("uid"); // NOLINT(cert-err58-cpp)
const QString AttrMirrored = QStringLiteral("mirrored"); // NOLINT(cert-err58-cpp)
const QString AttrForbidFlipping = QStringLiteral("forbidFlipping"); // NOLINT(cert-err58-cpp)
const QString AttrForceFlipping = QStringLiteral("forceFlipping"); // NOLINT(cert-err58-cpp)

View file

@ -81,7 +81,7 @@ extern const QString AttrBottom;
extern const QString AttrWidth;
extern const QString AttrLength;
extern const QString AttrFollowGrainline;
extern const QString AttrID;
extern const QString AttrUID;
extern const QString AttrMirrored;
extern const QString AttrForbidFlipping;
extern const QString AttrForceFlipping;

View file

@ -72,6 +72,7 @@
<xs:simpleContent>
<xs:extension base="PathOrEmpty">
<xs:attribute type="xs:boolean" name="enabled" use="optional"/>
<xs:attribute type="xs:boolean" name="builtIn" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
@ -244,6 +245,7 @@
<xs:simpleContent>
<xs:extension base="PathOrEmpty">
<xs:attribute type="xs:boolean" name="enabled" use="optional"/>
<xs:attribute type="xs:boolean" name="builtIn" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

View file

@ -93,6 +93,7 @@
</xs:element>
</xs:sequence>
<xs:attribute type="xs:boolean" name="enabled" use="optional"/>
<xs:attribute type="xs:boolean" name="builtIn" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="grainline">
@ -215,7 +216,7 @@
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="uuid" use="required"/>
<xs:attribute name="uid" type="uuid" use="required"/>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:boolean" name="mirrored"/>
<xs:attribute type="xs:boolean" name="forbidFlipping"/>
@ -287,6 +288,7 @@
</xs:element>
</xs:sequence>
<xs:attribute type="xs:boolean" name="enabled" use="optional"/>
<xs:attribute type="xs:boolean" name="builtIn" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="grainline">
@ -409,7 +411,7 @@
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="uuid" use="required"/>
<xs:attribute name="uid" type="uuid" use="required"/>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:boolean" name="mirrored"/>
<xs:attribute type="xs:boolean" name="forbidFlipping"/>

View file

@ -339,21 +339,28 @@ QDomElement VDomDocument::elementById(quint32 id, const QString &tagName, bool u
* @param id id value
* @return true if found
*/
bool VDomDocument::find(QHash<quint32, QDomElement> &cache, const QDomElement &node, quint32 id)
auto VDomDocument::find(QHash<quint32, QDomElement> &cache, const QDomElement &node, quint32 id) -> bool
{
if (node.hasAttribute(AttrId))
{
const quint32 elementId = GetParametrUInt(node, AttrId, NULL_ID_STR);
if (cache.contains(elementId))
try
{
qWarning() << tr("Not unique id (%1)").arg(elementId);
const quint32 elementId = GetParametrUInt(node, AttrId, NULL_ID_STR);
if (cache.contains(elementId))
{
qWarning() << tr("Not unique id (%1)").arg(elementId);
}
cache.insert(elementId, node);
if (elementId == id)
{
return true;
}
}
cache.insert(elementId, node);
if (elementId == id)
catch (const VExceptionConversionError &)
{
return true;
// ignore
}
}

View file

@ -54,10 +54,13 @@ Q_GLOBAL_STATIC_WITH_ARGS(const QString, strSeamAllowanceTag, (QLatin1String("se
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strInternalPathTag, (QLatin1String("internalPath"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strMarkerTag, (QLatin1String("marker"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strPointTag, (QLatin1String("point"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strPieceTag, (QLatin1String("piece"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrX, (QLatin1String("x"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrY, (QLatin1String("y"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrTurnPoint, (QLatin1String("turnPoint"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrCurvePoint, (QLatin1String("curvePoint"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrId, (QLatin1String("id"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strAttrUId, (QLatin1String("uid"))) // NOLINT
//const QChar groupSep = QLatin1Char(';');
const QChar coordintatesSep = QLatin1Char(',');
@ -210,6 +213,17 @@ void VLayoutConverter::ConvertPiecesToV0_1_3()
QDomElement node = tags.at(i).toElement();
RemoveAllChildren(node);
}
QDomNodeList pieceTags = elementsByTagName(*strPieceTag);
for (int i=0; i < pieceTags.size(); ++i)
{
QDomElement node = pieceTags.at(i).toElement();
if (node.isElement() && node.hasAttribute(*strAttrId))
{
node.setAttribute(*strAttrUId, node.attribute(*strAttrId));
node.removeAttribute(*strAttrId);
}
}
}
//---------------------------------------------------------------------------------------------------------------------

View file

@ -62,7 +62,6 @@ protected:
auto IsReadOnly() const -> bool override;
void ConvertPiecesToV0_1_3();
void ConvertPieceToV0_1_3(const QDomElement &piece);
void ConvertPathToV0_1_3(QDomElement &node);
void ToV0_1_3();