-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_utils.py
114 lines (87 loc) · 3.87 KB
/
misc_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import annotations
import os
from typing import TYPE_CHECKING, cast
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.utils import iface
if TYPE_CHECKING:
from typing import Literal
from qgis.core import QgsMapLayer
from qgis.gui import QgisInterface
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__))
# NOTE: Consider creating "layer_utils.py" or similar for layer related utils in the future
def get_layer_by_name(layer_name: str) -> QgsMapLayer | None:
"""
Retrieve a layer by name from the project.
If multiple layers with the same name exist, returns the first one. Returns None
if layer with a matching name is not found.
"""
layers = QgsProject.instance().mapLayersByName(layer_name)
if layers:
return layers[0]
iface.messageBar().pushWarning("Error", f"Layer '{layer_name}' not found")
return None
def get_additional_information_name(info_type: str, language: Literal["fin", "eng", "swe"] = "fin") -> str:
"""
Retrieve name of input additional information type from associated QGIS layer.
Returns input `info_type` if name is not found.
"""
type_of_additional_information_layer_name = "Lisätiedonlaji"
layer = get_layer_by_name(type_of_additional_information_layer_name)
if layer:
for feature in layer.getFeatures():
if feature["value"] == info_type:
return feature["name"][language]
return info_type
def check_layer_changes() -> bool:
"""Check if there are unsaved changes in any QGIS layers."""
project = QgsProject.instance()
layers = project.mapLayers().values()
return any(isinstance(layer, QgsVectorLayer) and layer.isModified() for layer in layers)
def prompt_commit_changes() -> bool:
"""Ask user if changes should be committed."""
response = QMessageBox.question(
None,
"Tallentamattomat muutokset",
"Tasoilla on tallentamattomia muutoksia. Tallenetaanko muutokset?",
QMessageBox.Yes | QMessageBox.No,
)
return response == QMessageBox.Yes
def commit_all_layer_changes() -> bool:
"""
Commit changes to all modified layers in the QGIS project.
Returns True if all changes were successfully committed, False if any failed.
"""
project = QgsProject.instance()
layers = project.mapLayers().values()
all_committed = True
for layer in layers:
if isinstance(layer, QgsVectorLayer) and layer.isModified() and not layer.commitChanges():
QMessageBox.critical(None, "Virhe", f"Tason {layer.name()} muutosten tallentaminen epäonnistui.")
all_committed = False
return all_committed
def handle_unsaved_changes() -> bool:
"""
Wrapper function to check for unsaved changes, prompt user to commit, and commit changes if chosen.
Returns:
bool: True if changes are committed or no changes were found;
False if user does not want to commit or if commit fails.
"""
if check_layer_changes():
if not prompt_commit_changes():
return False
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