Skip to content

Commit

Permalink
Use more complex logic for layer/group ordering (#88)
Browse files Browse the repository at this point in the history
Create a simplified, one-level deep, representation of the QGIS project structure and then use this to determine the desired
ordering of the Felt project. Always specify ordering keys for layers and groups.
  • Loading branch information
nyalldawson authored Jun 27, 2024
1 parent 7a5c6d7 commit 4117a9b
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 48 deletions.
60 changes: 56 additions & 4 deletions felt/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Union,
Tuple
)
from dataclasses import dataclass

from qgis.PyQt.QtCore import (
QUrl,
Expand Down Expand Up @@ -38,6 +39,16 @@
PLUGIN_VERSION = "0.7.0"


@dataclass
class CreatedGroupDetails:
"""
Encapsulates details of a created layer group
"""
group_id: Optional[str] = None
name: Optional[str] = None
ordering_key: Optional[int] = None


class FeltApiClient:
"""
Client for the Felt API
Expand Down Expand Up @@ -472,8 +483,9 @@ def update_layer_details(self,

def create_layer_groups(self,
map_id: str,
layer_group_names: List[str]) \
-> QgsNetworkReplyContent:
layer_group_names: List[str],
ordering_keys: Optional[Dict[str, int]] = None) \
-> List[CreatedGroupDetails]:
"""
Creates layer groups for a map
"""
Expand All @@ -483,9 +495,49 @@ def create_layer_groups(self,
version=2
)

if not ordering_keys:
group_post_data = [
{'name': g,
'ordering_key': i} for i, g in enumerate(layer_group_names)
]
else:
group_post_data = [
{'name': g,
'ordering_key': ordering_keys[g] or 0} for g in
layer_group_names
]

reply = QgsNetworkAccessManager.instance().blockingPost(
request,
json.dumps(group_post_data).encode()
)

return [
CreatedGroupDetails(
group_id=group['id'],
name=group['name'],
ordering_key=ordering_keys.get(group['name']))
for group in json.loads(reply.content().data().decode())
]

def apply_layer_groups_updates(self,
map_id: str,
group_details: List[CreatedGroupDetails]) \
-> QgsNetworkReplyContent:
"""
Updates layer group details
"""
request = self._build_request(
self.LAYER_GROUPS_ENDPOINT.format(map_id),
{'Content-Type': 'application/json'},
version=2
)

group_post_data = [
{'name': g,
'ordering_key': i} for i, g in enumerate(layer_group_names)
{'id': g.group_id,
'name': g.name,
'ordering_key': g.ordering_key} for g in
group_details
]

return QgsNetworkAccessManager.instance().blockingPost(
Expand Down
26 changes: 24 additions & 2 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ class ZippedExportResult:
qgis_style_xml: str
style: Optional[LayerStyle] = None
group_name: Optional[str] = None
ordering_key: Optional[int] = None


@dataclass
class ImportByUrlResult:
"""
Results of an import by URL operation
"""
layer_id: Optional[str] = None
error_message: Optional[str] = None
group_name: Optional[str] = None
ordering_key: Optional[int] = None


class LayerExporter(QObject):
Expand Down Expand Up @@ -178,7 +190,8 @@ def layer_import_url(layer: QgsMapLayer) -> Optional[str]:

@staticmethod
def import_from_url(layer: QgsMapLayer, target_map: Map,
feedback: Optional[QgsFeedback] = None) -> Dict:
feedback: Optional[QgsFeedback] = None) \
-> ImportByUrlResult:
"""
Imports a layer from URI to the given map
"""
Expand All @@ -191,7 +204,16 @@ def import_from_url(layer: QgsMapLayer, target_map: Map,
blocking=True,
feedback=feedback
)
return json.loads(reply.content().data().decode())
response = json.loads(reply.content().data().decode())

res = ImportByUrlResult()

if 'errors' in response:
res.error_message = response['errors'][0]['detail']
return res

res.layer_id = response['layer_id']
return res

@staticmethod
def merge_dicts(tgt: Dict, enhancer: Dict) -> Dict:
Expand Down
Loading

0 comments on commit 4117a9b

Please sign in to comment.