Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert/package faster! #68

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions libqfieldsync/offline_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from typing import Dict, List, Optional, Union

from qgis.core import (
Qgis,
QgsApplication,
QgsBilinearRasterResampler,
QgsCoordinateReferenceSystem,
Expand Down Expand Up @@ -125,7 +126,7 @@ def __init__(
self.project_configuration = ProjectConfiguration(project)

# flake8: noqa: max-complexity: 33
def convert(self) -> None:
def convert(self, reload_original_project: bool = True) -> None:
"""
Convert the project to a portable project.
"""
Expand All @@ -136,34 +137,50 @@ def convert(self) -> None:
try:
self._convert(project)
finally:
QCoreApplication.processEvents()
QgsProject.instance().clear()
QCoreApplication.processEvents()
if reload_original_project:
QCoreApplication.processEvents()
QgsProject.instance().clear()
QCoreApplication.processEvents()

open_project(str(self.original_filename), self.backup_filename)
open_project(str(self.original_filename), self.backup_filename)

self.total_progress_updated.emit(100, 100, self.tr("Finished"))

def _convert(self, project: QgsProject) -> None:
xml_elements_to_preserve = {}
on_original_project_read = self._on_original_project_read_wrapper(
xml_elements_to_preserve
)
project.readProject.connect(on_original_project_read)
tmp_project_filename = ""

if self.export_type == ExportType.Cable:
# the `backup_filename` is copied right after packaging is requested. It has all the unsaved
# project settings, which means they will be available in the packaged project too.
project.read(self.backup_filename)
tmp_project_filename = self.backup_filename
elif self.export_type == ExportType.Cloud:
# if you save the project without QGIS GUI, the project no longer has `theMapCanvas` canvas
# so we should use the original project file that already has `theMapCanvas`. There is no
# gain using the `backup_filename`, since there is no user to modify the project.
project.read(project.fileName())
tmp_project_filename = project.fileName()
else:
raise NotImplementedError(f"Unknown package type: {self.export_type}")

project.readProject.disconnect(on_original_project_read)
# Set flags that usually significantly speed-up project file read
read_flags = QgsProject.ReadFlags()
read_flags |= QgsProject.FlagDontResolveLayers
read_flags |= QgsProject.FlagDontLoadLayouts
if Qgis.versionInt() >= 32600:
read_flags |= QgsProject.FlagDontLoad3DViews

# Make a new function object that we can connect and disconnect easily
on_original_project_read = self._on_original_project_read_wrapper(
xml_elements_to_preserve
)

# Create a new temporary `QgsProject` instance just to make sure that `theMapCanvas`
# XML object is properly set within the XML document. Using a new `QgsProject`
# instead of the singleton `QgsProject.instance()` allows using the read flags.
tmp_project = QgsProject()
tmp_project.readProject.connect(on_original_project_read)
tmp_project.read(tmp_project_filename, read_flags)
tmp_project.readProject.disconnect(on_original_project_read)

self.export_folder.mkdir(parents=True, exist_ok=True)
self.total_progress_updated.emit(0, 100, self.trUtf8("Converting project…"))
Expand Down
Loading