Check version number.

--HG--
branch : feature
This commit is contained in:
dismine 2014-12-11 21:44:48 +02:00
parent fb8849f798
commit 1c4af06343
6 changed files with 84 additions and 24 deletions

View File

@ -37,6 +37,7 @@
#include "../libs/ifc/exception/vexceptionconversionerror.h"
#include "../libs/ifc/exception/vexceptionemptyparameter.h"
#include "../libs/ifc/exception/vexceptionundo.h"
#include "../libs/ifc/xml/vpatternconverter.h"
#include "../core/undoevent.h"
#include "../core/vsettings.h"
#include "vstandardmeasurements.h"
@ -146,7 +147,7 @@ void VPattern::CreateEmptyFile(const QString &tablePath)
patternElement.appendChild(createComment("Valentina pattern format."));
QDomElement version = createElement(TagVersion);
QDomText newNodeText = createTextNode(VAL_STR_VERSION);
QDomText newNodeText = createTextNode(VPatternConverter::PatternMaxVerStr);
version.appendChild(newNodeText);
patternElement.appendChild(version);
@ -2431,13 +2432,13 @@ void VPattern::SetNotes(const QString &text)
//---------------------------------------------------------------------------------------------------------------------
QString VPattern::GetVersion() const
{
return UniqueTagText(TagVersion, VAL_STR_VERSION);
return UniqueTagText(TagVersion, VPatternConverter::PatternMaxVerStr);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::SetVersion()
{
setTagText(TagVersion, VAL_STR_VERSION);
setTagText(TagVersion, VPatternConverter::PatternMaxVerStr);
emit patternChanged(false);
}

View File

@ -39,16 +39,6 @@ class VMainGraphicsScene;
enum class Document : char { LiteParse, LitePPParse, FullParse };
enum class LabelType : char {NewPatternPiece, NewLabel};
/*
VAL_VERSION is (major << 16) + (minor << 8) + patch.
*/
// version without patch part
#define VAL_MIN_VERSION 0x000100
// max support version of format
#define VAL_VERSION 0x000101
#define VAL_STR_VERSION "0.1.1"
/**
* @brief The VPattern class working with pattern file.
*/

View File

@ -29,17 +29,20 @@
#include "vabstractconverter.h"
#include "exception/vexception.h"
#include <QRegExpValidator>
//---------------------------------------------------------------------------------------------------------------------
VAbstractConverter::VAbstractConverter(const QString &fileName)
:VDomDocument(), fileName(fileName)
{
this->setXMLContent(fileName);
QString version = GetVersionStr();
const QString version = GetVersionStr();
int ver = GetVersion(version);
CheckVersion(ver);
}
//---------------------------------------------------------------------------------------------------------------------
VAbstractConverter::~VAbstractConverter()
{}
//---------------------------------------------------------------------------------------------------------------------
QString VAbstractConverter::GetVersionStr() const
{
@ -69,7 +72,7 @@ QString VAbstractConverter::GetVersionStr() const
}
//---------------------------------------------------------------------------------------------------------------------
int VAbstractConverter::GetVersion(QString &version) const
int VAbstractConverter::GetVersion(const QString &version) const
{
ValidateVersion(version);
@ -100,13 +103,11 @@ int VAbstractConverter::GetVersion(QString &version) const
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::ValidateVersion(QString &version) const
void VAbstractConverter::ValidateVersion(const QString &version) const
{
int pos = 0;
QRegExp rx(QStringLiteral("^(0|([1-9][0-9]*)).(0|([1-9][0-9]*)).(0|([1-9][0-9]*))$"));
QRegExpValidator v(rx, 0);
if (v.validate(version, pos) != QValidator::Acceptable)
if (rx.exactMatch(version) == false)
{
const QString errorMsg(tr("Version \"%1\" invalid.").arg(version));
throw VException(errorMsg);
@ -118,3 +119,19 @@ void VAbstractConverter::ValidateVersion(QString &version) const
throw VException(errorMsg);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractConverter::CheckVersion(int ver) const
{
if (ver < MinVer())
{
const QString errorMsg(tr("Invalid version. Minimum supported version is %1").arg(MinVerStr()));
throw VException(errorMsg);
}
if (ver > MaxVer())
{
const QString errorMsg(tr("Invalid version. Maximum supported version is %1").arg(MaxVerStr()));
throw VException(errorMsg);
}
}

View File

@ -36,16 +36,25 @@ class VAbstractConverter :public VDomDocument
public:
VAbstractConverter(const QString &fileName);
virtual ~VAbstractConverter(){}
virtual ~VAbstractConverter();
protected:
int GetVersion(const QString &version) const;
virtual int MinVer() const =0;
virtual int MaxVer() const =0;
virtual QString MinVerStr() const =0;
virtual QString MaxVerStr() const =0;
private:
Q_DISABLE_COPY(VAbstractConverter)
QString fileName;
QString GetVersionStr() const;
int GetVersion(QString &version) const;
void ValidateVersion(QString &version) const;
void ValidateVersion(const QString &version) const;
void CheckVersion(int ver) const;
};
#endif // VABSTRACTCONVERTER_H

View File

@ -28,14 +28,45 @@
#include "vpatternconverter.h"
const QString VPatternConverter::PatternMinVerStr = QStringLiteral("0.1.1");
const QString VPatternConverter::PatternMaxVerStr = QStringLiteral("0.1.1");
//---------------------------------------------------------------------------------------------------------------------
VPatternConverter::VPatternConverter(const QString &fileName)
:VAbstractConverter(fileName)
{
}
//---------------------------------------------------------------------------------------------------------------------
VPatternConverter::~VPatternConverter()
{}
//---------------------------------------------------------------------------------------------------------------------
QString VPatternConverter::CurrentSchema()
{
return QStringLiteral("://schema/pattern/v0.1.1.xsd");
}
//---------------------------------------------------------------------------------------------------------------------
int VPatternConverter::MinVer() const
{
return GetVersion(PatternMinVerStr);
}
//---------------------------------------------------------------------------------------------------------------------
int VPatternConverter::MaxVer() const
{
return GetVersion(PatternMaxVerStr);
}
//---------------------------------------------------------------------------------------------------------------------
QString VPatternConverter::MinVerStr() const
{
return PatternMinVerStr;
}
//---------------------------------------------------------------------------------------------------------------------
QString VPatternConverter::MaxVerStr() const
{
return PatternMaxVerStr;
}

View File

@ -35,8 +35,20 @@ class VPatternConverter : public VAbstractConverter
{
public:
VPatternConverter(const QString &fileName);
virtual ~VPatternConverter();
static QString CurrentSchema();
static const QString PatternMaxVerStr;
protected:
virtual int MinVer() const;
virtual int MaxVer() const;
virtual QString MinVerStr() const;
virtual QString MaxVerStr() const;
private:
Q_DISABLE_COPY(VPatternConverter)
static const QString PatternMinVerStr;
};
#endif // VPATTERNCONVERTER_H