Skip to content

Commit

Permalink
qgslayertreemodel: Ensure to refresh when elevation properties change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptitjano committed Dec 10, 2024
1 parent 00ed70b commit 4521e68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ Updates model when node's name has changed
void nodeLayerWillBeUnloaded();
void layerLegendChanged();

void layerProfileGenerationPropertyChanged();
%Docstring
Emitted when layer elevation properties have changed.

.. versionadded:: 3.42
%End

void layerFlagsChanged();
%Docstring
Emitted when layer flags have changed.
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/layertree/qgslayertreemodel.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ Updates model when node's name has changed
void nodeLayerWillBeUnloaded();
void layerLegendChanged();

void layerProfileGenerationPropertyChanged();
%Docstring
Emitted when layer elevation properties have changed.

.. versionadded:: 3.42
%End

void layerFlagsChanged();
%Docstring
Emitted when layer flags have changed.
Expand Down
22 changes: 22 additions & 0 deletions src/core/layertree/qgslayertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -991,6 +992,7 @@ void QgsLayerTreeModel::connectToLayer( QgsLayerTreeLayer *nodeLayer )
QgsMapLayer *layer = nodeLayer->layer();
connect( layer, &QgsMapLayer::legendChanged, this, &QgsLayerTreeModel::layerLegendChanged, Qt::UniqueConnection );
connect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeModel::layerFlagsChanged, Qt::UniqueConnection );
connect( layer->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
Expand Down Expand Up @@ -1781,5 +1783,25 @@ void QgsLayerTreeModel::invalidateLegendMapBasedData()
mInvalidatedNodes.clear();
}

void QgsLayerTreeModel::layerProfileGenerationPropertyChanged()
{
if ( !mRootNode )
return;

QgsMapLayerElevationProperties *elevationProperties = qobject_cast<QgsMapLayerElevationProperties *>( 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
///////////////////////////////////////////////////////////////////////////////
6 changes: 6 additions & 0 deletions src/core/layertree/qgslayertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
void nodeLayerWillBeUnloaded();
void layerLegendChanged();

/**
* Emitted when layer elevation properties have changed.
* \since QGIS 3.42
*/
void layerProfileGenerationPropertyChanged();

/**
* Emitted when layer flags have changed.
* \since QGIS 3.18
Expand Down

0 comments on commit 4521e68

Please sign in to comment.