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

Set the bad layers as a property on the QgsProject #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions libqfieldsync/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtXml import QDomDocument

from .utils.bad_layer_handler import bad_layer_handler
from .utils.file_utils import slugify
from .utils.logger import logger

Expand Down Expand Up @@ -871,7 +870,9 @@ def filename(self) -> str:
@property
def is_localized_path(self) -> bool:
# on QFieldCloud localized layers will be invalid and therefore we get the layer source from `bad_layer_handler`
source = bad_layer_handler.invalid_layer_sources_by_id.get(self.layer.id())
source = getattr(self.project, "__libqfieldsync_bad_layers_by_id", {}).get(
self.layer.id()
)
if source:
return (
source.startswith("localized:")
Expand Down
12 changes: 11 additions & 1 deletion libqfieldsync/utils/bad_layer_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class set_bad_layer_handler:

def __init__(self, project: QgsProject):
self.project = project
setattr(
self.project,
"__libqfieldsync_bad_layers_by_id",
bad_layer_handler.invalid_layer_sources_by_id,
)

def __enter__(self):
bad_layer_handler.clear()
Expand All @@ -60,13 +65,18 @@ def __enter__(self):
# self.project.setBadLayerHandler(bad_layer_handler)

def __exit__(self, exc_type, exc_value, traceback):
setattr(
self.project,
"__libqfieldsync_bad_layers_by_id",
{**bad_layer_handler.invalid_layer_sources_by_id},
)

# NOTE we should set the bad layer handler only when we need it.
# Unfortunately we cannot due to a crash when calling `QgsProject.read()` when we already used this context manager.
# The code below is used as documentation for future generations of engineers willing to fix this.

# global bad_layer_handler
# self.project.setBadLayerHandler(None)
pass

def __call__(self, func):
def wrapper(*args, **kwargs):
Expand Down
Loading