QGIS API Documentation 3.41.0-Master (88383c3d16f)
Loading...
Searching...
No Matches
qgslayertreeregistrybridge.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreeregistrybridge.cpp
3 --------------------------------------
4 Date : May 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgslayertreeregistrybridge.cpp"
18
19#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21#include "qgsproject.h"
22#include "qgslogger.h"
23
25 : QObject( parent )
26 , mRoot( root )
27 , mProject( project )
28 , mRegistryRemovingLayers( false )
29 , mEnabled( true )
30 , mNewLayersVisible( true )
31 , mInsertionPointGroup( root )
32{
34 connect( mProject, qOverload<const QStringList &>( &QgsProject::layersWillBeRemoved ), this, &QgsLayerTreeRegistryBridge::layersWillBeRemoved );
35
38}
39
45
47{
48 mInsertionPointGroup = insertionPoint.group;
49 mInsertionPointPosition = insertionPoint.position;
50}
51
57
58void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers )
59{
60 if ( !mEnabled )
61 return;
62
63 QList<QgsLayerTreeNode *> newNodes;
64 for ( QgsMapLayer *layer : layers )
65 {
66 QgsLayerTreeLayer *nodeLayer = nullptr;
67 switch ( mInsertionMethod )
68 {
70 {
72 if ( !targetGroup )
73 targetGroup = mRoot;
74
75 // returned layer is already owned by the group!
76 nodeLayer = QgsLayerTreeUtils::insertLayerAtOptimalPlacement( targetGroup, layer );
77 break;
78 }
79
82 {
83 nodeLayer = new QgsLayerTreeLayer( layer );
84 newNodes << nodeLayer;
85 break;
86 }
87 }
88
89 if ( !nodeLayer )
90 continue;
91
93
94 // check whether the layer is marked as embedded
95 const QString projectFile = mProject->layerIsEmbedded( nodeLayer->layerId() );
96 if ( !projectFile.isEmpty() )
97 {
98 nodeLayer->setCustomProperty( QStringLiteral( "embedded" ), 1 );
99 nodeLayer->setCustomProperty( QStringLiteral( "embedded_project" ), projectFile );
100 }
101 }
102
103 switch ( mInsertionMethod )
104 {
107 {
108 group->insertChildNodes( mInsertionPointPosition, newNodes );
109 break;
110 }
111 // if no group for the insertion point, then insert into root instead
112 [[fallthrough]];
114 mRoot->insertChildNodes( 0, newNodes );
115 break;
117 break;
118 }
119
120 // tell other components that layers have been added - this signal is used in QGIS to auto-select the first layer
121 emit addedLayersToLayerTree( layers );
122}
123
124void QgsLayerTreeRegistryBridge::layersWillBeRemoved( const QStringList &layerIds )
125{
126 QgsDebugMsgLevel( QStringLiteral( "%1 layers will be removed, enabled:%2" ).arg( layerIds.count() ).arg( mEnabled ), 4 );
127
128 if ( !mEnabled )
129 return;
130
131 // when we start removing child nodes, the bridge would try to remove those layers from
132 // the registry _again_ in groupRemovedChildren() - this prevents it
134
135 const auto constLayerIds = layerIds;
136 for ( const QString &layerId : constLayerIds )
137 {
138 QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layerId );
139 if ( nodeLayer )
140 qobject_cast<QgsLayerTreeGroup *>( nodeLayer->parent() )->removeChildNode( nodeLayer );
141 }
142
144}
145
146
147static void _collectLayerIdsInGroup( QgsLayerTreeGroup *group, int indexFrom, int indexTo, QStringList &lst )
148{
149 for ( int i = indexFrom; i <= indexTo; ++i )
150 {
151 QgsLayerTreeNode *child = group->children().at( i );
152 if ( QgsLayerTree::isLayer( child ) )
153 {
154 lst << QgsLayerTree::toLayer( child )->layerId();
155 }
156 else if ( QgsLayerTree::isGroup( child ) )
157 {
158 _collectLayerIdsInGroup( QgsLayerTree::toGroup( child ), 0, child->children().count() - 1, lst );
159 }
160 }
161}
162
164{
166 return; // do not try to remove those layers again
167
168 Q_ASSERT( mLayerIdsForRemoval.isEmpty() );
169
170 Q_ASSERT( QgsLayerTree::isGroup( node ) );
172
173 _collectLayerIdsInGroup( group, indexFrom, indexTo, mLayerIdsForRemoval );
174}
175
177{
179 return; // do not try to remove those layers again
180
181 // remove only those that really do not exist in the tree
182 // (ignores layers that were dragged'n'dropped: 1. drop new 2. remove old)
183 QStringList toRemove;
184 const auto constMLayerIdsForRemoval = mLayerIdsForRemoval;
185 for ( const QString &layerId : constMLayerIdsForRemoval )
186 if ( !mRoot->findLayer( layerId ) )
187 toRemove << layerId;
188 mLayerIdsForRemoval.clear();
189
190 QgsDebugMsgLevel( QStringLiteral( "%1 layers will be removed" ).arg( toRemove.count() ), 4 );
191
192 // delay the removal of layers from the registry. There may be other slots connected to map layer registry's signals
193 // that might disrupt the execution flow - e.g. a processEvents() call may force update of layer tree view with
194 // semi-broken tree model
195 QMetaObject::invokeMethod( this, "removeLayersFromRegistry", Qt::QueuedConnection, Q_ARG( QStringList, toRemove ) );
196}
197
199{
200 mProject->removeMapLayers( layerIds );
201}
@ TopOfTree
Layers are added at the top of the layer tree.
@ AboveInsertionPoint
Layers are added in the tree above the insertion point.
@ OptimalInInsertionGroup
Layers are added at optimal locations across the insertion point's group.
Layer tree group node serves as a container for layers and further groups.
void insertChildNodes(int index, const QList< QgsLayerTreeNode * > &nodes)
Insert existing nodes at specified position.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Layer tree node points to a map layer.
QString layerId() const
Returns the ID for the map layer associated with this node.
This class is a base class for nodes in a layer tree.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
void removeLayersFromRegistry(const QStringList &layerIds)
void layersWillBeRemoved(const QStringList &layerIds)
void layersAdded(const QList< QgsMapLayer * > &layers)
void addedLayersToLayerTree(const QList< QgsMapLayer * > &layers)
Tell others we have just added layers to the tree (used in QGIS to auto-select first newly added laye...
void groupWillRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
QPointer< QgsLayerTreeGroup > mInsertionPointGroup
QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, QgsProject *project, QObject *parent=nullptr)
Create the instance that synchronizes given project with a layer tree root.
Q_DECL_DEPRECATED void setLayerInsertionPoint(QgsLayerTreeGroup *parentGroup, int index)
Set where the new layers should be inserted - can be used to follow current selection.
InsertionPoint layerInsertionPoint() const
Returns the insertion point used to add layers to the tree.
Qgis::LayerTreeInsertionMethod mInsertionMethod
static QgsLayerTreeLayer * insertLayerAtOptimalPlacement(QgsLayerTreeGroup *group, QgsMapLayer *layer)
Inserts a layer within a given group at an optimal index position by insuring a given layer type will...
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the registry by layer ID.
void legendLayersAdded(const QList< QgsMapLayer * > &layers)
Emitted, when a layer was added to the registry and the legend.
QString layerIsEmbedded(const QString &id) const
Returns the source project file path if the layer with matching id is embedded from other project fil...
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
A structure to define the insertion point to the layer tree.