Skip to content
Permalink
Browse files

Fix relation reference widget when 'Chain Filters' is activated. Fixes

  • Loading branch information
pblottiere committed Aug 17, 2017
1 parent 987f1ac commit 40ad86024353a3d24b109877dbf2cdedad8a7bc6
@@ -785,13 +785,41 @@ void QgsRelationReferenceWidget::filterChanged()
{
QVariant nullValue = QSettings().value( "qgis/nullValue", "NULL" );

QStringList filters;
QMap<QString, QString> filters;
QgsAttributeList attrs;

QComboBox* scb = qobject_cast<QComboBox*>( sender() );

Q_ASSERT( scb );

QgsFeature f;
QgsFeatureIds featureIds;
QString filterExpression;

// comboboxes have to be disabled before building filters
if ( mChainFilters )
disableChainedComboBoxes( scb );

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

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

bool filtered = false;
if ( mChainFilters )
{
QComboBox* ccb = nullptr;
@@ -805,13 +833,11 @@ void QgsRelationReferenceWidget::filterChanged()
continue;
}

if ( ccb->currentIndex() == 0 )
{
cb->setCurrentIndex( 0 );
cb->setEnabled( false );
}
else
if ( ccb->currentIndex() != 0 )
{
const QString fieldName = cb->property( "Field" ).toString();
filtered = true;

cb->blockSignals( true );
cb->clear();
cb->addItem( cb->property( "FieldAlias" ).toString() );
@@ -821,8 +847,30 @@ void QgsRelationReferenceWidget::filterChanged()
QStringList texts;
Q_FOREACH ( const QString& txt, mFilterCache[ccb->property( "Field" ).toString()][ccb->currentText()] )
{
texts << txt;
QMap<QString, QString> filtersAttrs = filters;
filtersAttrs[fieldName] = QgsExpression::createFieldEqualityExpression( fieldName, txt );
QStringList vals = filtersAttrs.values();
QString expression = vals.join( QString( " AND " ) );

QgsAttributeList subset = attrs;
subset << mReferencedLayer->fieldNameIndex( 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 );

@@ -834,34 +882,21 @@ void QgsRelationReferenceWidget::filterChanged()
}
}

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

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

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

QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( QgsFeatureRequest().setFilterExpression( filterExpression ).setSubsetOfAttributes( attrs ) ) );
QgsFeatureRequest req = QgsFeatureRequest().setSubsetOfAttributes( attrs );
if ( !filterExpression.isEmpty() )
req.setFilterExpression( filterExpression );

QgsFeature f;
QgsFeatureIds featureIds;
QgsFeatureIterator it( mMasterModel->layerCache()->getFeatures( req ) );

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

mFilterModel->setFilteredFeatures( featureIds );
@@ -896,3 +931,28 @@ void QgsRelationReferenceWidget::updateAddEntryButton()
mAddEntryButton->setVisible( mAllowAddFeatures );
mAddEntryButton->setEnabled( mReferencedLayer && mReferencedLayer->isEditable() );
}

void QgsRelationReferenceWidget::disableChainedComboBoxes( const QComboBox *scb )
{
QComboBox *ccb = nullptr;
Q_FOREACH ( QComboBox *cb, mFilterComboBoxes )
{
if ( !ccb )
{
if ( cb == scb )
{
ccb = cb;
}

continue;
}

if ( ccb->currentIndex() == 0 )
{
cb->setCurrentIndex( 0 );
cb->setEnabled( false );
}
else
ccb = cb;
}
}
@@ -162,6 +162,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
private:
void highlightFeature( QgsFeature f = QgsFeature(), CanvasExtent canvasExtent = Fixed );
void updateAttributeEditorFrame( const QgsFeature& feature );
void disableChainedComboBoxes( const QComboBox *scb );

// initialized
QgsAttributeEditorContext mEditorContext;

0 comments on commit 40ad860

Please sign in to comment.
You can’t perform that action at this time.