Conversion a pattern file.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2015-08-27 11:25:11 +03:00
parent a988c4b185
commit 62a22b122e
7 changed files with 400 additions and 10 deletions

View File

@ -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));

View File

@ -5,10 +5,10 @@
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="version" type="formatVersion"></xs:element>
<xs:element name="author" type="xs:string"></xs:element>
<xs:element name="description" type="xs:string"></xs:element>
<xs:element name="notes" type="xs:string"></xs:element>
<xs:element name="unit" type="units"></xs:element>
<xs:element name="author" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="notes" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
<xs:element name="gradation" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>

View File

@ -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<int, QString> &tokens) const
{
if (bias == 0)
{
return;// Nothing to correct;
}
BiasTokens(position, bias, tokens);
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::BiasTokens(int position, int bias, QMap<int, QString> &tokens)
{
QMap<int, QString> newTokens;
QMap<int, QString>::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
{

View File

@ -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<int, QString> &tokens) const;
static void BiasTokens(int position, int bias, QMap<int, QString> &tokens);
private:
Q_DISABLE_COPY(VAbstractConverter)
QString GetVersionStr() const;
void ValidateVersion(const QString &version) const;
void ValidateVersion(const QString &version) const;
};
#endif // VABSTRACTCONVERTER_H

View File

@ -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();

View File

@ -28,7 +28,10 @@
#include "vpatternconverter.h"
#include "exception/vexception.h"
#include "exception/vexceptionemptyparameter.h"
#include "../qmuparser/qmutokenparser.h"
#include <QDir>
#include <QFile>
/*
@ -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<QString> names = FixIncrementsToV0_2_0();
FixPointExpressionsToV0_2_0(names);
FixArcExpressionsToV0_2_0(names);
FixPathPointExpressionsToV0_2_0(names);
}
//---------------------------------------------------------------------------------------------------------------------
QSet<QString> VPatternConverter::FixIncrementsToV0_2_0()
{
QSet<QString> 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<QString> &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<QString> &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<QString> &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<QString> &names)
{
qmu::QmuTokenParser *cal = new qmu::QmuTokenParser(formula, false, false);// Eval formula
QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements)
delete cal;
QList<int> tKeys = tokens.keys();// Take all tokens positions
QList<QString> 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;
}

View File

@ -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<QString> FixIncrementsToV0_2_0();
QString FixIncrementInFormulaToV0_2_0(const QString &formula, const QSet<QString> &names);
void FixPointExpressionsToV0_2_0(const QSet<QString> &names);
void FixArcExpressionsToV0_2_0(const QSet<QString> &names);
void FixPathPointExpressionsToV0_2_0(const QSet<QString> &names);
QString MUnitV0_1_4() const;
QDomElement TagMeasurementsV0_1_4() const;
QStringList ListPathPointExpressionsV0_1_4() const;
};
#endif // VPATTERNCONVERTER_H