Skip to content
Permalink
Browse files
allow string and number for QgsProcessingParameterMeshDatasetTime
  • Loading branch information
vcloarec authored and nyalldawson committed Jun 10, 2021
1 parent ad69c45 commit 58646292e62599511a4a6678fddeb3f9196222da
@@ -198,17 +198,27 @@ QString QgsProcessingParameterMeshDatasetTime::valueAsPythonString( const QVaria
{
Q_UNUSED( context );
QStringList parts;
const QVariantMap variantTimeDataset = value.toMap();
parts << QStringLiteral( "'type': " ) + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( QStringLiteral( "type" ) ).toString() );
QString type = QgsProcessingUtils::variantToPythonLiteral( valueAsTimeType( value ) );
parts << QStringLiteral( "'type': " ) + type;

if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "dataset-time-step" ) )
if ( value.toDateTime().isValid() )
{
QVariantList datasetIndex = variantTimeDataset.value( QStringLiteral( "value" ) ).toList();
parts << QStringLiteral( "'value': " ) + QString( "[%1,%2]" ).arg( datasetIndex.at( 0 ).toString(), datasetIndex.at( 1 ).toString() );
QDateTime dateTime = value.toDateTime();
dateTime.setTimeSpec( Qt::UTC );
parts << QStringLiteral( "'value': " ) + QgsProcessingUtils::variantToPythonLiteral( dateTime );
}
else if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "defined-date-time" ) )
else
{
parts << QStringLiteral( "'value': " ) + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( QStringLiteral( "value" ) ) );
const QVariantMap variantTimeDataset = value.toMap();
if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "dataset-time-step" ) )
{
QVariantList datasetIndex = variantTimeDataset.value( QStringLiteral( "value" ) ).toList();
parts << QStringLiteral( "'value': " ) + QString( "[%1,%2]" ).arg( datasetIndex.at( 0 ).toString(), datasetIndex.at( 1 ).toString() );
}
else if ( variantTimeDataset.value( QStringLiteral( "type" ) ) == QLatin1String( "defined-date-time" ) )
{
parts << QStringLiteral( "'value': " ) + QgsProcessingUtils::variantToPythonLiteral( variantTimeDataset.value( QStringLiteral( "value" ) ) );
}
}

return parts.join( ',' ).prepend( '{' ).append( '}' );
@@ -264,6 +274,9 @@ QString QgsProcessingParameterMeshDatasetTime::valueAsTimeType( const QVariant &
if ( !valueIsAcceptable( value, false ) )
return QString();

if ( value.toDateTime().isValid() )
return QStringLiteral( "defined-date-time" );

return value.toMap().value( QStringLiteral( "type" ) ).toString();
}

@@ -278,6 +291,13 @@ QgsMeshDatasetIndex QgsProcessingParameterMeshDatasetTime::timeValueAsDatasetInd

