@@ -814,6 +814,29 @@ void QgsRelationReferenceWidget::filterChanged()
814
814
815
815
Q_ASSERT ( scb );
816
816
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 ;
817
840
if ( mChainFilters )
818
841
{
819
842
QComboBox *ccb = nullptr ;
@@ -834,6 +857,9 @@ void QgsRelationReferenceWidget::filterChanged()
834
857
}
835
858
else
836
859
{
860
+ const QString fieldName = cb->property ( " Field" ).toString ();
861
+ filtered = true ;
862
+
837
863
cb->blockSignals ( true );
838
864
cb->clear ();
839
865
cb->addItem ( cb->property ( " FieldAlias" ).toString () );
@@ -843,8 +869,29 @@ void QgsRelationReferenceWidget::filterChanged()
843
869
QStringList texts;
844
870
Q_FOREACH ( const QString &txt, mFilterCache [ccb->property ( " Field" ).toString ()][ccb->currentText ()] )
845
871
{
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;
847
893
}
894
+
848
895
texts.sort ();
849
896
cb->addItems ( texts );
850
897
@@ -856,34 +903,16 @@ void QgsRelationReferenceWidget::filterChanged()
856
903
}
857
904
}
858
905
859
- Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
906
+ if ( ! mChainFilters || ( mChainFilters && !filtered ) )
860
907
{
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 " ) );
878
909
879
- QgsFeatureIterator it ( mMasterModel ->layerCache ()->getFeatures ( QgsFeatureRequest ().setFilterExpression ( filterExpression ).setSubsetOfAttributes ( attrs ) ) );
910
+ QgsFeatureIterator it ( mMasterModel ->layerCache ()->getFeatures ( QgsFeatureRequest ().setFilterExpression ( filterExpression ).setSubsetOfAttributes ( attrs ) ) );
880
911
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
+ }
887
916
}
888
917
889
918
mFilterModel ->setFilteredFeatures ( featureIds );
0 commit comments