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