QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 QList<QgsMapLayer *> QgsProjectUtils::layersMatchingPath( const QgsProject *project, const QString &path )
24 {
25  QList<QgsMapLayer *> layersList;
26  if ( !project )
27  return layersList;
28 
29  const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
30  for ( QgsMapLayer *layer : mapLayers )
31  {
32  if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, path ) )
33  {
34  layersList << layer;
35  }
36  }
37  return layersList;
38 }
39 
40 bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath )
41 {
42  if ( !project )
43  return false;
44 
45  bool res = false;
46  const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
47  for ( QgsMapLayer *layer : mapLayers )
48  {
49  if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, oldPath ) )
50  {
51  res = QgsMapLayerUtils::updateLayerSourcePath( layer, newPath ) || res;
52  }
53  }
54  return res;
55 }
56 
58 {
59  const QVector< QgsGroupLayer * > groupLayers = project->layers< QgsGroupLayer * >();
60  for ( QgsGroupLayer *groupLayer : groupLayers )
61  {
62  if ( groupLayer->childLayers().contains( layer ) )
63  return true;
64  }
65  return false;
66 }
QgsProject::layers
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
Definition: qgsproject.h:1175
QgsProjectUtils::layerIsContainedInGroupLayer
static bool layerIsContainedInGroupLayer(QgsProject *project, QgsMapLayer *layer)
Returns true if the specified layer is a child layer from any QgsGroupLayer in the given project.
Definition: qgsprojectutils.cpp:57
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3955
QgsGroupLayer
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Definition: qgsgrouplayer.h:41
QgsProjectUtils::updateLayerPath
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...
Definition: qgsprojectutils.cpp:40
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsMapLayerUtils::layerSourceMatchesPath
static bool layerSourceMatchesPath(const QgsMapLayer *layer, const QString &path)
Returns true if the source of the specified layer matches the given path.
Definition: qgsmaplayerutils.cpp:117
qgsprojectutils.h
qgsgrouplayer.h
qgsmaplayerutils.h
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapLayerUtils::updateLayerSourcePath
static bool updateLayerSourcePath(QgsMapLayer *layer, const QString &newPath)
Updates a layer's data source, replacing its data source with a path referring to newPath.
Definition: qgsmaplayerutils.cpp:126
QgsProjectUtils::layersMatchingPath
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.
Definition: qgsprojectutils.cpp:23
qgsproject.h