Navigation Menu

Skip to content

Commit

Permalink
Update layer variables for expression builder (#41417)
Browse files Browse the repository at this point in the history
Fixes #40255
  • Loading branch information
troopa81 committed Feb 9, 2021
1 parent 26844e7 commit 76cc050
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -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 )
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsexpressionbuilderwidget.py
Expand Up @@ -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 76cc050

Please sign in to comment.