Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix relation reference widget when 'Chain Filters' is activated. Fixes
  • Loading branch information
pblottiere committed Jul 26, 2017
1 parent d19b4aa commit 8642e88
Showing 1 changed file with 55 additions and 26 deletions.
81 changes: 55 additions & 26 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -814,6 +814,29 @@ void QgsRelationReferenceWidget::filterChanged()

Q_ASSERT( scb );

QgsFeature f;
QgsFeatureIds featureIds;
QString filterExpression;

Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
{
if ( cb->currentIndex() != 0 )
{
const QString fieldName = cb->property( "Field" ).toString();

if ( cb->currentText() == nullValue.toString() )
{
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
}
else
{
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
}
attrs << mReferencedLayer->fields().lookupField( fieldName );
}
}

bool filtered = false;
if ( mChainFilters )
{
QComboBox *ccb = nullptr;
Expand All @@ -834,6 +857,9 @@ void QgsRelationReferenceWidget::filterChanged()
}
else
{
const QString fieldName = cb->property( "Field" ).toString();
filtered = true;

cb->blockSignals( true );
cb->clear();
cb->addItem( cb->property( "FieldAlias" ).toString() );
Expand All @@ -843,8 +869,29 @@ void QgsRelationReferenceWidget::filterChanged()
QStringList texts;
Q_FOREACH ( const QString &txt, mFilterCache[ccb->property( "Field" ).toString()][ccb->currentText()] )
{
texts << txt;
QStringList filtersAttrs = filters;
filtersAttrs << QgsExpression::createFieldEqualityExpression( fieldName, txt );
QString expression = filtersAttrs.join( QStringLiteral( " AND " ) );

QgsAttributeList subset = attrs;
subset << mReferencedLayer->fields().lookupField( fieldName );

QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( QgsFeatureRequest().setFilterExpression( expression ).setSubsetOfAttributes( subset ) ) );

bool found = false;
while ( it.nextFeature( f ) )
{
if ( !featureIds.contains( f.id() ) )
featureIds << f.id();

found = true;
}

// item is only provided if at least 1 feature exists
if ( found )
texts << txt;
}

texts.sort();
cb->addItems( texts );

Expand All @@ -856,34 +903,16 @@ void QgsRelationReferenceWidget::filterChanged()
}
}

Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
if ( !mChainFilters || ( mChainFilters && !filtered ) )
{
if ( cb->currentIndex() != 0 )
{
const QString fieldName = cb->property( "Field" ).toString();

if ( cb->currentText() == nullValue.toString() )
{
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
}
else
{
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
}
attrs << mReferencedLayer->fields().lookupField( fieldName );
}
}

QString filterExpression = filters.join( QStringLiteral( " AND " ) );
filterExpression = filters.join( QStringLiteral( " AND " ) );

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

QgsFeature f;
QgsFeatureIds featureIds;

while ( it.nextFeature( f ) )
{
featureIds << f.id();
while ( it.nextFeature( f ) )
{
featureIds << f.id();
}
}

mFilterModel->setFilteredFeatures( featureIds );
Expand Down

0 comments on commit 8642e88

Please sign in to comment.