Skip to content

Commit

Permalink
set offline layers in value relation widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Jun 2, 2020
1 parent fe06d3c commit a6118f3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion qfieldsync/core/offline_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
QgsBilinearRasterResampler,
QgsApplication,
QgsProcessingFeedback,
QgsProcessingContext
QgsProcessingContext,
QgsMapLayer,
QgsEditorWidgetSetup
)
import qgis

Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down

0 comments on commit a6118f3

Please sign in to comment.