QDateTime QgsProcessingParameterMeshDatasetTime::timeValueAsDefinedDateTime( const QVariant &value )
{
if ( value.toDateTime().isValid() )
{
QDateTime dateTime = value.toDateTime();
dateTime.setTimeSpec( Qt::UTC );
return dateTime;
}

if ( !valueIsAcceptable( value, false ) && valueAsTimeType( value ) != QLatin1String( "defined-date-time" ) )
return QDateTime();

@@ -289,6 +309,11 @@ bool QgsProcessingParameterMeshDatasetTime::valueIsAcceptable( const QVariant &i
if ( !input.isValid() )
return allowEmpty;

QDateTime timeDate = input.toDateTime();

if ( input.toDateTime().isValid() )
return true;

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

@@ -427,7 +427,16 @@ void QgsProcessingMeshDatasetTimeWidgetWrapper::setDatasetGroupIndexesWrapperVal

QStringList QgsProcessingMeshDatasetTimeWidgetWrapper::compatibleParameterTypes() const
{
return QStringList() << QgsProcessingParameterMeshDatasetTime::typeName();
return QStringList()
<< QgsProcessingParameterMeshDatasetTime::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterDateTime::typeName();
}

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

QWidget *QgsProcessingMeshDatasetTimeWidgetWrapper::createWidget()
@@ -522,10 +531,19 @@ void QgsProcessingMeshDatasetTimeWidget::setDatasetGroupIndexes( const QList<int

void QgsProcessingMeshDatasetTimeWidget::setValue( const QVariant &value )
{
if ( !value.isValid() || value.type() != QVariant::Map )
if ( !value.isValid() || ( value.type() != QVariant::Map && !value.toDateTime().isValid() ) )
return;

mValue = value.toMap();
mValue.clear();
if ( value.toDateTime().isValid() )
{
QDateTime dateTime = value.toDateTime();
dateTime.setTimeSpec( Qt::UTC );
mValue.insert( QStringLiteral( "type" ), QStringLiteral( "defined-date-time" ) );
mValue.insert( QStringLiteral( "value" ), dateTime );
}
else
mValue = value.toMap();

if ( !mValue.contains( QStringLiteral( "type" ) ) || !mValue.contains( QStringLiteral( "value" ) ) )
return;
@@ -539,14 +557,14 @@ void QgsProcessingMeshDatasetTimeWidget::setValue( const QVariant &value )
}
else if ( type == QLatin1String( "dataset-time-step" ) )
{
whileBlocking( radioButtonDatasetGroupTimeStep )->setChecked( true );
QVariantList dataset = mValue.value( QStringLiteral( "value" ) ).toList();
whileBlocking( comboBoxDatasetTimeStep )->setCurrentIndex( comboBoxDatasetTimeStep->findData( dataset ) );
whileBlocking( radioButtonDatasetGroupTimeStep )->setChecked( true );
}
else if ( type == QLatin1String( "defined-date-time" ) )
{
radioButtonDefinedDateTime->setChecked( true );
whileBlocking( dateTimeEdit )->setDate( mValue.value( QStringLiteral( "value" ) ).toDate() );
whileBlocking( dateTimeEdit )->setDateTime( mValue.value( QStringLiteral( "value" ) ).toDateTime() );
whileBlocking( radioButtonDefinedDateTime )->setChecked( true );
}
else if ( type == QLatin1String( "current-context-time" ) )
{
@@ -168,7 +168,7 @@ class GUI_EXPORT QgsProcessingMeshDatasetTimeWidgetWrapper : public QgsAbstract

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;
@@ -8299,6 +8299,8 @@ void TestQgsProcessing::parameterMeshDatasetTime()

std::unique_ptr< QgsProcessingParameterMeshDatasetTime> def( new QgsProcessingParameterMeshDatasetTime( QStringLiteral( "dataset groups" ), QStringLiteral( "groups" ) ) );
QVERIFY( def->type() == QLatin1String( "meshdatasettime" ) );
QVERIFY( def->checkValueIsAcceptable( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ) ).toString() ) );
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
QVERIFY( !def->checkValueIsAcceptable( 1.0 ) );
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
@@ -8308,6 +8310,13 @@ void TestQgsProcessing::parameterMeshDatasetTime()
QVERIFY( !def->checkValueIsAcceptable( QStringList() ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

QCOMPARE( QgsProcessingParameterMeshDatasetTime::valueAsTimeType( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ) ),
QStringLiteral( "defined-date-time" ) );
QCOMPARE( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ),
QgsProcessingParameterMeshDatasetTime::timeValueAsDefinedDateTime( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ) ) );
QCOMPARE( def->valueAsPythonString( QDateTime( QDate( 2020, 01, 01 ), QTime( 10, 0, 0 ) ), context ),
QStringLiteral( "{'type': 'defined-date-time','value': QDateTime(QDate(2020, 1, 1), QTime(10, 0, 0))}" ) );

QVariantMap value;
QVERIFY( !def->checkValueIsAcceptable( value ) );
value[QStringLiteral( "test" )] = QStringLiteral( "test" );
@@ -8322,7 +8331,6 @@ void TestQgsProcessing::parameterMeshDatasetTime()
QCOMPARE( def->valueAsPythonString( value, context ), QStringLiteral( "{'type': 'static'}" ) );
QCOMPARE( QgsProcessingParameterMeshDatasetTime::valueAsTimeType( value ), QStringLiteral( "static" ) );


value[QStringLiteral( "type" )] = QStringLiteral( "current-context-time" );
QVERIFY( def->checkValueIsAcceptable( value ) );
QCOMPARE( def->valueAsPythonString( value, context ), QStringLiteral( "{'type': 'current-context-time'}" ) );
@@ -9481,6 +9481,13 @@ void TestProcessingGui::testMeshDatasetWrapperLayerInProject()
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {1, 2, 3} ) );
groupsWrapper.setWidgetValue( QgsProperty::fromExpression( QStringLiteral( "1+3" ) ), context );
QCOMPARE( datasetGroupWidget->value().toList(), QVariantList( {4} ) );

timeWrapper.setWidgetValue( QDateTime( QDate( 2020, 01, 02 ), QTime( 1, 2, 3 ) ), context );
pythonString = timeDefinition.valueAsPythonString( timeWrapper.widgetValue(), context );
QCOMPARE( pythonString, QStringLiteral( "{'type': 'defined-date-time','value': QDateTime(QDate(2020, 1, 2), QTime(1, 2, 3))}" ) );
timeWrapper.setWidgetValue( QVariant::fromValue( QDateTime( QDate( 2020, 02, 01 ), QTime( 3, 2, 1 ) ) ).toString(), context );
pythonString = timeDefinition.valueAsPythonString( timeWrapper.widgetValue(), context );
QCOMPARE( pythonString, QStringLiteral( "{'type': 'defined-date-time','value': QDateTime(QDate(2020, 2, 1), QTime(3, 2, 1))}" ) );
}

void TestProcessingGui::testMeshDatasetWrapperLayerOutsideProject()

0 comments on commit 5864629

Please sign in to comment.