Skip to content

Commit

Permalink
QgsField serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed May 18, 2015
1 parent 6e3cbe3 commit e474e09
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ bool QgsField::convertCompatible( QVariant& v ) const
return true;
}

QDataStream& operator<<( QDataStream& out, const QgsField& field )
{
out << field.name();
out << ( quint32 )field.type();
out << field.typeName();
out << field.length();
out << field.precision();
out << field.comment();
return out;
}

QDataStream& operator>>( QDataStream& in, QgsField& field )
{
quint32 type, length, precision;
QString name, typeName, comment;
in >> name >> type >> typeName >> length >> precision >> comment;
field.setName( name );
field.setType(( QVariant::Type )type );
field.setTypeName( typeName );
field.setLength(( int )length );
field.setPrecision(( int )precision );
field.setComment( comment );
return in;
}

////////////////////////////////////////////////////////////////////////////

QgsFields::QgsFields()
Expand Down Expand Up @@ -341,3 +366,27 @@ QgsAttributeList QgsFields::allAttributesList() const
lst.append( i );
return lst;
}

QDataStream& operator<<( QDataStream& out, const QgsFields& fields )
{
out << ( quint32 )fields.size();
for ( int i = 0; i < fields.size(); i++ )
{
out << fields.field( i );
}
return out;
}

QDataStream& operator>>( QDataStream& in, QgsFields& fields )
{
fields.clear();
quint32 size;
in >> size;
for ( quint32 i = 0; i < size; i++ )
{
QgsField field;
in >> field;
fields.append( field );
}
return in;
}
9 changes: 9 additions & 0 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class CORE_EXPORT QgsField

Q_DECLARE_METATYPE( QgsField );

/** Writes the field to stream out. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsField& field );
/** Reads a field from stream in into field. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsField& field );

/** \class QgsFields
* \ingroup core
Expand Down Expand Up @@ -274,4 +278,9 @@ class CORE_EXPORT QgsFields

Q_DECLARE_METATYPE( QgsFields );

/** Writes the fields to stream out. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFields& fields );
/** Reads fields from stream in into fields. QGIS version compatibility is not guaranteed. */
CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFields& fields );

#endif

0 comments on commit e474e09

Please sign in to comment.