QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 }
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
Definition: qgsgrouplayer.h:42
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:73
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:101
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
Definition: qgsproject.h:1109
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.