From c83ac5e493c80742688d8b2363cc86c710bf1e11 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sun, 19 Apr 2020 10:38:28 +0200 Subject: [PATCH] Changes for vlt Format version --- .../puzzle/xml/vpuzzlelayoutfilereader.cpp | 21 +++++++++++++++---- src/app/puzzle/xml/vpuzzlelayoutfilereader.h | 3 +++ .../puzzle/xml/vpuzzlelayoutfilewriter.cpp | 8 ++++++- src/app/puzzle/xml/vpuzzlelayoutfilewriter.h | 16 ++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp index c2cfc6d9f..76da5f53d 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp @@ -28,6 +28,7 @@ #include #include "vpuzzlelayoutfilereader.h" +#include "vpuzzlelayoutfilewriter.h" VPuzzleLayoutFileReader::VPuzzleLayoutFileReader() { @@ -53,14 +54,25 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file) // if it doesn't start with layout, error // if it starts with version > than current version, error - if (name() == QString("layout") - && attributes().value(QString("version")) == QLatin1String("1.0.0")) + if (name() == QString("layout")) { - ReadLayout(layout); + QString versionStr = attributes().value(QString("version")).toString(); + 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("The file is not a layout version 1.0.0 file.")); + raiseError(QObject::tr("Wrong file structure")); } } @@ -218,6 +230,7 @@ void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer) //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece) { + Q_UNUSED(piece); Q_ASSERT(isStartElement() && name() == QString("piece")); // TODO read the attributes diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.h b/src/app/puzzle/xml/vpuzzlelayoutfilereader.h index f2a08a23b..8ec5eb5ce 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.h +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.h @@ -30,6 +30,7 @@ #define VPUZZLELAYOUTFILEREADER_H #include +#include "../ifc/xml/vabstractconverter.h" #include "vpuzzlelayout.h" #include "vpuzzlelayer.h" #include "vpuzzlepiece.h" @@ -43,6 +44,8 @@ public: bool ReadFile(VPuzzleLayout *layout, QFile *file); private: + int m_layoutFormatVersion; + void ReadLayout(VPuzzleLayout *layout); void ReadProperties(VPuzzleLayout *layout); void ReadTiles(VPuzzleLayout *layout); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp index 87f676107..8c265e0c0 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.cpp @@ -34,6 +34,12 @@ VPuzzleLayoutFileWriter::VPuzzleLayoutFileWriter() } +//--------------------------------------------------------------------------------------------------------------------- +VPuzzleLayoutFileWriter::~VPuzzleLayoutFileWriter() +{ + +} + //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileWriter::WriteFile(VPuzzleLayout *layout, QFile *file) { @@ -51,7 +57,7 @@ void VPuzzleLayoutFileWriter::WriteFile(VPuzzleLayout *layout, QFile *file) void VPuzzleLayoutFileWriter::WriteLayout(VPuzzleLayout *layout) { writeStartElement("layout"); - writeAttribute("version", "1.0.0"); // TODO / FIXME : get the version properly + writeAttribute("version", LayoutFileFormatVersionStr); WriteProperties(layout); WriteLayers(layout); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h index 5c22fdb5c..6f25e6a10 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h +++ b/src/app/puzzle/xml/vpuzzlelayoutfilewriter.h @@ -30,6 +30,7 @@ #define VPUZZLELAYOUTFILEWRITER_H #include +#include "../ifc/xml/vabstractconverter.h" #include "vpuzzlelayout.h" #include "vpuzzlelayer.h" #include "vpuzzlepiece.h" @@ -38,9 +39,24 @@ class VPuzzleLayoutFileWriter : public QXmlStreamWriter { public: VPuzzleLayoutFileWriter(); + ~VPuzzleLayoutFileWriter(); 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);