24
24
from arho_feature_template .gui .dialogs .serialize_plan import SerializePlan
25
25
from arho_feature_template .gui .docks .new_feature_dock import NewFeatureDock
26
26
from arho_feature_template .gui .tools .inspect_plan_features_tool import InspectPlanFeatures
27
- from arho_feature_template .project .layers .code_layers import PlanRegulationGroupTypeLayer
27
+ from arho_feature_template .project .layers .code_layers import PlanRegulationGroupTypeLayer , code_layers
28
28
from arho_feature_template .project .layers .plan_layers import (
29
29
LandUseAreaLayer ,
30
30
LandUsePointLayer ,
@@ -79,14 +79,11 @@ def __init__(self):
79
79
self .json_plan_path = None
80
80
self .json_plan_outline_path = None
81
81
82
- # Initialize libraries
83
- self .feature_template_libraries = [
84
- FeatureTemplateLibrary .from_config_file (file ) for file in feature_template_library_config_files ()
85
- ]
86
- self .regulation_group_libraries = None
82
+ self .feature_template_libraries = []
83
+ self .regulation_group_libraries = []
87
84
88
85
# Initialize new feature dock
89
- self .new_feature_dock = NewFeatureDock (self . feature_template_libraries )
86
+ self .new_feature_dock = NewFeatureDock ()
90
87
self .new_feature_dock .tool_activated .connect (self .add_new_plan_feature )
91
88
self .new_feature_dock .hide ()
92
89
@@ -107,13 +104,36 @@ def __init__(self):
107
104
self .lambda_service = LambdaService ()
108
105
self .lambda_service .jsons_received .connect (self .save_plan_jsons )
109
106
110
- def get_regulation_group_libraries (self ):
111
- # Lazy initialization of regulation_group_libraries to avoid reading regulation code layer too early
112
- if self .regulation_group_libraries is None :
113
- self .regulation_group_libraries = [
114
- RegulationGroupLibrary .from_config_file (file ) for file in regulation_group_library_config_files ()
115
- ]
116
- return self .regulation_group_libraries
107
+ def initialize_from_project (self ):
108
+ # # If project is not open, don't try to initialize
109
+ # # NOTE: QgsProject.instance(), QgsProject().fileInfo() and QgsProject().fileIName()
110
+ # # don't seem to work as indicators whether a project is open?
111
+ # if not QgsProject.instance() or not QgsProject().fileInfo():
112
+ # return
113
+ self .initialize_libraries ()
114
+
115
+ def check_required_layers (self ):
116
+ missing_layers = []
117
+ for layer in code_layers + plan_layers :
118
+ if not layer .exists ():
119
+ missing_layers .append (layer .name ) # noqa: PERF401
120
+ if len (missing_layers ) > 0 : # noqa: SIM103
121
+ # iface.messageBar().pushWarning("", f"Project is missing required layers: {', '.join(missing_layers)}")
122
+ return False
123
+ return True
124
+
125
+ def initialize_libraries (self ):
126
+ if not self .check_required_layers ():
127
+ return
128
+
129
+ self .regulation_group_libraries = [
130
+ RegulationGroupLibrary .from_config_file (file ) for file in regulation_group_library_config_files ()
131
+ ]
132
+ self .feature_template_libraries = [
133
+ FeatureTemplateLibrary .from_config_file (file , self .regulation_group_libraries )
134
+ for file in feature_template_library_config_files ()
135
+ ]
136
+ self .new_feature_dock .initialize_feature_template_libraries (self .feature_template_libraries )
117
137
118
138
def toggle_identify_plan_features (self , activate : bool ): # noqa: FBT001
119
139
if activate :
@@ -179,7 +199,7 @@ def edit_plan(self):
179
199
return
180
200
plan_model = PlanLayer .model_from_feature (feature )
181
201
182
- attribute_form = PlanAttributeForm (plan_model , self .get_regulation_group_libraries () )
202
+ attribute_form = PlanAttributeForm (plan_model , self .regulation_group_libraries )
183
203
if attribute_form .exec_ ():
184
204
feature = save_plan (attribute_form .model )
185
205
@@ -210,7 +230,7 @@ def _plan_geom_digitized(self, feature: QgsFeature):
210
230
return
211
231
212
232
plan_model = Plan (geom = feature .geometry ())
213
- attribute_form = PlanAttributeForm (plan_model , self .get_regulation_group_libraries () )
233
+ attribute_form = PlanAttributeForm (plan_model , self .regulation_group_libraries )
214
234
if attribute_form .exec_ ():
215
235
feature = save_plan (attribute_form .model )
216
236
plan_to_be_activated = feature ["id" ]
@@ -237,7 +257,7 @@ def _plan_feature_geom_digitized(self, feature: QgsFeature):
237
257
238
258
plan_feature .geom = feature .geometry ()
239
259
attribute_form = PlanFeatureForm (
240
- plan_feature , title , [* self .get_regulation_group_libraries () , regulation_group_library_from_active_plan ()]
260
+ plan_feature , title , [* self .regulation_group_libraries , regulation_group_library_from_active_plan ()]
241
261
)
242
262
if attribute_form .exec_ ():
243
263
save_plan_feature (attribute_form .model )
@@ -249,7 +269,7 @@ def edit_plan_feature(self, feature: QgsFeature, layer_name: str):
249
269
# Geom editing handled with basic QGIS vertex editing?
250
270
title = plan_feature .name if plan_feature .name else layer_name
251
271
attribute_form = PlanFeatureForm (
252
- plan_feature , title , [* self .get_regulation_group_libraries () , regulation_group_library_from_active_plan ()]
272
+ plan_feature , title , [* self .regulation_group_libraries , regulation_group_library_from_active_plan ()]
253
273
)
254
274
if attribute_form .exec_ ():
255
275
save_plan_feature (attribute_form .model )
0 commit comments