Skip to content
Permalink
Browse files
allow string and number for QgsProcessingParameterMeshDatasetGroup
  • Loading branch information
vcloarec authored and nyalldawson committed Jun 10, 2021
1 parent b0c8c65 commit ad69c45f2f73c893e4757d6acd898a75f05b9239
@@ -275,8 +275,6 @@ bool QgsExportMeshOnElement::prepareAlgorithm( const QVariantMap &parameters, Qg
feedback->setProgressText( QObject::tr( "Preparing data" ) );
}

QDateTime layerReferenceTime = static_cast<QgsMeshLayerTemporalProperties *>( meshLayer->temporalProperties() )->referenceTime();

// Extract the date time used to export dataset values under a relative time
QVariant parameterTimeVariant = parameters.value( QStringLiteral( "DATASET_TIME" ) );
QgsInterval relativeTime = datasetRelativetime( parameterTimeVariant, meshLayer, context );
@@ -121,11 +121,20 @@ QList<int> QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( const Q
ret << 0;
else
{
QVariantList list = value.toList();
if ( list.isEmpty() )
ret << 0;
for ( const QVariant &v : list )
ret.append( v.toInt() );
if ( value.type() == QVariant::List )
{
//here we can't use QgsProcessingParameters::parameterAsInts() because this method return empry list when first value is 0...
const QVariantList varList = value.toList();
if ( varList.isEmpty() )
ret << 0;
else
for ( const QVariant &v : varList )
ret << v.toInt();
}
else
{
ret << value.toInt();
}
}

return ret;
@@ -137,15 +146,23 @@ bool QgsProcessingParameterMeshDatasetGroups::valueIsAcceptable( const QVariant
return allowEmpty;

if ( input.type() != QVariant::List )
return false;
{
bool ok = false;
input.toInt( &ok );
return ok;
}
const QVariantList list = input.toList();

if ( !allowEmpty && list.isEmpty() )
return false;

for ( const QVariant &var : list )
if ( var.type() != QVariant::Int )
{
bool ok = false;
var.toInt( &ok );
if ( !ok )
return false;
}

return true;
}
@@ -16,6 +16,7 @@
#include "qgsprocessingmeshdatasetwidget.h"
#include "qgsdatetimeedit.h"
#include "qgsprocessingmultipleselectiondialog.h"
#include "qgsprocessingoutputs.h"
#include "qgsmeshlayer.h"
#include "qgsmeshlayerutils.h"
#include "qgsmeshlayertemporalproperties.h"
@@ -267,7 +268,15 @@ void QgsProcessingMeshDatasetGroupsWidgetWrapper::setMeshLayerWrapperValue( cons

QStringList QgsProcessingMeshDatasetGroupsWidgetWrapper::compatibleParameterTypes() const
{
return QStringList() << QgsProcessingParameterMeshDatasetGroups::typeName();
return QStringList() << QgsProcessingParameterMeshDatasetGroups::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterNumber::typeName();
}

QStringList QgsProcessingMeshDatasetGroupsWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputString::typeName()
<< QgsProcessingOutputNumber::typeName();
}

QWidget *QgsProcessingMeshDatasetGroupsWidgetWrapper::createWidget() SIP_FACTORY
@@ -283,9 +292,25 @@ QWidget *QgsProcessingMeshDatasetGroupsWidgetWrapper::createWidget() SIP_FACTORY

void QgsProcessingMeshDatasetGroupsWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
Q_UNUSED( context );
if ( mWidget )
mWidget->setValue( value );
if ( !mWidget )
return;

QList<int> datasetGroupIndexes;
if ( value.type() == QVariant::List )
{
//here we can't use QgsProcessingParameters::parameterAsInts() because this method return empry list when first value is 0...
const QVariantList varList = value.toList();
for ( const QVariant &v : varList )
datasetGroupIndexes.append( QgsProcessingParameters::parameterAsInt( parameterDefinition(), v, context ) );
}
else
datasetGroupIndexes.append( QgsProcessingParameters::parameterAsInt( parameterDefinition(), value, context ) );

QVariantList varList;
for ( const int index : std::as_const( datasetGroupIndexes ) )
varList.append( index );

mWidget->setValue( varList );
}

