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

393 lines
13 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
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
VPLayoutFileReader::VPLayoutFileReader()
2020-04-18 16:32:54 +02:00
{
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
VPLayoutFileReader::~VPLayoutFileReader()
2020-04-18 20:24:25 +02:00
{
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
bool VPLayoutFileReader::ReadFile(VPLayout *layout, QFile *file)
2020-04-18 20:24:25 +02:00
{
setDevice(file);
if (readNextStartElement())
{
ReadLayout(layout);
2020-04-18 20:24:25 +02:00
}
return !error();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
void VPLayoutFileReader::ReadLayout(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
SCASSERT(isStartElement() && name() == ML::TagLayout);
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagProperties)
2020-04-18 20:24:25 +02:00
{
ReadProperties(layout);
}
2020-05-23 15:29:57 +02:00
else if (name() == ML::TagPieceLists)
2020-04-18 20:24:25 +02:00
{
2020-05-23 15:29:57 +02:00
ReadPieceLists(layout);
2020-04-18 20:24:25 +02:00
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
void VPLayoutFileReader::ReadProperties(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
SCASSERT(isStartElement() && name() == ML::TagProperties);
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
const QStringList tags = QStringList(
{
ML::TagUnit,
ML::TagDescription,
ML::TagSize,
ML::TagMargin,
ML::TagControl,
ML::TagTiles
});
switch (tags.indexOf(name().toString()))
2020-04-18 20:24:25 +02:00
{
case 0:// unit
2020-04-19 16:01:46 +02:00
qDebug("read unit");
layout->SetUnit(StrToUnits(readElementText()));
break;
case 1:// description
2020-04-18 20:24:25 +02:00
{
2020-04-19 16:01:46 +02:00
qDebug("read description");
2020-04-18 20:24:25 +02:00
QString description = readElementText();
// TODO read the description info
break;
2020-04-18 20:24:25 +02:00
}
case 2:// size
2020-04-18 20:24:25 +02:00
{
2020-04-19 16:01:46 +02:00
qDebug("read size");
2020-04-18 20:24:25 +02:00
QSizeF size = ReadSize();
layout->SetLayoutSize(size);
2020-04-19 16:01:46 +02:00
readElementText();
break;
2020-04-18 20:24:25 +02:00
}
case 3:// margin
2020-04-18 20:24:25 +02:00
{
2020-04-19 16:01:46 +02:00
qDebug("read margin");
2020-04-18 20:24:25 +02:00
QMarginsF margins = ReadMargins();
layout->SetLayoutMargins(margins);
2020-04-19 16:01:46 +02:00
readElementText();
break;
2020-04-18 20:24:25 +02:00
}
case 4:// control
2020-04-18 20:24:25 +02:00
{
2020-04-19 16:01:46 +02:00
qDebug("read control");
2020-04-18 20:24:25 +02:00
QXmlStreamAttributes attribs = attributes();
// attribs.value("followGrainLine"); // TODO
layout->SetWarningSuperpositionOfPieces(ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
layout->SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
layout->SetStickyEdges(ReadAttributeBool(attribs, ML::AttrStickyEdges, trueStr));
2020-04-18 20:24:25 +02:00
layout->SetPiecesGap(ReadAttributeDouble(attribs, ML::AttrPiecesGap, QChar('0')));
2020-04-19 16:01:46 +02:00
readElementText();
break;
2020-04-18 20:24:25 +02:00
}
case 5:// tiles
2020-04-19 16:01:46 +02:00
qDebug("read tiles");
2020-04-18 20:24:25 +02:00
ReadTiles(layout);
2020-04-19 16:01:46 +02:00
readElementText();
break;
default:
2020-04-18 20:24:25 +02:00
// TODO error handling, we encountered a tag that isn't defined in the specification
2020-04-19 16:01:46 +02:00
skipCurrentElement();
break;
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
{
Q_UNUSED(layout); // to be removed when used
SCASSERT(isStartElement() && name() == ML::TagTiles);
2020-04-18 20:24:25 +02:00
// QXmlStreamAttributes attribs = attributes();
// attribs.value(ML::AttrVisible); // TODO
// attribs.value(ML::AttrMatchingMarks); // TODO
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
if (name() == ML::TagSize)
2020-04-18 20:24:25 +02:00
{
QSizeF size = ReadSize();
// TODO set layout tiled size
Q_UNUSED(size);
2020-04-19 16:01:46 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
}
else if (name() == ML::TagMargin)
2020-04-18 20:24:25 +02:00
{
QMarginsF margins = ReadMargins();
// TODO set layout tiled margins
Q_UNUSED(margins);
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 15:34:11 +02:00
void VPLayoutFileReader::ReadPieceLists(VPLayout *layout)
2020-04-18 20:24:25 +02:00
{
2020-05-23 15:29:57 +02:00
SCASSERT(isStartElement() && name() == ML::TagPieceLists);
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2020-05-23 15:29:57 +02:00
if (name() == ML::TagUnplacedPieceList)
2020-04-18 20:24:25 +02:00
{
2020-05-23 15:29:57 +02:00
ReadPieceList(layout->GetUnplacedPieceList());
2020-04-18 20:24:25 +02:00
}
2020-05-23 15:29:57 +02:00
else if (name() == ML::TagPieceList)
2020-04-18 20:24:25 +02:00
{
2020-05-23 15:29:57 +02:00
VPPieceList *pieceList = layout->AddPieceList();
ReadPieceList(pieceList);
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 15:29:57 +02:00
void VPLayoutFileReader::ReadPieceList(VPPieceList *pieceList)
2020-04-18 20:24:25 +02:00
{
2020-05-23 15:29:57 +02:00
SCASSERT(isStartElement() && (name() == ML::TagPieceList || name() == ML::TagUnplacedPieceList));
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
{
VPuzzlePiece *piece = new VPuzzlePiece();
ReadPiece(piece);
2020-05-23 15:29:57 +02:00
pieceList->AddPiece(piece);
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
void VPLayoutFileReader::ReadPiece(VPuzzlePiece *piece)
2020-04-18 20:24:25 +02:00
{
2020-04-19 10:38:28 +02:00
Q_UNUSED(piece);
SCASSERT(isStartElement() && name() == 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
QString uuidStr = ReadAttributeString(attribs, ML::AttrID, QUuid().toString());// FIXME: is that correct to have a default value here?
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
return margins;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QSizeF VPLayoutFileReader::ReadSize()
2020-04-18 20:24:25 +02:00
{
QSizeF size = QSize();
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
return size;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:01:03 +02:00
QString VPLayoutFileReader::ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
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));
}
else
{
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,
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);
if (ok == false)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
throw excep;
}
return param;
}