Fix the layout format conversion.

This commit is contained in:
Roman Telezhynskyi 2022-02-18 16:32:43 +02:00
parent 499ec96691
commit ed910db2c3
11 changed files with 67 additions and 52 deletions

View File

@ -438,7 +438,27 @@ auto VPMainWindow::LoadFile(QString path) -> bool
VLayoutConverter converter(path); VLayoutConverter converter(path);
m_curFileFormatVersion = converter.GetCurrentFormatVersion(); m_curFileFormatVersion = converter.GetCurrentFormatVersion();
m_curFileFormatVersionStr = converter.GetFormatVersionStr(); m_curFileFormatVersionStr = converter.GetFormatVersionStr();
path = converter.Convert();
QFile file(converter.Convert());
file.open(QIODevice::ReadOnly);
VPLayoutFileReader fileReader;
m_layout->Clear();
fileReader.ReadFile(m_layout, &file);
if (fileReader.hasError())
{
qCCritical(pWindow, "%s\n\n%s", qUtf8Printable(tr("File error.")),
qUtf8Printable(tr("Unable to read a layout file. %1").arg(fileReader.errorString())));
lock.reset();
if (m_cmd->IsTestModeEnabled())
{
qApp->exit(V_EX_NOINPUT);
}
return false;
}
} }
catch (VException &e) catch (VException &e)
{ {
@ -448,27 +468,6 @@ auto VPMainWindow::LoadFile(QString path) -> bool
return false; return false;
} }
QFile file(path);
file.open(QIODevice::ReadOnly);
VPLayoutFileReader fileReader;
m_layout->Clear();
fileReader.ReadFile(m_layout, &file);
if (fileReader.hasError())
{
qCCritical(pWindow, "%s\n\n%s", qUtf8Printable(tr("File error.")),
qUtf8Printable(tr("Unable to read a layout file. %1").arg(fileReader.errorString())));
lock.reset();
if (m_cmd->IsTestModeEnabled())
{
qApp->exit(V_EX_NOINPUT);
}
return false;
}
SetCurrentFile(path); SetCurrentFile(path);
m_layout->SetFocusedSheet(); m_layout->SetFocusedSheet();

View File

@ -61,7 +61,6 @@ const QString TagLine = QStringLiteral("line");
const QString TagScale = QStringLiteral("scale"); const QString TagScale = QStringLiteral("scale");
const QString TagWatermark = QStringLiteral("watermark"); const QString TagWatermark = QStringLiteral("watermark");
const QString AttrVersion = QStringLiteral("version");
const QString AttrWarningSuperposition = QStringLiteral("warningSuperposition"); const QString AttrWarningSuperposition = QStringLiteral("warningSuperposition");
const QString AttrWarningOutOfBound = QStringLiteral("warningOutOfBound"); const QString AttrWarningOutOfBound = QStringLiteral("warningOutOfBound");
const QString AttrStickyEdges = QStringLiteral("stickyEdges"); const QString AttrStickyEdges = QStringLiteral("stickyEdges");

View File

@ -66,7 +66,6 @@ extern const QString TagLine;
extern const QString TagScale; extern const QString TagScale;
extern const QString TagWatermark; extern const QString TagWatermark;
extern const QString AttrVersion;
extern const QString AttrWarningSuperposition; extern const QString AttrWarningSuperposition;
extern const QString AttrWarningOutOfBound; extern const QString AttrWarningOutOfBound;
extern const QString AttrStickyEdges; extern const QString AttrStickyEdges;

View File

@ -143,6 +143,7 @@ const QString AttrCurve1Alias1 = QStringLiteral("curve1Alias1");
const QString AttrCurve1Alias2 = QStringLiteral("curve1Alias2"); const QString AttrCurve1Alias2 = QStringLiteral("curve1Alias2");
const QString AttrCurve2Alias1 = QStringLiteral("curve2Alias1"); const QString AttrCurve2Alias1 = QStringLiteral("curve2Alias1");
const QString AttrCurve2Alias2 = QStringLiteral("curve2Alias2"); const QString AttrCurve2Alias2 = QStringLiteral("curve2Alias2");
const QString AttrLayoutVersion = QStringLiteral("version");
const QString TypeLineDefault = QStringLiteral("default"); const QString TypeLineDefault = QStringLiteral("default");
const QString TypeLineNone = QStringLiteral("none"); const QString TypeLineNone = QStringLiteral("none");

