Skip to content

Commit 5eaf636

Browse files
committed
fix: only load layers when set visible
1 parent 15c0a81 commit 5eaf636

1 file changed

Lines changed: 81 additions & 12 deletions

File tree

plugin_code/qgis_shogun_editor.py

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
Qgis,
2929
QgsBrowserModel,
3030
QgsCoordinateReferenceSystem,
31+
QgsLayerTreeGroup,
3132
QgsMessageLog,
3233
QgsNetworkAccessManager,
3334
QgsPointXY,
3435
QgsProject,
3536
QgsRasterLayer,
3637
QgsSettings,
37-
QgsVectorLayer
38+
QgsVectorLayer,
39+
QgsLayerTreeLayer
3840
)
3941

40-
# some things for doing http requests
41-
from qgis.PyQt.QtCore import QCoreApplication, QSettings, Qt, QTranslator, QUrl
42+
from qgis.PyQt.QtCore import QCoreApplication, QSettings, Qt, QTranslator, QUrl, QTimer
4243
from qgis.PyQt.QtGui import QDesktopServices, QIcon, QPixmap
4344
from qgis.PyQt.QtNetwork import QNetworkRequest
4445
from qgis.PyQt.QtWidgets import QAction
@@ -286,10 +287,21 @@ def _handle_double_click(self, item):
286287
)
287288

288289
root = QgsProject.instance().layerTreeRoot()
290+
#disconnect old connections and clear root
291+
try:
292+
root.visibilityChanged.disconnect(self.on_visibility_changed)
293+
except TypeError:
294+
QgsMessageLog.logMessage(
295+
"Could not remove the signal visibilityChanged",
296+
'QgisShogunEditor',
297+
level=Qgis.Info
298+
)
289299
root.clear()
300+
290301
layer_ids = self.find_all_layer_ids(application.layer_tree)
291302
layers_content = self.layer_service.get_layers_by_ids(layer_ids)
292303
self.buildLayerTree(application.layer_tree, layers_content, root)
304+
root.visibilityChanged.connect(self.on_visibility_changed)
293305
except json.JSONDecodeError as e:
294306
QgsMessageLog.logMessage(
295307
f"Could not decode layer tree json: {e}", 'QgisShogunEditor',
@@ -319,6 +331,45 @@ def apply_mapview(self, application: Application):
319331
inches_per_meter = 39.37
320332
map_canvas.zoomScale(resolution * dpi * inches_per_meter)
321333

334+
def on_visibility_changed(self, node):
335+
if isinstance(node, QgsLayerTreeLayer):
336+
is_visible = node.isVisible()
337+
is_loaded = node.customProperty('loaded', False)
338+
if is_loaded:
339+
return
340+
if not is_visible:
341+
return
342+
343+
layer_in_tree = node.customProperty('layer_in_tree')
344+
layer_group = node.customProperty('layer_group')
345+
if not layer_in_tree:
346+
return
347+
348+
parent_node = node.parent()
349+
if not parent_node:
350+
return
351+
352+
index = parent_node.children().index(node)
353+
layer = self.addQgsLayer(
354+
layer_in_tree,
355+
layer_group,
356+
True,
357+
node.name(),
358+
index
359+
)
360+
if not layer:
361+
return
362+
363+
QTimer.singleShot(
364+
0,
365+
lambda n=node, p=parent_node: p.removeChildNode(n)
366+
)
367+
node.setCustomProperty('loaded', True)
368+
return
369+
370+
elif isinstance(node, QgsLayerTreeGroup):
371+
return
372+
322373
def open_project_link(self, event):
323374
if event.button() == Qt.LeftButton:
324375
QDesktopServices.openUrl(QUrl("https://www.terrestris.de/de/"))
@@ -421,7 +472,7 @@ def createLayer(self, layer_in_tree):
421472
print('create WFS layer')
422473
return self.createWfsLayerFromShogun(layer_src_conf)
423474

424-
def addQgsLayer(self, layer_in_tree, layer_group):
475+
def addQgsLayer(self, layer_in_tree, layer_group, layer_is_visible, title, index=None):
425476
self.qgisLayers = []
426477
# layerutils
427478
layer = self.createLayer(layer_in_tree)
@@ -432,21 +483,39 @@ def addQgsLayer(self, layer_in_tree, layer_group):
432483
)
433484
return
434485

435-
QgsProject.instance().addMapLayer(layer, False) # implicit addition
436-
layer_group.addLayer(layer) # eplicit addition
486+
# explicit addition
487+
if index is not None:
488+
layer_group_layer = layer_group.insertLayer(index, layer)
489+
else:
490+
layer_group_layer = layer_group.addLayer(layer)
491+
492+
if title or title !='':
493+
layer_group_layer.setName(title)
494+
if layer_is_visible:
495+
QgsProject.instance().addMapLayer(layer, False) # implicit addition
437496
return layer
438497

439498
def buildLayerTree(self, applications_layertree, layers_content, root):
440499
if 'layerId' not in applications_layertree:
441500
new_group = root.addGroup(applications_layertree['title']) # option: take the name of the application
501+
new_group.setItemVisibilityChecked(applications_layertree.get('checked', True))
442502

443-
if 'layerId' in applications_layertree:
444-
layer_in_tree = [layer for layer in layers_content if layer.get_id() == applications_layertree['layerId']][0]
445-
self.addQgsLayer(layer_in_tree, root)
503+
if 'children' in applications_layertree:
504+
for child in applications_layertree['children']:
505+
self.buildLayerTree(child, layers_content, new_group)
506+
return new_group
446507

447-
if 'children' in applications_layertree:
448-
for child in applications_layertree['children']:
449-
self.buildLayerTree(child, layers_content, new_group)
508+
if 'layerId' in applications_layertree:
509+
layer_in_tree = next((layer for layer in layers_content if layer.get_id() == applications_layertree['layerId']), None)
510+
layer_is_visible = applications_layertree.get('checked', False)
511+
layer = self.addQgsLayer(layer_in_tree, root, layer_is_visible, applications_layertree['title'])
512+
if layer:
513+
layer_node = root.findLayer(layer.id())
514+
if layer_node:
515+
layer_node.setItemVisibilityChecked(layer_is_visible)
516+
layer_node.setCustomProperty('layer_in_tree', layer_in_tree)
517+
layer_node.setCustomProperty('layer_group', root)
518+
return layer
450519

451520
def sanitize_shogun_url(self, shogun_url):
452521
if shogun_url.endswith('/graphql'):

0 commit comments

Comments
 (0)