Skip to content
Permalink
Browse files

Parentheses are needed in case the filter expression contains "OR" (#…

  • Loading branch information
suricactus committed Sep 11, 2020
1 parent c6e2882 commit c76a32a66f305a86ea855d88a7998b373635da19
@@ -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 );
@@ -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;
@@ -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.
You can’t perform that action at this time.