QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsprojectutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectutils.h
3 -------------------
4 begin : July 2021
5 copyright : (C) 2021 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsprojectutils.h"
19#include "qgsmaplayerutils.h"
20#include "qgsproject.h"
21#include "qgsgrouplayer.h"
22#include "qgslayertree.h"
23
24QList<QgsMapLayer *> QgsProjectUtils::layersMatchingPath( const QgsProject *project, const QString &path )
25{
26 QList<QgsMapLayer *> layersList;
27 if ( !project )
28 return layersList;
29
30 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
31 for ( QgsMapLayer *layer : mapLayers )
32 {
34 {
35 layersList << layer;
36 }
37 }
38 return layersList;
39}
40
41bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath )
42{
43 if ( !project )
44 return false;
45
46 bool res = false;
47 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
48 for ( QgsMapLayer *layer : mapLayers )
49 {
50 if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, oldPath ) )
51 {
52 res = QgsMapLayerUtils::updateLayerSourcePath( layer, newPath ) || res;
53 }
54 }
55 return res;
56}
57
59{
60 // two situations we need to catch here -- one is a group layer which isn't present in the layer
61 // tree, the other is a group layer associated with the project's layer tree which contains
62 // UNCHECKED child layers
63 const QVector< QgsGroupLayer * > groupLayers = project->layers< QgsGroupLayer * >();
64 for ( QgsGroupLayer *groupLayer : groupLayers )
65 {
66 if ( groupLayer->childLayers().contains( layer ) )
67 return true;
68 }
69
70 std::function< bool( QgsLayerTreeGroup *group ) > traverseTree;
71 traverseTree = [ &traverseTree, layer ]( QgsLayerTreeGroup * group ) -> bool
72 {
73 // is the group a layer group containing our target layer?
74 if ( group->groupLayer() && group->findLayer( layer ) )
75 {
76 return true;
77 }
78
79 const QList< QgsLayerTreeNode * > children = group->children();
80 for ( QgsLayerTreeNode *node : children )
81 {
82 if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( node ) )
83 {
84 if ( traverseTree( childGroup ) )
85 return true;
86 }
87 }
88 return false;
89 };
90 return traverseTree( project->layerTreeRoot() );
91}
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Layer tree group node serves as a container for layers and further groups.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
QgsGroupLayer * groupLayer()
Returns a reference to the associated group layer, if the layer tree group will be treated as group l...
This class is a base class for nodes in a layer tree.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
static bool updateLayerSourcePath(QgsMapLayer *layer, const QString &newPath)
Updates a layer's data source, replacing its data source with a path referring to newPath.
static bool layerSourceMatchesPath(const QgsMapLayer *layer, const QString &path)
Returns true if the source of the specified layer matches the given path.
Base class for all map layer types.
Definition qgsmaplayer.h:76
static bool updateLayerPath(QgsProject *project, const QString &oldPath, const QString &newPath)
Updates a project, replacing the data source for all layers which match the given oldPath with source...
static bool layerIsContainedInGroupLayer(QgsProject *project, QgsMapLayer *layer)
Returns true if the specified layer is a child layer from any QgsGroupLayer in the given project.
static QList< QgsMapLayer * > layersMatchingPath(const QgsProject *project, const QString &path)
Returns a list of all layers in the specified project which match the given path.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.