Skip to content

Commit b0f746a

Browse files
committed
Fixing the always embed symbols to project, and set the path to relative to project location
1 parent a98ae15 commit b0f746a

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

libqfieldsync/offline_converter.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from .utils.file_utils import (
5555
copy_attachments,
5656
copy_multifile,
57-
embed_layer_symbols_on_project,
57+
set_relative_embed_layer_symbols_on_project,
5858
)
5959
from .utils.logger import logger
6060
from .utils.qgis import make_temp_qgis_file, open_project
@@ -306,9 +306,6 @@ def _convert(self, project: QgsProject) -> None:
306306
elif layer_action == SyncAction.REMOVE:
307307
project.removeMapLayer(layer)
308308

309-
# Change symbol path to embedded marker
310-
embed_layer_symbols_on_project(layer, self._export_filename.parent)
311-
312309
self.remove_empty_groups_from_layer_tree_group(project.layerTreeRoot())
313310

314311
export_project_filename = self._export_filename
@@ -379,6 +376,11 @@ def _convert(self, project: QgsProject) -> None:
379376
# Disable project options that could create problems on a portable
380377
# project with offline layers
381378
self.post_process_offline_layers()
379+
# Change SVG and Raster symbols path to relative or embedded
380+
for layer in project_layers:
381+
set_relative_embed_layer_symbols_on_project(
382+
layer, self.original_filename.parent
383+
)
382384

383385
self._check_canceled()
384386

libqfieldsync/utils/file_utils.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
from qgis.core import (
3434
QgsCategorizedSymbolRenderer,
35-
QgsProject,
3635
QgsRasterMarkerSymbolLayer,
3736
QgsRuleBasedRenderer,
3837
QgsSingleSymbolRenderer,
@@ -230,11 +229,12 @@ def is_valid_filepath(path: str) -> bool:
230229
return True
231230

232231

233-
def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
232+
def update_symbols_to_relative_embedded(symbol: QgsSymbol, home_path: Path) -> None:
234233
"""
235-
Update SVG or Raster symbols layer to embed it in the QGIS project.
234+
Update SVG or Raster symbols layer to relative path or embed it in the QGIS project.
236235
Args:
237236
symbol: The QGIS symbol (from a renderer).
237+
home_path: QGIS Project home path.
238238
"""
239239
if symbol is None:
240240
return
@@ -248,10 +248,6 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
248248

249249
source_path = Path(symbol_layer.path())
250250

251-
# If the symbol's path is already relative, we have nothing to do
252-
if source_path.is_relative_to(new_path):
253-
continue
254-
255251
# Check if symbol is already embedded
256252
if str(source_path)[:8].startswith("base64:"):
257253
continue
@@ -260,20 +256,25 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
260256
if not source_path.is_file():
261257
continue
262258

263-
with open(source_path, "rb") as file:
264-
file_data = file.read()
265-
encoded_data = base64.b64encode(file_data).decode()
266-
symbol_layer.setPath(f"base64:{encoded_data}")
259+
# If the symbol's path is already relative, we have nothing to do
260+
if source_path.is_relative_to(str(home_path)):
261+
symbol_layer.setPath(str(source_path.relative_to(home_path)))
262+
else:
263+
with open(source_path, "rb") as file:
264+
file_data = file.read()
265+
encoded_data = base64.b64encode(file_data).decode()
266+
symbol_layer.setPath(f"base64:{encoded_data}")
267267

268268

269-
def embed_layer_symbols_on_project(
270-
layer: QgsVectorLayer, new_path: Optional[Path] = None
269+
def set_relative_embed_layer_symbols_on_project(
270+
layer: QgsVectorLayer, project_home: Path
271271
) -> None:
272272
"""
273-
Update the paths of symbols to embedded symbols in the QGIS project.
273+
Update the paths of symbols to relative or embedded symbols in the QGIS project if not relative to project home.
274274
275275
Args:
276276
layer: The QgsVectorLayer to update. The layer is a point layer.
277+
project_home: QGIS Project home path.
277278
"""
278279

279280
if (
@@ -287,15 +288,10 @@ def embed_layer_symbols_on_project(
287288
if not renderer:
288289
return
289290

290-
if new_path is None:
291-
project = QgsProject.instance()
292-
project_home = project.homePath()
293-
new_path = Path(project_home)
294-
295291
if isinstance(renderer, QgsSingleSymbolRenderer):
296292
symbol = renderer.symbol()
297293
if symbol:
298-
update_symbols_to_embedded(symbol=symbol, new_path=new_path)
294+
update_symbols_to_relative_embedded(symbol, project_home)
299295

300296
elif isinstance(renderer, QgsRuleBasedRenderer):
301297
for rule in renderer.rootRule().children():
@@ -305,16 +301,17 @@ def embed_layer_symbols_on_project(
305301
continue
306302

307303
for symbol in symbols:
308-
update_symbols_to_embedded(symbol=symbol, new_path=new_path)
304+
update_symbols_to_relative_embedded(symbol, project_home)
309305

310306
elif isinstance(renderer, QgsCategorizedSymbolRenderer):
311307
categories = renderer.categories()
312308
if categories:
313309
for index in range(len(categories)):
314-
# Get a fresh category. The renderer doesn't update in-place modifications.
310+
# Get a fresh category.
311+
# The renderer doesn't update in-place modifications on categorized.
315312
category = renderer.categories()[index]
316313
symbol = category.symbol().clone()
317-
update_symbols_to_embedded(symbol=symbol, new_path=new_path)
314+
update_symbols_to_relative_embedded(symbol, project_home)
318315
renderer.updateCategorySymbol(index, symbol)
319316

320317
layer.setRenderer(renderer)

0 commit comments

Comments
 (0)