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

1190 lines
40 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-05-23 14:01:03 +02:00
#include "vplayoutfilereader.h"
#include "../ifc/exception/vexception.h"
#include "../ifc/exception/vexceptionconversionerror.h"
2022-08-12 17:50:13 +02:00
#include "../layout/vplayout.h"
#include "../layout/vppiece.h"
2023-05-22 16:27:15 +02:00
#include "../layout/vpsheet.h"
#include "../vgeometry/vgeometrydef.h"
#include "../vgeometry/vlayoutplacelabel.h"
#include "../vlayout/vlayoutpiecepath.h"
#include "../vlayout/vtextmanager.h"
2023-07-17 15:33:57 +02:00
#include "../vmisc/compatibility.h"
2023-05-22 16:27:15 +02:00
#include "../vmisc/vcommonsettings.h"
#include "../vpatterndb/floatItemData/floatitemdef.h"
2023-07-17 15:33:57 +02:00
#include "../vwidgets/vpiecegrainline.h"
2023-05-22 16:27:15 +02:00
#include "vplayoutliterals.h"
#include <QFont>
#include <QXmlStreamAttributes>
2023-07-17 15:33:57 +02:00
// Header <ciso646> is removed in C++20.
#if __cplusplus <= 201703L
#include <ciso646> // and, not, or
#endif
2020-04-18 16:32:54 +02:00
using namespace Qt::Literals::StringLiterals;
2021-05-22 19:29:33 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
2022-08-12 17:50:13 +02:00
Q_LOGGING_CATEGORY(MLReader, "mlReader") // NOLINT
2021-05-22 19:29:33 +02:00
QT_WARNING_POP
2021-08-14 14:19:28 +02:00
namespace
{
//---------------------------------------------------------------------------------------------------------------------
auto StringToTransfrom(const QString &matrix) -> QTransform
{
if (QStringList const elements = matrix.split(ML::groupSep); elements.count() == 9)
2021-08-14 14:19:28 +02:00
{
2024-02-19 17:09:56 +01:00
qreal const m11 = elements.at(0).toDouble();
qreal const m12 = elements.at(1).toDouble();
qreal const m13 = elements.at(2).toDouble();
qreal const m21 = elements.at(3).toDouble();
qreal const m22 = elements.at(4).toDouble();
qreal const m23 = elements.at(5).toDouble();
qreal const m31 = elements.at(6).toDouble();
qreal const m32 = elements.at(7).toDouble();
qreal const m33 = elements.at(8).toDouble();
2021-08-14 14:19:28 +02:00
return {m11, m12, m13, m21, m22, m23, m31, m32, m33};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToPoint(const QString &point) -> QPointF
{
if (QStringList const coordinates = point.split(ML::coordintatesSep); coordinates.count() == 2)
2021-08-14 14:19:28 +02:00
{
return {coordinates.at(0).toDouble(), coordinates.at(1).toDouble()};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToPath(const QString &path) -> QVector<QPointF>
{
QVector<QPointF> p;
2022-11-21 15:48:29 +01:00
if (path.isEmpty())
{
return p;
}
2024-02-19 17:09:56 +01:00
QStringList const points = path.split(ML::pointsSep);
2022-08-12 17:50:13 +02:00
p.reserve(points.size());
2023-05-22 16:27:15 +02:00
for (const auto &point : points)
2021-08-14 14:19:28 +02:00
{
p.append(StringToPoint(point));
}
return p;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToGrainlineArrowDirrection(const QString &dirrection) -> GrainlineArrowDirection
{
2023-05-02 16:38:02 +02:00
const QStringList arrows{
ML::twoWaysUpDownStr, // 0
ML::oneWayUpStr, // 1
ML::oneWayDownStr, // 2
ML::fourWaysStr, // 3
ML::twoWaysUpLeftStr, // 4
ML::twoWaysUpRightStr, // 5
ML::twoWaysDownLeftStr, // 6
ML::twoWaysDownRightStr, // 7
ML::threeWaysUpDownLeftStr, // 8
ML::threeWaysUpDownRightStr, // 9
ML::threeWaysUpLeftRightStr, // 10
ML::threeWaysDownLeftRightStr, // 11
2021-08-14 14:19:28 +02:00
};
2023-05-02 16:38:02 +02:00
GrainlineArrowDirection arrowDirection = GrainlineArrowDirection::twoWaysUpDown;
2021-08-14 14:19:28 +02:00
switch (arrows.indexOf(dirrection))
{
2023-05-02 16:38:02 +02:00
case 1: // oneWayUp
arrowDirection = GrainlineArrowDirection::oneWayUp;
break;
case 2: // oneWayDown
arrowDirection = GrainlineArrowDirection::oneWayDown;
break;
case 3: // fourWays
arrowDirection = GrainlineArrowDirection::fourWays;
break;
case 4: // twoWaysUpLeft
arrowDirection = GrainlineArrowDirection::twoWaysUpLeft;
break;
case 5: // twoWaysUpRight
arrowDirection = GrainlineArrowDirection::twoWaysUpRight;
break;
case 6: // twoWaysDownLeft
arrowDirection = GrainlineArrowDirection::twoWaysDownLeft;
2021-08-14 14:19:28 +02:00
break;
2023-05-02 16:38:02 +02:00
case 7: // twoWaysDownRight
arrowDirection = GrainlineArrowDirection::twoWaysDownRight;
2021-08-14 14:19:28 +02:00
break;
2023-05-02 16:38:02 +02:00
case 8: // threeWaysUpDownLeft
arrowDirection = GrainlineArrowDirection::threeWaysUpDownLeft;
2023-04-07 09:05:29 +02:00
break;
2023-05-02 16:38:02 +02:00
case 9: // threeWaysUpDownRight
arrowDirection = GrainlineArrowDirection::threeWaysUpDownRight;
break;
case 10: // threeWaysUpLeftRight
arrowDirection = GrainlineArrowDirection::threeWaysUpLeftRight;
break;
case 11: // threeWaysDownLeftRight
arrowDirection = GrainlineArrowDirection::threeWaysDownLeftRight;
break;
case 0: // twoWaysUpDown
2021-08-14 14:19:28 +02:00
default:
2023-05-02 16:38:02 +02:00
arrowDirection = GrainlineArrowDirection::twoWaysUpDown;
2021-08-14 14:19:28 +02:00
break;
}
return arrowDirection;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToLine(const QString &string) -> QLineF
{
if (QStringList const points = string.split(ML::groupSep); points.count() == 2)
2021-08-14 14:19:28 +02:00
{
return {StringToPoint(points.at(0)), StringToPoint(points.at(1))};
}
return {};
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToLines(const QString &string) -> QVector<QLineF>
{
2024-02-19 17:09:56 +01:00
QStringList const lines = string.split(ML::itemsSep);
2021-08-14 14:19:28 +02:00
QVector<QLineF> path;
2022-08-12 17:50:13 +02:00
path.reserve(lines.size());
2021-08-14 14:19:28 +02:00
2023-05-22 16:27:15 +02:00
for (const auto &line : lines)
2021-08-14 14:19:28 +02:00
{
2024-02-19 17:09:56 +01:00
QLineF const l = StringToLine(line);
2021-08-14 14:19:28 +02:00
if (not l.isNull())
{
path.append(StringToLine(line));
}
}
return path;
}
//---------------------------------------------------------------------------------------------------------------------
auto StringToRect(const QString &string) -> QRectF
{
if (QStringList const points = string.split(ML::groupSep); points.count() == 4)
2021-08-14 14:19:28 +02:00
{
return {points.at(0).toDouble(), points.at(1).toDouble(), points.at(2).toDouble(), points.at(3).toDouble()};
}
return {};
}
2023-05-22 16:27:15 +02:00
} // namespace
2021-08-14 14:19:28 +02:00
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayoutFileReader::ReadFile(const VPLayoutPtr &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);
}
}
2023-05-22 16:27:15 +02:00
catch (const VException &e)
2021-05-22 19:29:33 +02:00
{
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
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadLayout(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagLayout);
2023-05-22 16:27:15 +02:00
const QStringList tags{
2021-07-29 16:11:18 +02:00
ML::TagProperties, // 0
ML::TagUnplacedPieces, // 1
2021-08-14 14:19:28 +02:00
ML::TagSheets // 2
2021-07-29 16:11:18 +02:00
};
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
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadProperties(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagProperties);
2023-05-22 16:27:15 +02:00
const QStringList tags{
2021-05-22 19:29:33 +02:00
ML::TagUnit, // 0
ML::TagTitle, // 1
ML::TagDescription, // 2
2021-09-01 08:20:58 +02:00
ML::TagControl, // 3
2021-09-06 15:56:56 +02:00
ML::TagTiles, // 4
2021-09-11 18:39:38 +02:00
ML::TagScale, // 5
ML::TagWatermark // 6
2021-05-22 19:29:33 +02:00
};
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
{
2023-05-22 16:27:15 +02:00
case 0: // unit
2021-05-22 19:29:33 +02:00
qDebug("read unit");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetUnit(StrToUnits(readElementText()));
2021-05-22 19:29:33 +02:00
break;
2023-05-22 16:27:15 +02:00
case 1: // title
2021-05-22 19:29:33 +02:00
qDebug("read title");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetTitle(readElementText());
2021-05-22 19:29:33 +02:00
break;
2023-05-22 16:27:15 +02:00
case 2: // description
2021-05-22 19:29:33 +02:00
qDebug("read description");
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetDescription(readElementText());
2021-05-22 19:29:33 +02:00
break;
2021-09-01 08:20:58 +02:00
case 3: // control
2021-05-22 19:29:33 +02:00
qDebug("read control");
ReadControl(layout);
break;
2021-09-01 08:20:58 +02:00
case 4: // tiles
2021-05-22 19:29:33 +02:00
qDebug("read tiles");
ReadTiles(layout);
break;
2021-09-06 15:56:56 +02:00
case 5: // scale
qDebug("read scale");
ReadScale(layout);
break;
2021-09-11 18:39:38 +02:00
case 6: // watermark
qDebug("read watermark");
ReadWatermark(layout);
break;
2021-05-22 19:29:33 +02:00
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
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadControl(const VPLayoutPtr &layout)
2021-05-22 19:29:33 +02:00
{
AssertRootTag(ML::TagControl);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetWarningSuperpositionOfPieces(
2023-05-22 16:27:15 +02:00
ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
2024-04-08 15:26:09 +02:00
layout->LayoutSettings().SetWarningPieceGapePosition(ReadAttributeBool(attribs, ML::AttrWarningPieceGape, trueStr));
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
2021-08-14 14:19:28 +02:00
layout->LayoutSettings().SetStickyEdges(ReadAttributeBool(attribs, ML::AttrStickyEdges, trueStr));
2021-08-30 17:45:27 +02:00
layout->LayoutSettings().SetPiecesGap(qMax(ReadAttributeDouble(attribs, ML::AttrPiecesGap, QChar('0')), 0.0));
2021-08-26 18:04:24 +02:00
layout->LayoutSettings().SetFollowGrainline(ReadAttributeBool(attribs, ML::AttrFollowGrainline, falseStr));
layout->LayoutSettings().SetBoundaryTogetherWithNotches(
ReadAttributeBool(attribs, ML::AttrBoundaryTogetherWithNotches, falseStr));
2024-01-06 13:20:56 +01:00
layout->LayoutSettings().SetCutOnFold(ReadAttributeBool(attribs, ML::AttrCutOnFold, falseStr));
2021-05-22 19:29:33 +02:00
readElementText();
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadUnplacedPieces(const VPLayoutPtr &layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagUnplacedPieces);
2021-07-29 16:11:18 +02:00
ReadPieces(layout);
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadTiles(const VPLayoutPtr &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
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetShowTiles(ReadAttributeBool(attribs, ML::AttrVisible, falseStr));
2021-09-13 16:27:46 +02:00
layout->LayoutSettings().SetPrintTilesScheme(ReadAttributeBool(attribs, ML::AttrPrintScheme, falseStr));
2022-02-10 13:07:05 +01:00
layout->LayoutSettings().SetShowTileNumber(ReadAttributeBool(attribs, ML::AttrTileNumber, falseStr));
2023-05-22 16:27:15 +02:00
// attribs.value(ML::AttrMatchingMarks); // TODO
2020-04-18 20:24:25 +02:00
2023-05-22 16:27:15 +02:00
const QStringList tags{
2021-05-22 19:29:33 +02:00
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
2021-07-29 16:11:18 +02:00
layout->LayoutSettings().SetTilesSize(ReadSize());
2021-05-22 19:29:33 +02:00
break;
case 1: // margin
2021-09-06 17:29:16 +02:00
ReadLayoutMargins(layout);
2021-05-22 19:29:33 +02:00
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
}
//---------------------------------------------------------------------------------------------------------------------
2021-09-06 15:56:56 +02:00
void VPLayoutFileReader::ReadScale(const VPLayoutPtr &layout)
{
AssertRootTag(ML::TagScale);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-06 15:56:56 +02:00
layout->LayoutSettings().SetHorizontalScale(ReadAttributeDouble(attribs, ML::AttrXScale, QChar('1')));
layout->LayoutSettings().SetVerticalScale(ReadAttributeDouble(attribs, ML::AttrYScale, QChar('1')));
readElementText();
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSheets(const VPLayoutPtr &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-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSheet(const VPLayoutPtr &layout)
{
2021-05-22 19:29:33 +02:00
AssertRootTag(ML::TagSheet);
2024-02-19 17:09:56 +01:00
VPSheetPtr const sheet(new VPSheet(layout));
2021-09-01 08:20:58 +02:00
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-01 08:20:58 +02:00
sheet->SetGrainlineType(StrToGrainlineType(ReadAttributeEmptyString(attribs, ML::AttrGrainlineType)));
2023-05-22 16:27:15 +02:00
const QStringList tags{
2021-05-22 19:29:33 +02:00
ML::TagName, // 0
2021-09-01 08:20:58 +02:00
ML::TagSize, // 1
ML::TagMargin, // 2
ML::TagPieces // 3
2021-05-22 19:29:33 +02:00
};
while (readNextStartElement())
{
switch (tags.indexOf(name().toString()))
{
case 0: // name
sheet->SetName(readElementText());
break;
2021-09-01 08:20:58 +02:00
case 1: // size
sheet->SetSheetSize(ReadSize());
break;
case 2: // margin
2021-09-06 17:29:16 +02:00
ReadSheetMargins(sheet);
2021-09-01 08:20:58 +02:00
break;
case 3: // pieces
2021-08-18 19:33:47 +02:00
ReadPieces(layout, sheet);
2021-05-22 19:29:33 +02:00
break;
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
}
}
readElementText();
2021-08-18 19:33:47 +02:00
layout->AddSheet(sheet);
}
2020-04-18 20:24:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadPieces(const VPLayoutPtr &layout, const VPSheetPtr &sheet)
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
{
2024-02-19 17:09:56 +01:00
VPPiecePtr const piece(new VPPiece());
2021-08-18 19:33:47 +02:00
ReadPiece(piece);
if (QString error; not piece->IsValid(error))
{
throw VException(tr("Piece %1 invalid. %2").arg(piece->GetName(), error));
}
2021-07-29 16:11:18 +02:00
piece->SetSheet(sheet);
2021-08-18 19:33:47 +02:00
VPLayout::AddPiece(layout, piece);
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-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadPiece(const VPPiecePtr &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
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));
2020-05-01 19:08:48 +02:00
2024-02-19 17:09:56 +01:00
QString const uuidStr = ReadAttributeString(attribs, ML::AttrUID, QUuid::createUuid().toString());
2020-06-25 14:17:31 +02:00
piece->SetUUID(QUuid(uuidStr));
2022-06-04 15:31:46 +02:00
piece->SetGradationId(ReadAttributeEmptyString(attribs, ML::AttrGradationLabel));
2021-08-31 12:08:59 +02:00
piece->SetCopyNumber(static_cast<quint16>(ReadAttributeUInt(attribs, ML::AttrCopyNumber, QChar('1'))));
2021-08-31 13:12:46 +02:00
piece->SetHideMainPath(not ReadAttributeBool(attribs, ML::AttrShowSeamline, trueStr));
piece->SetXScale(ReadAttributeDouble(attribs, ML::AttrXScale, QChar('1')));
piece->SetYScale(ReadAttributeDouble(attribs, ML::AttrYScale, QChar('1')));
2022-02-18 16:57:41 +01:00
piece->SetZValue(ReadAttributeDouble(attribs, ML::AttrZValue, QChar('1')));
2023-11-29 15:40:36 +01:00
piece->SetVerticallyFlipped(ReadAttributeBool(attribs, ML::AttrVerticallyFlipped, falseStr));
piece->SetHorizontallyFlipped(ReadAttributeBool(attribs, ML::AttrHorizontallyFlipped, falseStr));
piece->SetForbidFlipping(ReadAttributeBool(attribs, ML::AttrForbidFlipping, falseStr));
piece->SetForceFlipping(ReadAttributeBool(attribs, ML::AttrForceFlipping, falseStr));
2023-10-10 13:14:23 +02:00
piece->SetFollowGrainline(ReadAttributeBool(attribs, ML::AttrFollowGrainline, falseStr));
piece->SetSewLineOnDrawing(ReadAttributeBool(attribs, ML::AttrSewLineOnDrawing, falseStr));
piece->SetMatrix(StringToTransfrom(ReadAttributeEmptyString(attribs, ML::AttrTransform)));
2024-01-06 13:20:56 +01:00
piece->SetShowFullPiece(ReadAttributeBool(attribs, ML::AttrShowFullPiece, trueStr));
2021-08-14 14:19:28 +02:00
2023-05-22 16:27:15 +02:00
const QStringList tags{
2021-08-14 14:19:28 +02:00
ML::TagSeamLine, // 0
ML::TagSeamAllowance, // 1
ML::TagGrainline, // 2
ML::TagNotches, // 3
ML::TagInternalPaths, // 4
ML::TagMarkers, // 5
2024-01-06 13:20:56 +01:00
ML::TagLabels, // 6
ML::TagMirrorLine // 7
2021-08-14 14:19:28 +02:00
};
while (readNextStartElement())
{
switch (tags.indexOf(name().toString()))
{
case 0: // seam line
2022-10-28 15:16:02 +02:00
ReadSeamLine(piece);
2021-08-14 14:19:28 +02:00
break;
case 1: // seam allowance
ReadSeamAllowance(piece);
break;
case 2: // grainline
ReadGrainline(piece);
break;
case 3: // notches
ReadNotches(piece);
break;
case 4: // internal paths
ReadInternalPaths(piece);
break;
case 5: // markers
ReadMarkers(piece);
break;
case 6: // labels
ReadLabels(piece);
break;
2024-01-06 13:20:56 +01:00
case 7: // mirror line
ReadMirrorLines(piece);
break;
2021-08-14 14:19:28 +02:00
default:
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
break;
}
}
}
2022-10-28 15:16:02 +02:00
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VPLayoutFileReader::ReadLayoutPoint() -> VLayoutPoint
2022-10-28 15:16:02 +02:00
{
AssertRootTag(ML::TagPoint);
VLayoutPoint point;
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2022-10-28 15:16:02 +02:00
point.setX(ReadAttributeDouble(attribs, ML::AttrX, QChar('0')));
point.setY(ReadAttributeDouble(attribs, ML::AttrY, QChar('0')));
point.SetTurnPoint(ReadAttributeBool(attribs, ML::AttrTurnPoint, falseStr));
point.SetCurvePoint(ReadAttributeBool(attribs, ML::AttrCurvePoint, falseStr));
2022-11-21 15:48:29 +01:00
readElementText();
2022-10-28 15:16:02 +02:00
return point;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadLayoutPoints() -> QVector<VLayoutPoint>
{
QVector<VLayoutPoint> points;
while (readNextStartElement())
{
if (name() == ML::TagPoint)
{
points.append(ReadLayoutPoint());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
return points;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadSeamLine(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagSeamLine);
piece->SetContourPoints(ReadLayoutPoints(), piece->IsHideMainPath());
2022-10-28 15:16:02 +02:00
}
2021-08-14 14:19:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadSeamAllowance(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagSeamAllowance);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
bool const enabled = ReadAttributeBool(attribs, ML::AttrEnabled, falseStr);
2021-08-14 14:19:28 +02:00
piece->SetSeamAllowance(enabled);
2024-02-19 17:09:56 +01:00
bool const builtIn = ReadAttributeBool(attribs, ML::AttrBuiltIn, falseStr);
piece->SetSeamAllowanceBuiltIn(builtIn);
2021-08-14 14:19:28 +02:00
2024-02-19 17:09:56 +01:00
QVector<VLayoutPoint> const path = ReadLayoutPoints();
2022-11-21 15:48:29 +01:00
2022-10-28 15:16:02 +02:00
if (enabled && not builtIn)
2021-08-14 14:19:28 +02:00
{
2022-10-28 15:16:02 +02:00
if (path.isEmpty())
2021-08-14 14:19:28 +02:00
{
2022-10-28 15:16:02 +02:00
throw VException(tr("Error in line %1. Seam allowance is empty.").arg(lineNumber()));
2021-08-14 14:19:28 +02:00
}
2022-10-28 15:16:02 +02:00
piece->SetSeamAllowancePoints(path);
2021-08-14 14:19:28 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadGrainline(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagGrainline);
2023-05-02 16:38:02 +02:00
VPieceGrainline grainline;
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
bool const enabled = ReadAttributeBool(attribs, ML::AttrEnabled, falseStr);
2023-05-02 16:38:02 +02:00
grainline.SetEnabled(enabled);
2024-02-19 17:09:56 +01:00
QLineF const mainLine = StringToLine(readElementText());
2021-08-14 14:19:28 +02:00
if (enabled)
{
2024-02-19 17:09:56 +01:00
QString const arrowDirection = ReadAttributeEmptyString(attribs, ML::AttrArrowDirection);
2023-05-02 16:38:02 +02:00
grainline.SetArrowType(StringToGrainlineArrowDirrection(arrowDirection));
2021-08-14 14:19:28 +02:00
2023-05-02 16:38:02 +02:00
if (mainLine.isNull())
2021-09-04 14:57:31 +02:00
{
2023-05-02 16:38:02 +02:00
throw VException(tr("Error in line %1. Grainline main line is empty.").arg(lineNumber()));
2021-09-04 14:57:31 +02:00
}
2023-05-02 16:38:02 +02:00
grainline.SetMainLine(mainLine);
2021-08-14 14:19:28 +02:00
}
2023-05-02 16:38:02 +02:00
piece->SetVPGrainline(grainline);
2021-08-14 14:19:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadNotches(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagNotches);
QVector<VLayoutPassmark> passmarks;
2020-04-18 20:24:25 +02:00
2020-04-23 14:42:36 +02:00
while (readNextStartElement())
{
2021-08-14 14:19:28 +02:00
if (name() == ML::TagNotch)
2020-04-18 20:24:25 +02:00
{
2021-08-14 14:19:28 +02:00
passmarks.append(ReadNotch());
2020-04-18 20:24:25 +02:00
}
else
{
2021-08-14 14:19:28 +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-08-14 14:19:28 +02:00
piece->SetPassmarks(passmarks);
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadNotch() -> VLayoutPassmark
{
AssertRootTag(ML::TagNotch);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-08-14 14:19:28 +02:00
2021-09-27 10:47:09 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wnoexcept")
2021-08-14 14:19:28 +02:00
VLayoutPassmark passmark;
passmark.isBuiltIn = ReadAttributeBool(attribs, ML::AttrBuiltIn, falseStr);
passmark.baseLine = StringToLine(ReadAttributeEmptyString(attribs, ML::AttrBaseLine));
passmark.lines = StringToLines(ReadAttributeEmptyString(attribs, ML::AttrPath));
passmark.isClockwiseOpening = ReadAttributeBool(attribs, ML::AttrClockwiseOpening, falseStr);
2021-08-14 14:19:28 +02:00
2024-02-19 17:09:56 +01:00
QString const defaultType = QString::number(static_cast<int>(PassmarkLineType::OneLine));
2021-08-14 14:19:28 +02:00
passmark.type = static_cast<PassmarkLineType>(ReadAttributeUInt(attribs, ML::AttrType, defaultType));
2021-09-27 10:47:09 +02:00
QT_WARNING_POP
2021-08-14 14:19:28 +02:00
readElementText();
return passmark;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadInternalPaths(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagInternalPaths);
QVector<VLayoutPiecePath> internalPaths;
while (readNextStartElement())
{
if (name() == ML::TagInternalPath)
{
internalPaths.append(ReadInternalPath());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
piece->SetInternalPaths(internalPaths);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadInternalPath() -> VLayoutPiecePath
{
AssertRootTag(ML::TagInternalPath);
VLayoutPiecePath path;
2024-01-06 13:20:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-08-14 14:19:28 +02:00
path.SetCutPath(ReadAttributeBool(attribs, ML::AttrCut, falseStr));
path.SetPenStyle(LineStyleToPenStyle(ReadAttributeString(attribs, ML::AttrPenStyle, TypeLineLine)));
2024-01-06 13:20:56 +01:00
path.SetNotMirrored(ReadAttributeBool(attribs, ML::AttrNotMirrored, falseStr));
2021-09-04 14:57:31 +02:00
2024-01-06 13:20:56 +01:00
QVector<VLayoutPoint> const shape = ReadLayoutPoints();
2021-09-04 14:57:31 +02:00
if (shape.isEmpty())
{
throw VException(tr("Error in line %1. Internal path shape is empty.").arg(lineNumber()));
}
path.SetPoints(shape);
2021-08-14 14:19:28 +02:00
return path;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadMarkers(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagMarkers);
QVector<VLayoutPlaceLabel> markers;
while (readNextStartElement())
{
if (name() == ML::TagMarker)
{
markers.append(ReadMarker());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
piece->SetPlaceLabels(markers);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadMarker() -> VLayoutPlaceLabel
{
AssertRootTag(ML::TagMarker);
2021-09-27 10:47:09 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wnoexcept")
2021-08-14 14:19:28 +02:00
VLayoutPlaceLabel marker;
2024-01-06 13:20:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-08-14 14:19:28 +02:00
2024-01-06 13:20:56 +01:00
QString const matrix = ReadAttributeEmptyString(attribs, ML::AttrTransform);
2022-10-28 15:16:02 +02:00
marker.SetRotationMatrix(StringToTransfrom(matrix));
2021-09-04 14:57:31 +02:00
2022-10-28 15:16:02 +02:00
marker.SetType(static_cast<PlaceLabelType>(ReadAttributeUInt(attribs, ML::AttrType, QChar('0'))));
marker.SetCenter(StringToPoint(ReadAttributeEmptyString(attribs, ML::AttrCenter)));
marker.SetBox(StringToRect(ReadAttributeEmptyString(attribs, ML::AttrBox)));
2024-01-06 13:20:56 +01:00
marker.SetNotMirrored(ReadAttributeBool(attribs, ML::AttrNotMirrored, falseStr));
2021-08-14 14:19:28 +02:00
2022-08-08 14:25:14 +02:00
// cppcheck-suppress unknownMacro
2021-09-27 10:47:09 +02:00
QT_WARNING_POP
2022-11-21 15:48:29 +01:00
readElementText();
2021-08-14 14:19:28 +02:00
return marker;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayoutFileReader::ReadLabels(const VPPiecePtr &piece)
2021-08-14 14:19:28 +02:00
{
AssertRootTag(ML::TagLabels);
while (readNextStartElement())
{
if (name() == ML::TagPieceLabel)
{
2021-09-04 15:01:26 +02:00
ReadPieceLabel(piece);
}
else if (name() == ML::TagPatternLabel)
{
ReadPatternLabel(piece);
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadPieceLabel(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagPieceLabel);
2021-08-14 14:19:28 +02:00
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-04 15:01:26 +02:00
piece->SetPieceLabelRect(StringToPath(ReadAttributeEmptyString(attribs, ML::AttrShape)));
while (readNextStartElement())
{
if (name() == ML::TagLines)
{
2021-08-14 14:19:28 +02:00
piece->SetPieceLabelData(ReadLabelLines());
}
2021-09-04 15:01:26 +02:00
else
2021-08-14 14:19:28 +02:00
{
2021-09-04 15:01:26 +02:00
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
2021-08-14 14:19:28 +02:00
2021-09-04 15:01:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadPatternLabel(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagPatternLabel);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-04 15:01:26 +02:00
piece->SetPatternLabelRect(StringToPath(ReadAttributeEmptyString(attribs, ML::AttrShape)));
while (readNextStartElement())
{
if (name() == ML::TagLines)
{
2021-08-14 14:19:28 +02:00
piece->SetPatternLabelData(ReadLabelLines());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadLabelLines() -> VTextManager
{
2021-09-04 15:01:26 +02:00
AssertRootTag(ML::TagLines);
2021-08-14 14:19:28 +02:00
VTextManager text;
QVector<TextLine> lines;
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
text.SetFont(FontFromString(ReadAttributeEmptyString(attribs, ML::AttrFont)));
2021-08-14 14:19:28 +02:00
if (QStringList const svgFontData = ReadAttributeEmptyString(attribs, ML::AttrSVGFont).split(',');
!svgFontData.isEmpty())
2023-06-22 17:30:43 +02:00
{
text.SetSVGFontFamily(svgFontData.constFirst());
2023-06-22 17:30:43 +02:00
if (svgFontData.size() > 1)
{
text.SetSVGFontPointSize(svgFontData.at(1).toInt());
}
}
2021-08-14 14:19:28 +02:00
while (readNextStartElement())
{
if (name() == ML::TagLine)
{
lines.append(ReadLabelLine());
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
text.SetAllSourceLines(lines);
return text;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadLabelLine() -> TextLine
{
AssertRootTag(ML::TagLine);
TextLine line;
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-08-14 14:19:28 +02:00
2023-05-22 16:30:18 +02:00
line.m_iFontSize =
ReadAttributeInt(attribs, ML::AttrFontSize, QString::number(VCommonSettings::MinPieceLabelFontPointSize()));
2021-08-14 14:19:28 +02:00
line.m_bold = ReadAttributeBool(attribs, ML::AttrBold, falseStr);
line.m_italic = ReadAttributeBool(attribs, ML::AttrItalic, falseStr);
2024-02-19 17:09:56 +01:00
int const alignment =
ReadAttributeInt(attribs, ML::AttrAlignment, QString::number(static_cast<int>(Qt::AlignCenter)));
2021-08-14 14:19:28 +02:00
line.m_eAlign = static_cast<Qt::Alignment>(alignment);
line.m_qsText = readElementText();
return line;
}
2021-09-11 18:39:38 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadWatermark(const VPLayoutPtr &layout)
{
AssertRootTag(ML::TagWatermark);
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-11 18:39:38 +02:00
layout->LayoutSettings().SetShowWatermark(ReadAttributeBool(attribs, ML::AttrShowPreview, falseStr));
layout->LayoutSettings().SetWatermarkPath(readElementText());
}
2024-01-06 13:20:56 +01:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadMirrorLines(const VPPiecePtr &piece)
{
AssertRootTag(ML::TagMirrorLine);
QXmlStreamAttributes const attribs = attributes();
piece->SetFoldLineType(StringToFoldLineType(
2024-02-27 14:38:59 +01:00
ReadAttributeString(attribs, ML::AttrFoldLineType, FoldLineTypeToString(FoldLineType::TwoArrowsTextAbove))));
2024-01-06 13:20:56 +01:00
piece->SetFoldLineHeight(ReadAttributeDouble(attribs, ML::AttrFoldLineHeight, QChar('0')));
piece->SetFoldLineWidth(ReadAttributeDouble(attribs, ML::AttrFoldLineWidth, QChar('0')));
piece->SetFoldLineCenterPosition(ReadAttributeDouble(attribs, ML::AttrFoldLineCenter, QString::number(0.5)));
piece->SetFoldLineLabelFontBold(ReadAttributeBool(attribs, ML::AttrBold, falseStr));
piece->SetFoldLineLabelFontItalic(ReadAttributeBool(attribs, ML::AttrItalic, falseStr));
piece->SetFoldLineLabelAlignment(
ReadAttributeInt(attribs, ML::AttrAlignment, QString::number(static_cast<int>(Qt::AlignHCenter))));
piece->SetFoldLineLabel(ReadAttributeEmptyString(attribs, ML::AttrFoldLineLabel));
piece->SetFoldLineOutlineFont(FontFromString(ReadAttributeEmptyString(attribs, ML::AttrFont)));
piece->SetShowMirrorLine(ReadAttributeBool(attribs, ML::AttrVisible, trueStr));
2024-01-06 13:20:56 +01:00
if (QStringList const svgFontData = ReadAttributeEmptyString(attribs, ML::AttrSVGFont).split(','_L1);
!svgFontData.isEmpty())
2024-01-06 13:20:56 +01:00
{
piece->SetFoldLineSVGFontFamily(svgFontData.constFirst());
if (svgFontData.size() > 1)
{
piece->SetFoldLineSvgFontSize(svgFontData.at(1).toUInt());
}
}
while (readNextStartElement())
{
if (name() == ML::TagSeamLine)
{
piece->SetSeamMirrorLine(StringToLine(readElementText()));
}
else if (name() == ML::TagSeamAllowance)
{
piece->SetSeamAllowanceMirrorLine(StringToLine(readElementText()));
}
else
{
qCDebug(MLReader, "Ignoring tag %s", qUtf8Printable(name().toString()));
skipCurrentElement();
}
}
}
2021-08-14 14:19:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadLayoutMargins(const VPLayoutPtr &layout)
2020-04-18 20:24:25 +02:00
{
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-06 17:29:16 +02:00
2020-04-18 20:24:25 +02:00
QMarginsF margins = QMarginsF();
2021-09-06 17:29:16 +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')));
layout->LayoutSettings().SetTilesMargins(margins);
layout->LayoutSettings().SetIgnoreTilesMargins(ReadAttributeBool(attribs, ML::AttrIgnoreMargins, falseStr));
readElementText();
}
2020-04-18 20:24:25 +02:00
2021-09-06 17:29:16 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayoutFileReader::ReadSheetMargins(const VPSheetPtr &sheet)
2021-09-06 17:29:16 +02:00
{
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const attribs = attributes();
2021-09-06 17:29:16 +02:00
QMarginsF margins = QMarginsF();
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')));
2021-09-06 17:29:16 +02:00
sheet->SetSheetMargins(margins);
2020-04-18 20:24:25 +02:00
2021-09-06 17:29:16 +02:00
sheet->SetIgnoreMargins(ReadAttributeBool(attribs, ML::AttrIgnoreMargins, falseStr));
2021-05-22 19:29:33 +02:00
2021-09-06 17:29:16 +02:00
readElementText();
2020-04-18 20:24:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadSize() -> QSizeF
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
2024-02-19 17:09:56 +01:00
QXmlStreamAttributes const 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
{
2023-05-22 16:27:15 +02:00
if (not(isStartElement() && name() == tag))
2021-05-22 19:29:33 +02:00
{
2021-06-04 09:00:14 +02:00
throw VException(tr("Unexpected tag %1 in line %2").arg(name().toString()).arg(lineNumber()));
2021-05-22 19:29:33 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
2023-05-22 16:27:15 +02:00
const QString &defValue) -> QString
{
2024-02-19 17:20:46 +01:00
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;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name) -> QString
{
return attribs.value(name).toString();
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> bool
{
QString parametr;
bool val = true;
const QString message = QObject::tr("Can't convert toBool parameter");
try
{
parametr = ReadAttributeString(attribs, name, defValue);
2023-05-22 16:27:15 +02:00
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;
2023-05-22 16:27:15 +02:00
default: // others
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
2022-08-12 17:50:13 +02:00
throw excep; // NOLINT(cert-err09-cpp)
}
return val;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-14 14:19:28 +02:00
auto VPLayoutFileReader::ReadAttributeDouble(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> qreal
{
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(','_L1, '.'_L1).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());
2022-08-12 17:50:13 +02:00
throw excep; // NOLINT(cert-err09-cpp)
}
return param;
}
2021-08-14 14:19:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadAttributeUInt(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> quint32
{
bool ok = false;
quint32 param = 0;
const QString message = QObject::tr("Can't convert toUInt parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(','_L1, '.'_L1).toUInt(&ok);
2021-08-14 14:19:28 +02:00
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
2022-08-12 17:50:13 +02:00
throw excep; // NOLINT(cert-err09-cpp)
2021-08-14 14:19:28 +02:00
}
return param;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPLayoutFileReader::ReadAttributeInt(const QXmlStreamAttributes &attribs, const QString &name,
const QString &defValue) -> int
{
bool ok = false;
int param = 0;
const QString message = QObject::tr("Can't convert toInt parameter");
try
{
QString parametr = ReadAttributeString(attribs, name, defValue);
param = parametr.replace(','_L1, '.'_L1).toInt(&ok);
2021-08-14 14:19:28 +02:00
if (not ok)
{
throw VExceptionConversionError(message, name);
}
}
catch (const VException &e)
{
VExceptionConversionError excep(message, name);
excep.AddMoreInformation(e.ErrorMessage());
2022-08-12 17:50:13 +02:00
throw excep; // NOLINT(cert-err09-cpp)
2021-08-14 14:19:28 +02:00
}
return param;
}