Skip to content

Commit ad69c45

Browse files
vcloarecnyalldawson
authored andcommittedJun 10, 2021
allow string and number for QgsProcessingParameterMeshDatasetGroup
1 parent b0c8c65 commit ad69c45

File tree

6 files changed

+81
-20
lines changed

6 files changed

+81
-20
lines changed
 

‎src/analysis/processing/qgsalgorithmexportmesh.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ bool QgsExportMeshOnElement::prepareAlgorithm( const QVariantMap &parameters, Qg
275275
feedback->setProgressText( QObject::tr( "Preparing data" ) );
276276
}
277277

278-
QDateTime layerReferenceTime = static_cast<QgsMeshLayerTemporalProperties *>( meshLayer->temporalProperties() )->referenceTime();
279-
280278
// Extract the date time used to export dataset values under a relative time
281279
QVariant parameterTimeVariant = parameters.value( QStringLiteral( "DATASET_TIME" ) );
282280
QgsInterval relativeTime = datasetRelativetime( parameterTimeVariant, meshLayer, context );

‎src/core/processing/qgsprocessingparametermeshdataset.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,20 @@ QList<int> QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( const Q
121121
ret << 0;
122122
else
123123
{
124-
QVariantList list = value.toList();
125-
if ( list.isEmpty() )
126-
ret << 0;
127-
for ( const QVariant &v : list )
128-
ret.append( v.toInt() );
124+
if ( value.type() == QVariant::List )
125+
{
126+
//here we can't use QgsProcessingParameters::parameterAsInts() because this method return empry list when first value is 0...
127+
const QVariantList varList = value.toList();
128+
if ( varList.isEmpty() )
129+
ret << 0;
130+
else
131+
for ( const QVariant &v : varList )
132+
ret << v.toInt();
133+
}
134+
else
135+
{
136+
ret << value.toInt();
137+
}
129138
}
130139

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

139148
if ( input.type() != QVariant::List )
140-
return false;
149+
{
150+
bool ok = false;
151+
input.toInt( &ok );
152+
return ok;
153+
}
141154
const QVariantList list = input.toList();
142155

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

146159
for ( const QVariant &var : list )
147-
if ( var.type() != QVariant::Int )
160+
{
161+
bool ok = false;
162+
var.toInt( &ok );
163+
if ( !ok )
148164
return false;
165+
}
149166

150167
return true;
151168
}

‎src/gui/processing/qgsprocessingmeshdatasetwidget.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "qgsprocessingmeshdatasetwidget.h"
1717
#include "qgsdatetimeedit.h"
1818
#include "qgsprocessingmultipleselectiondialog.h"
19+
#include "qgsprocessingoutputs.h"
1920
#include "qgsmeshlayer.h"
2021
#include "qgsmeshlayerutils.h"
2122
#include "qgsmeshlayertemporalproperties.h"
@@ -267,7 +268,15 @@ void QgsProcessingMeshDatasetGroupsWidgetWrapper::setMeshLayerWrapperValue( cons
267268

268269
QStringList QgsProcessingMeshDatasetGroupsWidgetWrapper::compatibleParameterTypes() const
269270
{
270-
return QStringList() << QgsProcessingParameterMeshDatasetGroups::typeName();
271+
return QStringList() << QgsProcessingParameterMeshDatasetGroups::typeName()
272+
<< QgsProcessingParameterString::typeName()
273+
<< QgsProcessingParameterNumber::typeName();
274+
}
275+
276+
QStringList QgsProcessingMeshDatasetGroupsWidgetWrapper::compatibleOutputTypes() const
277+
{
278+
return QStringList() << QgsProcessingOutputString::typeName()
279+
<< QgsProcessingOutputNumber::typeName();
271280
}
272281

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

284293
void QgsProcessingMeshDatasetGroupsWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
285294
{
286-
Q_UNUSED( context );
287-
if ( mWidget )
288-
mWidget->setValue( value );
295+
if ( !mWidget )
296+
return;
297+
298+
QList<int> datasetGroupIndexes;
299+
if ( value.type() == QVariant::List )
300+
{
301+
//here we can't use QgsProcessingParameters::parameterAsInts() because this method return empry list when first value is 0...
302+
const QVariantList varList = value.toList();
303+
for ( const QVariant &v : varList )
304+
datasetGroupIndexes.append( QgsProcessingParameters::parameterAsInt( parameterDefinition(), v, context ) );
305+
}
306+
else
307+
datasetGroupIndexes.append( QgsProcessingParameters::parameterAsInt( parameterDefinition(), value, context ) );
308+
309+
QVariantList varList;
310+
for ( const int index : std::as_const( datasetGroupIndexes ) )
311+
varList.append( index );
312+
313+
mWidget->setValue( varList );
289314
}
290315

291316
QVariant QgsProcessingMeshDatasetGroupsWidgetWrapper::widgetValue() const

‎src/gui/processing/qgsprocessingmeshdatasetwidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class GUI_EXPORT QgsProcessingMeshDatasetGroupsWidgetWrapper : public QgsAbstra
8585

8686
protected:
8787
QStringList compatibleParameterTypes() const override;
88-
QStringList compatibleOutputTypes() const override {return QStringList();}
88+
QStringList compatibleOutputTypes() const override;
8989
QWidget *createWidget() override;
9090
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
9191
QVariant widgetValue() const override;

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8217,22 +8217,32 @@ void TestQgsProcessing::parameterMeshDatasetGroups()
82178217
QgsProject project;
82188218
context.setProject( &project );
82198219

8220+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariant() ), QList<int>( {0} ) );
8221+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList() ), QList<int>( {0} ) );
8222+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( 3 ), QList<int>( {3} ) );
8223+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariant( "3" ) ), QList<int>( {3} ) );
8224+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { "3", "4", "5"} ) ), QList<int>( {3, 4, 5 } ) );
8225+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { 3, 4, 5} ) ), QList<int>( {3, 4, 5 } ) );
8226+
QCOMPARE( QgsProcessingParameterMeshDatasetGroups::valueAsDatasetGroup( QVariantList( { 3.0, 4.0, 5.0} ) ), QList<int>( {3, 4, 5 } ) );
8227+
82208228
QSet<int> supportedData;
82218229
supportedData << QgsMeshDatasetGroupMetadata::DataOnVertices;
82228230
std::unique_ptr< QgsProcessingParameterMeshDatasetGroups> def(
82238231
new QgsProcessingParameterMeshDatasetGroups( QStringLiteral( "dataset groups" ), QStringLiteral( "groups" ), QString(), supportedData ) );
82248232

