From 0f2c922eadbe916761a5832aaed877b4ca0dafd7 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 21 Nov 2022 18:52:11 +0200 Subject: [PATCH] Fix manual layout format conversion and reading. --- src/app/puzzle/layout/vppiece.cpp | 6 +++++- src/app/puzzle/layout/vppiece.h | 2 +- src/app/puzzle/vpmainwindow.cpp | 5 +++-- src/app/puzzle/xml/vplayoutfilereader.cpp | 8 +++++--- src/app/puzzle/xml/vplayoutfilewriter.cpp | 2 +- src/app/puzzle/xml/vplayoutliterals.cpp | 2 +- src/app/puzzle/xml/vplayoutliterals.h | 2 +- src/libs/ifc/schema/layout/v0.1.2.xsd | 2 ++ src/libs/ifc/schema/layout/v0.1.3.xsd | 6 ++++-- src/libs/ifc/xml/vdomdocument.cpp | 25 +++++++++++++++-------- src/libs/ifc/xml/vlayoutconverter.cpp | 14 +++++++++++++ src/libs/ifc/xml/vlayoutconverter.h | 1 - 12 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/app/puzzle/layout/vppiece.cpp b/src/app/puzzle/layout/vppiece.cpp index 1632d47f0..8fd65d179 100644 --- a/src/app/puzzle/layout/vppiece.cpp +++ b/src/app/puzzle/layout/vppiece.cpp @@ -492,25 +492,29 @@ auto VPPiece::PathsSuperposition(const QVector &path1, const QVector 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; } diff --git a/src/app/puzzle/layout/vppiece.h b/src/app/puzzle/layout/vppiece.h index 9c6600d8e..19ae71c66 100644 --- a/src/app/puzzle/layout/vppiece.h +++ b/src/app/puzzle/layout/vppiece.h @@ -123,7 +123,7 @@ public: static auto PathsSuperposition(const QVector &path1, const QVector &path2) -> bool; - auto IsValid() const -> bool; + auto IsValid(QString &error) const -> bool; auto CopyNumber() const -> quint16; void SetCopyNumber(quint16 newCopyNumber); diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 4c4bf1d4a..fdf0d3764 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -3203,9 +3203,10 @@ auto VPMainWindow::AddLayoutPieces(const QVector &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()) { diff --git a/src/app/puzzle/xml/vplayoutfilereader.cpp b/src/app/puzzle/xml/vplayoutfilereader.cpp index 520599238..68407e0cf 100644 --- a/src/app/puzzle/xml/vplayoutfilereader.cpp +++ b/src/app/puzzle/xml/vplayoutfilereader.cpp @@ -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 path = ReadLayoutPoints(); diff --git a/src/app/puzzle/xml/vplayoutfilewriter.cpp b/src/app/puzzle/xml/vplayoutfilewriter.cpp index 0496f0253..37e4f04ca 100644 --- a/src/app/puzzle/xml/vplayoutfilewriter.cpp +++ b/src/app/puzzle/xml/vplayoutfilewriter.cpp @@ -251,7 +251,7 @@ void VPLayoutFileWriter::WritePieceList(const QList &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(ML::AttrMirrored, piece->IsMirror(), [](bool mirrored) noexcept {return not mirrored;}); diff --git a/src/app/puzzle/xml/vplayoutliterals.cpp b/src/app/puzzle/xml/vplayoutliterals.cpp index 03c757dbb..114eb50de 100644 --- a/src/app/puzzle/xml/vplayoutliterals.cpp +++ b/src/app/puzzle/xml/vplayoutliterals.cpp @@ -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) diff --git a/src/app/puzzle/xml/vplayoutliterals.h b/src/app/puzzle/xml/vplayoutliterals.h index 807e42fb5..a71920a76 100644 --- a/src/app/puzzle/xml/vplayoutliterals.h +++ b/src/app/puzzle/xml/vplayoutliterals.h @@ -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; diff --git a/src/libs/ifc/schema/layout/v0.1.2.xsd b/src/libs/ifc/schema/layout/v0.1.2.xsd index 9c2b0e82b..68af9f584 100644 --- a/src/libs/ifc/schema/layout/v0.1.2.xsd +++ b/src/libs/ifc/schema/layout/v0.1.2.xsd @@ -72,6 +72,7 @@ + @@ -244,6 +245,7 @@ + diff --git a/src/libs/ifc/schema/layout/v0.1.3.xsd b/src/libs/ifc/schema/layout/v0.1.3.xsd index b1d95ae0b..c1c1f86a0 100644 --- a/src/libs/ifc/schema/layout/v0.1.3.xsd +++ b/src/libs/ifc/schema/layout/v0.1.3.xsd @@ -93,6 +93,7 @@ + @@ -215,7 +216,7 @@ - + @@ -287,6 +288,7 @@ + @@ -409,7 +411,7 @@ - + diff --git a/src/libs/ifc/xml/vdomdocument.cpp b/src/libs/ifc/xml/vdomdocument.cpp index 8e229976d..159445960 100644 --- a/src/libs/ifc/xml/vdomdocument.cpp +++ b/src/libs/ifc/xml/vdomdocument.cpp @@ -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 &cache, const QDomElement &node, quint32 id) +auto VDomDocument::find(QHash &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 } } diff --git a/src/libs/ifc/xml/vlayoutconverter.cpp b/src/libs/ifc/xml/vlayoutconverter.cpp index c3f1d247f..f069e1fdb 100644 --- a/src/libs/ifc/xml/vlayoutconverter.cpp +++ b/src/libs/ifc/xml/vlayoutconverter.cpp @@ -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); + } + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/ifc/xml/vlayoutconverter.h b/src/libs/ifc/xml/vlayoutconverter.h index 50a18caa1..71871cf8b 100644 --- a/src/libs/ifc/xml/vlayoutconverter.h +++ b/src/libs/ifc/xml/vlayoutconverter.h @@ -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();