Define literals of each tags and attributes in manual layout format.

More efficient. A lot easier to maintain. Remove redundancy.
This commit is contained in:
Roman Telezhynskyi 2020-04-23 15:48:56 +03:00
parent 8138c1b430
commit 61effa2a82
5 changed files with 225 additions and 87 deletions

View File

@ -12,6 +12,7 @@ SOURCES += \
$$PWD/vpuzzlelayout.cpp \
$$PWD/vpuzzlelayer.cpp \
$$PWD/vpuzzlepiece.cpp \
$$PWD/xml/layoutliterals.cpp \
$$PWD/xml/vpuzzlelayoutfilewriter.cpp \
$$PWD/xml/vpuzzlelayoutfilereader.cpp
@ -28,6 +29,7 @@ HEADERS += \
$$PWD/vpuzzlelayout.h \
$$PWD/vpuzzlelayer.h \
$$PWD/vpuzzlepiece.h \
$$PWD/xml/layoutliterals.h \
$$PWD/xml/vpuzzlelayoutfilewriter.h \
$$PWD/xml/vpuzzlelayoutfilereader.h

View File

@ -0,0 +1,63 @@
/************************************************************************
**
** @file literals.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 23 4, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "layoutliterals.h"
namespace ML
{
const QString TagLayout = QStringLiteral("layout");
const QString TagProperties = QStringLiteral("properties");
const QString TagLayers = QStringLiteral("layers");
const QString TagUnit = QStringLiteral("unit");
const QString TagDescription = QStringLiteral("description");
const QString TagSize = QStringLiteral("size");
const QString TagMargin = QStringLiteral("margin");
const QString TagControl = QStringLiteral("control");
const QString TagTiles = QStringLiteral("tiles");
const QString TagUnplacedPiecesLayer = QStringLiteral("unplacedPiecesLayer");
const QString TagLayer = QStringLiteral("layer");
const QString TagPiece = QStringLiteral("piece");
const QString AttrVersion = QStringLiteral("version");
const QString AttrWarningSuperposition = QStringLiteral("warningSuperposition");
const QString AttrWarningOutOfBound = QStringLiteral("warningOutOfBound");
const QString AttrStickyEdges = QStringLiteral("stickyEdges");
const QString AttrPiecesGap = QStringLiteral("piecesGap");
const QString AttrVisible = QStringLiteral("visible");
const QString AttrMatchingMarks = QStringLiteral("matchingMarks");
const QString AttrName = QStringLiteral("name");
const QString AttrLeft = QStringLiteral("left");
const QString AttrTop = QStringLiteral("top");
const QString AttrRight = QStringLiteral("right");
const QString AttrBottom = QStringLiteral("bottom");
const QString AttrWidth = QStringLiteral("width");
const QString AttrLength = QStringLiteral("length");
const QString AttrFollowGrainLine = QStringLiteral("followGrainLine");
const QString AttrID = QStringLiteral("id");
const QString AttrMirrored = QStringLiteral("mirrored");
const QString AttrTransform = QStringLiteral("transform");
}

View File

@ -0,0 +1,71 @@
/************************************************************************
**
** @file literals.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 23 4, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef LAYOUTLITERALS_H
#define LAYOUTLITERALS_H
#include <QString>
// All tags and attributes present in manual layout file format
namespace ML
{
extern const QString TagLayout;
extern const QString TagProperties;
extern const QString TagLayers;
extern const QString TagUnit;
extern const QString TagDescription;
extern const QString TagSize;
extern const QString TagMargin;
extern const QString TagControl;
extern const QString TagTiles;
extern const QString TagUnplacedPiecesLayer;
extern const QString TagLayer;
extern const QString TagPiece;
extern const QString AttrVersion;
extern const QString AttrWarningSuperposition;
extern const QString AttrWarningOutOfBound;
extern const QString AttrStickyEdges;
extern const QString AttrPiecesGap;
extern const QString AttrVisible;
extern const QString AttrMatchingMarks;
extern const QString AttrName;
extern const QString AttrLeft;
extern const QString AttrTop;
extern const QString AttrRight;
extern const QString AttrBottom;
extern const QString AttrWidth;
extern const QString AttrLength;
extern const QString AttrFollowGrainLine;
extern const QString AttrID;
extern const QString AttrMirrored;
extern const QString AttrTransform;
}
#endif // LAYOUTLITERALS_H

View File

@ -29,6 +29,7 @@
#include <QXmlStreamAttributes>
#include "vpuzzlelayoutfilereader.h"
#include "vpuzzlelayoutfilewriter.h"
#include "layoutliterals.h"
VPuzzleLayoutFileReader::VPuzzleLayoutFileReader()
{
@ -54,9 +55,9 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file)
// if it doesn't start with layout, error
// if it starts with version > than current version, error
if (name() == QString("layout"))
if (name() == ML::TagLayout)
{
QString versionStr = attributes().value(QString("version")).toString();
QString versionStr = attributes().value(ML::AttrVersion).toString();
QStringList versionStrParts = versionStr.split('.');
m_layoutFormatVersion = FORMAT_VERSION(versionStrParts.at(0).toInt(),versionStrParts.at(1).toInt(),versionStrParts.at(2).toInt());
@ -82,15 +83,15 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("layout"));
Q_ASSERT(isStartElement() && name() == ML::TagLayout);
while (readNextStartElement())
{
if (name() == QString("properties"))
if (name() == ML::TagProperties)
{
ReadProperties(layout);
}
else if (name() == QString("layers"))
else if (name() == ML::TagLayers)
{
ReadLayers(layout);
}
@ -104,74 +105,75 @@ void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("properties"));
Q_ASSERT(isStartElement() && name() == ML::TagProperties);
while (readNextStartElement())
{
qDebug(name().toString().toLatin1());
if (name() == QString("unit"))
const QStringList tags = QStringList(
{
ML::TagUnit,
ML::TagDescription,
ML::TagSize,
ML::TagMargin,
ML::TagControl,
ML::TagTiles
});
switch (tags.indexOf(name().toString()))
{
case 0:// unit
qDebug("read unit");
QString unit = readElementText();
if(unit == UnitsToStr(Unit::Inch))
{
layout->SetUnit(Unit::Inch);
}
else if(unit == UnitsToStr(Unit::Mm))
{
layout->SetUnit(Unit::Cm);
}
else // no condition here to have a default value just in case
{
layout->SetUnit(Unit::Cm);
}
}
else if (name() == QString("description"))
layout->SetUnit(StrToUnits(readElementText()));
break;
case 1:// description
{
qDebug("read description");
QString description = readElementText();
// TODO read the description info
break;
}
else if (name() == QString("size"))
case 2:// size
{
qDebug("read size");
QSizeF size = ReadSize();
layout->SetLayoutSize(size);
readElementText();
break;
}
else if (name() == QString("margin"))
case 3:// margin
{
qDebug("read margin");
QMarginsF margins = ReadMargins();
layout->SetLayoutMargins(margins);
readElementText();
break;
}
else if (name() == QString("control"))
case 4:// control
{
qDebug("read control");
QXmlStreamAttributes attribs = attributes();
// attribs.value("followGrainLine"); // TODO
layout->SetWarningSuperpositionOfPieces(attribs.value("warningSuperposition") == "true");
layout->SetWarningPiecesOutOfBound(attribs.value("warningOutOfBound") == "true");
layout->SetStickyEdges(attribs.value("stickyEdges") == "true");
layout->SetWarningSuperpositionOfPieces(attribs.value(ML::AttrWarningSuperposition) == "true");
layout->SetWarningPiecesOutOfBound(attribs.value(ML::AttrWarningOutOfBound) == "true");
layout->SetStickyEdges(attribs.value(ML::AttrStickyEdges) == "true");
layout->SetPiecesGap(attribs.value("piecesGap").toDouble());
layout->SetPiecesGap(attribs.value(ML::AttrPiecesGap).toDouble());
readElementText();
break;
}
else if (name() == QString("tiles"))
{
case 5:// tiles
qDebug("read tiles");
ReadTiles(layout);
readElementText();
}
else
{
break;
default:
// TODO error handling, we encountered a tag that isn't defined in the specification
skipCurrentElement();
break;
}
}
}
@ -181,22 +183,22 @@ void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout)
{
Q_UNUSED(layout); // to be removed when used
Q_ASSERT(isStartElement() && name() == QString("tiles"));
Q_ASSERT(isStartElement() && name() == ML::TagTiles);
QXmlStreamAttributes attribs = attributes();
// attribs.value("visible"); // TODO
// attribs.value("matchingMarks"); // TODO
// QXmlStreamAttributes attribs = attributes();
// attribs.value(ML::AttrVisible); // TODO
// attribs.value(ML::AttrMatchingMarks); // TODO
while (readNextStartElement())
{
if (name() == QString("size"))
if (name() == ML::TagSize)
{
QSizeF size = ReadSize();
// TODO set layout tiled size
Q_UNUSED(size);
readElementText();
}
else if (name() == QString("margin"))
else if (name() == ML::TagMargin)
{
QMarginsF margins = ReadMargins();
// TODO set layout tiled margins
@ -214,15 +216,15 @@ void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("layers"));
Q_ASSERT(isStartElement() && name() == ML::TagLayers);
while (readNextStartElement())
{
if (name() == QString("unplacedPiecesLayer"))
if (name() == ML::TagUnplacedPiecesLayer)
{
ReadLayer(layout->GetUnplacedPiecesLayer());
}
else if (name() == QString("layer"))
else if (name() == ML::TagLayer)
{
VPuzzleLayer *layer = layout->AddLayer();
ReadLayer(layer);
@ -238,15 +240,15 @@ void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer)
{
Q_ASSERT(isStartElement() && name() == QString("layer"));
Q_ASSERT(isStartElement() && name() == ML::TagLayer);
QXmlStreamAttributes attribs = attributes();
layer->SetName(attribs.value("name").toString());
layer->SetIsVisible(attribs.value("visible") == "true");
layer->SetName(attribs.value(ML::AttrName).toString());
layer->SetIsVisible(attribs.value(ML::AttrVisible) == "true");
while (readNextStartElement())
{
if (name() == QString("piece"))
if (name() == ML::TagPiece)
{
VPuzzlePiece *piece = new VPuzzlePiece();
ReadPiece(piece);
@ -264,7 +266,7 @@ void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer)
void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece)
{
Q_UNUSED(piece);
Q_ASSERT(isStartElement() && name() == QString("piece"));
Q_ASSERT(isStartElement() && name() == ML::TagPiece);
// TODO read the attributes
@ -290,10 +292,10 @@ QMarginsF VPuzzleLayoutFileReader::ReadMargins()
QMarginsF margins = QMarginsF();
QXmlStreamAttributes attribs = attributes();
margins.setLeft(attribs.value("left").toDouble());
margins.setTop(attribs.value("top").toDouble());
margins.setRight(attribs.value("right").toDouble());
margins.setBottom(attribs.value("bottom").toDouble());
margins.setLeft(attribs.value(ML::AttrLeft).toDouble());
margins.setTop(attribs.value(ML::AttrTop).toDouble());
margins.setRight(attribs.value(ML::AttrRight).toDouble());
margins.setBottom(attribs.value(ML::AttrBottom).toDouble());
return margins;
}
@ -304,8 +306,8 @@ QSizeF VPuzzleLayoutFileReader::ReadSize()
QSizeF size = QSize();
QXmlStreamAttributes attribs = attributes();
size.setWidth(attribs.value("width").toDouble());
size.setHeight(attribs.value("length").toDouble());
size.setWidth(attribs.value(ML::AttrWidth).toDouble());
size.setHeight(attribs.value(ML::AttrLength).toDouble());
return size;
}

View File

@ -60,8 +60,8 @@ void VPuzzleLayoutFileWriter::WriteFile(VPuzzleLayout *layout, QFile *file)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteLayout(VPuzzleLayout *layout)
{
writeStartElement("layout");
writeAttribute("version", LayoutFileFormatVersionStr);
writeStartElement(ML::TagLayout);
writeAttribute(ML::AttrVersion, LayoutFileFormatVersionStr);
WriteProperties(layout);
WriteLayers(layout);
@ -72,22 +72,22 @@ void VPuzzleLayoutFileWriter::WriteLayout(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteProperties(VPuzzleLayout *layout)
{
writeStartElement("properties");
writeStartElement(ML::TagProperties);
writeTextElement("unit", UnitsToStr(layout->GetUnit()));
writeTextElement(ML::TagUnit, UnitsToStr(layout->GetUnit()));
writeTextElement("description", ""); // TODO : define the value in layout
writeTextElement(ML::TagDescription, QString()); // TODO : define the value in layout
WriteSize(layout->GetLayoutSize());
WriteMargins(layout->GetLayoutMargins());
writeStartElement("control");
writeAttribute("followGrainLine", "no"); // TODO / Fixme get the right value
writeAttribute("warningSuperposition", QString(layout->GetWarningSuperpositionOfPieces() ? "true" : "false"));
writeAttribute("warningOutOfBound", QString(layout->GetWarningPiecesOutOfBound() ? "true" : "false"));
writeAttribute("stickyEdges", QString(layout->GetStickyEdges() ? "true" : "false"));
writeAttribute("piecesGap", QString::number(layout->GetPiecesGap()));
writeStartElement(ML::TagControl);
writeAttribute(ML::AttrFollowGrainLine, "no"); // TODO / Fixme get the right value
writeAttribute(ML::AttrWarningSuperposition, QString(layout->GetWarningSuperpositionOfPieces() ? "true" : "false"));
writeAttribute(ML::AttrWarningOutOfBound, QString(layout->GetWarningPiecesOutOfBound() ? "true" : "false"));
writeAttribute(ML::AttrStickyEdges, QString(layout->GetStickyEdges() ? "true" : "false"));
writeAttribute(ML::AttrPiecesGap, QString::number(layout->GetPiecesGap()));
writeEndElement(); // control
// WriteTiles(layout); TODO: when tile functionality implemented, then uncomment this line
@ -100,9 +100,9 @@ void VPuzzleLayoutFileWriter::WriteTiles(VPuzzleLayout *layout)
{
Q_UNUSED(layout); // to be removed
writeStartElement("tiles");
writeAttribute("visible", QString(false ? "true" : "false")); // TODO / Fixme get the right value
writeAttribute("matchingMarks", "standard"); // TODO / Fixme get the right value
writeStartElement(ML::TagTiles);
writeAttribute(ML::AttrVisible, QString(false ? "true" : "false")); // TODO / Fixme get the right value
writeAttribute(ML::AttrMatchingMarks, "standard"); // TODO / Fixme get the right value
QSizeF size = QSizeF(); // TODO get the right size
WriteSize(size);
@ -117,9 +117,9 @@ void VPuzzleLayoutFileWriter::WriteTiles(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteLayers(VPuzzleLayout *layout)
{
writeStartElement("layers");
writeStartElement(ML::TagLayers);
WriteLayer(layout->GetUnplacedPiecesLayer(), "unplacedPieces");
WriteLayer(layout->GetUnplacedPiecesLayer(), ML::TagUnplacedPiecesLayer);
QList<VPuzzleLayer*> layers = layout->GetLayers();
for (auto layer : layers)
@ -134,15 +134,15 @@ void VPuzzleLayoutFileWriter::WriteLayers(VPuzzleLayout *layout)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteLayer(VPuzzleLayer *layer)
{
WriteLayer(layer, "layer");
WriteLayer(layer, ML::TagLayer);
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteLayer(VPuzzleLayer *layer, const QString &tagName)
{
writeStartElement(tagName); // layer
writeAttribute("name", layer->GetName());
writeAttribute("visible", QString(layer->GetIsVisible()? "true" : "false"));
writeAttribute(ML::AttrName, layer->GetName());
writeAttribute(ML::AttrVisible, QString(layer->GetIsVisible()? "true" : "false"));
// TODO selected info. Not sure how it's saved yet
//writeAttribute("selected", QString(layer->GetIsSelected()? "true" : "false"));
@ -161,11 +161,11 @@ void VPuzzleLayoutFileWriter::WritePiece(VPuzzlePiece *piece)
{
Q_UNUSED(piece);
writeStartElement("piece");
writeAttribute("id", "uuid1"); // TODO / Fixme get the right value
writeAttribute("name", "Piece name"); // TODO / Fixme get the right value
writeAttribute("mirrored", "false"); // TODO / Fixme get the right value
writeAttribute("transform", "string representation of the transformation"); // TODO / Fixme get the right value
writeStartElement(ML::TagPiece);
writeAttribute(ML::AttrID, "uuid1"); // TODO / Fixme get the right value
writeAttribute(ML::AttrName, "Piece name"); // TODO / Fixme get the right value
writeAttribute(ML::AttrMirrored, "false"); // TODO / Fixme get the right value
writeAttribute(ML::AttrTransform, "string representation of the transformation"); // TODO / Fixme get the right value
// TODO cuttingLine
// TODO seamLine
@ -182,11 +182,11 @@ void VPuzzleLayoutFileWriter::WritePiece(VPuzzlePiece *piece)
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileWriter::WriteMargins(QMarginsF margins)
{
writeStartElement("margin");
writeAttribute("left", QString::number(margins.left()));
writeAttribute("top", QString::number(margins.top()));
writeAttribute("right", QString::number(margins.right()));
writeAttribute("bottom", QString::number(margins.bottom()));
writeStartElement(ML::TagMargin);
writeAttribute(ML::AttrLeft, QString::number(margins.left()));
writeAttribute(ML::AttrTop, QString::number(margins.top()));
writeAttribute(ML::AttrRight, QString::number(margins.right()));
writeAttribute(ML::AttrBottom, QString::number(margins.bottom()));
writeEndElement(); // margin
}
@ -206,8 +206,8 @@ void VPuzzleLayoutFileWriter::WriteSize(QSizeF size)
length = 0;
}
writeStartElement("size");
writeAttribute("width", QString::number(width));
writeAttribute("length", QString::number(length));
writeStartElement(ML::TagSize);
writeAttribute(ML::AttrWidth, QString::number(width));
writeAttribute(ML::AttrLength, QString::number(length));
writeEndElement(); // size
}