From a6118f35edd7406b97dc77ff1cfbded725aded24 Mon Sep 17 00:00:00 2001 From: David Signer Date: Tue, 2 Jun 2020 13:18:39 +0200 Subject: [PATCH 1/2] set offline layers in value relation widgets --- qfieldsync/core/offline_converter.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/qfieldsync/core/offline_converter.py b/qfieldsync/core/offline_converter.py index eae79f9d..a2a5921d 100644 --- a/qfieldsync/core/offline_converter.py +++ b/qfieldsync/core/offline_converter.py @@ -43,7 +43,9 @@ QgsBilinearRasterResampler, QgsApplication, QgsProcessingFeedback, - QgsProcessingContext + QgsProcessingContext, + QgsMapLayer, + QgsEditorWidgetSetup ) import qgis @@ -78,6 +80,7 @@ def convert(self): """ project = QgsProject.instance() + original_project = project original_project_path = project.fileName() project_filename, _ = os.path.splitext(os.path.basename(original_project_path)) @@ -96,6 +99,10 @@ def convert(self): self.__offline_layers = list() self.__layers = list(project.mapLayers().values()) + original_names = {} + for layer in self.__layers: + original_names[layer.id()] = layer.name() + self.total_progress_updated.emit(0, 1, self.tr('Creating base map')) # Create the base map before layers are removed if self.project_configuration.create_base_map: @@ -150,6 +157,7 @@ def convert(self): offline_layer_ids, self.project_configuration.offline_copy_only_aoi, self.offline_editing.GPKG): raise Exception(self.tr("Error trying to convert layers to offline layers")) + except AttributeError: # Run the offline plugin for spatialite spatialite_filename = "data.sqlite" @@ -166,6 +174,18 @@ def convert(self): QgsProject.instance().setEvaluateDefaultValues(False) QgsProject.instance().setAutoTransaction(False) + # check and convert value relations + for layer in project.mapLayers().values(): + if layer.type() == QgsMapLayer.VectorLayer: + for field in layer.fields(): + ews = field.editorWidgetSetup() + if ews.type() == 'ValueRelation': + online_layer_id = ews.config()['Layer'] + layer_name = original_names[online_layer_id] + " (offline)" + layer_id = project.mapLayersByName(layer_name)[0].id() + ews.config()['Layer'] = layer_id + layer.setEditorWidgetSetup(layer.fields().indexOf(field.name()), ews) + # Now we have a project state which can be saved as offline project QgsProject.instance().write(project_path) finally: From 5402247a382a84535a08fe7473e07f191344db0b Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 2 Jun 2020 15:08:16 +0200 Subject: [PATCH 2/2] Fix setting editor widget setup --- qfieldsync/core/offline_converter.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/qfieldsync/core/offline_converter.py b/qfieldsync/core/offline_converter.py index a2a5921d..d2c1e19f 100644 --- a/qfieldsync/core/offline_converter.py +++ b/qfieldsync/core/offline_converter.py @@ -174,17 +174,22 @@ def convert(self): QgsProject.instance().setEvaluateDefaultValues(False) QgsProject.instance().setAutoTransaction(False) - # check and convert value relations + # check if value relations point to offline layers and adjust if necessary for layer in project.mapLayers().values(): if layer.type() == QgsMapLayer.VectorLayer: for field in layer.fields(): ews = field.editorWidgetSetup() if ews.type() == 'ValueRelation': - online_layer_id = ews.config()['Layer'] + widget_config = ews.config() + online_layer_id = widget_config['Layer'] + if project.mapLayer(online_layer_id): + continue + layer_name = original_names[online_layer_id] + " (offline)" layer_id = project.mapLayersByName(layer_name)[0].id() - ews.config()['Layer'] = layer_id - layer.setEditorWidgetSetup(layer.fields().indexOf(field.name()), ews) + widget_config['Layer'] = layer_id + offline_ews = QgsEditorWidgetSetup(ews.type(), widget_config) + layer.setEditorWidgetSetup(layer.fields().indexOf(field.name()), offline_ews) # Now we have a project state which can be saved as offline project QgsProject.instance().write(project_path)