Skip to content

Commit

Permalink
[layer tree] Avoid needless layer tree model computations for scale-b…
Browse files Browse the repository at this point in the history
…ased layer visibility
  • Loading branch information
nirvn committed Sep 28, 2020
1 parent ef7ecbf commit 9d105d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Expand Up @@ -374,7 +374,7 @@ Updates model when node's name has changed
emit :py:func:`~QgsLayerTreeModel.dataChanged` for layer tree node items
%End

void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );
%Docstring
Updates layer data for scale dependent layers, should be called when map scale changes.
Emits :py:func:`~QgsLayerTreeModel.dataChanged` for all scale dependent layers.
Expand Down
13 changes: 8 additions & 5 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -690,14 +690,16 @@ void QgsLayerTreeModel::setLegendMapViewData( double mapUnitsPerPixel, int dpi,
if ( mLegendMapViewDpi == dpi && qgsDoubleNear( mLegendMapViewMupp, mapUnitsPerPixel ) && qgsDoubleNear( mLegendMapViewScale, scale ) )
return;

double previousScale = mLegendMapViewScale;
mLegendMapViewScale = scale;
mLegendMapViewMupp = mapUnitsPerPixel;
mLegendMapViewDpi = dpi;
mLegendMapViewScale = scale;

// now invalidate legend nodes!
legendInvalidateMapBasedData();

refreshScaleBasedLayers();
if ( scale != previousScale )
refreshScaleBasedLayers( QModelIndex(), previousScale );
}

void QgsLayerTreeModel::legendMapViewData( double *mapUnitsPerPixel, int *dpi, double *scale ) const
Expand Down Expand Up @@ -1020,7 +1022,7 @@ void QgsLayerTreeModel::recursivelyEmitDataChanged( const QModelIndex &idx )
recursivelyEmitDataChanged( index( i, 0, idx ) );
}

void QgsLayerTreeModel::refreshScaleBasedLayers( const QModelIndex &idx )
void QgsLayerTreeModel::refreshScaleBasedLayers( const QModelIndex &idx, double previousScale )
{
QgsLayerTreeNode *node = index2node( idx );
if ( !node )
Expand All @@ -1031,12 +1033,13 @@ void QgsLayerTreeModel::refreshScaleBasedLayers( const QModelIndex &idx )
const QgsMapLayer *layer = QgsLayerTree::toLayer( node )->layer();
if ( layer && layer->hasScaleBasedVisibility() )
{
emit dataChanged( idx, idx );
if ( layer->isInScaleRange( mLegendMapViewScale ) != layer->isInScaleRange( previousScale ) )
emit dataChanged( idx, idx, QVector<int>() << Qt::FontRole << Qt::ForegroundRole );
}
}
int count = node->children().count();
for ( int i = 0; i < count; ++i )
refreshScaleBasedLayers( index( i, 0, idx ) );
refreshScaleBasedLayers( index( i, 0, idx ), previousScale );
}

Qt::DropActions QgsLayerTreeModel::supportedDropActions() const
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreemodel.h
Expand Up @@ -340,7 +340,7 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
* Emits dataChanged() for all scale dependent layers.
* \since QGIS 2.16
*/
void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex() );
void refreshScaleBasedLayers( const QModelIndex &index = QModelIndex(), double previousScale = 0.0 );

static QIcon iconGroup();

Expand Down

0 comments on commit 9d105d9

Please sign in to comment.