View File

@ -162,6 +162,7 @@ extern const QString AttrCurve1Alias1;
extern const QString AttrCurve1Alias2; extern const QString AttrCurve1Alias2;
extern const QString AttrCurve2Alias1; extern const QString AttrCurve2Alias1;
extern const QString AttrCurve2Alias2; extern const QString AttrCurve2Alias2;
extern const QString AttrLayoutVersion;
extern const QString TypeLineDefault; extern const QString TypeLineDefault;
extern const QString TypeLineNone; extern const QString TypeLineNone;

View File

@ -50,7 +50,6 @@
#include <QXmlSchemaValidator> #include <QXmlSchemaValidator>
#include "../exception/vexception.h" #include "../exception/vexception.h"
#include "../exception/vexceptionwrongid.h"
#include "vdomdocument.h" #include "vdomdocument.h"
//This class need for validation pattern file using XSD shema //This class need for validation pattern file using XSD shema
@ -339,17 +338,6 @@ void VAbstractConverter::ValidateInputFile(const QString &currentSchema) const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::Save() void VAbstractConverter::Save()
{ {
try
{
TestUniqueId();
}
catch (const VExceptionWrongId &e)
{
Q_UNUSED(e)
VException ex(tr("Error no unique id."));
throw ex;
}
m_tmpFile.resize(0);//clear previous content m_tmpFile.resize(0);//clear previous content
const int indent = 4; const int indent = 4;
QTextStream out(&m_tmpFile); QTextStream out(&m_tmpFile);
@ -358,8 +346,7 @@ void VAbstractConverter::Save()
if (not m_tmpFile.flush()) if (not m_tmpFile.flush())
{ {
VException e(m_tmpFile.errorString()); throw VException(m_tmpFile.errorString());
throw e;
} }
} }

View File

