Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

54 Kaavan avaus ei toimi, jos tasojen editointi päällä #58

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arho_feature_template/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions arho_feature_template/utils/misc_utils.py
Original file line number Diff line number Diff line change
@@ -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
Loading