Introducing new method SetAttributeOrRemoveIf.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-02-11 14:15:59 +02:00
parent 8612f6d0af
commit 7d1c4bc3ba
5 changed files with 26 additions and 132 deletions

View File

@ -4177,14 +4177,7 @@ void VPattern::SetDefCustomHeight(int value)
QDomElement domElement = domNode.toElement(); QDomElement domElement = domNode.toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (value == 0) SetAttributeOrRemoveIf(domElement, AttrDefHeight, value, 0);
{
domElement.removeAttribute(AttrDefHeight);
}
else
{
SetAttribute(domElement, AttrDefHeight, value);
}
modified = true; modified = true;
} }
else else
@ -4236,14 +4229,7 @@ void VPattern::SetDefCustomSize(int value)
QDomElement domElement = domNode.toElement(); QDomElement domElement = domNode.toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (value == 0) SetAttributeOrRemoveIf(domElement, AttrDefSize, value, 0);
{
domElement.removeAttribute(AttrDefSize);
}
else
{
SetAttribute(domElement, AttrDefSize, value);
}
modified = true; modified = true;
} }
else else
@ -4272,14 +4258,7 @@ void VPattern::SetReadOnly(bool rOnly)
if (not pattern.isNull()) if (not pattern.isNull())
{ {
if (rOnly) SetAttributeOrRemoveIf(pattern, AttrReadOnly, rOnly, false);
{
SetAttribute(pattern, AttrReadOnly, rOnly);
}
else
{// For better backward compatibility
pattern.removeAttribute(AttrReadOnly);
}
modified = true; modified = true;
} }
} }

View File

@ -101,6 +101,9 @@ public:
template <typename T> template <typename T>
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const; void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const;
template <typename T>
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 quint32 GetParametrUInt(const QDomElement& domElement, const QString &name, const QString &defValue);
static bool GetParametrBool(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<MeasurementsType>(QDomElement &domElement
QStringLiteral("individual")); QStringLiteral("individual"));
} }
//---------------------------------------------------------------------------------------------------------------------
template <typename T>
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 QT_WARNING_POP
#endif // VDOMDOCUMENT_H #endif // VDOMDOCUMENT_H

View File

@ -523,29 +523,8 @@ QDomElement VAbstractTool::AddSANode(VAbstractPattern *doc, const QString &tagNa
} }
} }
{ doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrNodeExcluded, node.IsExcluded(), false);
const bool excluded = node.IsExcluded(); doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrCheckUniqueness, node.IsCheckUniqueness(), true);
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);
}
}
switch (type) switch (type)
{ {
@ -602,17 +581,7 @@ QDomElement VAbstractTool::AddSANode(VAbstractPattern *doc, const QString &tagNa
nod.removeAttribute(VAbstractPattern::AttrNodePassmarkAngle); nod.removeAttribute(VAbstractPattern::AttrNodePassmarkAngle);
} }
{ doc->SetAttributeOrRemoveIf(nod, VAbstractPattern::AttrNodeShowSecondPassmark, node.IsShowSecondPassmark(), true);
const bool showSecond = node.IsShowSecondPassmark();
if (not showSecond)
{
doc->SetAttribute(nod, VAbstractPattern::AttrNodeShowSecondPassmark, showSecond);
}
else
{ // For backward compatebility.
nod.removeAttribute(VAbstractPattern::AttrNodeShowSecondPassmark);
}
}
return nod; return nod;
} }

View File

