Skip to content

Commit e89ff4f

Browse files
Mtk112LKajan
authored andcommitted
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 9e149a4 commit e89ff4f

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

arho_feature_template/plugin.py

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import TYPE_CHECKING, Callable, cast
44

5+
from qgis.core import QgsProject, QgsVectorLayer
56
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator
67
from qgis.PyQt.QtGui import QIcon
78
from qgis.PyQt.QtWidgets import QAction, QDialog, QMessageBox, QWidget
@@ -15,6 +16,11 @@
1516
from arho_feature_template.qgis_plugin_tools.tools.i18n import setup_translation
1617
from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name
1718
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
19+
from arho_feature_template.utils.misc_utils import (
20+
check_layer_changes,
21+
commit_all_layer_changes,
22+
prompt_commit_changes,
23+
)
1824

1925
if TYPE_CHECKING:
2026
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)