QGIS API Documentation  3.9.0-Master (224899f119)
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 
18 #include "qgslayertree.h"
19 
20 #include "qgsproject.h"
21 #include "qgslogger.h"
22 
24  : QObject( parent )
25  , mRoot( root )
26  , mProject( project )
27  , mRegistryRemovingLayers( false )
28  , mEnabled( true )
29  , mNewLayersVisible( true )
30  , mInsertionPointGroup( root )
31  , mInsertionPointIndex( 0 )
32 {
34  connect( mProject, static_cast < void ( QgsProject::* )( const QStringList & ) >( &QgsProject::layersWillBeRemoved ), this, &QgsLayerTreeRegistryBridge::layersWillBeRemoved );
35 
38 }
39 
41 {
42  mInsertionPointGroup = parentGroup;
43  mInsertionPointIndex = index;
44 }
45 
46 void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers )
47 {
48  if ( !mEnabled )
49  return;
50 
51  QList<QgsLayerTreeNode *> nodes;
52  const auto constLayers = layers;
53  for ( QgsMapLayer *layer : constLayers )
54  {
55  QgsLayerTreeLayer *nodeLayer = new QgsLayerTreeLayer( layer );
57 
58  nodes << nodeLayer;
59 
60  // check whether the layer is marked as embedded
61  QString projectFile = mProject->layerIsEmbedded( nodeLayer->layerId() );
62  if ( !projectFile.isEmpty() )
63  {
64  nodeLayer->setCustomProperty( QStringLiteral( "embedded" ), 1 );
65  nodeLayer->setCustomProperty( QStringLiteral( "embedded_project" ), projectFile );
66  }
67  }
68 
69  // add new layers to the right place
71 
72  // tell other components that layers have been added - this signal is used in QGIS to auto-select the first layer
73  emit addedLayersToLayerTree( layers );
74 }
75 
76 void QgsLayerTreeRegistryBridge::layersWillBeRemoved( const QStringList &layerIds )
77 {
78  QgsDebugMsgLevel( QStringLiteral( "%1 layers will be removed, enabled:%2" ).arg( layerIds.count() ).arg( mEnabled ), 4 );
79 
80  if ( !mEnabled )
81  return;
82 
83  // when we start removing child nodes, the bridge would try to remove those layers from
84  // the registry _again_ in groupRemovedChildren() - this prevents it
86 
87  const auto constLayerIds = layerIds;
88  for ( const QString &layerId : constLayerIds )
89  {
90  QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layerId );
91  if ( nodeLayer )
92  qobject_cast<QgsLayerTreeGroup *>( nodeLayer->parent() )->removeChildNode( nodeLayer );
93  }
94 
96 }
97 
98 
99 static void _collectLayerIdsInGroup( QgsLayerTreeGroup *group, int indexFrom, int indexTo, QStringList &lst )
100 {
101  for ( int i = indexFrom; i <= indexTo; ++i )
102  {
103  QgsLayerTreeNode *child = group->children().at( i );
104  if ( QgsLayerTree::isLayer( child ) )
105  {
106  lst << QgsLayerTree::toLayer( child )->layerId();
107  }
108  else if ( QgsLayerTree::isGroup( child ) )
109  {
110  _collectLayerIdsInGroup( QgsLayerTree::toGroup( child ), 0, child->children().count() - 1, lst );
111  }
112  }
113 }
114 
116 {
118  return; // do not try to remove those layers again
119 
120  Q_ASSERT( mLayerIdsForRemoval.isEmpty() );
121 
122  Q_ASSERT( QgsLayerTree::isGroup( node ) );
123  QgsLayerTreeGroup *group = QgsLayerTree::toGroup( node );
124 
125  _collectLayerIdsInGroup( group, indexFrom, indexTo, mLayerIdsForRemoval );
126 }
127 
129 {
131  return; // do not try to remove those layers again
132 
133  // remove only those that really do not exist in the tree
134  // (ignores layers that were dragged'n'dropped: 1. drop new 2. remove old)
135  QStringList toRemove;
136  const auto constMLayerIdsForRemoval = mLayerIdsForRemoval;
137  for ( const QString &layerId : constMLayerIdsForRemoval )
138  if ( !mRoot->findLayer( layerId ) )
139  toRemove << layerId;
140  mLayerIdsForRemoval.clear();
141 
142  QgsDebugMsgLevel( QStringLiteral( "%1 layers will be removed" ).arg( toRemove.count() ), 4 );
143 
144  // delay the removal of layers from the registry. There may be other slots connected to map layer registry's signals
145  // that might disrupt the execution flow - e.g. a processEvents() call may force update of layer tree view with
146  // semi-broken tree model
147  QMetaObject::invokeMethod( this, "removeLayersFromRegistry", Qt::QueuedConnection, Q_ARG( QStringList, toRemove ) );
148 }
149 
150 void QgsLayerTreeRegistryBridge::removeLayersFromRegistry( const QStringList &layerIds )
151 {
152  mProject->removeMapLayers( layerIds );
153 }
Layer tree group node serves as a container for layers and further groups.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
Base class for all map layer types.
Definition: qgsmaplayer.h:79
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:64
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
void layersWillBeRemoved(const QStringList &layerIds)
void legendLayersAdded(const QList< QgsMapLayer *> &layers)
Emitted, when a layer was added to the registry and the legend.
QString layerId() const
Returns the ID for the map layer associated with this node.
void groupWillRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
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 removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
void layersAdded(const QList< QgsMapLayer *> &layers)
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QString layerIsEmbedded(const QString &id) const
Returns project file path if layer is embedded from other project file. Returns empty string if layer...
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:90
void insertChildNodes(int index, const QList< QgsLayerTreeNode *> &nodes)
Insert existing nodes at specified position.
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
void removeLayersFromRegistry(const QStringList &layerIds)
void setLayerInsertionPoint(QgsLayerTreeGroup *parentGroup, int index)
Set where the new layers should be inserted - can be used to follow current selection.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
void removeMapLayers(const QStringList &layerIds)
Remove a set of layers from the registry by layer ID.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root, QgsProject *project, QObject *parent=nullptr)
Create the instance that synchronizes given project with a layer tree root.
Layer tree node points to a map layer.