-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan_manager.py
100 lines (76 loc) · 3.87 KB
/
plan_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
from qgis.PyQt.QtWidgets import QDialog, QMessageBox
from qgis.utils import iface
from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
from arho_feature_template.utils.misc_utils import get_layer_by_name, handle_unsaved_changes
class PlanManager:
def __init__(self):
self.kaava_layer = get_layer_by_name("Kaava")
def add_new_plan(self):
"""Initiate the process to add a new plan to the Kaava layer."""
if not handle_unsaved_changes():
return
self.clear_all_filters()
if not self.kaava_layer:
return
if not self.kaava_layer.isEditable():
self.kaava_layer.startEditing()
iface.setActiveLayer(self.kaava_layer)
iface.actionAddFeature().trigger()
# Connect the featureAdded signal to a callback method
self.kaava_layer.featureAdded.connect(self.feature_added)
def feature_added(self):
"""Callback for when a new feature is added to the Kaava layer."""
if not self.kaava_layer:
return
# Disconnect the signal to avoid repeated triggers
self.kaava_layer.featureAdded.disconnect()
feature_ids_before_commit = self.kaava_layer.allFeatureIds()
if self.kaava_layer.isEditable():
if not self.kaava_layer.commitChanges():
iface.messageBar().pushMessage("Error", "Failed to commit changes to the layer.", level=3)
return
else:
iface.messageBar().pushMessage("Error", "Layer is not editable.", level=3)
return
feature_ids_after_commit = self.kaava_layer.allFeatureIds()
new_feature_id = next((fid for fid in feature_ids_after_commit if fid not in feature_ids_before_commit), None)
if new_feature_id is not None:
new_feature = self.kaava_layer.getFeature(new_feature_id)
if new_feature.isValid():
feature_id_value = new_feature["id"]
update_selected_plan(LandUsePlan(feature_id_value))
else:
iface.messageBar().pushMessage("Error", "Invalid feature retrieved.", level=3)
else:
iface.messageBar().pushMessage("Error", "No new feature was added.", level=3)
def load_land_use_plan(self):
"""Load an existing land use plan using a dialog selection."""
connections = get_existing_database_connection_names()
if not connections:
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()
self.commit_all_editable_layers()
if not selected_plan_id:
QMessageBox.critical(None, "Error", "No plan was selected.")
return
plan = LandUsePlan(selected_plan_id)
update_selected_plan(plan)
def clear_all_filters(self):
"""Clear active_plan_id and filters for all vector layers in the project."""
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", None)
for layer in QgsProject.instance().mapLayers().values():
if isinstance(layer, QgsVectorLayer):
layer.setSubsetString("")
def commit_all_editable_layers(self):
"""Commit all changes in any editable layers."""
for layer in QgsProject.instance().mapLayers().values():
if isinstance(layer, QgsVectorLayer) and layer.isEditable():
layer.commitChanges()