Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

61 Lisää plugin asetuksiin lambda-kutsujen osoite #67

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions arho_feature_template/core/plan_manager.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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("")
Expand Down
3 changes: 2 additions & 1 deletion arho_feature_template/core/update_plan.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions arho_feature_template/gui/plugin_settings.py
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 48 additions & 0 deletions arho_feature_template/gui/plugin_settings.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PluginSettings</class>
<widget class="QDialog" name="PluginSettings">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>Asetukset</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="labelHost">
<property name="text">
<string>Lambdan isäntä:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="hostInput"/>
</item>
<item>
<widget class="QLabel" name="labelPort">
<property name="text">
<string>Lambdan portti:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="portInput"/>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Tallenna asetukset</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
15 changes: 15 additions & 0 deletions arho_feature_template/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion arho_feature_template/utils/misc_utils.py
Original file line number Diff line number Diff line change
@@ -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__))
Expand Down Expand Up @@ -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
Loading