From 9afa533459ad1e62c95bd84e99abd6048ef7e009 Mon Sep 17 00:00:00 2001 From: Miikka Kallio Date: Wed, 30 Oct 2024 12:06:21 +0200 Subject: [PATCH 1/2] 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. --- arho_feature_template/plugin.py | 14 ++++++++ arho_feature_template/utils/misc_utils.py | 39 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/arho_feature_template/plugin.py b/arho_feature_template/plugin.py index dadbc2e..a4fca64 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,11 @@ 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 ( + check_layer_changes, + commit_all_layer_changes, + prompt_commit_changes, +) if TYPE_CHECKING: from qgis.gui import QgisInterface, QgsMapTool @@ -181,11 +187,19 @@ def load_existing_land_use_plan(self) -> None: QMessageBox.critical(None, "Error", "No database connections found.") return + if check_layer_changes() and (not prompt_commit_changes() or not commit_all_layer_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..8810c16 100644 --- a/arho_feature_template/utils/misc_utils.py +++ b/arho_feature_template/utils/misc_utils.py @@ -1,3 +1,42 @@ 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 From efc99a8a7c90726755e7b2039ef95e65ef3b4a6d Mon Sep 17 00:00:00 2001 From: Miikka Kallio Date: Wed, 30 Oct 2024 13:09:29 +0200 Subject: [PATCH 2/2] Added wrapper function for checking, asking and committing changes. Added wrapper function --- arho_feature_template/plugin.py | 8 ++------ arho_feature_template/utils/misc_utils.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/arho_feature_template/plugin.py b/arho_feature_template/plugin.py index a4fca64..551d4b9 100644 --- a/arho_feature_template/plugin.py +++ b/arho_feature_template/plugin.py @@ -16,11 +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 ( - check_layer_changes, - commit_all_layer_changes, - prompt_commit_changes, -) +from arho_feature_template.utils.misc_utils import handle_unsaved_changes if TYPE_CHECKING: from qgis.gui import QgisInterface, QgsMapTool @@ -187,7 +183,7 @@ def load_existing_land_use_plan(self) -> None: QMessageBox.critical(None, "Error", "No database connections found.") return - if check_layer_changes() and (not prompt_commit_changes() or not commit_all_layer_changes()): + if not handle_unsaved_changes(): return dialog = LoadPlanDialog(None, connections) diff --git a/arho_feature_template/utils/misc_utils.py b/arho_feature_template/utils/misc_utils.py index 8810c16..5b20f2a 100644 --- a/arho_feature_template/utils/misc_utils.py +++ b/arho_feature_template/utils/misc_utils.py @@ -40,3 +40,18 @@ def commit_all_layer_changes() -> bool: 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