From 62a22b122e0b9f18fdb0d2a58c30da273b63352d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 27 Aug 2015 11:25:11 +0300 Subject: [PATCH] Conversion a pattern file. --HG-- branch : feature --- src/app/valentina/xml/vpattern.cpp | 8 +- src/libs/ifc/schema/pattern/v0.2.0.xsd | 6 +- src/libs/ifc/xml/vabstractconverter.cpp | 38 +++ src/libs/ifc/xml/vabstractconverter.h | 6 +- src/libs/ifc/xml/vabstractpattern.cpp | 4 +- src/libs/ifc/xml/vpatternconverter.cpp | 334 ++++++++++++++++++++++++ src/libs/ifc/xml/vpatternconverter.h | 14 + 7 files changed, 400 insertions(+), 10 deletions(-) diff --git a/src/app/valentina/xml/vpattern.cpp b/src/app/valentina/xml/vpattern.cpp index 1ee235205..3c5a747db 100644 --- a/src/app/valentina/xml/vpattern.cpp +++ b/src/app/valentina/xml/vpattern.cpp @@ -76,15 +76,15 @@ void VPattern::CreateEmptyFile() version.appendChild(newNodeText); patternElement.appendChild(version); - patternElement.appendChild(createElement(TagAuthor)); - patternElement.appendChild(createElement(TagDescription)); - patternElement.appendChild(createElement(TagNotes)); - QDomElement unit = createElement(TagUnit); newNodeText = createTextNode(UnitsToStr(qApp->patternUnit())); unit.appendChild(newNodeText); patternElement.appendChild(unit); + patternElement.appendChild(createElement(TagAuthor)); + patternElement.appendChild(createElement(TagDescription)); + patternElement.appendChild(createElement(TagNotes)); + patternElement.appendChild(createElement(TagMeasurements)); patternElement.appendChild(createElement(TagIncrements)); diff --git a/src/libs/ifc/schema/pattern/v0.2.0.xsd b/src/libs/ifc/schema/pattern/v0.2.0.xsd index d6686e302..72519bb93 100644 --- a/src/libs/ifc/schema/pattern/v0.2.0.xsd +++ b/src/libs/ifc/schema/pattern/v0.2.0.xsd @@ -5,10 +5,10 @@ - - - + + + diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index a58575b95..814c22b58 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -144,6 +144,44 @@ void VAbstractConverter::ValidateVersion(const QString &version) const } } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::Replace(QString &formula, const QString &newName, int position, const QString &token, + int &bias) const +{ + formula.replace(position, token.length(), newName); + bias = token.length() - newName.length(); +} + +void VAbstractConverter::CorrectionsPositions(int position, int bias, QMap &tokens) const +{ + if (bias == 0) + { + return;// Nothing to correct; + } + + BiasTokens(position, bias, tokens); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::BiasTokens(int position, int bias, QMap &tokens) +{ + QMap newTokens; + QMap::const_iterator i = tokens.constBegin(); + while (i != tokens.constEnd()) + { + if (i.key()<= position) + { // Tokens before position "position" did not change his positions. + newTokens.insert(i.key(), i.value()); + } + else + { + newTokens.insert(i.key()-bias, i.value()); + } + ++i; + } + tokens = newTokens; +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractConverter::CheckVersion(int ver) const { diff --git a/src/libs/ifc/xml/vabstractconverter.h b/src/libs/ifc/xml/vabstractconverter.h index 0d56c0c94..ea5f48223 100644 --- a/src/libs/ifc/xml/vabstractconverter.h +++ b/src/libs/ifc/xml/vabstractconverter.h @@ -59,12 +59,16 @@ protected: virtual QString XSDSchema(int ver) const =0; virtual void ApplyPatches() =0; + void Replace(QString &formula, const QString &newName, int position, const QString &token, int &bias) const; + void CorrectionsPositions(int position, int bias, QMap &tokens) const; + static void BiasTokens(int position, int bias, QMap &tokens); + private: Q_DISABLE_COPY(VAbstractConverter) QString GetVersionStr() const; - void ValidateVersion(const QString &version) const; + void ValidateVersion(const QString &version) const; }; #endif // VABSTRACTCONVERTER_H diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 0c13dfd56..79ca5f56d 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -1083,7 +1083,7 @@ QStringList VAbstractPattern::ListPointExpressions() const QStringList VAbstractPattern::ListArcExpressions() const { QStringList expressions; - const QDomNodeList list = elementsByTagName(TagPoint); + const QDomNodeList list = elementsByTagName(TagArc); for (int i=0; i < list.size(); ++i) { const QDomElement dom = list.at(i).toElement(); @@ -1140,7 +1140,7 @@ QStringList VAbstractPattern::ListSplineExpressions() const QStringList VAbstractPattern::ListPathPointExpressions() const { QStringList expressions; - const QDomNodeList list = elementsByTagName(TagPoint); + const QDomNodeList list = elementsByTagName(AttrPathPoint); for (int i=0; i < list.size(); ++i) { const QDomElement dom = list.at(i).toElement(); diff --git a/src/libs/ifc/xml/vpatternconverter.cpp b/src/libs/ifc/xml/vpatternconverter.cpp index 76875f185..775553761 100644 --- a/src/libs/ifc/xml/vpatternconverter.cpp +++ b/src/libs/ifc/xml/vpatternconverter.cpp @@ -28,7 +28,10 @@ #include "vpatternconverter.h" #include "exception/vexception.h" +#include "exception/vexceptionemptyparameter.h" +#include "../qmuparser/qmutokenparser.h" +#include #include /* @@ -205,5 +208,336 @@ void VPatternConverter::ToV0_1_4() void VPatternConverter::ToV0_2_0() { SetVersion(QStringLiteral("0.2.0")); + TagUnitToV0_2_0(); + TagIncrementToV0_2_0(); + TagMeasurementsToV0_2_0();//Alwayse last!!! Save(); } + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::TagUnitToV0_2_0() +{ + QDomElement unit = createElement("unit"); + QDomText newNodeText = createTextNode(MUnitV0_1_4()); + unit.appendChild(newNodeText); + + QDomElement patternElement = documentElement(); + patternElement.insertAfter(unit, patternElement.firstChild()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::TagIncrementToV0_2_0() +{ + const QSet names = FixIncrementsToV0_2_0(); + + FixPointExpressionsToV0_2_0(names); + FixArcExpressionsToV0_2_0(names); + FixPathPointExpressionsToV0_2_0(names); +} + +//--------------------------------------------------------------------------------------------------------------------- +QSet VPatternConverter::FixIncrementsToV0_2_0() +{ + QSet names; + const QDomElement ms = TagMeasurementsV0_1_4(); + QDomNode domNode = ms.firstChild(); + while (domNode.isNull() == false) + { + if (domNode.isElement()) + { + QDomElement domElement = domNode.toElement(); + if (domElement.isNull() == false) + { + if (domElement.tagName() == "increment") + { + try + { + const QString name = GetParametrString(domElement, "name"); + names.insert(name); + domElement.setAttribute("name", "#"+name); + + const QString base = GetParametrString(domElement, "base"); + domElement.setAttribute("formula", base); + } + catch (VExceptionEmptyParameter &e) + { + VException excep("Can't get increment."); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; + } + domElement.removeAttribute("id"); + domElement.removeAttribute("kgrowth"); + domElement.removeAttribute("ksize"); + domElement.removeAttribute("base"); + } + } + } + domNode = domNode.nextSibling(); + } + return names; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::FixPointExpressionsToV0_2_0(const QSet &names) +{ + QString formula; + const QDomNodeList list = elementsByTagName("point"); + for (int i=0; i < list.size(); ++i) + { + QDomElement dom = list.at(i).toElement(); + + try + { + formula = GetParametrString(dom, "length"); + dom.setAttribute("length", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "angle"); + dom.setAttribute("angle", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + try + { + formula = GetParametrString(dom, "c1Radius"); + dom.setAttribute("c1Radius", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "c2Radius"); + dom.setAttribute("c2Radius", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "cRadius"); + dom.setAttribute("cRadius", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::FixArcExpressionsToV0_2_0(const QSet &names) +{ + QString formula; + const QDomNodeList list = elementsByTagName("arc"); + for (int i=0; i < list.size(); ++i) + { + QDomElement dom = list.at(i).toElement(); + + try + { + formula = GetParametrString(dom, "angle1"); + dom.setAttribute("angle1", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "angle2"); + dom.setAttribute("angle2", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "radius"); + dom.setAttribute("radius", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "length"); + dom.setAttribute("length", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::FixPathPointExpressionsToV0_2_0(const QSet &names) +{ + QString formula; + const QDomNodeList list = elementsByTagName("pathPoint"); + for (int i=0; i < list.size(); ++i) + { + QDomElement dom = list.at(i).toElement(); + + try + { + formula = GetParametrString(dom, "kAsm1"); + dom.setAttribute("kAsm1", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "kAsm2"); + dom.setAttribute("kAsm2", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + formula = GetParametrString(dom, "angle"); + dom.setAttribute("angle", FixIncrementInFormulaToV0_2_0(formula, names)); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + } +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VPatternConverter::FixIncrementInFormulaToV0_2_0(const QString &formula, const QSet &names) +{ + qmu::QmuTokenParser *cal = new qmu::QmuTokenParser(formula, false, false);// Eval formula + QMap tokens = cal->GetTokens();// Tokens (variables, measurements) + delete cal; + + QList tKeys = tokens.keys();// Take all tokens positions + QList tValues = tokens.values(); + + QString newFormula = formula;// Local copy for making changes + for (int i = 0; i < tValues.size(); ++i) + { + if (not names.contains(tValues.at(i))) + { + continue; + } + + int bias = 0; + Replace(newFormula, "#"+tValues.at(i), tKeys.at(i), tValues.at(i), bias); + if (bias != 0) + {// Translated token has different length than original. Position next tokens need to be corrected. + CorrectionsPositions(tKeys.at(i), bias, tokens); + tKeys = tokens.keys(); + tValues = tokens.values(); + } + } + return newFormula; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::TagMeasurementsToV0_2_0() +{ + QDomElement ms = TagMeasurementsV0_1_4(); + const QString path = GetParametrString(ms, "path"); + + ms.removeAttribute("unit"); + ms.removeAttribute("type"); + ms.removeAttribute("path"); + + QDomText newNodeText = createTextNode(QFileInfo(fileName).absoluteDir().relativeFilePath(path)); + ms.appendChild(newNodeText); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VPatternConverter::MUnitV0_1_4() const +{ + const QDomElement element = TagMeasurementsV0_1_4(); + try + { + return GetParametrString(element, "unit"); + } + catch (VExceptionEmptyParameter &e) + { + VException excep("Can't get unit."); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +QDomElement VPatternConverter::TagMeasurementsV0_1_4() const +{ + const QDomNodeList list = elementsByTagName("measurements"); + const QDomElement element = list.at(0).toElement(); + if (not element.isElement()) + { + VException excep("Can't get tag measurements."); + throw excep; + } + return element; +} + +//--------------------------------------------------------------------------------------------------------------------- +QStringList VPatternConverter::ListPathPointExpressionsV0_1_4() const +{ + QStringList expressions; + const QDomNodeList list = elementsByTagName("pathPoint"); + for (int i=0; i < list.size(); ++i) + { + const QDomElement dom = list.at(i).toElement(); + + try + { + expressions.append(GetParametrString(dom, "kAsm1")); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + expressions.append(GetParametrString(dom, "kAsm2")); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + + try + { + expressions.append(GetParametrString(dom, "angle")); + } + catch (VExceptionEmptyParameter &e) + { + Q_UNUSED(e) + } + } + + return expressions; +} diff --git a/src/libs/ifc/xml/vpatternconverter.h b/src/libs/ifc/xml/vpatternconverter.h index a8badd867..51e4e630c 100644 --- a/src/libs/ifc/xml/vpatternconverter.h +++ b/src/libs/ifc/xml/vpatternconverter.h @@ -60,6 +60,20 @@ private: void ToV0_1_3(); void ToV0_1_4(); void ToV0_2_0(); + + void TagUnitToV0_2_0(); + void TagIncrementToV0_2_0(); + void TagMeasurementsToV0_2_0(); + + QSet FixIncrementsToV0_2_0(); + QString FixIncrementInFormulaToV0_2_0(const QString &formula, const QSet &names); + void FixPointExpressionsToV0_2_0(const QSet &names); + void FixArcExpressionsToV0_2_0(const QSet &names); + void FixPathPointExpressionsToV0_2_0(const QSet &names); + + QString MUnitV0_1_4() const; + QDomElement TagMeasurementsV0_1_4() const; + QStringList ListPathPointExpressionsV0_1_4() const; }; #endif // VPATTERNCONVERTER_H