From c54da53bef79cc932985e83028b7856244ff493f Mon Sep 17 00:00:00 2001 From: Johnny Sequeira Date: Thu, 10 Oct 2024 17:05:28 -0600 Subject: [PATCH] Adding the functionality for add packaged project filename and title Co-authored-by: Ivan Ivanov Co-authored-by: Ivan Ivanov Co-authored-by: Ivan Ivanov Co-authored-by: Ivan Ivanov Co-authored-by: Ivan Ivanov Co-authored-by: Ivan Ivanov --- libqfieldsync/offline_converter.py | 17 +++++++++-------- libqfieldsync/offliners.py | 12 ++++++++++-- libqfieldsync/utils/qgis.py | 10 ++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/libqfieldsync/offline_converter.py b/libqfieldsync/offline_converter.py index 3551ea9..1b8075e 100644 --- a/libqfieldsync/offline_converter.py +++ b/libqfieldsync/offline_converter.py @@ -84,7 +84,7 @@ class OfflineConverter(QObject): def __init__( self, project: QgsProject, - export_folder: str, + export_filename: str, area_of_interest_wkt: str, area_of_interest_crs: Union[str, QgsCoordinateReferenceSystem], attachment_dirs: List[str], @@ -92,6 +92,7 @@ def __init__( export_type: ExportType = ExportType.Cable, create_basemap: bool = True, dirs_to_copy: Optional[Dict[str, bool]] = None, + export_title: Optional[str] = None, ): super(OfflineConverter, self).__init__(parent=None) self.__max_task_progress = 0 @@ -102,7 +103,9 @@ def __init__( # elipsis workaround self.trUtf8 = self.tr - self.export_folder = Path(export_folder) + self.export_folder = Path(export_filename).parent + self._export_filename = Path(export_filename).stem + self._export_title = export_title self.export_type = export_type self.create_basemap = create_basemap self.area_of_interest = QgsPolygon() @@ -135,7 +138,7 @@ def convert(self, reload_original_project: bool = True) -> None: """ project = QgsProject.instance() self.original_filename = Path(project.fileName()) - self.backup_filename = make_temp_qgis_file(project) + self.backup_filename = make_temp_qgis_file(project, self._export_title) try: self._convert(project) @@ -272,8 +275,8 @@ def _convert(self, project: QgsProject) -> None: elif layer_action == SyncAction.REMOVE: project.removeMapLayer(layer) - export_project_filename = self.export_folder.joinpath( - f"{self.original_filename.stem}_qfield.qgs" + export_project_filename = Path(self.export_folder).joinpath( + f"{self._export_filename}.qgs" ) # save the original project path @@ -317,9 +320,7 @@ def _convert(self, project: QgsProject) -> None: ).transformBoundingBox(self.area_of_interest.boundingBox()) is_success = self.offliner.convert_to_offline( - gpkg_filename, - offline_layers, - bbox, + gpkg_filename, offline_layers, bbox, self._export_title ) if not is_success: diff --git a/libqfieldsync/offliners.py b/libqfieldsync/offliners.py index 8404381..6284d70 100644 --- a/libqfieldsync/offliners.py +++ b/libqfieldsync/offliners.py @@ -53,6 +53,7 @@ def convert_to_offline( offline_db_filename: str, layers: List[QgsMapLayer], bbox: Optional[QgsRectangle], + exported_project_title: Optional[str] = None, ) -> bool: raise NotImplementedError( "Expected `BaseOffliner` to be extended by a class that implements `convert_to_offline`." @@ -86,6 +87,7 @@ def convert_to_offline( offline_db_filename: str, layers: List[QgsMapLayer], bbox: Optional[QgsRectangle], + exported_project_title: Optional[str] = None, ) -> bool: project = QgsProject.instance() offline_db_path = Path(offline_db_filename).parent @@ -144,8 +146,11 @@ def convert_to_offline( offline_db_filename: str, layers: List[QgsMapLayer], bbox: Optional[QgsRectangle], + exported_project_title: Optional[str] = None, ) -> bool: - self._convert_to_offline_project(str(offline_db_filename), layers, bbox) + self._convert_to_offline_project( + str(offline_db_filename), layers, bbox, exported_project_title + ) return True def ogr_field_type(self, field: QgsField) -> ogr.FieldDefn: @@ -351,6 +356,7 @@ def _convert_to_offline_project( offline_gpkg_path: str, offline_layers: Optional[List[QgsMapLayer]], bbox: Optional[QgsRectangle], + exported_project_title: Optional[str] = None, ) -> None: """Converts the currently loaded QgsProject to an offline project. Offline layers are written to ``offline_gpkg_path``. Only valid vector layers are written. @@ -444,7 +450,9 @@ class LayerInfo(NamedTuple): self.update_data_provider(layer_info.layer, source) layer_info.layer.setSubsetString(layer_info.subset_string) - project_title = project.title() + project_title = ( + exported_project_title if exported_project_title else project.title() + ) if not project_title: project_title = QFileInfo(project.fileName()).baseName() diff --git a/libqfieldsync/utils/qgis.py b/libqfieldsync/utils/qgis.py index f109a63..611b711 100644 --- a/libqfieldsync/utils/qgis.py +++ b/libqfieldsync/utils/qgis.py @@ -19,8 +19,8 @@ ***************************************************************************/ """ -import logging import os +import logging import tempfile from pathlib import Path from typing import List, Optional, Union @@ -48,13 +48,19 @@ def open_project(filename: str, filename_to_read: Optional[str] = None) -> bool: return is_success -def make_temp_qgis_file(project: QgsProject) -> str: +def make_temp_qgis_file( + project: QgsProject, + exported_project_title: Optional[str] = None, +) -> str: project_backup_dir = tempfile.mkdtemp() original_filename = project.fileName() backup_filename = os.path.join(project_backup_dir, f"{project.baseName()}.qgs") project.write(backup_filename) project.setFileName(original_filename) + if exported_project_title: + project.setTitle(exported_project_title) + return backup_filename