QGIS API Documentation 3.99.0-Master (c22de0620c0)
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
20#include "qgsaction.h"
21#include "qgsactionmanager.h"
22#include "qgsapplication.h"
23#include "qgsgrouplayer.h"
24#include "qgslayertree.h"
25#include "qgsmaplayerutils.h"
26#include "qgsproject.h"
27#include "qgsprojectstorage.h"
32
33QList<QgsMapLayer *> QgsProjectUtils::layersMatchingPath( const QgsProject *project, const QString &path )
34{
35 QList<QgsMapLayer *> layersList;
36 if ( !project )
37 return layersList;
38
39 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
40 for ( QgsMapLayer *layer : mapLayers )
41 {
43 {
44 layersList << layer;
45 }
46 }
47 return layersList;
48}
49
50QList<QgsMapLayer *> QgsProjectUtils::layersMatchingUri( const QgsProject *project, const QString &provider, const QString &uri, Qgis::SourceHierarchyLevel level )
51{
52 QList<QgsMapLayer *> layersList;
53 if ( !project )
54 return layersList;
55
56 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
57 for ( QgsMapLayer *layer : mapLayers )
58 {
59 if ( layer->providerType() != provider )
60 continue;
61
62 try
63 {
64 if ( QgsMapLayerUtils::layerRefersToUri( layer, uri, level ) )
65 {
66 layersList << layer;
67 }
68 }
69 catch ( QgsNotSupportedException &e )
70 {
71 // expected
72 ( void )e;
73 }
74 }
75 return layersList;
76}
77
78bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath )
79{
80 if ( !project )
81 return false;
82
83 bool res = false;
84 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
85 for ( QgsMapLayer *layer : mapLayers )
86 {
87 if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, oldPath ) )
88 {
89 res = QgsMapLayerUtils::updateLayerSourcePath( layer, newPath ) || res;
90 }
91 }
92 return res;
93}
94
96{
97 // two situations we need to catch here -- one is a group layer which isn't present in the layer
98 // tree, the other is a group layer associated with the project's layer tree which contains
99 // UNCHECKED child layers
100 const QVector< QgsGroupLayer * > groupLayers = project->layers< QgsGroupLayer * >();
101 for ( QgsGroupLayer *groupLayer : groupLayers )
102 {
103 if ( groupLayer->childLayers().contains( layer ) )
104 return true;
105 }
106
107 std::function< bool( QgsLayerTreeGroup *group ) > traverseTree;
108 traverseTree = [ &traverseTree, layer ]( QgsLayerTreeGroup * group ) -> bool
109 {
110 // is the group a layer group containing our target layer?
111 if ( group->groupLayer() && group->findLayer( layer ) )
112 {
113 return true;
114 }
115
116 const QList< QgsLayerTreeNode * > children = group->children();
117 for ( QgsLayerTreeNode *node : children )
118 {
119 if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( node ) )
120 {
121 if ( traverseTree( childGroup ) )
122 return true;
123 }
124 }
125 return false;
126 };
127 return traverseTree( project->layerTreeRoot() );
128}
129
131{
133 switch ( embeddedScriptMode )
134 {
136 {
137 // A user having changed the behavior to always allow is considered as determined
139 }
140
142 {
143 // A user having changed the behavior to always deny is considered as determined
145 }
146
151 {
152 QString absoluteFilePath;
153 QString absolutePath;
155 if ( storage )
156 {
157 if ( !storage->filePath( project->fileName() ).isEmpty() )
158 {
159 QFileInfo fileInfo( storage->filePath( project->fileName() ) );
160 absoluteFilePath = fileInfo.absoluteFilePath();
161 absolutePath = fileInfo.absolutePath();
162 }
163 else
164 {
165 // Match non-file based URIs too
166 absolutePath = project->fileName();
167 }
168 }
169 else
170 {
171 QFileInfo fileInfo( project->fileName() );
172 absoluteFilePath = fileInfo.absoluteFilePath();
173 absolutePath = fileInfo.absolutePath();
174 }
175
177 for ( const QString &path : untrustedProjectsFolders )
178 {
179 if ( absoluteFilePath == path || absolutePath == path )
180 {
182 }
183 }
184
186 for ( const QString &path : trustedProjectsFolders )
187 {
188 if ( absoluteFilePath == path || absolutePath == path )
189 {
191 }
192 }
193
195 }
196 }
197
199}
ProjectTrustStatus
Project trust status.
Definition qgis.h:473
@ Untrusted
The project has been determined by the user as trusted.
Definition qgis.h:476
@ Trusted
The project trust has not yet been determined by the user.
Definition qgis.h:475
SourceHierarchyLevel
Defines the structural levels within a data source hierarchy.
Definition qgis.h:1451
EmbeddedScriptMode
Authorisation to run script embedded in projects.
Definition qgis.h:445
@ Always
Embedded scripts are always run.
Definition qgis.h:449
@ NotForThisSession
Embedded scripts will not be run for this session (only used prior to QGIS 4.0).
Definition qgis.h:450
@ Never
Embedded scripts never run.
Definition qgis.h:446
@ Ask
User is prompted before running scripts.
Definition qgis.h:447
@ NeverAsk
The user is never prompted, embedded scripts are only run on trusted projects and folders.
Definition qgis.h:451
@ SessionOnly
Only during this session (only used prior to QGIS 4.0).
Definition qgis.h:448
static QgsProjectStorageRegistry * projectStorageRegistry()
Returns registry of available project storage implementations.
static QStringList temporarilyTrustedProjectsFolders()
Returns the list of projects and folders that have been temporarily determined as trusted by the user...
static QStringList temporarilyUntrustedProjectsFolders()
Returns the list of projects and folders that have been temporarily determined as untrusted by the us...
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...
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 layerRefersToUri(const QgsMapLayer *layer, const QString &uri, Qgis::SourceHierarchyLevel level=Qgis::SourceHierarchyLevel::Object)
Returns true if a layer and uri point to the same resource at the specified hierarchy level.
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:83
Custom exception class which is raised when an operation is not supported.
QgsProjectStorage * projectStorageFromUri(const QString &uri)
Returns storage implementation if the URI matches one. Returns nullptr otherwise (it is a normal file...
Abstract interface for project storage - to be implemented by various backends and registered in QgsP...
virtual QString filePath(const QString &uri)
Extracts and returns the file path from a storage backend uri, filesystem-based storage backends shou...
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 * > layersMatchingUri(const QgsProject *project, const QString &provider, const QString &uri, Qgis::SourceHierarchyLevel level=Qgis::SourceHierarchyLevel::Object)
Returns a list of all layers in the specified project point to the same uri resource at the specified...
static Qgis::ProjectTrustStatus checkUserTrust(QgsProject *project)
Returns the current trust status of the specified project.
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:113
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.
QString fileName
Definition qgsproject.h:117
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
static const QgsSettingsEntryStringList * settingsCodeExecutionTrustedProjectsFolders
Settings entry for projects and folders that are allowed execution of embedded scripts across session...
static const QgsSettingsEntryStringList * settingsCodeExecutionUntrustedProjectsFolders
Settings entry for projects and folders that are denied execution of embedded scripts across sessions...
static const QgsSettingsEntryEnumFlag< Qgis::EmbeddedScriptMode > * settingsCodeExecutionBehaviorUndeterminedProjects
Settings entry for behavior handling embedded scripts within projects.