QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
22 QList<QgsMapLayer *> QgsProjectUtils::layersMatchingPath( const QgsProject *project, const QString &path )
23 {
24  QList<QgsMapLayer *> layersList;
25  if ( !project )
26  return layersList;
27 
28  const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
29  for ( QgsMapLayer *layer : mapLayers )
30  {
31  if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, path ) )
32  {
33  layersList << layer;
34  }
35  }
36  return layersList;
37 }
38 
39 bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath )
40 {
41  if ( !project )
42  return false;
43 
44  bool res = false;
45  const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
46  for ( QgsMapLayer *layer : mapLayers )
47  {
48  if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, oldPath ) )
49  {
50  res = QgsMapLayerUtils::updateLayerSourcePath( layer, newPath ) || res;
51  }
52  }
53  return res;
54 }
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 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
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.