Skip to content
Permalink
Browse files

Update layer variables for expression builder (#41417)

Fixes #40255
  • Loading branch information
troopa81 authored and github-actions committed Feb 11, 2021
1 parent dbd1c41 commit 01a03cf805a373fd9146277f5668e96edb054a56
@@ -286,11 +286,18 @@ void QgsExpressionBuilderWidget::setLayer( QgsVectorLayer *layer )
if ( mLayer )
{
mExpressionContext << QgsExpressionContextUtils::layerScope( mLayer );

expressionContextUpdated();
txtExpressionString->setFields( mLayer->fields() );
}
}

void QgsExpressionBuilderWidget::expressionContextUpdated()
{
txtExpressionString->setExpressionContext( mExpressionContext );
mExpressionTreeView->setExpressionContext( mExpressionContext );
mExpressionPreviewWidget->setExpressionContext( mExpressionContext );
}

QgsVectorLayer *QgsExpressionBuilderWidget::layer() const
{
return mLayer;
@@ -627,9 +634,7 @@ void QgsExpressionBuilderWidget::setExpectedOutputFormat( const QString &expecte
void QgsExpressionBuilderWidget::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
txtExpressionString->setExpressionContext( mExpressionContext );
mExpressionTreeView->setExpressionContext( context );
mExpressionPreviewWidget->setExpressionContext( context );
expressionContextUpdated();
}

void QgsExpressionBuilderWidget::txtExpressionString_textChanged()
@@ -417,6 +417,9 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QString loadFunctionHelp( QgsExpressionItem *functionName );
QString helpStylesheet() const;

// To be called whenever expression context has been updated
void expressionContextUpdated();

// Will hold items with
// * a display string that matches the represented field values
// * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
@@ -194,6 +194,26 @@ def testStoredExpressions(self):
items = w.findExpressions('Stored Expression Number One')
self.assertEqual(len(items), 0)

def testLayerVariables(self):
""" check through widget model to ensure it is populated with layer variables """
w = QgsExpressionBuilderWidget()
m = w.model()

p = QgsProject.instance()
layer = QgsVectorLayer("Point", "layer1", "memory")
p.addMapLayers([layer])

w.setLayer(layer)

items = m.findItems("layer", Qt.MatchRecursive)
self.assertEqual(len(items), 1)
items = m.findItems("layer_id", Qt.MatchRecursive)
self.assertEqual(len(items), 1)
items = m.findItems("layer_name", Qt.MatchRecursive)
self.assertEqual(len(items), 1)

p.removeMapLayer(layer)


if __name__ == '__main__':
unittest.main()

0 comments on commit 01a03cf

Please sign in to comment.