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

263 lines
8.2 KiB
C++
Raw Normal View History

2020-04-18 16:32:54 +02:00
/************************************************************************
**
** @file vpuzzlelayoutfilereader.cpp
** @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-04-18 16:32:54 +02:00
#include "vpuzzlelayoutfilereader.h"
VPuzzleLayoutFileReader::VPuzzleLayoutFileReader()
{
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPuzzleLayoutFileReader::~VPuzzleLayoutFileReader()
{
// TODO
}
//---------------------------------------------------------------------------------------------------------------------
bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file)
{
setDevice(file);
if (readNextStartElement())
{
// TODO extend the handling
// if it doesn't start with layout, error
// if it starts with version > than current version, error
if (name() == QString("layout")
&& attributes().value(QString("version")) == QLatin1String("1.0.0"))
{
ReadLayout(layout);
}
else
{
raiseError(QObject::tr("The file is not a layout version 1.0.0 file."));
}
}
return !error();
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("layout"));
while (readNextStartElement()) {
if (name() == QString("properties"))
{
ReadProperties(layout);
}
else if (name() == QString("layers"))
{
ReadLayers(layout);
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("properties"));
while (readNextStartElement()) {
if (name() == QString("unit"))
{
QString unit = readElementText();
// TODO read unit infos
}
else if (name() == QString("description"))
{
QString description = readElementText();
// TODO read the description info
}
else if (name() == QString("size"))
{
QSizeF size = ReadSize();
layout->SetLayoutSize(size);
}
else if (name() == QString("margin"))
{
QMarginsF margins = ReadMargins();
layout->SetLayoutMargins(margins);
}
else if (name() == QString("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->SetPiecesGap(attribs.value("piecesGap").toDouble());
}
else if (name() == QString("tiles"))
{
ReadTiles(layout);
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout)
{
Q_UNUSED(layout); // to be removed when used
Q_ASSERT(isStartElement() && name() == QString("tiles"));
QXmlStreamAttributes attribs = attributes();
// attribs.value("visible"); // TODO
// attribs.value("matchingMarks"); // TODO
while (readNextStartElement()) {
if (name() == QString("size"))
{
QSizeF size = ReadSize();
// TODO set layout tiled size
Q_UNUSED(size);
}
else if (name() == QString("margin"))
{
QMarginsF margins = ReadMargins();
// TODO set layout tiled margins
Q_UNUSED(margins);
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout)
{
Q_ASSERT(isStartElement() && name() == QString("layers"));
while (readNextStartElement()) {
if (name() == QString("unplacedPiecesLayer"))
{
ReadLayer(layout->GetUnplacedPiecesLayer());
}
else if (name() == QString("layer"))
{
VPuzzleLayer *layer = layout->AddLayer();
ReadLayer(layer);
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer)
{
Q_ASSERT(isStartElement() && name() == QString("layer"));
QXmlStreamAttributes attribs = attributes();
layer->SetName(attribs.value("name").toString());
layer->SetIsVisible(attribs.value("visible") == "true");
while (readNextStartElement()) {
if (name() == QString("piece"))
{
VPuzzlePiece *piece = new VPuzzlePiece();
ReadPiece(piece);
layer->AddPiece(piece);
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece)
{
Q_ASSERT(isStartElement() && name() == QString("piece"));
// TODO read the attributes
while (readNextStartElement()) {
if (name() == QString("..."))
{
// TODO
}
else
{
// TODO error handling, we encountered a tag that isn't defined in the specification
}
}
}
//---------------------------------------------------------------------------------------------------------------------
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());
return margins;
}
//---------------------------------------------------------------------------------------------------------------------
QSizeF VPuzzleLayoutFileReader::ReadSize()
{
QSizeF size = QSize();
QXmlStreamAttributes attribs = attributes();
size.setWidth(attribs.value("width").toDouble());
size.setHeight(attribs.value("height").toDouble());
return size;
}