Refactoring if-else. Static members instead strings.

--HG--
branch : feature
This commit is contained in:
dismine 2014-02-25 20:03:17 +02:00
parent 1b3de9e4a2
commit 313d4aad6b
5 changed files with 142 additions and 85 deletions

View File

@ -62,8 +62,9 @@ const QString VAbstractTool::AttrPathPoint = QStringLiteral("pathPoint");
const QString VAbstractTool::AttrPSpline = QStringLiteral("pSpline"); const QString VAbstractTool::AttrPSpline = QStringLiteral("pSpline");
const QString VAbstractTool::AttrAxisP1 = QStringLiteral("axisP1"); const QString VAbstractTool::AttrAxisP1 = QStringLiteral("axisP1");
const QString VAbstractTool::AttrAxisP2 = QStringLiteral("axisP2"); const QString VAbstractTool::AttrAxisP2 = QStringLiteral("axisP2");
const QString VAbstractTool::TypeLineNone = QStringLiteral("none");
const QString VAbstractTool::TypeLineLine = QStringLiteral("hair"); const QString VAbstractTool::TypeLineNone = QStringLiteral("none");
const QString VAbstractTool::TypeLineLine = QStringLiteral("hair");
const QString VAbstractTool::TypeLineDashLine = QStringLiteral("dashLine"); const QString VAbstractTool::TypeLineDashLine = QStringLiteral("dashLine");
const QString VAbstractTool::TypeLineDotLine = QStringLiteral("dotLine"); const QString VAbstractTool::TypeLineDotLine = QStringLiteral("dotLine");
const QString VAbstractTool::TypeLineDashDotLine = QStringLiteral("dashDotLine"); const QString VAbstractTool::TypeLineDashDotLine = QStringLiteral("dashDotLine");

View File

