diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 07d61d720..716be7f92 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -438,7 +438,27 @@ auto VPMainWindow::LoadFile(QString path) -> bool VLayoutConverter converter(path); m_curFileFormatVersion = converter.GetCurrentFormatVersion(); 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) { @@ -448,27 +468,6 @@ auto VPMainWindow::LoadFile(QString path) -> bool 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); m_layout->SetFocusedSheet(); diff --git a/src/app/puzzle/xml/vplayoutliterals.cpp b/src/app/puzzle/xml/vplayoutliterals.cpp index 34a452008..e31b5d67e 100644 --- a/src/app/puzzle/xml/vplayoutliterals.cpp +++ b/src/app/puzzle/xml/vplayoutliterals.cpp @@ -61,7 +61,6 @@ const QString TagLine = QStringLiteral("line"); const QString TagScale = QStringLiteral("scale"); const QString TagWatermark = QStringLiteral("watermark"); -const QString AttrVersion = QStringLiteral("version"); const QString AttrWarningSuperposition = QStringLiteral("warningSuperposition"); const QString AttrWarningOutOfBound = QStringLiteral("warningOutOfBound"); const QString AttrStickyEdges = QStringLiteral("stickyEdges"); diff --git a/src/app/puzzle/xml/vplayoutliterals.h b/src/app/puzzle/xml/vplayoutliterals.h index a3c5c8a33..aa6b0e460 100644 --- a/src/app/puzzle/xml/vplayoutliterals.h +++ b/src/app/puzzle/xml/vplayoutliterals.h @@ -66,7 +66,6 @@ extern const QString TagLine; extern const QString TagScale; extern const QString TagWatermark; -extern const QString AttrVersion; extern const QString AttrWarningSuperposition; extern const QString AttrWarningOutOfBound; extern const QString AttrStickyEdges; diff --git a/src/libs/ifc/ifcdef.cpp b/src/libs/ifc/ifcdef.cpp index 4763c56ce..191d22326 100644 --- a/src/libs/ifc/ifcdef.cpp +++ b/src/libs/ifc/ifcdef.cpp @@ -143,6 +143,7 @@ const QString AttrCurve1Alias1 = QStringLiteral("curve1Alias1"); const QString AttrCurve1Alias2 = QStringLiteral("curve1Alias2"); const QString AttrCurve2Alias1 = QStringLiteral("curve2Alias1"); const QString AttrCurve2Alias2 = QStringLiteral("curve2Alias2"); +const QString AttrLayoutVersion = QStringLiteral("version"); const QString TypeLineDefault = QStringLiteral("default"); const QString TypeLineNone = QStringLiteral("none"); diff --git a/src/libs/ifc/ifcdef.h b/src/libs/ifc/ifcdef.h index e37920783..4f3ffdbfa 100644 --- a/src/libs/ifc/ifcdef.h +++ b/src/libs/ifc/ifcdef.h @@ -162,6 +162,7 @@ extern const QString AttrCurve1Alias1; extern const QString AttrCurve1Alias2; extern const QString AttrCurve2Alias1; extern const QString AttrCurve2Alias2; +extern const QString AttrLayoutVersion; extern const QString TypeLineDefault; extern const QString TypeLineNone; diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index 2ff316e70..b6cea05e4 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -50,7 +50,6 @@ #include #include "../exception/vexception.h" -#include "../exception/vexceptionwrongid.h" #include "vdomdocument.h" //This class need for validation pattern file using XSD shema @@ -339,17 +338,6 @@ void VAbstractConverter::ValidateInputFile(const QString ¤tSchema) const //--------------------------------------------------------------------------------------------------------------------- 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 const int indent = 4; QTextStream out(&m_tmpFile); @@ -358,8 +346,7 @@ void VAbstractConverter::Save() if (not m_tmpFile.flush()) { - VException e(m_tmpFile.errorString()); - throw e; + throw VException(m_tmpFile.errorString()); } } diff --git a/src/libs/ifc/xml/vabstractconverter.h b/src/libs/ifc/xml/vabstractconverter.h index 2a59bc2c9..a243dca37 100644 --- a/src/libs/ifc/xml/vabstractconverter.h +++ b/src/libs/ifc/xml/vabstractconverter.h @@ -44,7 +44,6 @@ QT_WARNING_DISABLE_GCC("-Wsuggest-final-methods") class VAbstractConverter :public VDomDocument { - Q_DECLARE_TR_FUNCTIONS(VAbstractConverter) public: explicit VAbstractConverter(const QString &fileName); virtual ~VAbstractConverter() Q_DECL_EQ_DEFAULT; @@ -60,8 +59,8 @@ protected: void ValidateInputFile(const QString ¤tSchema) const; Q_NORETURN void InvalidVersion(unsigned ver) const; - void Save(); - void SetVersion(const QString &version); + virtual void Save(); + virtual void SetVersion(const QString &version); virtual unsigned MinVer() const =0; virtual unsigned MaxVer() const =0; diff --git a/src/libs/ifc/xml/vlayoutconverter.cpp b/src/libs/ifc/xml/vlayoutconverter.cpp index 035085567..9772638b7 100644 --- a/src/libs/ifc/xml/vlayoutconverter.cpp +++ b/src/libs/ifc/xml/vlayoutconverter.cpp @@ -26,6 +26,8 @@ ** *************************************************************************/ #include "vlayoutconverter.h" +#include "../exception/vexception.h" +#include "ifcdef.h" /* * Version rules: @@ -42,18 +44,11 @@ const QString VLayoutConverter::CurrentSchema = QStringLiteral("://schema/layo //VLayoutConverter::LayoutMinVer; // <== 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) : VAbstractConverter(fileName) { - m_ver = GetFormatVersion(GetFormatVersionStr()); + m_ver = GetFormatVersion(VLayoutConverter::GetFormatVersionStr()); ValidateInputFile(CurrentSchema); } @@ -66,12 +61,28 @@ auto VLayoutConverter::GetFormatVersionStr() const -> QString const QDomElement layoutElement = root.toElement(); 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"); } +//--------------------------------------------------------------------------------------------------------------------- +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 { diff --git a/src/libs/ifc/xml/vlayoutconverter.h b/src/libs/ifc/xml/vlayoutconverter.h index 1dff7432c..956e5aeeb 100644 --- a/src/libs/ifc/xml/vlayoutconverter.h +++ b/src/libs/ifc/xml/vlayoutconverter.h @@ -35,7 +35,6 @@ class QString; class VLayoutConverter : public VAbstractConverter { - Q_DECLARE_TR_FUNCTIONS(VLayoutConverter) public: explicit VLayoutConverter(const QString &fileName); virtual ~VLayoutConverter() Q_DECL_EQ_DEFAULT; @@ -48,6 +47,8 @@ public: static Q_DECL_CONSTEXPR const unsigned LayoutMaxVer = FormatVersion(0, 1, 0); protected: + void SetVersion(const QString &version) override; + virtual unsigned MinVer() const override; virtual unsigned MaxVer() const override; diff --git a/src/libs/ifc/xml/vpatternconverter.cpp b/src/libs/ifc/xml/vpatternconverter.cpp index ac5c5d825..59f4b5099 100644 --- a/src/libs/ifc/xml/vpatternconverter.cpp +++ b/src/libs/ifc/xml/vpatternconverter.cpp @@ -45,9 +45,9 @@ #include "../exception/vexception.h" #include "../exception/vexceptionemptyparameter.h" +#include "../exception/vexceptionwrongid.h" #include "../qmuparser/qmutokenparser.h" #include "../vmisc/def.h" -#include "vabstractconverter.h" class QDomElement; @@ -179,6 +179,22 @@ VPatternConverter::VPatternConverter(const QString &fileName) 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 { diff --git a/src/libs/ifc/xml/vpatternconverter.h b/src/libs/ifc/xml/vpatternconverter.h index b75adbacc..b43becc48 100644 --- a/src/libs/ifc/xml/vpatternconverter.h +++ b/src/libs/ifc/xml/vpatternconverter.h @@ -56,6 +56,8 @@ public: static Q_DECL_CONSTEXPR const unsigned PatternMaxVer = FormatVersion(0, 9, 0); protected: + void Save() override; + virtual unsigned MinVer() const override; virtual unsigned MaxVer() const override;