Skip to content

Commit

Permalink
Fix relation reference widget when 'Chain Filters' is activated. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Aug 17, 2017
1 parent 987f1ac commit 40ad860
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 32 deletions.
124 changes: 92 additions & 32 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -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;
Expand All @@ -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() );
Expand All @@ -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 );

Expand All @@ -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 );
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -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;
Expand Down

0 comments on commit 40ad860

Please sign in to comment.