Skip to content
Permalink
Browse files

accept layer list and individual layers as dxf inputs

  • Loading branch information
alexbruy committed Nov 2, 2020
1 parent ad6922b commit 6f10e65120b88b9d6d37f0f44f54a66d1c750675
@@ -22,6 +22,9 @@ item is a map (QVariantMap) in this form:
'attributeIndex': int
}

Also it can accept lists (either string lists or QgsVectorLayer list)
as well as individual layer object or string representing layer source.

Static functions :py:func:`~parametersAsLayers`, :py:func:`~variantMapAsLayer`,
:py:func:`~layerAsVariantMap` provide conversion between QgsDxfExport.DxfLayer
representation and QVariant representation.
@@ -37,40 +37,89 @@ bool QgsProcessingParameterDxfLayers::checkValueIsAcceptable( const QVariant &in
if ( !input.isValid() )
return mFlags & FlagOptional;

if ( input.type() != QVariant::List )
return false;

const QVariantList layerList = input.toList();
if ( layerList.isEmpty() )
return false;

for ( const QVariant &variantLayer : layerList )
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
{
if ( variantLayer.type() != QVariant::Map )
return false;

QVariantMap layerMap = variantLayer.toMap();
return true;
}

if ( !layerMap.contains( QStringLiteral( "layer" ) ) || !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
return false;
if ( input.type() == QVariant::String )
{
if ( input.toString().isEmpty() )
return mFlags & FlagOptional;

if ( !context )
continue; // when called without context, we will skip checking whether the layer can be resolved
return true;

QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
return false;
QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
return mapLayer && ( mapLayer->type() == QgsMapLayerType::VectorLayer );
}
else if ( input.type() == QVariant::List )
{
if ( input.toList().isEmpty() )
return mFlags & FlagOptional;;

QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
const QVariantList layerList = input.toList();
for ( const QVariant &variantLayer : layerList )
{
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( variantLayer ) ) )
continue;

if ( variantLayer.type() == QVariant::String )
{
if ( !context )
return true;

QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
return false;
}
else if ( variantLayer.type() == QVariant::Map )
{
QVariantMap layerMap = variantLayer.toMap();

if ( !layerMap.contains( QStringLiteral( "layer" ) ) && !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
return false;

if ( !context )
return true;

QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
return false;

QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );

if ( !vectorLayer )
return false;

if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
return false;
}
else
{
return false;
}
}
return true;
}
else if ( input.type() == QVariant::StringList )
{
const auto constToStringList = input.toStringList();
if ( constToStringList.isEmpty() )
return mFlags & FlagOptional;

if ( !vectorLayer )
return false;
if ( !context )
return true;

if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
return false;
for ( const QString &v : constToStringList )
{
if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
return false;
}
return true;
}

return true;
return false;
}

QString QgsProcessingParameterDxfLayers::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
@@ -32,6 +32,9 @@
* 'attributeIndex': int
* }
*
* Also it can accept lists (either string lists or QgsVectorLayer list)
* as well as individual layer object or string representing layer source.
*
* Static functions parametersAsLayers(), variantMapAsLayer(),
* layerAsVariantMap() provide conversion between QgsDxfExport::DxfLayer
* representation and QVariant representation.
@@ -8378,8 +8378,20 @@ void TestQgsProcessing::parameterDxfLayers()

std::unique_ptr< QgsProcessingParameterDxfLayers > def( new QgsProcessingParameterDxfLayers( "dxf input layer" ) );
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
QVERIFY( def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( vectorLayer ) ) );

// should also be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
QVERIFY( def->checkValueIsAcceptable( QStringList() << "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
QVERIFY( def->checkValueIsAcceptable( QVariantList() << "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
// ... unless we use context, when the check that the layer actually exists is performed
QVERIFY( !def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp", &context ) );
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp", &context ) );
QVERIFY( !def->checkValueIsAcceptable( QVariantList() << "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp", &context ) );

QVariantList layerList;
QVERIFY( !def->checkValueIsAcceptable( layerList ) );
QVariantMap layerMap;

0 comments on commit 6f10e65

Please sign in to comment.
You can’t perform that action at this time.