valentina/src/app/puzzle/xml/vplayoutfilereader.cpp

446 lines
14 KiB
C++
Raw Normal View History

2020-04-18 16:32:54 +02:00
/************************************************************************
**
2020-05-23 14:04:39 +02:00
** @file vplayoutfilereader.cpp
2020-04-18 16:32:54 +02:00
** @author Ronan Le Tiec
** @date 18 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/>.
**
** *************************************************************************/
2020-04-18 20:24:25 +02:00
#include <QXmlStreamAttributes>
2020-05-23 14:01:03 +02:00
#include "vplayoutfilereader.h"
2020-05-23 14:02:39 +02:00
#include "vplayoutfilewriter.h"
2020-05-23 13:51:57 +02:00
#include "vplayoutliterals.h"
#include "../ifc/exception/vexception.h"
#include "../ifc/exception/vexceptionconversionerror.h"
2020-04-18 16:32:54 +02:00
2021-05-22 19:29:33 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(MLReader, "mlReader")
QT_WARNING_POP
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-05-22 19:29:33 +02:00
auto VPLayoutFileReader::ReadFile(VPLayout *layout, QFile *file) -> bool
2020-04-18 20:24:25 +02:00
{
setDevice(file);
2021-05-22 19:29:33 +02:00
try
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
if (readNextStartElement())
{
ReadLayout(layout);
}
}
catch(const VException &e)
{
raiseError(e.ErrorMessage());
2020-04-18 20:24:25 +02:00
}
2021-05-21 17:08:37 +02:00
return hasError();
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
void VPLayoutFileReader::ReadLayout(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagLayout);
const QStringList tags({ML::TagProperties, ML::TagUnplacedPieces, ML::TagSheets});
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-05-22 19:29:33 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0: // ML::TagProperties
ReadProperties(layout);
break;
case 1: // ML::TagUnplacedPieces
ReadUnplacedPieces(layout);
break;
case 2: // ML::TagSheets
ReadSheets(layout);
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
void VPLayoutFileReader::ReadProperties(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagProperties);
const QStringList tags
{
ML::TagUnit, // 0
ML::TagTitle, // 1
ML::TagDescription, // 2
ML::TagControl, // 3
ML::TagTiles // 4
};
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2020-04-23 14:49:35 +02:00
qDebug() << name().toString();
2020-04-19 16:01:46 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0:// unit
qDebug("read unit");
layout->SetUnit(StrToUnits(readElementText()));
break;
case 1:// title
qDebug("read title");
layout->SetTitle(readElementText());
break;
case 2:// description
qDebug("read description");
layout->SetDescription(readElementText());
break;
case 3:// control
qDebug("read control");
ReadControl(layout);
break;
case 4:// tiles
qDebug("read tiles");
ReadTiles(layout);
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
}
2021-05-22 19:29:33 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadControl(VPLayout *layout)
{
AssertRootTag(ML::TagControl);
QXmlStreamAttributes attribs = attributes();
layout->SetWarningSuperpositionOfPieces(ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
layout->SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
readElementText();
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadUnplacedPieces(VPLayout *layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagUnplacedPieces);
ReadPieceList(layout->GetUnplacedPieceList());
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
void VPLayoutFileReader::ReadTiles(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagTiles);
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
QXmlStreamAttributes attribs = attributes();
layout->SetShowTiles(ReadAttributeBool(attribs, ML::AttrVisible, falseStr));
// attribs.value(ML::AttrMatchingMarks); // TODO
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
const QStringList tags
{
ML::TagSize, // 0
ML::TagMargin // 1
};
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-05-22 19:29:33 +02:00
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
case 0: // size
layout->SetTilesSize(ReadSize());
break;
case 1: // margin
layout->SetTilesMargins(ReadMargins());
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
2020-04-18 20:24:25 +02:00
}
}
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadSheets(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagSheets);
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagSheet)
2020-04-18 20:24:25 +02:00
{
ReadSheet(layout);
2020-04-18 20:24:25 +02:00
}
else
{
2021-05-22 19:29:33 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
}
2021-05-22 19:29:33 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadSheetPieces(VPSheet *sheet)
{
AssertRootTag(ML::TagPieces);
ReadPieceList(sheet->GetPieceList());
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadSheet(VPLayout *layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagSheet);
const QStringList tags
{
ML::TagName, // 0
ML::TagSize, // 1
ML::TagMargin, // 2
ML::TagPieces // 3
};
QScopedPointer<VPSheet> sheet (new VPSheet(layout));
while (readNextStartElement())
{
switch (tags.indexOf(name().toString()))
{
case 0: // name
sheet->SetName(readElementText());
break;
case 1: // size
sheet->SetSheetSize(ReadSize());
break;
case 2: // margin
sheet->SetSheetMargins(ReadMargins());
break;
case 3: // pieces
ReadSheetPieces(sheet.get());
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
}
}
readElementText();
layout->AddSheet(sheet.take());
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:29:57 +02:00
void VPLayoutFileReader::ReadPieceList(VPPieceList *pieceList)
2020-04-18 20:24:25 +02:00
{
QXmlStreamAttributes attribs = attributes();
2020-05-23 15:29:57 +02:00
pieceList->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece List")));
pieceList->SetIsVisible(ReadAttributeBool(attribs, ML::AttrVisible, trueStr));
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagPiece)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
QScopedPointer<VPPiece>piece(new VPPiece());
ReadPiece(piece.data());
pieceList->AddPiece(piece.take());
2020-04-18 20:24:25 +02:00
}
else
{
2021-05-22 19:29:33 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
void VPLayoutFileReader::ReadPiece(VPPiece *piece)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagPiece);
2020-04-18 20:24:25 +02:00
QXmlStreamAttributes attribs = attributes();
piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));
2020-05-01 19:08:48 +02:00
2021-05-22 19:29:33 +02:00
QString uuidStr = ReadAttributeString(attribs, ML::AttrID, QUuid::createUuid().toString());
2020-06-25 14:17:31 +02:00
piece->SetUUID(QUuid(uuidStr));
2020-05-01 19:08:48 +02:00
bool showSeamline = ReadAttributeBool(attribs, ML::AttrShowSeamline, trueStr);
piece->SetShowSeamLine(showSeamline);
bool pieceMirrored = ReadAttributeBool(attribs, ML::AttrMirrored, falseStr);
piece->SetPieceMirrored(pieceMirrored);
// TODO read the further attributes
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2020-04-18 20:24:25 +02:00
if (name() == QString("..."))
{
// TODO
2020-04-19 16:01:46 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
2020-04-19 16:01:46 +02:00
skipCurrentElement();
2020-04-18 20:24:25 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QMarginsF VPLayoutFileReader::ReadMargins()
2020-04-18 20:24:25 +02:00
{
QMarginsF margins = QMarginsF();
QXmlStreamAttributes attribs = attributes();
margins.setLeft(ReadAttributeDouble(attribs, ML::AttrLeft, QChar('0')));
margins.setTop(ReadAttributeDouble(attribs, ML::AttrTop, QChar('0')));
margins.setRight(ReadAttributeDouble(attribs, ML::AttrRight, QChar('0')));
margins.setBottom(ReadAttributeDouble(attribs, ML::AttrBottom, QChar('0')));
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
return margins;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QSizeF VPLayoutFileReader::ReadSize()
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
QSizeF size;
2020-04-18 20:24:25 +02:00
QXmlStreamAttributes attribs = attributes();
size.setWidth(ReadAttributeDouble(attribs, ML::AttrWidth, QChar('0')));
size.setHeight(ReadAttributeDouble(attribs, ML::AttrLength, QChar('0')));
2020-04-18 20:24:25 +02:00
2021-05-22 19:29:33 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
return size;
}
2021-05-22 19:29:33 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::AssertRootTag(const QString &tag) const
{
if (not (isStartElement() && name() == tag))
{
throw VException(tr("Unexpected tag %1 in line %2").arg(name()).arg(lineNumber()));
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QString VPLayoutFileReader::ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
2021-05-22 19:29:33 +02:00
const QString &defValue)
{
const QString parameter = attribs.value(name).toString();
if (parameter.isEmpty())
{
if (defValue.isEmpty())
{
throw VException(tr("Got empty attribute '%1'").arg(name));
}
2021-05-22 19:29:33 +02:00
return defValue;
}
return parameter;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QString VPLayoutFileReader::ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name)
{
return attribs.value(name).toString();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
bool VPLayoutFileReader::ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue)
{
QString parametr;
bool val = true;
const QString message = QObject::tr("Can't convert toBool parameter");
try
{
parametr = ReadAttributeString(attribs, name, defValue);
const QStringList bools {trueStr, falseStr, QChar('1'), QChar('0')};
switch (bools.indexOf(parametr))
{
case 0: // true
case 2: // 1
val = true;
break;
case 1: // false
case 3: // 0
val = false;
break;
default:// others
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return val;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
qreal VPLayoutFileReader::ReadAttributeDouble(const QXmlStreamAttributes &attribs, const QString &name,
2021-05-22 19:29:33 +02:00
const QString &defValue)
{
bool ok = false;
qreal param = 0;
const QString message = QObject::tr("Can't convert toDouble parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(QChar(','), QChar('.')).toDouble(&ok);
2021-05-22 19:29:33 +02:00
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return param;
}