@ -275,17 +275,7 @@ void VToolSeamAllowance::AddAttributes(VAbstractPattern *doc, QDomElement &domEl
doc->SetAttribute(domElement, AttrForceFlipping, piece.IsForceFlipping()); doc->SetAttribute(domElement, AttrForceFlipping, piece.IsForceFlipping());
doc->SetAttribute(domElement, AttrSeamAllowance, piece.IsSeamAllowance()); doc->SetAttribute(domElement, AttrSeamAllowance, piece.IsSeamAllowance());
doc->SetAttribute(domElement, AttrHideMainPath, piece.IsHideMainPath()); doc->SetAttribute(domElement, AttrHideMainPath, piece.IsHideMainPath());
doc->SetAttributeOrRemoveIf(domElement, AttrSeamAllowanceBuiltIn, piece.IsSeamAllowanceBuiltIn(), false);
const bool saBuiltIn = piece.IsSeamAllowanceBuiltIn();
if (saBuiltIn)
{
doc->SetAttribute(domElement, AttrSeamAllowanceBuiltIn, saBuiltIn);
}
else
{ // For backward compatebility.
domElement.removeAttribute(AttrSeamAllowanceBuiltIn);
}
doc->SetAttribute(domElement, AttrWidth, piece.GetFormulaSAWidth()); doc->SetAttribute(domElement, AttrWidth, piece.GetFormulaSAWidth());
doc->SetAttribute(domElement, AttrUnited, piece.IsUnited()); 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, AttrHeight, data.GetLabelHeight());
doc->SetAttribute(domData, AttrFont, data.GetFontSize()); doc->SetAttribute(domData, AttrFont, data.GetFontSize());
doc->SetAttribute(domData, VAbstractPattern::AttrRotation, data.GetRotation()); doc->SetAttribute(domData, VAbstractPattern::AttrRotation, data.GetRotation());
doc->SetAttributeOrRemoveIf<bool>(domData, AttrCenterPin, data.CenterPin(), not (data.CenterPin() > NULL_ID));
if (data.CenterPin() > NULL_ID) doc->SetAttributeOrRemoveIf<bool>(domData, AttrTopLeftPin, data.TopLeftPin(), not (data.TopLeftPin() > NULL_ID));
{ doc->SetAttributeOrRemoveIf<bool>(domData, AttrBottomRightPin, data.BottomRightPin(),
doc->SetAttribute(domData, AttrCenterPin, data.CenterPin()); not (data.BottomRightPin() > NULL_ID));
}
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->SetLabelTemplate(domData, data.GetLabelTemplate()); doc->SetLabelTemplate(domData, data.GetLabelTemplate());
domElement.appendChild(domData); domElement.appendChild(domData);
@ -456,33 +422,9 @@ void VToolSeamAllowance::AddGrainline(VAbstractPattern *doc, QDomElement &domEle
doc->SetAttribute(domData, AttrLength, glGeom.GetLength()); doc->SetAttribute(domData, AttrLength, glGeom.GetLength());
doc->SetAttribute(domData, VAbstractPattern::AttrRotation, glGeom.GetRotation()); doc->SetAttribute(domData, VAbstractPattern::AttrRotation, glGeom.GetRotation());
doc->SetAttribute(domData, VAbstractPattern::AttrArrows, int(glGeom.GetArrowType())); doc->SetAttribute(domData, VAbstractPattern::AttrArrows, int(glGeom.GetArrowType()));
doc->SetAttributeOrRemoveIf<bool>(domData, AttrCenterPin, glGeom.CenterPin(), not (glGeom.CenterPin() > NULL_ID));
if (glGeom.CenterPin() > NULL_ID) doc->SetAttributeOrRemoveIf<bool>(domData, AttrTopPin, glGeom.TopPin(), not (glGeom.TopPin() > NULL_ID));
{ doc->SetAttributeOrRemoveIf<bool>(domData, AttrBottomPin, glGeom.BottomPin(), not (glGeom.BottomPin() > 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);
}
domElement.appendChild(domData); domElement.appendChild(domData);
} }

View File

@ -75,14 +75,7 @@ void TogglePieceInLayout::Do(bool state)
QDomElement detail = doc->elementById(m_id, VAbstractPattern::TagDetail); QDomElement detail = doc->elementById(m_id, VAbstractPattern::TagDetail);
if (detail.isElement()) if (detail.isElement())
{ {
if (state == false) doc->SetAttributeOrRemoveIf(detail, AttrInLayout, state, true);
{
doc->SetAttribute(detail, AttrInLayout, state);
}
else
{
detail.removeAttribute(AttrInLayout);
}
VPiece det = m_data->DataPieces()->value(m_id); VPiece det = m_data->DataPieces()->value(m_id);
det.SetInLayout(state); det.SetInLayout(state);