diff --git a/arho_feature_template/core/plan_manager.py b/arho_feature_template/core/plan_manager.py index a4f911b..b5b2ebb 100644 --- a/arho_feature_template/core/plan_manager.py +++ b/arho_feature_template/core/plan_manager.py @@ -1,4 +1,4 @@ -from qgis.core import QgsProject, QgsVectorLayer +from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer from qgis.PyQt.QtWidgets import QDialog, QMessageBox from qgis.utils import iface @@ -95,7 +95,8 @@ def load_land_use_plan(self): update_selected_plan(plan) def clear_all_filters(self): - """Clear filters for all vector layers in the project.""" + """Clear active_plan_id and filters for all vector layers in the project.""" + QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", None) for layer in QgsProject.instance().mapLayers().values(): if isinstance(layer, QgsVectorLayer): layer.setSubsetString("") diff --git a/arho_feature_template/core/update_plan.py b/arho_feature_template/core/update_plan.py index d083090..8552fc7 100644 --- a/arho_feature_template/core/update_plan.py +++ b/arho_feature_template/core/update_plan.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from qgis.core import QgsMapLayer, QgsProject, QgsVectorLayer +from qgis.core import QgsExpressionContextUtils, QgsMapLayer, QgsProject, QgsVectorLayer from qgis.utils import iface @@ -24,6 +24,7 @@ class LandUsePlan: def update_selected_plan(new_plan: LandUsePlan): """Update the project layers based on the selected land use plan.""" plan_id = new_plan.id + QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", plan_id) for layer_name, field_name in LAYER_PLAN_ID_MAP.items(): # Set the filter on each layer using the plan_id diff --git a/arho_feature_template/gui/plugin_settings.py b/arho_feature_template/gui/plugin_settings.py new file mode 100644 index 0000000..1bc1061 --- /dev/null +++ b/arho_feature_template/gui/plugin_settings.py @@ -0,0 +1,37 @@ +from importlib import resources + +from qgis.PyQt import uic +from qgis.PyQt.QtCore import QSettings +from qgis.PyQt.QtWidgets import QDialog + +ui_path = resources.files(__package__) / "plugin_settings.ui" +FormClass, _ = uic.loadUiType(ui_path) + + +class PluginSettings(QDialog, FormClass): # type: ignore + def __init__(self, parent=None): + super().__init__(parent) + + self.setupUi(self) + + self.saveButton.clicked.connect(self.save_settings) + + self.load_settings() + + def load_settings(self): + """Load settings from QSettings with default values.""" + settings = QSettings("ArhoFeatureTemplate") + + lambda_host = settings.value("lambda_host", "localhost") + lambda_port = settings.value("lambda_port", "5435") + + self.hostInput.setText(lambda_host) + self.portInput.setText(lambda_port) + + def save_settings(self): + """Save settings to QSettings.""" + settings = QSettings("ArhoFeatureTemplate") + settings.setValue("lambda_host", self.hostInput.text()) + settings.setValue("lambda_port", self.portInput.text()) + + self.accept() diff --git a/arho_feature_template/gui/plugin_settings.ui b/arho_feature_template/gui/plugin_settings.ui new file mode 100644 index 0000000..f438cd5 --- /dev/null +++ b/arho_feature_template/gui/plugin_settings.ui @@ -0,0 +1,48 @@ + + + PluginSettings + + + + 0 + 0 + 300 + 150 + + + + Asetukset + + + + + + Lambdan isäntä: + + + + + + + + + + Lambdan portti: + + + + + + + + + + Tallenna asetukset + + + + + + + + diff --git a/arho_feature_template/plugin.py b/arho_feature_template/plugin.py index ad9488a..bcbabcf 100644 --- a/arho_feature_template/plugin.py +++ b/arho_feature_template/plugin.py @@ -10,6 +10,7 @@ from arho_feature_template.core.feature_template_library import FeatureTemplater, TemplateGeometryDigitizeMapTool from arho_feature_template.core.plan_manager import PlanManager from arho_feature_template.gui.new_plan_regulation_group_form import NewPlanRegulationGroupForm +from arho_feature_template.gui.plugin_settings import PluginSettings from arho_feature_template.qgis_plugin_tools.tools.custom_logging import setup_logger, teardown_logger from arho_feature_template.qgis_plugin_tools.tools.i18n import setup_translation from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name @@ -171,6 +172,15 @@ def initGui(self) -> None: # noqa N802 add_to_toolbar=True, ) + self.plugin_settings_action = self.add_action( + "", + text="Asetukset", + triggered_callback=self.open_settings, + add_to_menu=True, + add_to_toolbar=False, + status_tip="Säädä pluginin asetuksia", + ) + def on_map_tool_changed(self, new_tool: QgsMapTool, old_tool: QgsMapTool) -> None: # noqa: ARG002 if not isinstance(new_tool, TemplateGeometryDigitizeMapTool): self.template_dock_action.setChecked(False) @@ -181,6 +191,11 @@ def add_new_plan(self): def load_existing_land_use_plan(self): self.plan_manager.load_land_use_plan() + def open_settings(self): + """Open the plugin settings dialog.""" + settings = PluginSettings() + settings.exec_() + def unload(self) -> None: """Removes the plugin menu item and icon from QGIS GUI.""" for action in self.actions: diff --git a/arho_feature_template/utils/misc_utils.py b/arho_feature_template/utils/misc_utils.py index 5b20f2a..b9fe118 100644 --- a/arho_feature_template/utils/misc_utils.py +++ b/arho_feature_template/utils/misc_utils.py @@ -1,6 +1,7 @@ import os -from qgis.core import QgsProject, QgsVectorLayer +from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer +from qgis.PyQt.QtCore import QSettings from qgis.PyQt.QtWidgets import QMessageBox PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__)) @@ -55,3 +56,17 @@ def handle_unsaved_changes() -> bool: if not commit_all_layer_changes(): return False return True + + +def get_active_plan_id(): + """Retrieve the active plan ID stored as a project variable.""" + # return QgsExpressionContextUtils.projectScope(QgsProject.instance(), "active_plan_id") + return QgsExpressionContextUtils.projectScope(QgsProject.instance()).variable("active_plan_id") + + +def get_lambda_settings(): + """Retrieve Lambda settings, using defaults if not set.""" + settings = QSettings("ArhoFeatureTemplate") + lambda_host = settings.value("lambda_host", "localhost") + lambda_port = settings.value("lambda_port", "8083") + return lambda_host, lambda_port