@ -44,7 +44,6 @@ QT_WARNING_DISABLE_GCC("-Wsuggest-final-methods")
class VAbstractConverter :public VDomDocument class VAbstractConverter :public VDomDocument
{ {
Q_DECLARE_TR_FUNCTIONS(VAbstractConverter)
public: public:
explicit VAbstractConverter(const QString &fileName); explicit VAbstractConverter(const QString &fileName);
virtual ~VAbstractConverter() Q_DECL_EQ_DEFAULT; virtual ~VAbstractConverter() Q_DECL_EQ_DEFAULT;
@ -60,8 +59,8 @@ protected:
void ValidateInputFile(const QString &currentSchema) const; void ValidateInputFile(const QString &currentSchema) const;
Q_NORETURN void InvalidVersion(unsigned ver) const; Q_NORETURN void InvalidVersion(unsigned ver) const;
void Save(); virtual void Save();
void SetVersion(const QString &version); virtual void SetVersion(const QString &version);
virtual unsigned MinVer() const =0; virtual unsigned MinVer() const =0;
virtual unsigned MaxVer() const =0; virtual unsigned MaxVer() const =0;

View File

@ -26,6 +26,8 @@
** **
*************************************************************************/ *************************************************************************/
#include "vlayoutconverter.h" #include "vlayoutconverter.h"
#include "../exception/vexception.h"
#include "ifcdef.h"
/* /*
* Version rules: * Version rules:
@ -42,18 +44,11 @@ const QString VLayoutConverter::CurrentSchema = QStringLiteral("://schema/layo
//VLayoutConverter::LayoutMinVer; // <== DON'T FORGET TO UPDATE TOO!!!! //VLayoutConverter::LayoutMinVer; // <== DON'T FORGET TO UPDATE TOO!!!!
//VLayoutConverter::LayoutMaxVer; // <== DON'T FORGET TO UPDATE TOO!!!! //VLayoutConverter::LayoutMaxVer; // <== DON'T FORGET TO UPDATE TOO!!!!
namespace
{
// The list of all string we use for conversion
// Better to use global variables because repeating QStringLiteral blows up code size
Q_GLOBAL_STATIC_WITH_ARGS(const QString, strVersion, (QLatin1String("version")))
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VLayoutConverter::VLayoutConverter(const QString &fileName) VLayoutConverter::VLayoutConverter(const QString &fileName)
: VAbstractConverter(fileName) : VAbstractConverter(fileName)
{ {
m_ver = GetFormatVersion(GetFormatVersionStr()); m_ver = GetFormatVersion(VLayoutConverter::GetFormatVersionStr());
ValidateInputFile(CurrentSchema); ValidateInputFile(CurrentSchema);
} }
@ -66,12 +61,28 @@ auto VLayoutConverter::GetFormatVersionStr() const -> QString
const QDomElement layoutElement = root.toElement(); const QDomElement layoutElement = root.toElement();
if (not layoutElement.isNull()) if (not layoutElement.isNull())
{ {
return GetParametrString(layoutElement, *strVersion, QStringLiteral("0.0.0")); return GetParametrString(layoutElement, AttrLayoutVersion, QStringLiteral("0.0.0"));
} }
} }
return QStringLiteral("0.0.0"); return QStringLiteral("0.0.0");
} }
//---------------------------------------------------------------------------------------------------------------------
void VLayoutConverter::SetVersion(const QString &version)
{
ValidateVersion(version);
QDomElement root = documentElement().toElement();
if (root.isElement() && root.hasAttribute(AttrLayoutVersion))
{
root.setAttribute(AttrLayoutVersion, version);
}
else
{
throw VException(tr("Could not change version."));
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VLayoutConverter::XSDSchema(unsigned ver) const -> QString auto VLayoutConverter::XSDSchema(unsigned ver) const -> QString
{ {

View File

@ -35,7 +35,6 @@ class QString;
class VLayoutConverter : public VAbstractConverter class VLayoutConverter : public VAbstractConverter
{ {
Q_DECLARE_TR_FUNCTIONS(VLayoutConverter)
public: public:
explicit VLayoutConverter(const QString &fileName); explicit VLayoutConverter(const QString &fileName);
virtual ~VLayoutConverter() Q_DECL_EQ_DEFAULT; virtual ~VLayoutConverter() Q_DECL_EQ_DEFAULT;
@ -48,6 +47,8 @@ public:
static Q_DECL_CONSTEXPR const unsigned LayoutMaxVer = FormatVersion(0, 1, 0); static Q_DECL_CONSTEXPR const unsigned LayoutMaxVer = FormatVersion(0, 1, 0);
protected: protected:
void SetVersion(const QString &version) override;
virtual unsigned MinVer() const override; virtual unsigned MinVer() const override;
virtual unsigned MaxVer() const override; virtual unsigned MaxVer() const override;

View File

@ -45,9 +45,9 @@
#include "../exception/vexception.h" #include "../exception/vexception.h"
#include "../exception/vexceptionemptyparameter.h" #include "../exception/vexceptionemptyparameter.h"
#include "../exception/vexceptionwrongid.h"
#include "../qmuparser/qmutokenparser.h" #include "../qmuparser/qmutokenparser.h"
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "vabstractconverter.h"
class QDomElement; class QDomElement;
@ -179,6 +179,22 @@ VPatternConverter::VPatternConverter(const QString &fileName)
ValidateInputFile(CurrentSchema); ValidateInputFile(CurrentSchema);
} }
//---------------------------------------------------------------------------------------------------------------------
void VPatternConverter::Save()
{
try
{
TestUniqueId();
}
catch (const VExceptionWrongId &e)
{
Q_UNUSED(e)
throw VException(tr("Error no unique id."));
}
VAbstractConverter::Save();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VPatternConverter::XSDSchema(unsigned ver) const -> QString auto VPatternConverter::XSDSchema(unsigned ver) const -> QString
{ {

View File

@ -56,6 +56,8 @@ public:
static Q_DECL_CONSTEXPR const unsigned PatternMaxVer = FormatVersion(0, 9, 0); static Q_DECL_CONSTEXPR const unsigned PatternMaxVer = FormatVersion(0, 9, 0);
protected: protected:
void Save() override;
virtual unsigned MinVer() const override; virtual unsigned MinVer() const override;
virtual unsigned MaxVer() const override; virtual unsigned MaxVer() const override;