Skip to content

Commit

Permalink
Merge pull request #46439 from elpaso/bugfix-gh46369-value-relation-n…
Browse files Browse the repository at this point in the history
…ull-filtering

Treat empty array as NULL; and fix value relation filter with NULL
  • Loading branch information
elpaso committed Dec 11, 2021
2 parents 3864026 + a133362 commit 089d68f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -331,7 +331,8 @@ QSet<QString> QgsValueRelationFieldFormatter::expressionParentFormAttributes( co
QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[f->fnIndex()];
if ( formFunctions.contains( fd->name( ) ) )
{
for ( const auto &param : f->args( )->list() )
const QList<QgsExpressionNode *> cExpressionNodes { f->args( )->list() };
for ( const auto &param : std::as_const( cExpressionNodes ) )
{
attributes.insert( param->eval( &exp, &context ).toString() );
}
Expand All @@ -355,7 +356,8 @@ QSet<QString> QgsValueRelationFieldFormatter::expressionFormAttributes( const QS
QgsExpressionFunction *fd = QgsExpression::QgsExpression::Functions()[f->fnIndex()];
if ( formFunctions.contains( fd->name( ) ) )
{
for ( const auto &param : f->args( )->list() )
const QList<QgsExpressionNode *> cExpressionNodes { f->args( )->list() };
for ( const auto &param : std::as_const( cExpressionNodes ) )
{
attributes.insert( param->eval( &exp, &context ).toString() );
}
Expand All @@ -371,7 +373,7 @@ bool QgsValueRelationFieldFormatter::expressionIsUsable( const QString &expressi
const QSet<QString> attrs = expressionFormAttributes( expression );
for ( auto it = attrs.constBegin() ; it != attrs.constEnd(); it++ )
{
if ( ! feature.attribute( *it ).isValid() )
if ( feature.fieldNameIndex( *it ) < 0 )
return false;
}

Expand Down
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -77,6 +77,12 @@ QVariant QgsValueRelationWidgetWrapper::value() const
}
}

// If there is no selection and allow NULL is not checked return invalid.
if ( selection.isEmpty() && ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
return QVariant();
}

QVariantList vl;
//store as QVariantList because the field type supports data structure
for ( const QString &s : std::as_const( selection ) )
Expand Down
23 changes: 23 additions & 0 deletions tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -1337,6 +1337,10 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()

// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );
//check if first feature checked correctly (NULL)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
Expand All @@ -1346,9 +1350,16 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = false;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );

cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );
//check if first feature checked correctly (blank)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
Expand Down Expand Up @@ -1495,6 +1506,11 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

//check if first feature checked correctly (NULL)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );

Expand All @@ -1505,10 +1521,17 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = false;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

//check if first feature checked correctly (blank)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );

Expand Down

0 comments on commit 089d68f

Please sign in to comment.