From 82ce2aadfe09ece1c492a0fb91695abe92018351 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 23 Apr 2020 18:05:00 +0300 Subject: [PATCH] Added initial support for the manual layout format version converter. --- src/app/puzzle/puzzlemainwindow.cpp | 18 +- src/app/puzzle/puzzlemainwindow.h | 2 +- .../puzzle/xml/vpuzzlelayoutfilereader.cpp | 26 +-- src/app/puzzle/xml/vpuzzlelayoutfilereader.h | 4 - .../puzzle/xml/vpuzzlelayoutfilewriter.cpp | 5 +- src/app/puzzle/xml/vpuzzlelayoutfilewriter.h | 22 +- src/libs/ifc/schema.qrc | 1 + src/libs/ifc/schema/layout/v0.1.0.xsd | 221 ++++++++++++++++++ src/libs/ifc/xml/vlayoutconverter.cpp | 91 ++++++++ src/libs/ifc/xml/vlayoutconverter.h | 90 +++++++ src/libs/ifc/xml/xml.pri | 2 + 11 files changed, 432 insertions(+), 50 deletions(-) create mode 100644 src/libs/ifc/schema/layout/v0.1.0.xsd create mode 100644 src/libs/ifc/xml/vlayoutconverter.cpp create mode 100644 src/libs/ifc/xml/vlayoutconverter.h diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index 1ccf11782..66fb4aa05 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -36,6 +36,8 @@ #include "puzzleapplication.h" #include "../vlayout/vrawlayout.h" #include "../vmisc/vsysexits.h" +#include "../ifc/xml/vlayoutconverter.h" +#include "../ifc/exception/vexception.h" #include @@ -79,12 +81,24 @@ PuzzleMainWindow::~PuzzleMainWindow() } //--------------------------------------------------------------------------------------------------------------------- -bool PuzzleMainWindow::LoadFile(const QString &path) +bool PuzzleMainWindow::LoadFile(QString path) { + try + { + VLayoutConverter converter(path); + path = converter.Convert(); + } + catch (VException &e) + { + qCCritical(pWindow, "%s\n\n%s\n\n%s", qUtf8Printable(tr("File error.")), + qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation())); + return false; + } + QFile file(path); file.open(QIODevice::ReadOnly); - VPuzzleLayoutFileReader *fileReader = new VPuzzleLayoutFileReader(); + QScopedPointer fileReader(new VPuzzleLayoutFileReader()); if(m_layout == nullptr) { diff --git a/src/app/puzzle/puzzlemainwindow.h b/src/app/puzzle/puzzlemainwindow.h index 3f2dfb6c2..2a39e7633 100644 --- a/src/app/puzzle/puzzlemainwindow.h +++ b/src/app/puzzle/puzzlemainwindow.h @@ -57,7 +57,7 @@ public: * @param path * @return */ - bool LoadFile(const QString &path); + bool LoadFile(QString path); /** * @brief SaveFile Saves the current layout to the layout file of given path diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp index e2ef1cbe9..106338f60 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp @@ -53,31 +53,7 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file) if (readNextStartElement()) { - - // TODO extend the handling - // if it doesn't start with layout, error - // if it starts with version > than current version, error - - if (name() == ML::TagLayout) - { - QString versionStr = ReadAttributeEmptyString(attributes(), ML::AttrVersion); - 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")); - } - } - else - { - raiseError(QObject::tr("Wrong file structure")); - } + ReadLayout(layout); } return !error(); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.h b/src/app/puzzle/xml/vpuzzlelayoutfilereader.h index 6ab25bce1..7f4c00e1a 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.h +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.h @@ -46,10 +46,6 @@ public: private: Q_DISABLE_COPY(VPuzzleLayoutFileReader) - /** - * @brief m_layoutFormatVersion holds the version of the layout currently being read - */ - int m_layoutFormatVersion; void ReadLayout(VPuzzleLayout *layout); void ReadProperties(VPuzzleLayout *layout); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp index e2195a1a1..c85f725b9 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp @@ -31,6 +31,7 @@ #include "vpuzzlelayer.h" #include "vpuzzlepiece.h" #include "layoutliterals.h" +#include "../ifc/xml/vlayoutconverter.h" //--------------------------------------------------------------------------------------------------------------------- VPuzzleLayoutFileWriter::VPuzzleLayoutFileWriter() @@ -61,7 +62,7 @@ void VPuzzleLayoutFileWriter::WriteFile(VPuzzleLayout *layout, QFile *file) void VPuzzleLayoutFileWriter::WriteLayout(VPuzzleLayout *layout) { writeStartElement(ML::TagLayout); - SetAttribute(ML::AttrVersion, LayoutFileFormatVersionStr); + SetAttribute(ML::AttrVersion, VLayoutConverter::LayoutMaxVerStr); WriteProperties(layout); WriteLayers(layout); @@ -180,7 +181,7 @@ void VPuzzleLayoutFileWriter::WritePiece(VPuzzlePiece *piece) } //--------------------------------------------------------------------------------------------------------------------- -void VPuzzleLayoutFileWriter::WriteMargins(QMarginsF margins) +void VPuzzleLayoutFileWriter::WriteMargins(const QMarginsF &margins) { writeStartElement(ML::TagMargin); SetAttribute(ML::AttrLeft, margins.left()); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h index 5de4f6164..abbc7377f 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h +++ b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h @@ -29,12 +29,16 @@ #ifndef VPUZZLELAYOUTFILEWRITER_H #define VPUZZLELAYOUTFILEWRITER_H +#include #include -#include "../ifc/xml/vabstractconverter.h" + +#include "../vmisc/literals.h" class VPuzzleLayout; class VPuzzleLayer; class VPuzzlePiece; +class QFile; +class QMarginsF; class VPuzzleLayoutFileWriter : public QXmlStreamWriter { @@ -44,20 +48,6 @@ public: void WriteFile(VPuzzleLayout *layout, QFile *file); - /* - * Version rules: - * 1. Version have three parts "major.minor.patch"; - * 2. major part only for stable releases; - * 3. minor - 10 or more patch changes, or one big change; - * 4. patch - little change. - */ - /** - * @brief LayoutFileFormatVersion holds the version - * - */ - static Q_DECL_CONSTEXPR const int LayoutFileFormatVersion = FORMAT_VERSION(1, 0, 0); - const QString LayoutFileFormatVersionStr = QStringLiteral("1.0.0"); - private: void WriteLayout(VPuzzleLayout *layout); @@ -68,7 +58,7 @@ private: void WriteLayer(VPuzzleLayer *layer, const QString &tagName); void WritePiece(VPuzzlePiece *piece); - void WriteMargins(QMarginsF margins); + void WriteMargins(const QMarginsF &margins); void WriteSize(QSizeF size); template diff --git a/src/libs/ifc/schema.qrc b/src/libs/ifc/schema.qrc index 79d978c82..fbfe6b75c 100644 --- a/src/libs/ifc/schema.qrc +++ b/src/libs/ifc/schema.qrc @@ -75,5 +75,6 @@ schema/individual_measurements/v0.5.0.xsd schema/label_template/v1.0.0.xsd schema/watermark/v1.0.0.xsd + schema/layout/v0.1.0.xsd diff --git a/src/libs/ifc/schema/layout/v0.1.0.xsd b/src/libs/ifc/schema/layout/v0.1.0.xsd new file mode 100644 index 000000000..0d6086b2e --- /dev/null +++ b/src/libs/ifc/schema/layout/v0.1.0.xsd @@ -0,0 +1,221 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/libs/ifc/xml/vlayoutconverter.cpp b/src/libs/ifc/xml/vlayoutconverter.cpp new file mode 100644 index 000000000..8671199dc --- /dev/null +++ b/src/libs/ifc/xml/vlayoutconverter.cpp @@ -0,0 +1,91 @@ +/************************************************************************ + ** + ** @file vlayoutconverter.cpp + ** @author Roman Telezhynskyi + ** @date 23 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 + ** 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 . + ** + *************************************************************************/ +#include "vlayoutconverter.h" + +/* + * Version rules: + * 1. Version have three parts "major.minor.patch"; + * 2. major part only for stable releases; + * 3. minor - 10 or more patch changes, or one big change; + * 4. patch - little change. + */ + +const QString VLayoutConverter::LayoutMinVerStr = QStringLiteral("0.1.0"); +const QString VLayoutConverter::LayoutMaxVerStr = QStringLiteral("0.1.0"); +const QString VLayoutConverter::CurrentSchema = QStringLiteral("://schema/layout/v0.1.0.xsd"); + +//VLayoutConverter::LayoutMinVer; // <== DON'T FORGET TO UPDATE TOO!!!! +//VLayoutConverter::LayoutMaxVer; // <== DON'T FORGET TO UPDATE TOO!!!! + +//--------------------------------------------------------------------------------------------------------------------- +VLayoutConverter::VLayoutConverter(const QString &fileName) + : VAbstractConverter(fileName) +{ + ValidateInputFile(CurrentSchema); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VLayoutConverter::XSDSchema(int ver) const +{ + QHash schemas = + { + std::make_pair(FORMAT_VERSION(0, 1, 0), CurrentSchema) + }; + + if (schemas.contains(ver)) + { + return schemas.value(ver); + } + + InvalidVersion(ver); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutConverter::ApplyPatches() +{ + switch (m_ver) + { + case (FORMAT_VERSION(0, 1, 0)): + break; + default: + InvalidVersion(m_ver); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutConverter::DowngradeToCurrentMaxVersion() +{ + SetVersion(LayoutMaxVerStr); + Save(); +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VLayoutConverter::IsReadOnly() const +{ + return false; +} diff --git a/src/libs/ifc/xml/vlayoutconverter.h b/src/libs/ifc/xml/vlayoutconverter.h new file mode 100644 index 000000000..5e271b9bb --- /dev/null +++ b/src/libs/ifc/xml/vlayoutconverter.h @@ -0,0 +1,90 @@ +/************************************************************************ + ** + ** @file vlayoutconverter.h + ** @author Roman Telezhynskyi + ** @date 23 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 + ** 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 . + ** + *************************************************************************/ +#ifndef VLAYOUTCONVERTER_H +#define VLAYOUTCONVERTER_H + +#include "vabstractconverter.h" + +class QDomElement; +class QString; + +class VLayoutConverter : public VAbstractConverter +{ + Q_DECLARE_TR_FUNCTIONS(VLayoutConverter) +public: + explicit VLayoutConverter(const QString &fileName); + virtual ~VLayoutConverter() Q_DECL_EQ_DEFAULT; + + static const QString LayoutMaxVerStr; + static const QString CurrentSchema; + static Q_DECL_CONSTEXPR const int LayoutMinVer = FORMAT_VERSION(0, 1, 0); + static Q_DECL_CONSTEXPR const int LayoutMaxVer = FORMAT_VERSION(0, 1, 0); + +protected: + virtual int MinVer() const override; + virtual int MaxVer() const override; + + virtual QString MinVerStr() const override; + virtual QString MaxVerStr() const override; + + virtual QString XSDSchema(int ver) const override; + virtual void ApplyPatches() override; + virtual void DowngradeToCurrentMaxVersion() override; + + virtual bool IsReadOnly() const override; + +private: + Q_DISABLE_COPY(VLayoutConverter) + static const QString LayoutMinVerStr; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline int VLayoutConverter::MinVer() const +{ + return LayoutMinVer; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline int VLayoutConverter::MaxVer() const +{ + return LayoutMaxVer; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline QString VLayoutConverter::MinVerStr() const +{ + return LayoutMinVerStr; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline QString VLayoutConverter::MaxVerStr() const +{ + return LayoutMaxVerStr; +} + +#endif // VLAYOUTCONVERTER_H diff --git a/src/libs/ifc/xml/xml.pri b/src/libs/ifc/xml/xml.pri index 1c558ea04..0a6f39591 100644 --- a/src/libs/ifc/xml/xml.pri +++ b/src/libs/ifc/xml/xml.pri @@ -4,6 +4,7 @@ HEADERS += \ $$PWD/vabstractconverter.h \ $$PWD/vdomdocument.h \ + $$PWD/vlayoutconverter.h \ $$PWD/vpatternconverter.h \ $$PWD/vtoolrecord.h \ $$PWD/vabstractpattern.h \ @@ -16,6 +17,7 @@ HEADERS += \ SOURCES += \ $$PWD/vabstractconverter.cpp \ $$PWD/vdomdocument.cpp \ + $$PWD/vlayoutconverter.cpp \ $$PWD/vpatternconverter.cpp \ $$PWD/vtoolrecord.cpp \ $$PWD/vabstractpattern.cpp \