Skip to content

Commit 8642e88

Browse files
committed
Fix relation reference widget when 'Chain Filters' is activated. Fixes #16903
1 parent d19b4aa commit 8642e88

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,29 @@ void QgsRelationReferenceWidget::filterChanged()
814814

815815
Q_ASSERT( scb );
816816

817+
QgsFeature f;
818+
QgsFeatureIds featureIds;
819+
QString filterExpression;
820+
821+
Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
822+
{
823+
if ( cb->currentIndex() != 0 )
824+
{
825+
const QString fieldName = cb->property( "Field" ).toString();
826+
827+
if ( cb->currentText() == nullValue.toString() )
828+
{
829+
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
830+
}
831+
else
832+
{
833+
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
834+
}
835+
attrs << mReferencedLayer->fields().lookupField( fieldName );
836+
}
837+
}
838+
839+
bool filtered = false;
817840
if ( mChainFilters )
818841
{
819842
QComboBox *ccb = nullptr;
@@ -834,6 +857,9 @@ void QgsRelationReferenceWidget::filterChanged()
834857
}
835858
else
836859
{
860+
const QString fieldName = cb->property( "Field" ).toString();
861+
filtered = true;
862+
837863
cb->blockSignals( true );
838864
cb->clear();
839865
cb->addItem( cb->property( "FieldAlias" ).toString() );
@@ -843,8 +869,29 @@ void QgsRelationReferenceWidget::filterChanged()
843869
QStringList texts;
844870
Q_FOREACH ( const QString &txt, mFilterCache[ccb->property( "Field" ).toString()][ccb->currentText()] )
845871
{
846-
texts << txt;
872+
QStringList filtersAttrs = filters;
873+
filtersAttrs << QgsExpression::createFieldEqualityExpression( fieldName, txt );
874+
QString expression = filtersAttrs.join( QStringLiteral( " AND " ) );
875+
876+
QgsAttributeList subset = attrs;
877+
subset << mReferencedLayer->fields().lookupField( fieldName );
878+
879+
QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( QgsFeatureRequest().setFilterExpression( expression ).setSubsetOfAttributes( subset ) ) );
880+
881+
bool found = false;
882+
while ( it.nextFeature( f ) )
883+
{
884+
if ( !featureIds.contains( f.id() ) )
885+
featureIds << f.id();
886+
887+
found = true;
888+
}
889+
890+
// item is only provided if at least 1 feature exists
891+
if ( found )
892+
texts << txt;
847893
}
894+
848895
texts.sort();
849896
cb->addItems( texts );
850897

@@ -856,34 +903,16 @@ void QgsRelationReferenceWidget::filterChanged()
856903
}
857904
}
858905

859-
Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
906+
if ( !mChainFilters || ( mChainFilters && !filtered ) )
860907
{
861-
if ( cb->currentIndex() != 0 )
862-
{
863-
const QString fieldName = cb->property( "Field" ).toString();
864-
865-
if ( cb->currentText() == nullValue.toString() )
866-
{
867-
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
868-
}
869-
else
870-
{
871-
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
872-
}
873-
attrs << mReferencedLayer->fields().lookupField( fieldName );
874-
}
875-
}
876-
877-
QString filterExpression = filters.join( QStringLiteral( " AND " ) );
908+
filterExpression = filters.join( QStringLiteral( " AND " ) );
878909

879-
QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( QgsFeatureRequest().setFilterExpression( filterExpression ).setSubsetOfAttributes( attrs ) ) );
910+
QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( QgsFeatureRequest().setFilterExpression( filterExpression ).setSubsetOfAttributes( attrs ) ) );
880911

881-
QgsFeature f;
882-
QgsFeatureIds featureIds;
883-
884-
while ( it.nextFeature( f ) )
885-
{
886-
featureIds << f.id();
912+
while ( it.nextFeature( f ) )
913+
{
914+
featureIds << f.id();
915+
}
887916
}
888917

889918
mFilterModel->setFilteredFeatures( featureIds );

0 commit comments

Comments
 (0)