Skip to content

Commit eda38d8

Browse files
committed
Toggle editing off before filtering layers
Ask if uncommitted changes should be committed, and toggle editing off before filtering layers when loading a plan. Added utils for checking uncommitted changes on layers, ask to commit prompt and committing each layer with uncommitted changes.
1 parent 161e3ba commit eda38d8

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

arho_feature_template/plugin.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
from typing import TYPE_CHECKING, Callable, cast
55

6+
from qgis.core import QgsProject, QgsVectorLayer
67
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator
78
from qgis.PyQt.QtGui import QIcon
89
from qgis.PyQt.QtWidgets import QAction, QDialog, QMessageBox, QWidget
@@ -16,7 +17,12 @@
1617
from arho_feature_template.qgis_plugin_tools.tools.i18n import setup_translation
1718
from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name
1819
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
19-
from arho_feature_template.utils.misc_utils import PLUGIN_PATH
20+
from arho_feature_template.utils.misc_utils import (
21+
PLUGIN_PATH,
22+
check_layer_changes,
23+
commit_all_layer_changes,
24+
prompt_commit_changes,
25+
)
2026

2127
if TYPE_CHECKING:
2228
from qgis.gui import QgisInterface, QgsMapTool
@@ -181,11 +187,19 @@ def load_existing_land_use_plan(self) -> None:
181187
QMessageBox.critical(None, "Error", "No database connections found.")
182188
return
183189

190+
if check_layer_changes() and (not prompt_commit_changes() or not commit_all_layer_changes()):
191+
return
192+
184193
dialog = LoadPlanDialog(None, connections)
185194

186195
if dialog.exec_() == QDialog.Accepted:
187196
selected_plan_id = dialog.get_selected_plan_id()
188197

198+
project = QgsProject.instance()
199+
for layer in project.mapLayers().values():
200+
if isinstance(layer, QgsVectorLayer) and layer.isEditable():
201+
layer.commitChanges()
202+
189203
if not selected_plan_id:
190204
QMessageBox.critical(None, "Error", "No plan was selected.")
191205
return
+39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
11
import os
22

3+
from qgis.core import QgsProject, QgsVectorLayer
4+
from qgis.PyQt.QtWidgets import QMessageBox
5+
36
PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__))
7+
8+
9+
def check_layer_changes() -> bool:
10+
"""Check if there are unsaved changes in any QGIS layers."""
11+
project = QgsProject.instance()
12+
layers = project.mapLayers().values()
13+
14+
return any(isinstance(layer, QgsVectorLayer) and layer.isModified() for layer in layers)
15+
16+
17+
def prompt_commit_changes() -> bool:
18+
"""Ask user if changes should be committed."""
19+
response = QMessageBox.question(
20+
None,
21+
"Tallentamattomat muutokset",
22+
"Tasoilla on tallentamattomia muutoksia. Tallenetaanko muutokset?",
23+
QMessageBox.Yes | QMessageBox.No,
24+
)
25+
return response == QMessageBox.Yes
26+
27+
28+
def commit_all_layer_changes() -> bool:
29+
"""
30+
Commit changes to all modified layers in the QGIS project.
31+
Returns True if all changes were successfully committed, False if any failed.
32+
"""
33+
project = QgsProject.instance()
34+
layers = project.mapLayers().values()
35+
all_committed = True
36+
37+
for layer in layers:
38+
if isinstance(layer, QgsVectorLayer) and layer.isModified() and not layer.commitChanges():
39+
QMessageBox.critical(None, "Virhe", f"Tason {layer.name()} muutosten tallentaminen epäonnistui.")
40+
all_committed = False
41+
42+
return all_committed

0 commit comments

Comments
 (0)