Skip to content

Commit

Permalink
Adding the functionality for add packaged project filename and title
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Ivanov <[email protected]>

Co-authored-by: Ivan Ivanov <[email protected]>

Co-authored-by: Ivan Ivanov <[email protected]>

Co-authored-by: Ivan Ivanov <[email protected]>

Co-authored-by: Ivan Ivanov <[email protected]>

Co-authored-by: Ivan Ivanov <[email protected]>
  • Loading branch information
SeqLaz and suricactus committed Oct 22, 2024
1 parent 7901b65 commit c54da53
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
17 changes: 9 additions & 8 deletions libqfieldsync/offline_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ 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],
offliner: BaseOffliner,
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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions libqfieldsync/offliners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 8 additions & 2 deletions libqfieldsync/utils/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
***************************************************************************/
"""

import logging
import os
import logging
import tempfile
from pathlib import Path
from typing import List, Optional, Union
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit c54da53

Please sign in to comment.