11
11
from arho_feature_template .core .lambda_service import LambdaService
12
12
from arho_feature_template .core .models import (
13
13
FeatureTemplateLibrary ,
14
+ Plan ,
14
15
PlanFeature ,
15
16
RegulationGroupCategory ,
16
17
RegulationGroupLibrary ,
50
51
if TYPE_CHECKING :
51
52
from qgis .core import QgsFeature
52
53
53
- from arho_feature_template .core .models import Plan , Regulation , RegulationGroup
54
+ from arho_feature_template .core .models import Regulation , RegulationGroup
54
55
55
56
logger = logging .getLogger (__name__ )
56
57
@@ -154,6 +155,22 @@ def add_new_plan(self):
154
155
self .digitize_map_tool .setLayer (plan_layer )
155
156
iface .mapCanvas ().setMapTool (self .digitize_map_tool )
156
157
158
+ def edit_plan (self ):
159
+ plan_layer = PlanLayer .get_from_project ()
160
+ if not plan_layer :
161
+ return
162
+
163
+ active_plan_id = QgsExpressionContextUtils .projectScope (QgsProject .instance ()).variable ("active_plan_id" )
164
+ feature = PlanLayer .get_feature_by_id (active_plan_id )
165
+ if feature is None :
166
+ iface .messageBar ().pushWarning ("" , "No active/open plan found!" )
167
+ return
168
+ plan_model = PlanLayer .model_from_feature (feature )
169
+
170
+ attribute_form = PlanAttributeForm (plan_model , self .regulation_group_libraries )
171
+ if attribute_form .exec_ ():
172
+ feature = save_plan (attribute_form .model )
173
+
157
174
def add_new_plan_feature (self ):
158
175
if not handle_unsaved_changes ():
159
176
return
@@ -180,11 +197,10 @@ def _plan_geom_digitized(self, feature: QgsFeature):
180
197
if not plan_layer :
181
198
return
182
199
183
- attribute_form = PlanAttributeForm (self .regulation_group_libraries )
200
+ plan_model = Plan (geom = feature .geometry ())
201
+ attribute_form = PlanAttributeForm (plan_model , self .regulation_group_libraries )
184
202
if attribute_form .exec_ ():
185
- plan_attributes = attribute_form .get_plan_attributes ()
186
- plan_attributes .geom = feature .geometry ()
187
- feature = save_plan (plan_attributes )
203
+ feature = save_plan (attribute_form .model )
188
204
plan_to_be_activated = feature ["id" ]
189
205
else :
190
206
plan_to_be_activated = self .previous_active_plan_id
@@ -379,33 +395,37 @@ def _delete_feature(feature: QgsFeature, layer: QgsVectorLayer, delete_text: str
379
395
layer .commitChanges (stopEditing = False )
380
396
381
397
382
- def save_plan (plan_data : Plan ) -> QgsFeature :
383
- plan_layer = PlanLayer .get_from_project ()
384
- in_edit_mode = plan_layer .isEditable ()
385
- if not in_edit_mode :
386
- plan_layer .startEditing ()
387
-
388
- edit_message = "Kaavan lisäys" if plan_data .id_ is None else "Kaavan muokkaus"
389
- plan_layer .beginEditCommand (edit_message )
390
-
391
- # plan_data.organisation_id = "99e20d66-9730-4110-815f-5947d3f8abd3"
392
- plan_feature = PlanLayer .feature_from_model (plan_data )
398
+ def save_plan (plan : Plan ) -> QgsFeature :
399
+ feature = PlanLayer .feature_from_model (plan )
400
+ layer = PlanLayer .get_from_project ()
393
401
394
- if plan_data .id_ is None :
395
- plan_layer .addFeature (plan_feature )
396
- else :
397
- plan_layer .updateFeature (plan_feature )
402
+ editing = plan .id_ is not None
403
+ _save_feature (
404
+ feature = feature ,
405
+ layer = layer ,
406
+ id_ = plan .id_ ,
407
+ edit_text = "Kaavan muokkaus" if editing else "Kaavan luominen" ,
408
+ )
398
409
399
- plan_layer .endEditCommand ()
400
- plan_layer .commitChanges (stopEditing = False )
410
+ # Check for deleted general regulations
411
+ if editing :
412
+ for association in RegulationGroupAssociationLayer .get_dangling_associations (
413
+ plan .general_regulations , feature ["id" ], PlanLayer .name
414
+ ):
415
+ _delete_feature (
416
+ association ,
417
+ RegulationGroupAssociationLayer .get_from_project (),
418
+ "Kaavamääräysryhmän assosiaation poisto" ,
419
+ )
401
420
402
- if plan_data .general_regulations :
403
- for regulation_group in plan_data .general_regulations :
404
- plan_id = plan_feature ["id" ]
421
+ # Save general regulations
422
+ if plan .general_regulations :
423
+ for regulation_group in plan .general_regulations :
424
+ plan_id = feature ["id" ]
405
425
regulation_group_feature = save_regulation_group (regulation_group , plan_id )
406
426
save_regulation_group_association (regulation_group_feature ["id" ], PlanLayer .name , plan_id )
407
427
408
- return plan_feature
428
+ return feature
409
429
410
430
411
431
def save_plan_feature (plan_feature : PlanFeature , plan_id : str | None = None ) -> QgsFeature :
0 commit comments