diff --git a/src/app/valentina/xml/vpattern.cpp b/src/app/valentina/xml/vpattern.cpp index c607a93e5..a35d5dfc1 100644 --- a/src/app/valentina/xml/vpattern.cpp +++ b/src/app/valentina/xml/vpattern.cpp @@ -4177,14 +4177,7 @@ void VPattern::SetDefCustomHeight(int value) QDomElement domElement = domNode.toElement(); if (domElement.isNull() == false) { - if (value == 0) - { - domElement.removeAttribute(AttrDefHeight); - } - else - { - SetAttribute(domElement, AttrDefHeight, value); - } + SetAttributeOrRemoveIf(domElement, AttrDefHeight, value, 0); modified = true; } else @@ -4236,14 +4229,7 @@ void VPattern::SetDefCustomSize(int value) QDomElement domElement = domNode.toElement(); if (domElement.isNull() == false) { - if (value == 0) - { - domElement.removeAttribute(AttrDefSize); - } - else - { - SetAttribute(domElement, AttrDefSize, value); - } + SetAttributeOrRemoveIf(domElement, AttrDefSize, value, 0); modified = true; } else @@ -4272,14 +4258,7 @@ void VPattern::SetReadOnly(bool rOnly) if (not pattern.isNull()) { - if (rOnly) - { - SetAttribute(pattern, AttrReadOnly, rOnly); - } - else - {// For better backward compatibility - pattern.removeAttribute(AttrReadOnly); - } + SetAttributeOrRemoveIf(pattern, AttrReadOnly, rOnly, false); modified = true; } } diff --git a/src/libs/ifc/xml/vdomdocument.h b/src/libs/ifc/xml/vdomdocument.h index a21f7a366..26a06ef3e 100644 --- a/src/libs/ifc/xml/vdomdocument.h +++ b/src/libs/ifc/xml/vdomdocument.h @@ -101,6 +101,9 @@ public: template void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const; + template + void SetAttributeOrRemoveIf(QDomElement &domElement, const QString &name, const T &value, const T &condition) const; + static quint32 GetParametrUInt(const QDomElement& domElement, const QString &name, const QString &defValue); static bool GetParametrBool(const QDomElement& domElement, const QString &name, const QString &defValue); @@ -210,6 +213,14 @@ inline void VDomDocument::SetAttribute(QDomElement &domElement QStringLiteral("individual")); } +//--------------------------------------------------------------------------------------------------------------------- +template +inline void VDomDocument::SetAttributeOrRemoveIf(QDomElement &domElement, const QString &name, const T &value, + const T &condition) const +{ + value != condition ? domElement.setAttribute(name, value) : domElement.removeAttribute(name); +} + QT_WARNING_POP #endif // VDOMDOCUMENT_H diff --git a/src/libs/vtools/tools/vabstracttool.cpp b/src/libs/vtools/tools/vabstracttool.cpp index efd09b52e..98a19d9c2 100644 --- a/src/libs/vtools/tools/vabstracttool.cpp +++ b/src/libs/vtools/tools/vabstracttool.cpp @@ -523,29 +523,8 @@ QDomElement VAbstractTool::AddSANode(VAbstractPattern *doc, const QString &tagNa } } - { - const bool excluded = node.IsExcluded(); - if (excluded) - { - doc->SetAttribute(nod, VAbstractPattern::AttrNodeExcluded, excluded); - } - else - { // For backward compatebility. - nod.removeAttribute(VAbstractPattern::AttrNodeExcluded); - } - } - - { - const bool uniqueness = node.IsCheckUniqueness(); - if (not uniqueness) - { - doc->SetAttribute(nod, VAbstractPattern::AttrCheckUniqueness, uniqueness); - } - else - { // For backward compatebility. - nod.removeAttribute(VAbstractPattern::AttrCheckUniqueness); - } - } + doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrNodeExcluded, node.IsExcluded(), false); + doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrCheckUniqueness, node.IsCheckUniqueness(), true); switch (type) { @@ -602,17 +581,7 @@ QDomElement VAbstractTool::AddSANode(VAbstractPattern *doc, const QString &tagNa nod.removeAttribute(VAbstractPattern::AttrNodePassmarkAngle); } - { - const bool showSecond = node.IsShowSecondPassmark(); - if (not showSecond) - { - doc->SetAttribute(nod, VAbstractPattern::AttrNodeShowSecondPassmark, showSecond); - } - else - { // For backward compatebility. - nod.removeAttribute(VAbstractPattern::AttrNodeShowSecondPassmark); - } - } + doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrNodeShowSecondPassmark, node.IsShowSecondPassmark(), true); return nod; } diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index 52f788253..dcf54302f 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -275,17 +275,7 @@ void VToolSeamAllowance::AddAttributes(VAbstractPattern *doc, QDomElement &domEl doc->SetAttribute(domElement, AttrForceFlipping, piece.IsForceFlipping()); doc->SetAttribute(domElement, AttrSeamAllowance, piece.IsSeamAllowance()); doc->SetAttribute(domElement, AttrHideMainPath, piece.IsHideMainPath()); - - const bool saBuiltIn = piece.IsSeamAllowanceBuiltIn(); - if (saBuiltIn) - { - doc->SetAttribute(domElement, AttrSeamAllowanceBuiltIn, saBuiltIn); - } - else - { // For backward compatebility. - domElement.removeAttribute(AttrSeamAllowanceBuiltIn); - } - + doc->SetAttributeOrRemoveIf(domElement, AttrSeamAllowanceBuiltIn, piece.IsSeamAllowanceBuiltIn(), false); doc->SetAttribute(domElement, AttrWidth, piece.GetFormulaSAWidth()); doc->SetAttribute(domElement, AttrUnited, piece.IsUnited()); } @@ -368,34 +358,10 @@ void VToolSeamAllowance::AddPatternPieceData(VAbstractPattern *doc, QDomElement doc->SetAttribute(domData, AttrHeight, data.GetLabelHeight()); doc->SetAttribute(domData, AttrFont, data.GetFontSize()); doc->SetAttribute(domData, VAbstractPattern::AttrRotation, data.GetRotation()); - - if (data.CenterPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrCenterPin, data.CenterPin()); - } - else - { - domData.removeAttribute(AttrCenterPin); - } - - if (data.TopLeftPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrTopLeftPin, data.TopLeftPin()); - } - else - { - domData.removeAttribute(AttrTopLeftPin); - } - - if (data.BottomRightPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrBottomRightPin, data.BottomRightPin()); - } - else - { - domData.removeAttribute(AttrBottomRightPin); - } - + doc->SetAttributeOrRemoveIf(domData, AttrCenterPin, data.CenterPin(), not (data.CenterPin() > NULL_ID)); + doc->SetAttributeOrRemoveIf(domData, AttrTopLeftPin, data.TopLeftPin(), not (data.TopLeftPin() > NULL_ID)); + doc->SetAttributeOrRemoveIf(domData, AttrBottomRightPin, data.BottomRightPin(), + not (data.BottomRightPin() > NULL_ID)); doc->SetLabelTemplate(domData, data.GetLabelTemplate()); domElement.appendChild(domData); @@ -456,33 +422,9 @@ void VToolSeamAllowance::AddGrainline(VAbstractPattern *doc, QDomElement &domEle doc->SetAttribute(domData, AttrLength, glGeom.GetLength()); doc->SetAttribute(domData, VAbstractPattern::AttrRotation, glGeom.GetRotation()); doc->SetAttribute(domData, VAbstractPattern::AttrArrows, int(glGeom.GetArrowType())); - - if (glGeom.CenterPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrCenterPin, glGeom.CenterPin()); - } - else - { - domData.removeAttribute(AttrCenterPin); - } - - if (glGeom.TopPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrTopPin, glGeom.TopPin()); - } - else - { - domData.removeAttribute(AttrTopPin); - } - - if (glGeom.BottomPin() > NULL_ID) - { - doc->SetAttribute(domData, AttrBottomPin, glGeom.BottomPin()); - } - else - { - domData.removeAttribute(AttrBottomPin); - } + doc->SetAttributeOrRemoveIf(domData, AttrCenterPin, glGeom.CenterPin(), not (glGeom.CenterPin() > NULL_ID)); + doc->SetAttributeOrRemoveIf(domData, AttrTopPin, glGeom.TopPin(), not (glGeom.TopPin() > NULL_ID)); + doc->SetAttributeOrRemoveIf(domData, AttrBottomPin, glGeom.BottomPin(), not (glGeom.BottomPin() > NULL_ID)); domElement.appendChild(domData); } diff --git a/src/libs/vtools/undocommands/togglepiecestate.cpp b/src/libs/vtools/undocommands/togglepiecestate.cpp index 4ceae383c..d7febff87 100644 --- a/src/libs/vtools/undocommands/togglepiecestate.cpp +++ b/src/libs/vtools/undocommands/togglepiecestate.cpp @@ -75,14 +75,7 @@ void TogglePieceInLayout::Do(bool state) QDomElement detail = doc->elementById(m_id, VAbstractPattern::TagDetail); if (detail.isElement()) { - if (state == false) - { - doc->SetAttribute(detail, AttrInLayout, state); - } - else - { - detail.removeAttribute(AttrInLayout); - } + doc->SetAttributeOrRemoveIf(detail, AttrInLayout, state, true); VPiece det = m_data->DataPieces()->value(m_id); det.SetInLayout(state);