@ -32,14 +32,21 @@
const QString VToolDetail::TagName = QStringLiteral("detail"); const QString VToolDetail::TagName = QStringLiteral("detail");
const QString VToolDetail::TagNode = QStringLiteral("node"); const QString VToolDetail::TagNode = QStringLiteral("node");
const QString VToolDetail::AttrSupplement = QStringLiteral("supplement"); const QString VToolDetail::AttrSupplement = QStringLiteral("supplement");
const QString VToolDetail::AttrClosed = QStringLiteral("closed"); const QString VToolDetail::AttrClosed = QStringLiteral("closed");
const QString VToolDetail::AttrWidth = QStringLiteral("width"); const QString VToolDetail::AttrWidth = QStringLiteral("width");
const QString VToolDetail::AttrIdObject = QStringLiteral("idObject"); const QString VToolDetail::AttrIdObject = QStringLiteral("idObject");
const QString VToolDetail::AttrNodeType = QStringLiteral("nodeType"); const QString VToolDetail::AttrNodeType = QStringLiteral("nodeType");
const QString VToolDetail::NodeTypeContour = QStringLiteral("Contour"); const QString VToolDetail::NodeTypeContour = QStringLiteral("Contour");
const QString VToolDetail::NodeTypeModeling = QStringLiteral("Modeling"); const QString VToolDetail::NodeTypeModeling = QStringLiteral("Modeling");
const QString VToolDetail::NodeArc = QStringLiteral("NodeArc");
const QString VToolDetail::NodePoint = QStringLiteral("NodePoint");
const QString VToolDetail::NodeSpline = QStringLiteral("NodeSpline");
const QString VToolDetail::NodeSplinePath = QStringLiteral("NodeSplinePath");
VToolDetail::VToolDetail(VPattern *doc, VContainer *data, const quint32 &id, const Tool::Sources &typeCreation, VToolDetail::VToolDetail(VPattern *doc, VContainer *data, const quint32 &id, const Tool::Sources &typeCreation,
VMainGraphicsScene *scene, QGraphicsItem *parent) VMainGraphicsScene *scene, QGraphicsItem *parent)
:VAbstractTool(doc, data, id), QGraphicsPathItem(parent), dialog(0), sceneDetails(scene) :VAbstractTool(doc, data, id), QGraphicsPathItem(parent), dialog(0), sceneDetails(scene)
@ -376,16 +383,16 @@ void VToolDetail::AddNode(QDomElement &domElement, const VNodeDetail &node)
switch (node.getTypeTool()) switch (node.getTypeTool())
{ {
case (Tool::NodeArc): case (Tool::NodeArc):
doc->SetAttribute(nod, AttrType, QStringLiteral("NodeArc")); doc->SetAttribute(nod, AttrType, NodeArc);
break; break;
case (Tool::NodePoint): case (Tool::NodePoint):
doc->SetAttribute(nod, AttrType, QStringLiteral("NodePoint")); doc->SetAttribute(nod, AttrType, NodePoint);
break; break;
case (Tool::NodeSpline): case (Tool::NodeSpline):
doc->SetAttribute(nod, AttrType, QStringLiteral("NodeSpline")); doc->SetAttribute(nod, AttrType, NodeSpline);
break; break;
case (Tool::NodeSplinePath): case (Tool::NodeSplinePath):
doc->SetAttribute(nod, AttrType, QStringLiteral("NodeSplinePath")); doc->SetAttribute(nod, AttrType, NodeSplinePath);
break; break;
default: default:
qWarning()<<"May be wrong tool type!!! Ignoring."<<Q_FUNC_INFO; qWarning()<<"May be wrong tool type!!! Ignoring."<<Q_FUNC_INFO;

View File

@ -102,6 +102,10 @@ public:
static const QString AttrNodeType; static const QString AttrNodeType;
static const QString NodeTypeContour; static const QString NodeTypeContour;
static const QString NodeTypeModeling; static const QString NodeTypeModeling;
static const QString NodeArc;
static const QString NodePoint;
static const QString NodeSpline;
static const QString NodeSplinePath;
/** /**
* @brief Remove full delete detail. * @brief Remove full delete detail.
*/ */

View File

@ -38,6 +38,7 @@
#include <QMessageBox> #include <QMessageBox>
const QString VPattern::TagPattern = QStringLiteral("pattern");
const QString VPattern::TagCalculation = QStringLiteral("calculation"); const QString VPattern::TagCalculation = QStringLiteral("calculation");
const QString VPattern::TagModeling = QStringLiteral("modeling"); const QString VPattern::TagModeling = QStringLiteral("modeling");
const QString VPattern::TagDetails = QStringLiteral("details"); const QString VPattern::TagDetails = QStringLiteral("details");
@ -45,8 +46,22 @@ const QString VPattern::TagAuthor = QStringLiteral("author");
const QString VPattern::TagDescription = QStringLiteral("description"); const QString VPattern::TagDescription = QStringLiteral("description");
const QString VPattern::TagNotes = QStringLiteral("notes"); const QString VPattern::TagNotes = QStringLiteral("notes");
const QString VPattern::TagIncrements = QStringLiteral("increments"); const QString VPattern::TagIncrements = QStringLiteral("increments");
const QString VPattern::TagIncrement = QStringLiteral("increment");
const QString VPattern::TagDraw = QStringLiteral("draw"); const QString VPattern::TagDraw = QStringLiteral("draw");
const QString VPattern::TagPoint = QStringLiteral("point");
const QString VPattern::TagLine = QStringLiteral("line");
const QString VPattern::TagSpline = QStringLiteral("spline");
const QString VPattern::TagArc = QStringLiteral("arc");
const QString VPattern::TagTools = QStringLiteral("tools");
const QString VPattern::AttrName = QStringLiteral("name"); const QString VPattern::AttrName = QStringLiteral("name");
const QString VPattern::AttrType = QStringLiteral("type");
const QString VPattern::IncrementName = QStringLiteral("name");
const QString VPattern::IncrementBase = QStringLiteral("base");
const QString VPattern::IncrementKsize = QStringLiteral("ksize");
const QString VPattern::IncrementKgrowth = QStringLiteral("kgrowth");
const QString VPattern::IncrementDescription = QStringLiteral("description");
VPattern::VPattern(VContainer *data, QComboBox *comboBoxDraws, Draw::Draws *mode, QObject *parent) VPattern::VPattern(VContainer *data, QComboBox *comboBoxDraws, Draw::Draws *mode, QObject *parent)
: QObject(parent), VDomDocument(data), nameActivDraw(QString()), tools(QHash<quint32, VDataTool*>()), : QObject(parent), VDomDocument(data), nameActivDraw(QString()), tools(QHash<quint32, VDataTool*>()),
@ -56,7 +71,7 @@ VPattern::VPattern(VContainer *data, QComboBox *comboBoxDraws, Draw::Draws *mode
void VPattern::CreateEmptyFile() void VPattern::CreateEmptyFile()
{ {
QDomElement patternElement = this->createElement("pattern"); QDomElement patternElement = this->createElement(TagPattern);
patternElement.appendChild(createComment("Valentina pattern format.")); patternElement.appendChild(createComment("Valentina pattern format."));
patternElement.appendChild(createElement(TagAuthor)); patternElement.appendChild(createElement(TagAuthor));
@ -172,29 +187,35 @@ void VPattern::Parse(const Document::Documents &parse, VMainGraphicsScene *scene
const QDomElement domElement = domNode.toElement(); const QDomElement domElement = domNode.toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (domElement.tagName()==TagDraw) QStringList tags;
tags << TagDraw << TagIncrements;
switch(tags.indexOf(domElement.tagName()))
{ {
if (parse == Document::FullParse) case 0: // TagDraw
{ if (parse == Document::FullParse)
if (nameActivDraw.isEmpty())
{ {
SetActivDraw(GetParametrString(domElement, AttrName)); if (nameActivDraw.isEmpty())
{
SetActivDraw(GetParametrString(domElement, AttrName));
}
else
{
ChangeActivDraw(GetParametrString(domElement, AttrName));
}
comboBoxDraws->addItem(GetParametrString(domElement, AttrName));
} }
else else
{ {
ChangeActivDraw(GetParametrString(domElement, AttrName)); ChangeActivDraw(GetParametrString(domElement, AttrName), Document::LiteParse);
} }
comboBoxDraws->addItem(GetParametrString(domElement, AttrName)); ParseDrawElement(sceneDraw, sceneDetail, domElement, parse);
} break;
else case 1: // TagIncrements
{ ParseIncrementsElement(domElement);
ChangeActivDraw(GetParametrString(domElement, AttrName), Document::LiteParse); break;
} default:
ParseDrawElement(sceneDraw, sceneDetail, domElement, parse); qWarning()<<"Wrong tag name"<<Q_FUNC_INFO;
} break;
if (domElement.tagName()=="increments")
{
ParseIncrementsElement(domElement);
} }
} }
} }
@ -311,7 +332,7 @@ quint32 VPattern::SPointActiveDraw()
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (domElement.tagName() == VToolPoint::TagName && if (domElement.tagName() == VToolPoint::TagName &&
domElement.attribute("type", "") == VToolSinglePoint::ToolType) domElement.attribute(AttrType, "") == VToolSinglePoint::ToolType)
{ {
return GetParametrId(domElement); return GetParametrId(domElement);
} }
@ -427,18 +448,23 @@ void VPattern::ParseDrawElement(VMainGraphicsScene *sceneDraw, VMainGraphicsScen
const QDomElement domElement = domNode.toElement(); const QDomElement domElement = domNode.toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (domElement.tagName() == "calculation") QStringList tags;
tags << TagCalculation << TagModeling << TagDetails;
switch(tags.indexOf(domElement.tagName()))
{ {
data->ClearCalculationGObjects(); case 0: // TagCalculation
ParseDrawMode(sceneDraw, sceneDetail, domElement, parse, Draw::Calculation); data->ClearCalculationGObjects();
} ParseDrawMode(sceneDraw, sceneDetail, domElement, parse, Draw::Calculation);
if (domElement.tagName() == "modeling") break;
{ case 1: // TagModeling
ParseDrawMode(sceneDraw, sceneDetail, domElement, parse, Draw::Modeling); ParseDrawMode(sceneDraw, sceneDetail, domElement, parse, Draw::Modeling);
} break;
if (domElement.tagName() == "details") case 2: // TagDetails
{ ParseDetails(sceneDetail, domElement, parse);
ParseDetails(sceneDetail, domElement, parse); break;
default:
qWarning()<<"Wrong tag name"<<Q_FUNC_INFO;
break;
} }
} }
} }
@ -467,30 +493,28 @@ void VPattern::ParseDrawMode(VMainGraphicsScene *sceneDraw, VMainGraphicsScene *
const QDomElement domElement = nodeList.at(i).toElement(); const QDomElement domElement = nodeList.at(i).toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (domElement.tagName() == "point") QStringList tags;
tags << TagPoint << TagLine << TagSpline << TagArc << TagTools;
switch(tags.indexOf(domElement.tagName()))
{ {
ParsePointElement(scene, domElement, parse, domElement.attribute("type", "")); case 0: // TagPoint
continue; ParsePointElement(scene, domElement, parse, domElement.attribute(AttrType, ""));
} break;
if (domElement.tagName() == "line") case 1: // TagLine
{ ParseLineElement(scene, domElement, parse);
ParseLineElement(scene, domElement, parse); break;
continue; case 2: // TagSpline
} ParseSplineElement(scene, domElement, parse, domElement.attribute(AttrType, ""));
if (domElement.tagName() == "spline") break;
{ case 3: // TagArc
ParseSplineElement(scene, domElement, parse, domElement.attribute("type", "")); ParseArcElement(scene, domElement, parse, domElement.attribute(AttrType, ""));
continue; break;
} case 4: // TagTools
if (domElement.tagName() == "arc") ParseToolsElement(scene, domElement, parse, domElement.attribute(AttrType, ""));
{ break;
ParseArcElement(scene, domElement, parse, domElement.attribute("type", "")); default:
continue; qWarning()<<"Wrong tag name"<<Q_FUNC_INFO;
} break;
if (domElement.tagName() == "tools")
{
ParseToolsElement(scene, domElement, parse, domElement.attribute("type", ""));
continue;
} }
} }
} }
@ -525,23 +549,29 @@ void VPattern::ParseDetailElement(VMainGraphicsScene *sceneDetail, const QDomEle
const qreal mx = toPixel(GetParametrDouble(element, VAbstractTool::AttrMx, "0.0")); const qreal mx = toPixel(GetParametrDouble(element, VAbstractTool::AttrMx, "0.0"));
const qreal my = toPixel(GetParametrDouble(element, VAbstractTool::AttrMy, "0.0")); const qreal my = toPixel(GetParametrDouble(element, VAbstractTool::AttrMy, "0.0"));
const NodeDetail::NodeDetails nodeType = NodeDetail::Contour; const NodeDetail::NodeDetails nodeType = NodeDetail::Contour;
const QString t = GetParametrString(element, "type", "NodePoint");
const QString t = GetParametrString(element, AttrType, "NodePoint");
Tool::Tools tool; Tool::Tools tool;
if (t == "NodePoint") QStringList types;
types << VToolDetail::NodePoint << VToolDetail::NodeArc << VToolDetail::NodeSpline <<
VToolDetail::NodeSplinePath;
switch(types.indexOf(t))
{ {
tool = Tool::NodePoint; case 0: // VToolDetail::NodePoint
} tool = Tool::NodePoint;
else if (t == "NodeArc") break;
{ case 1: // VToolDetail::NodeArc
tool = Tool::NodeArc; tool = Tool::NodeArc;
} break;
else if (t == "NodeSpline") case 2: // VToolDetail::NodeSpline
{ tool = Tool::NodeSpline;
tool = Tool::NodeSpline; break;
} case 3: // VToolDetail::NodeSplinePath
else if (t == "NodeSplinePath") tool = Tool::NodeSplinePath;
{ break;
tool = Tool::NodeSplinePath; default:
qWarning()<<"Wrong node type."<<Q_FUNC_INFO;
break;
} }
detail.append(VNodeDetail(id, tool, nodeType, mx, my)); detail.append(VNodeDetail(id, tool, nodeType, mx, my));
} }
@ -944,7 +974,8 @@ void VPattern::ParsePointElement(VMainGraphicsScene *scene, const QDomElement &d
} }
} }
void VPattern::ParseLineElement(VMainGraphicsScene *scene, const QDomElement &domElement, const Document::Documents &parse) void VPattern::ParseLineElement(VMainGraphicsScene *scene, const QDomElement &domElement,
const Document::Documents &parse)
{ {
Q_CHECK_PTR(scene); Q_CHECK_PTR(scene);
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
@ -953,7 +984,8 @@ void VPattern::ParseLineElement(VMainGraphicsScene *scene, const QDomElement &do
const quint32 id = GetParametrId(domElement); const quint32 id = GetParametrId(domElement);
const quint32 firstPoint = GetParametrUInt(domElement, VAbstractTool::AttrFirstPoint, "0"); const quint32 firstPoint = GetParametrUInt(domElement, VAbstractTool::AttrFirstPoint, "0");
const quint32 secondPoint = GetParametrUInt(domElement, VAbstractTool::AttrSecondPoint, "0"); const quint32 secondPoint = GetParametrUInt(domElement, VAbstractTool::AttrSecondPoint, "0");
const QString typeLine = GetParametrString(domElement, VAbstractTool::AttrTypeLine, VAbstractTool::TypeLineLine); const QString typeLine = GetParametrString(domElement, VAbstractTool::AttrTypeLine,
VAbstractTool::TypeLineLine);
VToolLine::Create(id, firstPoint, secondPoint, typeLine, scene, this, data, parse, Tool::FromFile); VToolLine::Create(id, firstPoint, secondPoint, typeLine, scene, this, data, parse, Tool::FromFile);
} }
@ -1189,14 +1221,14 @@ void VPattern::ParseIncrementsElement(const QDomNode &node)
const QDomElement domElement = domNode.toElement(); const QDomElement domElement = domNode.toElement();
if (domElement.isNull() == false) if (domElement.isNull() == false)
{ {
if (domElement.tagName() == "increment") if (domElement.tagName() == TagIncrement)
{ {
quint32 id = GetParametrId(domElement); const quint32 id = GetParametrId(domElement);
QString name = GetParametrString(domElement, "name", ""); const QString name = GetParametrString(domElement, IncrementName, "");
qreal base = GetParametrDouble(domElement, "base", "0"); const qreal base = GetParametrDouble(domElement, IncrementBase, "0");
qreal ksize = GetParametrDouble(domElement, "ksize", "0"); const qreal ksize = GetParametrDouble(domElement, IncrementKsize, "0");
qreal kgrowth = GetParametrDouble(domElement, "kgrowth", "0"); const qreal kgrowth = GetParametrDouble(domElement, IncrementKgrowth, "0");
QString desc = GetParametrString(domElement, "description", "Description"); const QString desc = GetParametrString(domElement, IncrementDescription, "Description");
data->UpdateId(id); data->UpdateId(id);
data->AddIncrementTableRow(name, VIncrementTableRow(id, base, ksize, kgrowth, desc)); data->AddIncrementTableRow(name, VIncrementTableRow(id, base, ksize, kgrowth, desc));
} }
@ -1209,7 +1241,7 @@ void VPattern::ParseIncrementsElement(const QDomNode &node)
quint32 VPattern::GetParametrId(const QDomElement &domElement) const quint32 VPattern::GetParametrId(const QDomElement &domElement) const
{ {
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
const quint32 id = GetParametrUInt(domElement, "id", "0"); const quint32 id = GetParametrUInt(domElement, VAbstractTool::AttrId, "0");
if (id <= 0) if (id <= 0)
{ {
throw VExceptionWrongParameterId(tr("Got wrong parameter id. Need only id > 0."), domElement); throw VExceptionWrongParameterId(tr("Got wrong parameter id. Need only id > 0."), domElement);
@ -1219,7 +1251,7 @@ quint32 VPattern::GetParametrId(const QDomElement &domElement) const
void VPattern::CollectId(const QDomElement &node, QVector<quint32> &vector) const void VPattern::CollectId(const QDomElement &node, QVector<quint32> &vector) const
{ {
if (node.hasAttribute("id")) if (node.hasAttribute(VAbstractTool::AttrId))
{ {
const quint32 id = GetParametrId(node); const quint32 id = GetParametrId(node);
if (vector.contains(id)) if (vector.contains(id))

View File

@ -164,6 +164,7 @@ public:
* @return true if found. * @return true if found.
*/ */
bool GetActivNodeElement(const QString& name, QDomElement& element); bool GetActivNodeElement(const QString& name, QDomElement& element);
static const QString TagPattern;
static const QString TagCalculation; static const QString TagCalculation;
static const QString TagModeling; static const QString TagModeling;
static const QString TagDetails; static const QString TagDetails;
@ -171,8 +172,20 @@ public:
static const QString TagDescription; static const QString TagDescription;
static const QString TagNotes; static const QString TagNotes;
static const QString TagIncrements; static const QString TagIncrements;
static const QString TagIncrement;
static const QString TagDraw; static const QString TagDraw;
static const QString TagPoint;
static const QString TagLine;
static const QString TagSpline;
static const QString TagArc;
static const QString TagTools;
static const QString AttrName; static const QString AttrName;
static const QString AttrType;
static const QString IncrementName;
static const QString IncrementBase;
static const QString IncrementKsize;
static const QString IncrementKgrowth;
static const QString IncrementDescription;
signals: signals:
/** /**
* @brief ChangedActivDraw change active pattern peace. * @brief ChangedActivDraw change active pattern peace.