Skip to content

Commit

Permalink
Add method to get equivalent QVariant type for a point cloud attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2021
1 parent 176e972 commit d6c0b2f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Expand Up @@ -57,6 +57,15 @@ Returns size of the attribute in bytes
DataType type() const;
%Docstring
Returns the data type

.. seealso:: :py:func:`variantType`
%End

QVariant::Type variantType() const;
%Docstring
Returns the most suitable equivalent QVariant data type to this attribute type.

.. seealso:: :py:func:`type`
%End

QString displayType() const;
Expand Down
17 changes: 17 additions & 0 deletions src/core/pointcloud/qgspointcloudattribute.cpp
Expand Up @@ -27,6 +27,23 @@ QgsPointCloudAttribute::QgsPointCloudAttribute( const QString &name, DataType ty
updateSize();
}

QVariant::Type QgsPointCloudAttribute::variantType() const
{
switch ( mType )
{
case DataType::Char:
case DataType::Short:
case DataType::UShort:
case DataType::Int32:
return QVariant::Int;

case DataType::Float:
case DataType::Double:
return QVariant::Double;
}
return QVariant::Invalid;
}

QString QgsPointCloudAttribute::displayType() const
{
switch ( mType )
Expand Down
13 changes: 12 additions & 1 deletion src/core/pointcloud/qgspointcloudattribute.h
Expand Up @@ -60,9 +60,20 @@ class CORE_EXPORT QgsPointCloudAttribute
//! Returns size of the attribute in bytes
int size() const { return mSize; }

//! Returns the data type
/**
* Returns the data type
*
* \see variantType()
*/
DataType type() const { return mType; }

/**
* Returns the most suitable equivalent QVariant data type to this attribute type.
*
* \see type()
*/
QVariant::Type variantType() const;

/**
* Returns the type to use when displaying this field.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/src/core/testqgspointcloudattribute.cpp
Expand Up @@ -40,6 +40,7 @@ class TestQgsPointCloudAttribute: public QObject
void cleanup();// will be called after every testfunction.
void testAttribute();
void testAttributeDisplayType();
void testVariantType();
void testIsNumeric();
void testCollection();

Expand Down Expand Up @@ -92,6 +93,16 @@ void TestQgsPointCloudAttribute::testAttributeDisplayType()
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Double ).displayType(), QStringLiteral( "Double" ) );
}

void TestQgsPointCloudAttribute::testVariantType()
{
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Char ).variantType(), QVariant::Int );
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Short ).variantType(), QVariant::Int );
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::UShort ).variantType(), QVariant::Int );
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Int32 ).variantType(), QVariant::Int );
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Float ).variantType(), QVariant::Double );
QCOMPARE( QgsPointCloudAttribute( QStringLiteral( "x" ), QgsPointCloudAttribute::DataType::Double ).variantType(), QVariant::Double );
}

void TestQgsPointCloudAttribute::testIsNumeric()
{
QVERIFY( !QgsPointCloudAttribute::isNumeric( QgsPointCloudAttribute::DataType::Char ) );
Expand Down

0 comments on commit d6c0b2f

Please sign in to comment.