Skip to content

Commit

Permalink
Update scopes used when evaluating expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Aug 26, 2024
1 parent 223aff4 commit 6a15f16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
7 changes: 4 additions & 3 deletions dynamic_layers/dynamic_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 27 additions & 11 deletions dynamic_layers/dynamic_layers_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6a15f16

Please sign in to comment.