Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Parentheses are needed in case the filter expression contains "OR" (#…
  • Loading branch information
suricactus committed Sep 11, 2020
1 parent c6e2882 commit c76a32a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -865,6 +865,10 @@ void QgsRelationReferenceWidget::filterChanged()
QgsFeatureIds featureIds;
QString filterExpression = mFilterExpression;

// wrap the expression with parentheses as it might contain `OR`
if ( !filterExpression.isEmpty() )
filterExpression = QStringLiteral( " ( %1 ) " ).arg( filterExpression );

// comboboxes have to be disabled before building filters
if ( mChainFilters )
disableChainedComboBoxes( scb );
Expand Down
38 changes: 38 additions & 0 deletions tests/src/gui/testqgsrelationreferencewidget.cpp
Expand Up @@ -67,6 +67,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testAddEntryNoGeom();
void testDependencies(); // Test relation datasource, id etc. config storage
void testSetFilterExpression();
void testSetFilterExpressionWithOrClause();

private:
std::unique_ptr<QgsVectorLayer> mLayer1;
Expand Down Expand Up @@ -735,5 +736,42 @@ void TestQgsRelationReferenceWidget::testSetFilterExpression()
QCOMPARE( w.mComboBox->count(), 3 );
}



void TestQgsRelationReferenceWidget::testSetFilterExpressionWithOrClause()
{

// init a relation reference widget
QStringList filterFields = { "material", "diameter", "raccord" };

QWidget parentWidget;
QgsRelationReferenceWidget w( &parentWidget );

QEventLoop loop;
connect( qobject_cast<QgsFeatureFilterModel *>( w.mComboBox->model() ), &QgsFeatureFilterModel::filterJobCompleted, &loop, &QEventLoop::quit );

w.setChainFilters( true );
w.setFilterFields( filterFields );
w.setRelation( *mRelation, true );
w.setFilterExpression( QStringLiteral( " \"raccord\" = 'sleeve' OR FALSE " ) );
w.init();

QStringList items = getComboBoxItems( w.mComboBox );

loop.exec();

// in case there is no filter, the number of filtered features will be 4
QCOMPARE( w.mComboBox->count(), 2 );

QList<QComboBox *> cbs = w.mFilterComboBoxes;
cbs[0]->setCurrentIndex( cbs[0]->findText( "steel" ) );

loop.exec();

QCOMPARE( w.mComboBox->currentText(), QStringLiteral( "NULL" ) );
// in case there is no field filter, the number of filtered features will be 2
QCOMPARE( w.mComboBox->count(), 1 );
}

QGSTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"

0 comments on commit c76a32a

Please sign in to comment.