55from qgis .core import QgsMapLayerProxyModel , QgsWkbTypes
66from qgis .PyQt import uic
77
8+ from ...main .helpers import ColumnMatcher , get_layer_names
9+
810
911class StratigraphicLayersWidget (QWidget ):
1012 def __init__ (self , parent = None , data_manager = None ):
@@ -50,6 +52,8 @@ def __init__(self, parent=None, data_manager=None):
5052 self .useStructuralPointsZCoordinatesCheckBox .stateChanged .connect (
5153 self .onStructuralDataFieldChanged
5254 )
55+ self ._guess_layers_and_fields ()
56+ self ._restore_selection ()
5357
5458 def enableBasalContactsZCheckBox (self , enable ):
5559 self .useBasalContactsZCoordinatesCheckBox .setEnabled (enable )
@@ -116,6 +120,7 @@ def set_orientations_layer(
116120 def onBasalContactsChanged (self , layer ):
117121 self .unitNameField .setLayer (layer )
118122 self .data_manager .set_basal_contacts (layer , self .unitNameField .currentField ())
123+ self ._persist_selection ()
119124
120125 def onOrientationTypeChanged (self , index ):
121126 if index == 0 :
@@ -153,6 +158,7 @@ def onStructuralDataFieldChanged(self, field):
153158 self .orientationType .currentText (),
154159 use_z_coordinate = self .structural_points_use_z ,
155160 )
161+ self ._persist_selection ()
156162 # self.updateDataManager()
157163
158164 def onUnitFieldChanged (self , field ):
@@ -161,5 +167,91 @@ def onUnitFieldChanged(self, field):
161167 field ,
162168 use_z_coordinate = self .basal_contacts_use_z ,
163169 )
170+ self ._persist_selection ()
164171
165172 # self.updateDataManager()
173+
174+ def _guess_layers_and_fields (self ):
175+ if not self .data_manager :
176+ return
177+ # Basal contacts
178+ basal_names = get_layer_names (self .basalContactsLayer )
179+ basal_matcher = ColumnMatcher (basal_names )
180+ basal_match = basal_matcher .find_match ('BASAL_CONTACTS' )
181+ if basal_match :
182+ layer = self .data_manager .find_layer_by_name (basal_match )
183+ if layer :
184+ self .basalContactsLayer .setLayer (layer )
185+ fields = [f .name () for f in layer .fields ()]
186+ fmatcher = ColumnMatcher (fields )
187+ if unit_match := fmatcher .find_match ('UNITNAME' ):
188+ self .unitNameField .setField (unit_match )
189+ # Structural data
190+ structural_names = get_layer_names (self .structuralDataLayer )
191+ structural_matcher = ColumnMatcher (structural_names )
192+ structural_match = structural_matcher .find_match ('STRUCTURE' ) or structural_matcher .find_match (
193+ 'ORIENTATION'
194+ )
195+ if structural_match :
196+ layer = self .data_manager .find_layer_by_name (structural_match )
197+ if layer :
198+ self .structuralDataLayer .setLayer (layer )
199+ fields = [f .name () for f in layer .fields ()]
200+ fmatcher = ColumnMatcher (fields )
201+ if strike_match := fmatcher .find_match ('STRIKE' ) or fmatcher .find_match ('DIPDIR' ):
202+ self .orientationField .setField (strike_match )
203+ if dip_match := fmatcher .find_match ('DIP' ):
204+ self .dipField .setField (dip_match )
205+ if unit_match := fmatcher .find_match ('UNITNAME' ):
206+ self .structuralDataUnitName .setField (unit_match )
207+
208+ def _persist_selection (self ):
209+ if not self .data_manager :
210+ return
211+ settings = {
212+ 'basal_layer' : self .basalContactsLayer .currentLayer ().name ()
213+ if self .basalContactsLayer .currentLayer ()
214+ else None ,
215+ 'structural_layer' : self .structuralDataLayer .currentLayer ().name ()
216+ if self .structuralDataLayer .currentLayer ()
217+ else None ,
218+ 'unit_name_field' : self .unitNameField .currentField (),
219+ 'orientation_field' : self .orientationField .currentField (),
220+ 'dip_field' : self .dipField .currentField (),
221+ 'structural_unit_field' : self .structuralDataUnitName .currentField (),
222+ 'orientation_type' : self .orientationType .currentText (),
223+ 'use_basal_z' : self .useBasalContactsZCoordinatesCheckBox .isChecked (),
224+ 'use_structural_z' : self .useStructuralPointsZCoordinatesCheckBox .isChecked (),
225+ }
226+ self .data_manager .set_widget_settings ('stratigraphic_layers_widget' , settings )
227+
228+ def _restore_selection (self ):
229+ if not self .data_manager :
230+ return
231+ settings = self .data_manager .get_widget_settings ('stratigraphic_layers_widget' , {})
232+ if not settings :
233+ return
234+ if layer_name := settings .get ('basal_layer' ):
235+ layer = self .data_manager .find_layer_by_name (layer_name )
236+ if layer :
237+ self .basalContactsLayer .setLayer (layer )
238+ if layer_name := settings .get ('structural_layer' ):
239+ layer = self .data_manager .find_layer_by_name (layer_name )
240+ if layer :
241+ self .structuralDataLayer .setLayer (layer )
242+ if field := settings .get ('unit_name_field' ):
243+ self .unitNameField .setField (field )
244+ if field := settings .get ('orientation_field' ):
245+ self .orientationField .setField (field )
246+ if field := settings .get ('dip_field' ):
247+ self .dipField .setField (field )
248+ if field := settings .get ('structural_unit_field' ):
249+ self .structuralDataUnitName .setField (field )
250+ if 'orientation_type' in settings :
251+ idx = self .orientationType .findText (settings ['orientation_type' ], Qt .MatchFixedString )
252+ if idx >= 0 :
253+ self .orientationType .setCurrentIndex (idx )
254+ if 'use_basal_z' in settings :
255+ self .useBasalContactsZCoordinatesCheckBox .setChecked (settings ['use_basal_z' ])
256+ if 'use_structural_z' in settings :
257+ self .useStructuralPointsZCoordinatesCheckBox .setChecked (settings ['use_structural_z' ])
0 commit comments