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"
|
2020-04-19 10:38:28 +02:00
|
|
|
#include "vpuzzlelayoutfilewriter.h"
|
2020-04-23 14:48:56 +02:00
|
|
|
#include "layoutliterals.h"
|
2020-04-18 16:32:54 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-23 14:48:56 +02:00
|
|
|
if (name() == ML::TagLayout)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
QString versionStr = attributes().value(ML::AttrVersion).toString();
|
2020-04-19 10:38:28 +02:00
|
|
|
QStringList versionStrParts = versionStr.split('.');
|
|
|
|
m_layoutFormatVersion = FORMAT_VERSION(versionStrParts.at(0).toInt(),versionStrParts.at(1).toInt(),versionStrParts.at(2).toInt());
|
|
|
|
|
|
|
|
if(VPuzzleLayoutFileWriter::LayoutFileFormatVersion >= m_layoutFormatVersion)
|
|
|
|
{
|
|
|
|
ReadLayout(layout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO better error handling
|
|
|
|
raiseError(QObject::tr("You're trying to open a layout that was created with a newer version of puzzle"));
|
|
|
|
}
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-19 10:38:28 +02:00
|
|
|
raiseError(QObject::tr("Wrong file structure"));
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !error();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout)
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(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-04-23 14:48:56 +02:00
|
|
|
else if (name() == ML::TagLayers)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
|
|
|
ReadLayers(layout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO error handling, we encountered a tag that isn't defined in the specification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout)
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(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::TagSize,
|
|
|
|
ML::TagMargin,
|
|
|
|
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-04-23 14:48:56 +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();
|
2020-04-23 14:48:56 +02:00
|
|
|
break;
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
2020-04-23 14:48:56 +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();
|
2020-04-23 14:48:56 +02:00
|
|
|
break;
|
2020-04-18 20:24:25 +02:00
|
|
|
}
|
2020-04-23 14:48:56 +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
|
|
|
|
|
2020-04-23 14:48:56 +02:00
|
|
|
layout->SetWarningSuperpositionOfPieces(attribs.value(ML::AttrWarningSuperposition) == "true");
|
|
|
|
layout->SetWarningPiecesOutOfBound(attribs.value(ML::AttrWarningOutOfBound) == "true");
|
|
|
|
layout->SetStickyEdges(attribs.value(ML::AttrStickyEdges) == "true");
|
2020-04-18 20:24:25 +02:00
|
|
|
|
2020-04-23 14:48:56 +02:00
|
|
|
layout->SetPiecesGap(attribs.value(ML::AttrPiecesGap).toDouble());
|
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-04-23 14:48:56 +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();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout)
|
|
|
|
{
|
|
|
|
Q_UNUSED(layout); // to be removed when used
|
|
|
|
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout)
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(isStartElement() && name() == ML::TagLayers);
|
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::TagUnplacedPiecesLayer)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
|
|
|
ReadLayer(layout->GetUnplacedPiecesLayer());
|
|
|
|
}
|
2020-04-23 14:48:56 +02:00
|
|
|
else if (name() == ML::TagLayer)
|
2020-04-18 20:24:25 +02:00
|
|
|
{
|
|
|
|
VPuzzleLayer *layer = layout->AddLayer();
|
|
|
|
ReadLayer(layer);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer)
|
|
|
|
{
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(isStartElement() && name() == ML::TagLayer);
|
2020-04-18 20:24:25 +02:00
|
|
|
|
|
|
|
QXmlStreamAttributes attribs = attributes();
|
2020-04-23 14:48:56 +02:00
|
|
|
layer->SetName(attribs.value(ML::AttrName).toString());
|
|
|
|
layer->SetIsVisible(attribs.value(ML::AttrVisible) == "true");
|
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
|
|
|
{
|
|
|
|
VPuzzlePiece *piece = new VPuzzlePiece();
|
|
|
|
ReadPiece(piece);
|
|
|
|
layer->AddPiece(piece);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece)
|
|
|
|
{
|
2020-04-19 10:38:28 +02:00
|
|
|
Q_UNUSED(piece);
|
2020-04-23 14:48:56 +02:00
|
|
|
Q_ASSERT(isStartElement() && name() == ML::TagPiece);
|
2020-04-18 20:24:25 +02:00
|
|
|
|
|
|
|
// TODO read the attributes
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QMarginsF VPuzzleLayoutFileReader::ReadMargins()
|
|
|
|
{
|
|
|
|
QMarginsF margins = QMarginsF();
|
|
|
|
|
|
|
|
QXmlStreamAttributes attribs = attributes();
|
2020-04-23 14:48:56 +02:00
|
|
|
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());
|
2020-04-18 20:24:25 +02:00
|
|
|
|
|
|
|
return margins;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QSizeF VPuzzleLayoutFileReader::ReadSize()
|
|
|
|
{
|
|
|
|
QSizeF size = QSize();
|
|
|
|
|
|
|
|
QXmlStreamAttributes attribs = attributes();
|
2020-04-23 14:48:56 +02:00
|
|
|
size.setWidth(attribs.value(ML::AttrWidth).toDouble());
|
|
|
|
size.setHeight(attribs.value(ML::AttrLength).toDouble());
|
2020-04-18 20:24:25 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|