diff --git a/arho_feature_template/plugin.py b/arho_feature_template/plugin.py index dadbc2e..551d4b9 100644 --- a/arho_feature_template/plugin.py +++ b/arho_feature_template/plugin.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, Callable, cast +from qgis.core import QgsProject, QgsVectorLayer from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtWidgets import QAction, QDialog, QMessageBox, QWidget @@ -15,6 +16,7 @@ from arho_feature_template.qgis_plugin_tools.tools.i18n import setup_translation from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name from arho_feature_template.utils.db_utils import get_existing_database_connection_names +from arho_feature_template.utils.misc_utils import handle_unsaved_changes if TYPE_CHECKING: from qgis.gui import QgisInterface, QgsMapTool @@ -181,11 +183,19 @@ def load_existing_land_use_plan(self) -> None: QMessageBox.critical(None, "Error", "No database connections found.") return + if not handle_unsaved_changes(): + return + dialog = LoadPlanDialog(None, connections) if dialog.exec_() == QDialog.Accepted: selected_plan_id = dialog.get_selected_plan_id() + project = QgsProject.instance() + for layer in project.mapLayers().values(): + if isinstance(layer, QgsVectorLayer) and layer.isEditable(): + layer.commitChanges() + if not selected_plan_id: QMessageBox.critical(None, "Error", "No plan was selected.") return diff --git a/arho_feature_template/utils/misc_utils.py b/arho_feature_template/utils/misc_utils.py index 42f38d6..5b20f2a 100644 --- a/arho_feature_template/utils/misc_utils.py +++ b/arho_feature_template/utils/misc_utils.py @@ -1,3 +1,57 @@ import os +from qgis.core import QgsProject, QgsVectorLayer +from qgis.PyQt.QtWidgets import QMessageBox + PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__)) + + +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