Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Update layer variables for expression builder (#41417)
- Loading branch information
|
@@ -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() |