Skip to content

Commit d7fa028

Browse files
authoredMay 27, 2019
do not insert layers in embedded groups (#10004)
fixes #29678
1 parent 09126de commit d7fa028

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed
 

‎python/core/auto_generated/layertree/qgslayertreeutils.sip.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ of a map layer within a layer tree and only connecting/disconnecting when
126126
there is only one occurrence of that layer.
127127

128128
.. versionadded:: 3.4
129+
%End
130+
131+
static QgsLayerTreeGroup *firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property );
132+
%Docstring
133+
Returns the first parent which doesn't have the given custom property
134+
or the group itself if it doesn't hold the property
135+
136+
:param group: the layer tree group
137+
:param property: the property
138+
139+
.. versionadded:: 3.8
129140
%End
130141
};
131142

‎src/app/qgisapp.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4066,31 +4066,39 @@ void QgisApp::setupLayerTreeViewFromSettings()
40664066
void QgisApp::updateNewLayerInsertionPoint()
40674067
{
40684068
// defaults
4069-
QgsLayerTreeGroup *parentGroup = mLayerTreeView->layerTreeModel()->rootGroup();
4070-
int index = 0;
4069+
QgsLayerTreeGroup *insertGroup = mLayerTreeView->layerTreeModel()->rootGroup();
40714070
QModelIndex current = mLayerTreeView->currentIndex();
4071+
int index = 0;
40724072

40734073
if ( current.isValid() )
40744074
{
4075+
index = current.row();
4076+
40754077
if ( QgsLayerTreeNode *currentNode = mLayerTreeView->currentNode() )
40764078
{
40774079
// if the insertion point is actually a group, insert new layers into the group
40784080
if ( QgsLayerTree::isGroup( currentNode ) )
40794081
{
4080-
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( QgsLayerTree::toGroup( currentNode ), 0 );
4082+
// if the group is embedded go to the first non-embedded group, at worst the top level item
4083+
QgsLayerTreeGroup *insertGroup = QgsLayerTreeUtils::firstGroupWithoutCustomProperty( QgsLayerTree::toGroup( currentNode ), QStringLiteral( "embedded" ) );
4084+
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( insertGroup, 0 );
40814085
return;
40824086
}
40834087

40844088
// otherwise just set the insertion point in front of the current node
40854089
QgsLayerTreeNode *parentNode = currentNode->parent();
40864090
if ( QgsLayerTree::isGroup( parentNode ) )
4087-
parentGroup = QgsLayerTree::toGroup( parentNode );
4091+
{
4092+
// if the group is embedded go to the first non-embedded group, at worst the top level item
4093+
QgsLayerTreeGroup *parentGroup = QgsLayerTree::toGroup( parentNode );
4094+
insertGroup = QgsLayerTreeUtils::firstGroupWithoutCustomProperty( parentGroup, QStringLiteral( "embedded" ) );
4095+
if ( parentGroup != insertGroup )
4096+
index = 0;
4097+
}
40884098
}
4089-
4090-
index = current.row();
40914099
}
40924100

4093-
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( parentGroup, index );
4101+
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( insertGroup, index );
40944102
}
40954103

40964104
void QgisApp::autoSelectAddedLayer( QList<QgsMapLayer *> layers )

‎src/core/layertree/qgslayertreeutils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,19 @@ int QgsLayerTreeUtils::countMapLayerInTree( QgsLayerTreeNode *tree, QgsMapLayer
512512
cnt += countMapLayerInTree( child, layer );
513513
return cnt;
514514
}
515+
516+
QgsLayerTreeGroup *QgsLayerTreeUtils::firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property )
517+
{
518+
// if the group is embedded go to the first non-embedded group, at worst the top level item
519+
while ( group->customProperty( property ).toInt() )
520+
{
521+
if ( !group->parent() )
522+
break;
523+
524+
if ( QgsLayerTree::isGroup( group->parent() ) )
525+
group = QgsLayerTree::toGroup( group->parent() );
526+
else
527+
Q_ASSERT( false );
528+
}
529+
return group;
530+
}

‎src/core/layertree/qgslayertreeutils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ class CORE_EXPORT QgsLayerTreeUtils
115115
* \since QGIS 3.4
116116
*/
117117
static int countMapLayerInTree( QgsLayerTreeNode *tree, QgsMapLayer *layer );
118+
119+
/**
120+
* Returns the first parent which doesn't have the given custom property
121+
* or the group itself if it doesn't hold the property
122+
* \param group the layer tree group
123+
* \param property the property
124+
* \since QGIS 3.8
125+
*/
126+
static QgsLayerTreeGroup *firstGroupWithoutCustomProperty( QgsLayerTreeGroup *group, const QString &property );
118127
};
119128

120129
#endif // QGSLAYERTREEUTILS_H

0 commit comments

Comments
 (0)
Please sign in to comment.