82258233
QVERIFY( def->type() == QLatin1String( "meshdatasetgroups" ) );
82268234
QVERIFY( def->isDataTypeSupported( QgsMeshDatasetGroupMetadata::DataOnVertices ) );
8227-
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
8228-
QVERIFY( !def->checkValueIsAcceptable( 1.0 ) );
8235+
QVERIFY( def->checkValueIsAcceptable( 1 ) );
8236+
QVERIFY( def->checkValueIsAcceptable( 1.0 ) );
82298237
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
82308238
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
82318239
QVERIFY( !def->checkValueIsAcceptable( QVariantList() << "a" << "b" ) );
82328240
QVERIFY( !def->checkValueIsAcceptable( "" ) );
8233-
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
8241+
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) ); //not optional
8242+
82348243
QVariantList groupsList;
8235-
QVERIFY( !def->checkValueIsAcceptable( groupsList ) );
8244+
QVERIFY( !def->checkValueIsAcceptable( groupsList ) ); //not optional
8245+
82368246
groupsList.append( 0 );
82378247
QVERIFY( def->checkValueIsAcceptable( groupsList ) );
82388248
groupsList.append( 5 );
@@ -8255,8 +8265,8 @@ void TestQgsProcessing::parameterMeshDatasetGroups()
82558265
QStringLiteral( "layer parameter" ),
82568266
supportedData, true ) );
82578267
QVERIFY( def->isDataTypeSupported( QgsMeshDatasetGroupMetadata::DataOnFaces ) );
8258-
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
8259-
QVERIFY( !def->checkValueIsAcceptable( 1.0 ) );
8268+
QVERIFY( def->checkValueIsAcceptable( 1 ) );
8269+
QVERIFY( def->checkValueIsAcceptable( 1.0 ) );
82608270
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
82618271
QVERIFY( !def->checkValueIsAcceptable( "" ) );
82628272
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
@@ -8295,9 +8305,11 @@ void TestQgsProcessing::parameterMeshDatasetTime()
82958305
QVERIFY( !def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
82968306
QVERIFY( !def->checkValueIsAcceptable( QVariantList() << "a" << "b" ) );
82978307
QVERIFY( !def->checkValueIsAcceptable( "" ) );
8308+
QVERIFY( !def->checkValueIsAcceptable( QStringList() ) );
82988309
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
82998310

83008311
QVariantMap value;
8312+
QVERIFY( !def->checkValueIsAcceptable( value ) );
83018313
value[QStringLiteral( "test" )] = QStringLiteral( "test" );
83028314
QVERIFY( !def->checkValueIsAcceptable( value ) );
83038315

‎tests/src/gui/testprocessinggui.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9472,6 +9472,15 @@ void TestProcessingGui::testMeshDatasetWrapperLayerInProject()
94729472
QCOMPARE( pythonString, QStringLiteral( "{'type': 'static'}" ) );
94739473
QVERIFY( timeDefinition.checkValueIsAcceptable( timeWrapper.widgetValue() ) );
94749474
QCOMPARE( QgsProcessingParameterMeshDatasetTime::valueAsTimeType( timeWrapper.widgetValue() ), QStringLiteral( "static" ) );
9475+
9476+
groupsWrapper.setWidgetValue( 3, context );
9477+
QCOMPARE( datasetGroupWidget->value(), QVariantList( {3} ) );
9478+
groupsWrapper.setWidgetValue( QVariantList( {1, 2, 3} ), context );
9479+
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {1, 2, 3} ) );
9480+
groupsWrapper.setWidgetValue( QVariantList( {"1", "2", "3"} ), context );
9481+
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {1, 2, 3} ) );
9482+
groupsWrapper.setWidgetValue( QgsProperty::fromExpression( QStringLiteral( "1+3" ) ), context );
9483+
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {4} ) );
94759484
}
94769485

94779486
void TestProcessingGui::testMeshDatasetWrapperLayerOutsideProject()

0 commit comments

Comments
 (0)
Please sign in to comment.