From 6a15f16711d2ea015d1b2df1c0984db7c753faf9 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Mon, 26 Aug 2024 17:59:25 +0200 Subject: [PATCH] Update scopes used when evaluating expressions --- dynamic_layers/dynamic_layers.py | 7 +++-- dynamic_layers/dynamic_layers_dialog.py | 38 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/dynamic_layers/dynamic_layers.py b/dynamic_layers/dynamic_layers.py index c6a8f94..0536901 100644 --- a/dynamic_layers/dynamic_layers.py +++ b/dynamic_layers/dynamic_layers.py @@ -73,12 +73,13 @@ def __init__(self, iface: QgisInterface): # initialize locale locale = QSettings().value('locale/userLocale')[0:2] locale_path = plugin_path('i18n', f'qgis_plugin_{locale}.qm') - self.translator = None if locale_path.exists(): + # noinspection PyArgumentList self.translator = QTranslator() self.translator.load(str(locale_path)) - - QgsMessageLog.logMessage(f'Translation file {locale_path} found', PLUGIN_MESSAGE, Qgis.Warning) + # noinspection PyUnresolvedReferences + QgsMessageLog.logMessage(f'Translation file {locale_path} found', PLUGIN_MESSAGE, Qgis.Success) + # noinspection PyArgumentList QCoreApplication.installTranslator(self.translator) # noinspection PyArgumentList diff --git a/dynamic_layers/dynamic_layers_dialog.py b/dynamic_layers/dynamic_layers_dialog.py index 54b1608..8f346f5 100644 --- a/dynamic_layers/dynamic_layers_dialog.py +++ b/dynamic_layers/dynamic_layers_dialog.py @@ -7,11 +7,14 @@ from typing import Union from qgis.core import ( + Qgis, QgsApplication, QgsExpression, QgsExpressionContext, QgsExpressionContextScope, QgsExpressionContextUtils, + QgsMessageLog, + QgsProject, ) from qgis.gui import QgsExpressionBuilderDialog from qgis.PyQt import uic @@ -25,7 +28,7 @@ QWidget, ) -from dynamic_layers.definitions import QtVar +from dynamic_layers.definitions import PLUGIN_MESSAGE, QtVar from dynamic_layers.tools import tr folder = Path(__file__).resolve().parent @@ -155,11 +158,10 @@ def origin_variable_toggled(self): def variables(self) -> dict[str, str]: """ The list of variables in the table. """ - data: dict[str, str] = {} - if not self.is_table_variable_based: - return data + return {} + data: dict[str, str] = {} for row in range(self.twVariableList.rowCount()): v_name = self.twVariableList.item(row, 0).data(QtVar.EditRole) v_value = self.twVariableList.item(row, 1).data(QtVar.EditRole) @@ -196,24 +198,38 @@ def validate_expression(self, source: str): def open_expression_builder(self, source: str): """ Open the expression builder helper. """ + context = QgsExpressionContext() + context.appendScope(QgsExpressionContextUtils.globalScope()) + context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance())) + if self.is_table_variable_based: layer = None + scope = QgsExpressionContextScope() + for key, value in self.variables().items(): + scope.addVariable(QgsExpressionContextScope.StaticVariable(key, value)) else: layer = self.inVariableSourceLayer.currentLayer() if not layer: return - - context = QgsExpressionContext() - context.appendScope(QgsExpressionContextUtils.globalScope()) - - scope = QgsExpressionContextScope() - for key, value in self.variables().items(): - scope.addVariable(QgsExpressionContextScope.StaticVariable(key, value)) + scope = QgsExpressionContextUtils.layerScope(layer) context.appendScope(scope) widget = self.input_expression_widget(source) + if layer: + QgsMessageLog.logMessage( + f'Layer set in the expression builder : {layer.name()}', + PLUGIN_MESSAGE, + Qgis.Info, + ) + else: + QgsMessageLog.logMessage( + f'List of variables : {",".join([j for j in self.variables().keys()])}', + PLUGIN_MESSAGE, + Qgis.Info, + ) + dialog = QgsExpressionBuilderDialog(layer, context=context) dialog.setExpressionText(self.text_widget(widget)) result = dialog.exec()