Skip to content

Commit

Permalink
Fixes relation reference widget when filters are reset
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jul 27, 2017
1 parent 783e454 commit 497662a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
57 changes: 43 additions & 14 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -811,7 +811,7 @@ void QgsRelationReferenceWidget::filterChanged()
{
QVariant nullValue = QgsApplication::nullRepresentation();

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

QComboBox *scb = qobject_cast<QComboBox *>( sender() );
Expand All @@ -822,6 +822,11 @@ void QgsRelationReferenceWidget::filterChanged()
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 )
Expand All @@ -830,11 +835,11 @@ void QgsRelationReferenceWidget::filterChanged()

if ( cb->currentText() == nullValue.toString() )
{
filters << QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
filters[fieldName] = QStringLiteral( "\"%1\" IS NULL" ).arg( fieldName );
}
else
{
filters << QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
filters[fieldName] = QgsExpression::createFieldEqualityExpression( fieldName, cb->currentText() );
}
attrs << mReferencedLayer->fields().lookupField( fieldName );
}
Expand All @@ -854,12 +859,7 @@ 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;
Expand All @@ -873,9 +873,9 @@ void QgsRelationReferenceWidget::filterChanged()
QStringList texts;
Q_FOREACH ( const QString &txt, mFilterCache[ccb->property( "Field" ).toString()][ccb->currentText()] )
{
QStringList filtersAttrs = filters;
filtersAttrs << QgsExpression::createFieldEqualityExpression( fieldName, txt );
QString expression = filtersAttrs.join( QStringLiteral( " AND " ) );
QMap<QString, QString> filtersAttrs = filters;
filtersAttrs[fieldName] = QgsExpression::createFieldEqualityExpression( fieldName, txt );
QString expression = filtersAttrs.values().join( QStringLiteral( " AND " ) );

QgsAttributeList subset = attrs;
subset << mReferencedLayer->fields().lookupField( fieldName );
Expand Down Expand Up @@ -909,9 +909,13 @@ void QgsRelationReferenceWidget::filterChanged()

if ( !mChainFilters || ( mChainFilters && !filtered ) )
{
filterExpression = filters.join( QStringLiteral( " AND " ) );
filterExpression = filters.values().join( QStringLiteral( " AND " ) );

QgsFeatureRequest req = QgsFeatureRequest().setSubsetOfAttributes( attrs );
if ( !filterExpression.isEmpty() )
req.setFilterExpression( filterExpression );

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

while ( it.nextFeature( f ) )
{
Expand Down Expand Up @@ -951,3 +955,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 @@ -185,6 +185,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 *cb );

// initialized
QgsAttributeEditorContext mEditorContext;
Expand Down

0 comments on commit 497662a

Please sign in to comment.