Make full serialization/deserialization for class VLayoutPiece.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-07-01 12:26:08 +03:00
parent 35eb1cecff
commit 4ab84bce38
4 changed files with 97 additions and 0 deletions

View File

@ -42,6 +42,9 @@
#include <QPainterPath> #include <QPainterPath>
#include <QTemporaryFile> #include <QTemporaryFile>
const quint32 VAbstractPieceData::streamHeader = 0x05CDD73A; // CRC-32Q string "VAbstractPieceData"
const quint16 VAbstractPieceData::classVersion = 1;
const qreal maxL = 2.5; const qreal maxL = 2.5;
const qreal VSAPoint::passmarkFactor = 0.5; const qreal VSAPoint::passmarkFactor = 0.5;
@ -767,6 +770,21 @@ QT_WARNING_POP
#endif #endif
} }
// Friend functions
//---------------------------------------------------------------------------------------------------------------------
QDataStream &operator<<(QDataStream &dataStream, const VAbstractPiece &piece)
{
dataStream << *piece.d;
return dataStream;
}
//---------------------------------------------------------------------------------------------------------------------
QDataStream &operator>>(QDataStream &dataStream, VAbstractPiece &piece)
{
dataStream >> *piece.d;
return dataStream;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VAbstractPiece::VAbstractPiece() VAbstractPiece::VAbstractPiece()
: d(new VAbstractPieceData) : d(new VAbstractPieceData)

View File

@ -231,6 +231,9 @@ public:
static QVector<QPointF> GrainlinePoints(const VGrainlineData &geom, const VContainer *pattern, static QVector<QPointF> GrainlinePoints(const VGrainlineData &geom, const VContainer *pattern,
const QRectF &boundingRect, qreal &dAng); const QRectF &boundingRect, qreal &dAng);
friend QDataStream& operator<< (QDataStream& dataStream, const VAbstractPiece& piece);
friend QDataStream& operator>> (QDataStream& dataStream, VAbstractPiece& piece);
protected: protected:
template <class T> template <class T>
static QVector<T> RemoveDublicates(const QVector<T> &points, bool removeFirstAndLast = true); static QVector<T> RemoveDublicates(const QVector<T> &points, bool removeFirstAndLast = true);

View File

@ -34,7 +34,9 @@
#include <QCoreApplication> #include <QCoreApplication>
#include "../vmisc/diagnostic.h" #include "../vmisc/diagnostic.h"
#include "../vmisc/vdatastreamenum.h"
#include "../vmisc/defglobal.h" #include "../vmisc/defglobal.h"
#include "../ifc/exception/vexception.h"
QT_WARNING_PUSH QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Weffc++") QT_WARNING_DISABLE_GCC("-Weffc++")
@ -71,6 +73,9 @@ public:
~VAbstractPieceData() Q_DECL_EQ_DEFAULT; ~VAbstractPieceData() Q_DECL_EQ_DEFAULT;
friend QDataStream& operator<<(QDataStream& dataStream, const VAbstractPieceData& piece);
friend QDataStream& operator>>(QDataStream& dataStream, VAbstractPieceData& piece);
QString m_name; QString m_name;
/** @brief forbidFlipping forbid piece be mirrored in a layout. */ /** @brief forbidFlipping forbid piece be mirrored in a layout. */
bool m_forbidFlipping; bool m_forbidFlipping;
@ -84,9 +89,78 @@ public:
private: private:
Q_DISABLE_ASSIGN(VAbstractPieceData) Q_DISABLE_ASSIGN(VAbstractPieceData)
static const quint32 streamHeader;
static const quint16 classVersion;
}; };
QT_WARNING_POP QT_WARNING_POP
// Friend functions
//---------------------------------------------------------------------------------------------------------------------
inline QDataStream &operator<<(QDataStream &dataStream, const VAbstractPieceData &piece)
{
dataStream << VAbstractPieceData::streamHeader << VAbstractPieceData::classVersion;
// Added in classVersion = 1
dataStream << piece.m_name;
dataStream << piece.m_forbidFlipping;
dataStream << piece.m_forceFlipping;
dataStream << piece.m_seamAllowance;
dataStream << piece.m_seamAllowanceBuiltIn;
dataStream << piece.m_hideMainPath;
dataStream << piece.m_width;
dataStream << piece.m_mx;
dataStream << piece.m_my;
// Added in classVersion = 2
return dataStream;
}
//---------------------------------------------------------------------------------------------------------------------
inline QDataStream &operator>>(QDataStream &dataStream, VAbstractPieceData &piece)
{
quint32 actualStreamHeader = 0;
dataStream >> actualStreamHeader;
if (actualStreamHeader != VAbstractPieceData::streamHeader)
{
QString message = QCoreApplication::tr("VAbstractPieceData prefix mismatch error: actualStreamHeader = 0x%1 "
"and streamHeader = 0x%2")
.arg(actualStreamHeader, 8, 0x10, QChar('0'))
.arg(VAbstractPieceData::streamHeader, 8, 0x10, QChar('0'));
throw VException(message);
}
quint16 actualClassVersion = 0;
dataStream >> actualClassVersion;
if (actualClassVersion > VAbstractPieceData::classVersion)
{
QString message = QCoreApplication::tr("VAbstractPieceData compatibility error: actualClassVersion = %1 and "
"classVersion = %2")
.arg(actualClassVersion).arg(VAbstractPieceData::classVersion);
throw VException(message);
}
dataStream >> piece.m_name;
dataStream >> piece.m_forbidFlipping;
dataStream >> piece.m_forceFlipping;
dataStream >> piece.m_seamAllowance;
dataStream >> piece.m_seamAllowanceBuiltIn;
dataStream >> piece.m_hideMainPath;
dataStream >> piece.m_width;
dataStream >> piece.m_mx;
dataStream >> piece.m_my;
// if (actualClassVersion >= 2)
// {
// }
return dataStream;
}
#endif // VABSTRACTPIECE_P_H #endif // VABSTRACTPIECE_P_H

View File

@ -363,6 +363,7 @@ QVector<VLayoutPassmark> ConvertPassmarks(const VPiece &piece, const VContainer
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QDataStream &operator<<(QDataStream &dataStream, const VLayoutPiece &piece) QDataStream &operator<<(QDataStream &dataStream, const VLayoutPiece &piece)
{ {
dataStream << static_cast<VAbstractPiece>(piece);
dataStream << *piece.d; dataStream << *piece.d;
return dataStream; return dataStream;
} }
@ -370,6 +371,7 @@ QDataStream &operator<<(QDataStream &dataStream, const VLayoutPiece &piece)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QDataStream &operator>>(QDataStream &dataStream, VLayoutPiece &piece) QDataStream &operator>>(QDataStream &dataStream, VLayoutPiece &piece)
{ {
dataStream >> static_cast<VAbstractPiece &>(piece);
dataStream >> *piece.d; dataStream >> *piece.d;
return dataStream; return dataStream;
} }