Skip to content

Commit

Permalink
do not insert layers in embedded groups (#10004)
Browse files Browse the repository at this point in the history
fixes #29678
  • Loading branch information
3nids committed May 27, 2019
1 parent 09126de commit d7fa028
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
11 changes: 11 additions & 0 deletions python/core/auto_generated/layertree/qgslayertreeutils.sip.in
Expand Up @@ -126,6 +126,17 @@ of a map layer within a layer tree and only connecting/disconnecting when
there is only one occurrence of that layer.

.. versionadded:: 3.4
%End

static QgsLayerTreeGroup *firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property );
%Docstring
Returns the first parent which doesn't have the given custom property
or the group itself if it doesn't hold the property

:param group: the layer tree group
:param property: the property

.. versionadded:: 3.8
%End
};

Expand Down
22 changes: 15 additions & 7 deletions src/app/qgisapp.cpp
Expand Up @@ -4066,31 +4066,39 @@ void QgisApp::setupLayerTreeViewFromSettings()
void QgisApp::updateNewLayerInsertionPoint()
{
// defaults
QgsLayerTreeGroup *parentGroup = mLayerTreeView->layerTreeModel()->rootGroup();
int index = 0;
QgsLayerTreeGroup *insertGroup = mLayerTreeView->layerTreeModel()->rootGroup();
QModelIndex current = mLayerTreeView->currentIndex();
int index = 0;

if ( current.isValid() )
{
index = current.row();

if ( QgsLayerTreeNode *currentNode = mLayerTreeView->currentNode() )
{
// if the insertion point is actually a group, insert new layers into the group
if ( QgsLayerTree::isGroup( currentNode ) )
{
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( QgsLayerTree::toGroup( currentNode ), 0 );
// if the group is embedded go to the first non-embedded group, at worst the top level item
QgsLayerTreeGroup *insertGroup = QgsLayerTreeUtils::firstGroupWithoutCustomProperty( QgsLayerTree::toGroup( currentNode ), QStringLiteral( "embedded" ) );
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( insertGroup, 0 );
return;
}

// otherwise just set the insertion point in front of the current node
QgsLayerTreeNode *parentNode = currentNode->parent();
if ( QgsLayerTree::isGroup( parentNode ) )
parentGroup = QgsLayerTree::toGroup( parentNode );
{
// if the group is embedded go to the first non-embedded group, at worst the top level item
QgsLayerTreeGroup *parentGroup = QgsLayerTree::toGroup( parentNode );
insertGroup = QgsLayerTreeUtils::firstGroupWithoutCustomProperty( parentGroup, QStringLiteral( "embedded" ) );
if ( parentGroup != insertGroup )
index = 0;
}
}

index = current.row();
}

QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( parentGroup, index );
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( insertGroup, index );
}

void QgisApp::autoSelectAddedLayer( QList<QgsMapLayer *> layers )
Expand Down
16 changes: 16 additions & 0 deletions src/core/layertree/qgslayertreeutils.cpp
Expand Up @@ -512,3 +512,19 @@ int QgsLayerTreeUtils::countMapLayerInTree( QgsLayerTreeNode *tree, QgsMapLayer
cnt += countMapLayerInTree( child, layer );
return cnt;
}

QgsLayerTreeGroup *QgsLayerTreeUtils::firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property )
{
// if the group is embedded go to the first non-embedded group, at worst the top level item
while ( group->customProperty( property ).toInt() )
{
if ( !group->parent() )
break;

if ( QgsLayerTree::isGroup( group->parent() ) )
group = QgsLayerTree::toGroup( group->parent() );
else
Q_ASSERT( false );
}
return group;
}
9 changes: 9 additions & 0 deletions src/core/layertree/qgslayertreeutils.h
Expand Up @@ -115,6 +115,15 @@ class CORE_EXPORT QgsLayerTreeUtils
* \since QGIS 3.4
*/
static int countMapLayerInTree( QgsLayerTreeNode *tree, QgsMapLayer *layer );

/**
* Returns the first parent which doesn't have the given custom property
* or the group itself if it doesn't hold the property
* \param group the layer tree group
* \param property the property
* \since QGIS 3.8
*/
static QgsLayerTreeGroup *firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property );
};

#endif // QGSLAYERTREEUTILS_H

0 comments on commit d7fa028

Please sign in to comment.