From 0fb39fe99d493ffb6eb7a8275e195dae8ff4e73e Mon Sep 17 00:00:00 2001 From: Jean Felder Date: Tue, 10 Dec 2024 18:08:29 +0100 Subject: [PATCH 1/2] qgslayertreemodel: Ensure to refresh when elevation properties change If an elevation profile widget is already open, when a new raster layer is added, it is not added to the elevation widget treeview. Indeed, when a new raster is added, `QgsRasterLayer::elevationProperties::hasElevation()` returns `False` by default and the proxy model of the elevation filters out the layers which do not have an elevation. Later on, `QgsAppLayerHandling::postProcessAddedLayer` is called on this raster and it sets the elevation to `True` if it looks like a DEM (See `QgsRasterLayerElevationProperties::layerLooksLikeDem()`). However, the layer tree of the elevation widget has already been populated and it is not updated. This issue is fixed by emitting the the `dataChanged` signal every time the elevation properties of a layer changes. Indeed, this forces a full refresh of the model and displays the raster in that case. --- src/core/layertree/qgslayertreemodel.cpp | 26 ++++++++++++++++++++++++ src/core/layertree/qgslayertreemodel.h | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/src/core/layertree/qgslayertreemodel.cpp b/src/core/layertree/qgslayertreemodel.cpp index ce38e3ae6a76..139e31365c00 100644 --- a/src/core/layertree/qgslayertreemodel.cpp +++ b/src/core/layertree/qgslayertreemodel.cpp @@ -22,6 +22,7 @@ #include "qgsapplication.h" #include "qgslayertree.h" #include "qgslayertreemodellegendnode.h" +#include "qgsmaplayerelevationproperties.h" #include "qgsproject.h" #include "qgsmaphittest.h" #include "qgsmaplayer.h" @@ -992,6 +993,11 @@ void QgsLayerTreeModel::connectToLayer( QgsLayerTreeLayer *nodeLayer ) connect( layer, &QgsMapLayer::legendChanged, this, &QgsLayerTreeModel::layerLegendChanged, Qt::UniqueConnection ); connect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeModel::layerFlagsChanged, Qt::UniqueConnection ); + if ( QgsMapLayerElevationProperties *elevationProperties = layer->elevationProperties() ) + { + connect( elevationProperties, &QgsMapLayerElevationProperties::profileGenerationPropertyChanged, this, &QgsLayerTreeModel::layerProfileGenerationPropertyChanged, Qt::UniqueConnection ); + } + // using unique connection because there may be temporarily more nodes for a layer than just one // which would create multiple connections, however disconnect() would disconnect all multiple connections // even if we wanted to disconnect just one connection in each call. @@ -1781,5 +1787,25 @@ void QgsLayerTreeModel::invalidateLegendMapBasedData() mInvalidatedNodes.clear(); } +void QgsLayerTreeModel::layerProfileGenerationPropertyChanged() +{ + if ( !mRootNode ) + return; + + QgsMapLayerElevationProperties *elevationProperties = qobject_cast( sender() ); + if ( !elevationProperties ) + return; + + if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( elevationProperties->parent() ) ) + { + QgsLayerTreeLayer *nodeLayer = mRootNode->findLayer( layer->id() ); + if ( !nodeLayer ) + return; + + QModelIndex index = node2index( nodeLayer ); + emit dataChanged( index, index ); + } +} + // Legend nodes routines - end /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/layertree/qgslayertreemodel.h b/src/core/layertree/qgslayertreemodel.h index ec7a33ff0406..8bc0f8c04ce2 100644 --- a/src/core/layertree/qgslayertreemodel.h +++ b/src/core/layertree/qgslayertreemodel.h @@ -394,6 +394,14 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel void invalidateLegendMapBasedData(); + private slots: + + /** + * Triggered when layer elevation properties have changed. + * \since QGIS 3.42 + */ + void layerProfileGenerationPropertyChanged(); + protected: void removeLegendFromLayer( QgsLayerTreeLayer *nodeLayer ); void addLegendToLayer( QgsLayerTreeLayer *nodeL ); From cfc2418675afab5cca50663758cef0785f20139d Mon Sep 17 00:00:00 2001 From: Jean Felder Date: Wed, 11 Dec 2024 11:02:35 +0100 Subject: [PATCH 2/2] qgslayertreemodel: Fix layerFlagsChanged docstring --- .../core/auto_generated/layertree/qgslayertreemodel.sip.in | 2 +- python/core/auto_generated/layertree/qgslayertreemodel.sip.in | 2 +- src/core/layertree/qgslayertreemodel.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/PyQt6/core/auto_generated/layertree/qgslayertreemodel.sip.in b/python/PyQt6/core/auto_generated/layertree/qgslayertreemodel.sip.in index c6646508147d..c908db893138 100644 --- a/python/PyQt6/core/auto_generated/layertree/qgslayertreemodel.sip.in +++ b/python/PyQt6/core/auto_generated/layertree/qgslayertreemodel.sip.in @@ -410,7 +410,7 @@ Updates model when node's name has changed void layerFlagsChanged(); %Docstring -Emitted when layer flags have changed. +Triggered when layer flags have changed. .. versionadded:: 3.18 %End diff --git a/python/core/auto_generated/layertree/qgslayertreemodel.sip.in b/python/core/auto_generated/layertree/qgslayertreemodel.sip.in index d519b813a42f..8d4a3c3f0ea8 100644 --- a/python/core/auto_generated/layertree/qgslayertreemodel.sip.in +++ b/python/core/auto_generated/layertree/qgslayertreemodel.sip.in @@ -410,7 +410,7 @@ Updates model when node's name has changed void layerFlagsChanged(); %Docstring -Emitted when layer flags have changed. +Triggered when layer flags have changed. .. versionadded:: 3.18 %End diff --git a/src/core/layertree/qgslayertreemodel.h b/src/core/layertree/qgslayertreemodel.h index 8bc0f8c04ce2..54f0fbc242b2 100644 --- a/src/core/layertree/qgslayertreemodel.h +++ b/src/core/layertree/qgslayertreemodel.h @@ -383,7 +383,7 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel void layerLegendChanged(); /** - * Emitted when layer flags have changed. + * Triggered when layer flags have changed. * \since QGIS 3.18 */ void layerFlagsChanged();