Make method ValidatePattern static.

--HG--
branch : feature
This commit is contained in:
dismine 2014-02-25 16:55:02 +02:00
parent e184fcc6a7
commit 1b3de9e4a2
18 changed files with 128 additions and 125 deletions

View File

@ -174,13 +174,13 @@ ptrdiff_t VDetail::Edge(const quint32 &p1, const quint32 &p2) const
void VDetail::NodeOnEdge(const quint32 &index, VNodeDetail &p1, VNodeDetail &p2) const
{
QVector<VNodeDetail> list = listNodePoint();
if (index < 0 || index > list.size())
if (index > static_cast<quint32>(list.size()))
{
qWarning()<<"Wrong edge index index ="<<index;
return;
}
p1 = list.at(index);
if (index + 1 > list.size() - 1)
if (index + 1 > static_cast<quint32>(list.size()) - 1)
{
p2 = list.at(0);
}
@ -196,9 +196,9 @@ VDetail VDetail::RemoveEdge(const quint32 &index) const
det.ClearNodes();
QVector<VNodeDetail> list = this->listNodePoint();
qint32 edge = list.size();
ptrdiff_t k = 0;
for (ptrdiff_t i=0; i<edge; ++i)
quint32 edge = static_cast<quint32>(list.size());
quint32 k = 0;
for (quint32 i=0; i<edge; ++i)
{
if (i == index)
{

View File

@ -44,36 +44,8 @@
#include <QShowEvent>
#include <QScrollBar>
#include <QFileDialog>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QSourceLocation>
#include <QAbstractMessageHandler>
//This class need for validation pattern file using XSD shema
class MessageHandler : public QAbstractMessageHandler
{
public:
MessageHandler() : QAbstractMessageHandler(0), m_messageType(QtMsgType()), m_description(QString()),
m_sourceLocation(QSourceLocation()){}
inline QString statusMessage() const {return m_description;}
inline qint64 line() const {return m_sourceLocation.line();}
inline qint64 column() const {return m_sourceLocation.column();}
protected:
virtual void handleMessage(QtMsgType type, const QString &description,
const QUrl &identifier, const QSourceLocation &sourceLocation)
{
Q_UNUSED(type);
Q_UNUSED(identifier);
m_messageType = type;
m_description = description;
m_sourceLocation = sourceLocation;
}
private:
QtMsgType m_messageType;
QString m_description;
QSourceLocation m_sourceLocation;
};
MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent), ui(new Ui::MainWindow), pattern(0), doc(0), tool(Tool::ArrowTool), currentScene(0),
@ -1146,56 +1118,6 @@ void MainWindow::MinimumScrollBar()
verScrollBar->setValue(verScrollBar->minimum());
}
bool MainWindow::ValidatePattern(const QString &schema, const QString &fileName, QString &errorMsg, qint64 &errorLine,
qint64 &errorColumn) const
{
errorLine = -1;
errorColumn = -1;
QFile pattern(fileName);
if (pattern.open(QIODevice::ReadOnly) == false)
{
errorMsg = QString(tr("Can't open pattern file %1:\n%2.").arg(fileName).arg(pattern.errorString()));
return false;
}
QFile fileSchema(schema);
if (fileSchema.open(QIODevice::ReadOnly) == false)
{
errorMsg = QString(tr("Can't open schema file %1:\n%2.").arg(schema).arg(fileSchema.errorString()));
return false;
}
MessageHandler messageHandler;
QXmlSchema sch;
sch.setMessageHandler(&messageHandler);
sch.load(&fileSchema, QUrl::fromLocalFile(fileSchema.fileName()));
bool errorOccurred = false;
if (sch.isValid() == false)
{
errorOccurred = true;
}
else
{
QXmlSchemaValidator validator(sch);
if (validator.validate(&pattern, QUrl::fromLocalFile(pattern.fileName())) == false)
{
errorOccurred = true;
}
}
if (errorOccurred)
{
errorMsg = messageHandler.statusMessage();
errorLine = messageHandler.line();
errorColumn = messageHandler.column();
return false;
}
else
{
return true;
}
}
bool MainWindow::SavePattern(const QString &fileName)
{
try
@ -1455,7 +1377,7 @@ void MainWindow::LoadPattern(const QString &fileName)
qint64 errorColumn = 0;
if (file.open(QIODevice::ReadOnly))
{
if (ValidatePattern("://schema/pattern.xsd", fileName, errorMsg, errorLine, errorColumn))
if (VDomDocument::ValidatePattern("://schema/pattern.xsd", fileName, errorMsg, errorLine, errorColumn))
{
qint32 errorLine = 0;
qint32 errorColumn = 0;

View File

@ -516,17 +516,6 @@ private:
* @brief MinimumScrollBar set scroll bar to minimum.
*/
void MinimumScrollBar();
/**
* @brief ValidatePattern validate pattern file by xsd schema.
* @param schema path to schema file.
* @param fileName name of pattern file.
* @param errorMsg error message.
* @param errorLine number error line.
* @param errorColumn number error column.
* @return true if validation successful.
*/
bool ValidatePattern(const QString &schema, const QString &curFile, QString &errorMsg, qint64 &errorLine,
qint64 &errorColumn) const;
template <typename DrawTool>
/**
* @brief ClosedDialog handle close dialog

View File

@ -56,8 +56,8 @@ void VToolAlongLine::FullUpdateFromFile()
{
typeLine = domElement.attribute(AttrTypeLine, "");
formula = domElement.attribute(AttrLength, "");
basePointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
secondPointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
basePointId = domElement.attribute(AttrFirstPoint, "").toUInt();
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
}
RefreshGeometry();
}

View File

@ -155,9 +155,9 @@ void VToolBisector::FullUpdateFromFile()
{
typeLine = domElement.attribute(AttrTypeLine, "");
formula = domElement.attribute(AttrLength, "");
firstPointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
basePointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
thirdPointId = domElement.attribute(AttrThirdPoint, "").toLongLong();
firstPointId = domElement.attribute(AttrFirstPoint, "").toUInt();
basePointId = domElement.attribute(AttrSecondPoint, "").toUInt();
thirdPointId = domElement.attribute(AttrThirdPoint, "").toUInt();
}
RefreshGeometry();
}

View File

@ -165,7 +165,7 @@ void VToolCutArc::FullUpdateFromFile()
if (domElement.isElement())
{
formula = domElement.attribute(AttrLength, "");
arcId = domElement.attribute(AttrArc, "").toLongLong();
arcId = domElement.attribute(AttrArc, "").toUInt();
}
RefreshGeometry();
}

View File

@ -166,7 +166,7 @@ void VToolCutSpline::FullUpdateFromFile()
if (domElement.isElement())
{
formula = domElement.attribute(AttrLength, "");
splineId = domElement.attribute(AttrSpline, "").toLongLong();
splineId = domElement.attribute(AttrSpline, "").toUInt();
}
RefreshGeometry();
}

View File

@ -247,7 +247,7 @@ void VToolCutSplinePath::FullUpdateFromFile()
if (domElement.isElement())
{
formula = domElement.attribute(AttrLength, "");
splinePathId = domElement.attribute(AttrSplinePath, "").toLongLong();
splinePathId = domElement.attribute(AttrSplinePath, "").toUInt();
}
RefreshGeometry();
}

View File

@ -127,7 +127,7 @@ void VToolEndLine::FullUpdateFromFile()
{
typeLine = domElement.attribute(AttrTypeLine, "");
formula = domElement.attribute(AttrLength, "");
basePointId = domElement.attribute(AttrBasePoint, "").toLongLong();
basePointId = domElement.attribute(AttrBasePoint, "").toUInt();
angle = domElement.attribute(AttrAngle, "").toInt();
}
RefreshGeometry();

View File

@ -131,9 +131,9 @@ void VToolHeight::FullUpdateFromFile()
if (domElement.isElement())
{
typeLine = domElement.attribute(AttrTypeLine, "");
basePointId = domElement.attribute(AttrBasePoint, "").toLongLong();
p1LineId = domElement.attribute(AttrP1Line, "").toLongLong();
p2LineId = domElement.attribute(AttrP2Line, "").toLongLong();
basePointId = domElement.attribute(AttrBasePoint, "").toUInt();
p1LineId = domElement.attribute(AttrP1Line, "").toUInt();
p2LineId = domElement.attribute(AttrP2Line, "").toUInt();
}
RefreshGeometry();

View File

@ -136,10 +136,10 @@ void VToolLineIntersect::FullUpdateFromFile()
QDomElement domElement = doc->elementById(QString().setNum(id));
if (domElement.isElement())
{
p1Line1 = domElement.attribute(AttrP1Line1, "").toLongLong();
p2Line1 = domElement.attribute(AttrP2Line1, "").toLongLong();
p1Line2 = domElement.attribute(AttrP1Line2, "").toLongLong();
p2Line2 = domElement.attribute(AttrP2Line2, "").toLongLong();
p1Line1 = domElement.attribute(AttrP1Line1, "").toUInt();
p2Line1 = domElement.attribute(AttrP2Line1, "").toUInt();
p1Line2 = domElement.attribute(AttrP1Line2, "").toUInt();
p2Line2 = domElement.attribute(AttrP2Line2, "").toUInt();
}
RefreshPointGeometry(*VAbstractTool::data.GeometricObject<const VPointF *>(id));
}

View File

@ -140,8 +140,8 @@ void VToolNormal::FullUpdateFromFile()
{
typeLine = domElement.attribute(AttrTypeLine, "");
formula = domElement.attribute(AttrLength, "");
basePointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
secondPointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
basePointId = domElement.attribute(AttrFirstPoint, "").toUInt();
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
angle = domElement.attribute(AttrAngle, "").toDouble();
}
RefreshGeometry();

View File

@ -160,9 +160,9 @@ void VToolPointOfContact::FullUpdateFromFile()
if (domElement.isElement())
{
arcRadius = domElement.attribute(AttrRadius, "");
center = domElement.attribute(AttrCenter, "").toLongLong();
firstPointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
secondPointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
center = domElement.attribute(AttrCenter, "").toUInt();
firstPointId = domElement.attribute(AttrFirstPoint, "").toUInt();
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
}
RefreshPointGeometry(*VAbstractTool::data.GeometricObject<const VPointF *>(id));
}

View File

@ -111,8 +111,8 @@ void VToolPointOfIntersection::FullUpdateFromFile()
QDomElement domElement = doc->elementById(QString().setNum(id));
if (domElement.isElement())
{
firstPointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
secondPointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
firstPointId = domElement.attribute(AttrFirstPoint, "").toUInt();
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
}
VToolPoint::RefreshPointGeometry(*VDrawTool::data.GeometricObject<const VPointF *>(id));
}

View File

@ -162,9 +162,9 @@ void VToolShoulderPoint::FullUpdateFromFile()
{
typeLine = domElement.attribute(AttrTypeLine, "");
formula = domElement.attribute(AttrLength, "");
basePointId = domElement.attribute(AttrP1Line, "").toLongLong();
p2Line = domElement.attribute(AttrP2Line, "").toLongLong();
pShoulder = domElement.attribute(AttrPShoulder, "").toLongLong();
basePointId = domElement.attribute(AttrP1Line, "").toUInt();
p2Line = domElement.attribute(AttrP2Line, "").toUInt();
pShoulder = domElement.attribute(AttrPShoulder, "").toUInt();
}
RefreshGeometry();
}

View File

@ -163,10 +163,10 @@ void VToolTriangle::FullUpdateFromFile()
QDomElement domElement = doc->elementById(QString().setNum(id));
if (domElement.isElement())
{
axisP1Id = domElement.attribute(AttrAxisP1, "").toLongLong();
axisP2Id = domElement.attribute(AttrAxisP2, "").toLongLong();
firstPointId = domElement.attribute(AttrFirstPoint, "").toLongLong();
secondPointId = domElement.attribute(AttrSecondPoint, "").toLongLong();
axisP1Id = domElement.attribute(AttrAxisP1, "").toUInt();
axisP2Id = domElement.attribute(AttrAxisP2, "").toUInt();
firstPointId = domElement.attribute(AttrFirstPoint, "").toUInt();
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
}
VToolPoint::RefreshPointGeometry(*VDrawTool::data.GeometricObject<const VPointF *>(id));
}

View File

@ -31,6 +31,37 @@
#include "../exception/vexceptionemptyparameter.h"
#include "../exception/vexceptionbadid.h"
#include <QAbstractMessageHandler>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QFile>
//This class need for validation pattern file using XSD shema
class MessageHandler : public QAbstractMessageHandler
{
public:
MessageHandler() : QAbstractMessageHandler(0), m_messageType(QtMsgType()), m_description(QString()),
m_sourceLocation(QSourceLocation()){}
inline QString statusMessage() const {return m_description;}
inline qint64 line() const {return m_sourceLocation.line();}
inline qint64 column() const {return m_sourceLocation.column();}
protected:
virtual void handleMessage(QtMsgType type, const QString &description,
const QUrl &identifier, const QSourceLocation &sourceLocation)
{
Q_UNUSED(type);
Q_UNUSED(identifier);
m_messageType = type;
m_description = description;
m_sourceLocation = sourceLocation;
}
private:
QtMsgType m_messageType;
QString m_description;
QSourceLocation m_sourceLocation;
};
VDomDocument::VDomDocument(VContainer *data)
: QDomDocument(), data(data), map(QHash<QString, QDomElement>())
{
@ -162,3 +193,53 @@ QString VDomDocument::UniqueTagText(const QString &tagName, const QString &defVa
}
return defVal;
}
bool VDomDocument::ValidatePattern(const QString &schema, const QString &fileName, QString &errorMsg, qint64 &errorLine,
qint64 &errorColumn)
{
errorLine = -1;
errorColumn = -1;
QFile pattern(fileName);
if (pattern.open(QIODevice::ReadOnly) == false)
{
errorMsg = QString(tr("Can't open file %1:\n%2.").arg(fileName).arg(pattern.errorString()));
return false;
}
QFile fileSchema(schema);
if (fileSchema.open(QIODevice::ReadOnly) == false)
{
errorMsg = QString(tr("Can't open schema file %1:\n%2.").arg(schema).arg(fileSchema.errorString()));
return false;
}
MessageHandler messageHandler;
QXmlSchema sch;
sch.setMessageHandler(&messageHandler);
sch.load(&fileSchema, QUrl::fromLocalFile(fileSchema.fileName()));
bool errorOccurred = false;
if (sch.isValid() == false)
{
errorOccurred = true;
}
else
{
QXmlSchemaValidator validator(sch);
if (validator.validate(&pattern, QUrl::fromLocalFile(pattern.fileName())) == false)
{
errorOccurred = true;
}
}
if (errorOccurred)
{
errorMsg = messageHandler.statusMessage();
errorLine = messageHandler.line();
errorColumn = messageHandler.column();
return false;
}
else
{
return true;
}
}

View File

@ -101,6 +101,17 @@ public:
*/
qreal GetParametrDouble(const QDomElement& domElement, const QString &name, const QString &defValue) const;
QString UniqueTagText(const QString &tagName, const QString &defVal = QString()) const;
/**
* @brief ValidatePattern validate pattern file by xsd schema.
* @param schema path to schema file.
* @param fileName name of pattern file.
* @param errorMsg error message.
* @param errorLine number error line.
* @param errorColumn number error column.
* @return true if validation successful.
*/
static bool ValidatePattern(const QString &schema, const QString &fileName, QString &errorMsg, qint64 &errorLine,
qint64 &errorColumn);
protected:
/**
* @brief data container with data.