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"
|
2020-04-23 16:22:58 +02:00
|
|
|
#include "../ifc/exception/vexception.h"
|
|
|
|
#include "../ifc/exception/vexceptionconversionerror.h"
|
2020-04-18 16:32:54 +02:00
|
|
|
|
2020-04-23 16:22:58 +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())
|
|
|
|
{
|
2020-04-23 17:05:00 +02:00
|
|
|
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
|
|
|
{
|
2020-05-01 18:26:02 +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())
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
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 17:47:04 +02:00
|
|
|
ReadSheets(layout);
|
|
|
|
}
|
|
|
|
else if (name() == ML::TagUnplacedPieceList)
|
|
|
|
{
|
|
|
|
ReadUnplacedPieces(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
|
|
|
{
|
2020-05-01 18:26:02 +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
|
|
|
|
2020-04-23 14:48:56 +02:00
|
|
|
const QStringList tags = QStringList(
|
|
|
|
{
|
|
|
|
ML::TagUnit,
|
|
|
|
ML::TagDescription,
|
|
|
|
ML::TagControl,
|
|
|
|
ML::TagTiles
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (tags.indexOf(name().toString()))
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
case 0:// unit
|
2020-04-19 16:01:46 +02:00
|
|
|
qDebug("read unit");
|
2020-04-23 14:48:56 +02:00
|
|
|
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
|
2020-04-23 14:48:56 +02:00
|
|
|
break;
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
2020-05-23 17:47:04 +02:00
|
|
|
case 2:// 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();
|
2020-04-23 16:22:58 +02:00
|
|
|
layout->SetWarningSuperpositionOfPieces(ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
|
|
|
|
layout->SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
|
2020-04-18 20:24:25 +02:00
|
|
|
|
2020-04-19 16:01:46 +02:00
|
|
|
readElementText();
|
2020-04-23 14:48:56 +02:00
|
|
|
break;
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
2020-05-23 17:47:04 +02:00
|
|
|
case 3:// 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();
|
2020-04-23 14:48:56 +02:00
|
|
|
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();
|
2020-04-23 14:48:56 +02:00
|
|
|
break;
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:47:04 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPLayoutFileReader::ReadUnplacedPieces(VPLayout *layout)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
Q_UNUSED(layout); // to be removed when used
|
|
|
|
|
2020-05-01 18:26:02 +02:00
|
|
|
SCASSERT(isStartElement() && name() == ML::TagTiles);
|
2020-04-18 20:24:25 +02:00
|
|
|
|
2020-04-23 14:48:56 +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())
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
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
|
|
|
}
|
2020-04-23 14:48:56 +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 17:47:04 +02:00
|
|
|
void VPLayoutFileReader::ReadSheets(VPLayout *layout)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-05-23 17:47:04 +02:00
|
|
|
SCASSERT(isStartElement() && name() == ML::TagSheets);
|
2020-04-18 20:24:25 +02:00
|
|
|
|
2020-04-23 14:42:36 +02:00
|
|
|
while (readNextStartElement())
|
|
|
|
{
|
2020-05-23 17:47:04 +02:00
|
|
|
if (name() == ML::TagSheet)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-05-23 17:47:04 +02:00
|
|
|
ReadSheet(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-04-19 16:01:46 +02:00
|
|
|
skipCurrentElement();
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:47:04 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPLayoutFileReader::ReadSheet(VPLayout *layout)
|
|
|
|
{
|
|
|
|
Q_UNUSED(layout);
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
if (name() == ML::TagPiece)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-05-23 15:42:51 +02:00
|
|
|
VPPiece *piece = new VPPiece();
|
2020-04-18 20:24:25 +02:00
|
|
|
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 15:42:51 +02:00
|
|
|
void VPLayoutFileReader::ReadPiece(VPPiece *piece)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-04-19 10:38:28 +02:00
|
|
|
Q_UNUSED(piece);
|
2020-05-01 18:26:02 +02:00
|
|
|
SCASSERT(isStartElement() && name() == ML::TagPiece);
|
2020-04-18 20:24:25 +02:00
|
|
|
|
2020-04-26 14:12:20 +02:00
|
|
|
QXmlStreamAttributes attribs = attributes();
|
|
|
|
piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));
|
2020-05-01 19:08:48 +02:00
|
|
|
|
2020-04-26 14:12:20 +02:00
|
|
|
QString uuidStr = ReadAttributeString(attribs, ML::AttrID, QUuid().toString());// FIXME: is that correct to have a default value here?
|
2020-06-25 14:17:31 +02:00
|
|
|
piece->SetUUID(QUuid(uuidStr));
|
2020-04-26 14:12:20 +02:00
|
|
|
|
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);
|
2020-04-26 14:12:20 +02:00
|
|
|
// 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();
|
2020-04-23 16:22:58 +02:00
|
|
|
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();
|
2020-04-23 16:22:58 +02:00
|
|
|
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-04-23 16:22:58 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:01:03 +02:00
|
|
|
QString VPLayoutFileReader::ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
|
2020-04-23 16:22:58 +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));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:01:03 +02:00
|
|
|
QString VPLayoutFileReader::ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name)
|
2020-04-23 16:22:58 +02:00
|
|
|
{
|
|
|
|
return attribs.value(name).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:01:03 +02:00
|
|
|
bool VPLayoutFileReader::ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name,
|
2020-04-23 16:22:58 +02:00
|
|
|
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,
|
2020-04-23 16:22:58 +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);
|
|
|
|
if (ok == false)
|
|
|
|
{
|
|
|
|
throw VExceptionConversionError(message, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const VException &e)
|
|
|
|
{
|
|
|
|
VExceptionConversionError excep(message, name);
|
|
|
|
excep.AddMoreInformation(e.ErrorMessage());
|
|
|
|
throw excep;
|
|
|
|
}
|
|
|
|
return param;
|
|
|
|
}
|