QVariant QgsProcessingMeshDatasetGroupsWidgetWrapper::widgetValue() const
@@ -85,7 +85,7 @@ class GUI_EXPORT QgsProcessingMeshDatasetGroupsWidgetWrapper : public QgsAbstra

protected:
QStringList compatibleParameterTypes() const override;
QStringList compatibleOutputTypes() const override {return QStringList();}
QStringList compatibleOutputTypes() const override;
QWidget *createWidget() override;
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;
@@ -8217,22 +8217,32 @@ void TestQgsProcessing::parameterMeshDatasetGroups()
QgsProject project;
context.setProject( &project );

QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariant() ), QList<int>( {0} ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList() ), QList<int>( {0} ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( 3 ), QList<int>( {3} ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariant( "3" ) ), QList<int>( {3} ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { "3", "4", "5"} ) ), QList<int>( {3, 4, 5 } ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { 3, 4, 5} ) ), QList<int>( {3, 4, 5 } ) );
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { 3.0, 4.0, 5.0} ) ), QList<int>( {3, 4, 5 } ) );

QSet<int> supportedData;
supportedData << QgsMeshDatasetGroupMetadata::DataOnVertices;
std::unique_ptr< QgsProcessingParameterMeshDatasetGroups> def(
new QgsProcessingParameterMeshDatasetGroups( QStringLiteral( "dataset groups" ), QStringLiteral( "groups" ), QString(), supportedData ) );

QVERIFY( def->type() == QLatin1String( "meshdatasetgroups" ) );
QVERIFY( def->isDataTypeSupported( QgsMeshDatasetGroupMetadata::DataOnVertices ) );
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
QVERIFY( !def->checkValueIsAcceptable( 1.0 ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( 1.0 ) );
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariantList() << "a" << "b" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) ); //not optional

QVariantList groupsList;
QVERIFY( !def->checkValueIsAcceptable( groupsList ) );
QVERIFY( !def->checkValueIsAcceptable( groupsList ) ); //not optional

groupsList.append( 0 );
QVERIFY( def->checkValueIsAcceptable( groupsList ) );
groupsList.append( 5 );
@@ -8255,8 +8265,8 @@ void TestQgsProcessing::parameterMeshDatasetGroups()
QStringLiteral( "layer parameter" ),
supportedData, true ) );
QVERIFY( def->isDataTypeSupported( QgsMeshDatasetGroupMetadata::DataOnFaces ) );
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
QVERIFY( !def->checkValueIsAcceptable( 1.0 ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( 1.0 ) );
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
@@ -8295,9 +8305,11 @@ void TestQgsProcessing::parameterMeshDatasetTime()
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariantList() << "a" << "b" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QStringList() ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

QVariantMap value;
QVERIFY( !def->checkValueIsAcceptable( value ) );
value[QStringLiteral( "test" )] = QStringLiteral( "test" );
QVERIFY( !def->checkValueIsAcceptable( value ) );

@@ -9472,6 +9472,15 @@ void TestProcessingGui::testMeshDatasetWrapperLayerInProject()
QCOMPARE( pythonString, QStringLiteral( "{'type': 'static'}" ) );
QVERIFY( timeDefinition.checkValueIsAcceptable( timeWrapper.widgetValue() ) );
QCOMPARE( QgsProcessingParameterMeshDatasetTime::valueAsTimeType( timeWrapper.widgetValue() ), QStringLiteral( "static" ) );

groupsWrapper.setWidgetValue( 3, context );
QCOMPARE( datasetGroupWidget->value(), QVariantList( {3} ) );
groupsWrapper.setWidgetValue( QVariantList( {1, 2, 3} ), context );
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {1, 2, 3} ) );
groupsWrapper.setWidgetValue( QVariantList( {"1", "2", "3"} ), context );
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {1, 2, 3} ) );
groupsWrapper.setWidgetValue( QgsProperty::fromExpression( QStringLiteral( "1+3" ) ), context );
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {4} ) );
}

void TestProcessingGui::testMeshDatasetWrapperLayerOutsideProject()

0 comments on commit ad69c45

Please sign in to comment.