Skip to content

Commit

Permalink
Fix potential crashes when layer tree insertion target group is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 5, 2025
1 parent 4a1761b commit d26104f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Tell others we have just added layers to the tree (used in QGIS to auto-select f

protected:


};

/************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Tell others we have just added layers to the tree (used in QGIS to auto-select f

protected:


};

/************************************************************************
Expand Down
24 changes: 17 additions & 7 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root,
, mRegistryRemovingLayers( false )
, mEnabled( true )
, mNewLayersVisible( true )
, mInsertionPoint( root, 0 )
, mInsertionPointGroup( root )
{
connect( mProject, &QgsProject::legendLayersAdded, this, &QgsLayerTreeRegistryBridge::layersAdded );
connect( mProject, qOverload<const QStringList &>( &QgsProject::layersWillBeRemoved ), this, &QgsLayerTreeRegistryBridge::layersWillBeRemoved );
Expand All @@ -39,13 +39,14 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root,

void QgsLayerTreeRegistryBridge::setLayerInsertionPoint( QgsLayerTreeGroup *parentGroup, int index )
{
mInsertionPoint.group = parentGroup;
mInsertionPoint.position = index;
mInsertionPointGroup = parentGroup;
mInsertionPointPosition = index;
}

void QgsLayerTreeRegistryBridge::setLayerInsertionPoint( const InsertionPoint &insertionPoint )
{
mInsertionPoint = insertionPoint;
mInsertionPointGroup = insertionPoint.group;
mInsertionPointPosition = insertionPoint.position;
}

void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers )
Expand All @@ -61,7 +62,11 @@ void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers
{
case Qgis::LayerTreeInsertionMethod::OptimalInInsertionGroup:
{
nodeLayer = QgsLayerTreeUtils::insertLayerAtOptimalPlacement( mInsertionPoint.group, layer );
QgsLayerTreeGroup *targetGroup = mInsertionPointGroup;
if ( !targetGroup )
targetGroup = mRoot;

nodeLayer = QgsLayerTreeUtils::insertLayerAtOptimalPlacement( targetGroup, layer );
break;
}

Expand Down Expand Up @@ -89,8 +94,13 @@ void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers
switch ( mInsertionMethod )
{
case Qgis::LayerTreeInsertionMethod::AboveInsertionPoint:
mInsertionPoint.group->insertChildNodes( mInsertionPoint.position, nodes );
break;
if ( QgsLayerTreeGroup *group = mInsertionPointGroup )
{
group->insertChildNodes( mInsertionPointPosition, nodes );
break;
}
// if no group for the insertion point, then insert into root instead
[[fallthrough]];
case Qgis::LayerTreeInsertionMethod::TopOfTree:
mRoot->insertChildNodes( 0, nodes );
break;
Expand Down
9 changes: 6 additions & 3 deletions src/core/layertree/qgslayertreeregistrybridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

#include <QObject>
#include <QStringList>
#include <QPointer>

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgis.h"
#include "qgslayertreegroup.h"

class QgsLayerTreeGroup;
class QgsLayerTreeNode;
class QgsMapLayer;
class QgsProject;
Expand Down Expand Up @@ -57,7 +58,7 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
InsertionPoint( QgsLayerTreeGroup *group, int position )
: group( group ), position( position ) {}

QgsLayerTreeGroup *group = nullptr;
QgsLayerTreeGroup *group;
int position = 0;
};

Expand Down Expand Up @@ -120,7 +121,9 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
bool mEnabled;
bool mNewLayersVisible;

InsertionPoint mInsertionPoint;
QPointer< QgsLayerTreeGroup > mInsertionPointGroup;
int mInsertionPointPosition = 0;

Qgis::LayerTreeInsertionMethod mInsertionMethod = Qgis::LayerTreeInsertionMethod::AboveInsertionPoint;
};

Expand Down

0 comments on commit d26104f

Please sign in to comment.