Specialized functions to read tag attributes.
Provide easier conversion, return default value if an attribute is not provided, throw exception in case of error.
This commit is contained in:
parent
dcfb00aec8
commit
b2f26f02bc
|
@ -30,7 +30,10 @@
|
||||||
#include "vpuzzlelayoutfilereader.h"
|
#include "vpuzzlelayoutfilereader.h"
|
||||||
#include "vpuzzlelayoutfilewriter.h"
|
#include "vpuzzlelayoutfilewriter.h"
|
||||||
#include "layoutliterals.h"
|
#include "layoutliterals.h"
|
||||||
|
#include "../ifc/exception/vexception.h"
|
||||||
|
#include "../ifc/exception/vexceptionconversionerror.h"
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
VPuzzleLayoutFileReader::VPuzzleLayoutFileReader()
|
VPuzzleLayoutFileReader::VPuzzleLayoutFileReader()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -57,7 +60,7 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file)
|
||||||
|
|
||||||
if (name() == ML::TagLayout)
|
if (name() == ML::TagLayout)
|
||||||
{
|
{
|
||||||
QString versionStr = attributes().value(ML::AttrVersion).toString();
|
QString versionStr = ReadAttributeEmptyString(attributes(), ML::AttrVersion);
|
||||||
QStringList versionStrParts = versionStr.split('.');
|
QStringList versionStrParts = versionStr.split('.');
|
||||||
m_layoutFormatVersion = FORMAT_VERSION(versionStrParts.at(0).toInt(),versionStrParts.at(1).toInt(),versionStrParts.at(2).toInt());
|
m_layoutFormatVersion = FORMAT_VERSION(versionStrParts.at(0).toInt(),versionStrParts.at(1).toInt(),versionStrParts.at(2).toInt());
|
||||||
|
|
||||||
|
@ -157,11 +160,11 @@ void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout)
|
||||||
|
|
||||||
// attribs.value("followGrainLine"); // TODO
|
// attribs.value("followGrainLine"); // TODO
|
||||||
|
|
||||||
layout->SetWarningSuperpositionOfPieces(attribs.value(ML::AttrWarningSuperposition) == "true");
|
layout->SetWarningSuperpositionOfPieces(ReadAttributeBool(attribs, ML::AttrWarningSuperposition, trueStr));
|
||||||
layout->SetWarningPiecesOutOfBound(attribs.value(ML::AttrWarningOutOfBound) == "true");
|
layout->SetWarningPiecesOutOfBound(ReadAttributeBool(attribs, ML::AttrWarningOutOfBound, trueStr));
|
||||||
layout->SetStickyEdges(attribs.value(ML::AttrStickyEdges) == "true");
|
layout->SetStickyEdges(ReadAttributeBool(attribs, ML::AttrStickyEdges, trueStr));
|
||||||
|
|
||||||
layout->SetPiecesGap(attribs.value(ML::AttrPiecesGap).toDouble());
|
layout->SetPiecesGap(ReadAttributeDouble(attribs, ML::AttrPiecesGap, QChar('0')));
|
||||||
readElementText();
|
readElementText();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -243,8 +246,8 @@ void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer)
|
||||||
Q_ASSERT(isStartElement() && name() == ML::TagLayer);
|
Q_ASSERT(isStartElement() && name() == ML::TagLayer);
|
||||||
|
|
||||||
QXmlStreamAttributes attribs = attributes();
|
QXmlStreamAttributes attribs = attributes();
|
||||||
layer->SetName(attribs.value(ML::AttrName).toString());
|
layer->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Layer")));
|
||||||
layer->SetIsVisible(attribs.value(ML::AttrVisible) == "true");
|
layer->SetIsVisible(ReadAttributeBool(attribs, ML::AttrVisible, trueStr));
|
||||||
|
|
||||||
while (readNextStartElement())
|
while (readNextStartElement())
|
||||||
{
|
{
|
||||||
|
@ -292,10 +295,10 @@ QMarginsF VPuzzleLayoutFileReader::ReadMargins()
|
||||||
QMarginsF margins = QMarginsF();
|
QMarginsF margins = QMarginsF();
|
||||||
|
|
||||||
QXmlStreamAttributes attribs = attributes();
|
QXmlStreamAttributes attribs = attributes();
|
||||||
margins.setLeft(attribs.value(ML::AttrLeft).toDouble());
|
margins.setLeft(ReadAttributeDouble(attribs, ML::AttrLeft, QChar('0')));
|
||||||
margins.setTop(attribs.value(ML::AttrTop).toDouble());
|
margins.setTop(ReadAttributeDouble(attribs, ML::AttrTop, QChar('0')));
|
||||||
margins.setRight(attribs.value(ML::AttrRight).toDouble());
|
margins.setRight(ReadAttributeDouble(attribs, ML::AttrRight, QChar('0')));
|
||||||
margins.setBottom(attribs.value(ML::AttrBottom).toDouble());
|
margins.setBottom(ReadAttributeDouble(attribs, ML::AttrBottom, QChar('0')));
|
||||||
|
|
||||||
return margins;
|
return margins;
|
||||||
}
|
}
|
||||||
|
@ -306,8 +309,96 @@ QSizeF VPuzzleLayoutFileReader::ReadSize()
|
||||||
QSizeF size = QSize();
|
QSizeF size = QSize();
|
||||||
|
|
||||||
QXmlStreamAttributes attribs = attributes();
|
QXmlStreamAttributes attribs = attributes();
|
||||||
size.setWidth(attribs.value(ML::AttrWidth).toDouble());
|
size.setWidth(ReadAttributeDouble(attribs, ML::AttrWidth, QChar('0')));
|
||||||
size.setHeight(attribs.value(ML::AttrLength).toDouble());
|
size.setHeight(ReadAttributeDouble(attribs, ML::AttrLength, QChar('0')));
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
QString VPuzzleLayoutFileReader::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
QString VPuzzleLayoutFileReader::ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name)
|
||||||
|
{
|
||||||
|
return attribs.value(name).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
bool VPuzzleLayoutFileReader::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
qreal VPuzzleLayoutFileReader::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;
|
||||||
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
class VPuzzleLayoutFileReader : public QXmlStreamReader
|
class VPuzzleLayoutFileReader : public QXmlStreamReader
|
||||||
{
|
{
|
||||||
|
Q_DECLARE_TR_FUNCTIONS(VPuzzleLayoutFileReader)
|
||||||
public:
|
public:
|
||||||
VPuzzleLayoutFileReader();
|
VPuzzleLayoutFileReader();
|
||||||
~VPuzzleLayoutFileReader();
|
~VPuzzleLayoutFileReader();
|
||||||
|
@ -60,6 +61,12 @@ private:
|
||||||
QMarginsF ReadMargins();
|
QMarginsF ReadMargins();
|
||||||
QSizeF ReadSize();
|
QSizeF ReadSize();
|
||||||
|
|
||||||
|
static QString ReadAttributeString(const QXmlStreamAttributes &attribs, const QString &name,
|
||||||
|
const QString &defValue);
|
||||||
|
static QString ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name);
|
||||||
|
static bool ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name, const QString &defValue);
|
||||||
|
static qreal ReadAttributeDouble(const QXmlStreamAttributes &attribs, const QString &name,
|
||||||
|
const QString &defValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VPUZZLELAYOUTFILEREADER_H
|
#endif // VPUZZLELAYOUTFILEREADER_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user