QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
50bool QgsProjectUtils::updateLayerPath( QgsProject *project, const QString &oldPath, const QString &newPath )
51{
52 if ( !project )
53 return false;
54
55 bool res = false;
56 const QMap<QString, QgsMapLayer *> mapLayers( project->mapLayers() );
57 for ( QgsMapLayer *layer : mapLayers )
58 {
59 if ( QgsMapLayerUtils::layerSourceMatchesPath( layer, oldPath ) )
60 {
61 res = QgsMapLayerUtils::updateLayerSourcePath( layer, newPath ) || res;
62 }
63 }
64 return res;
65}
66
68{
69 // two situations we need to catch here -- one is a group layer which isn't present in the layer
70 // tree, the other is a group layer associated with the project's layer tree which contains
71 // UNCHECKED child layers
72 const QVector< QgsGroupLayer * > groupLayers = project->layers< QgsGroupLayer * >();
73 for ( QgsGroupLayer *groupLayer : groupLayers )
74 {
75 if ( groupLayer->childLayers().contains( layer ) )
76 return true;
77 }
78
79 std::function< bool( QgsLayerTreeGroup *group ) > traverseTree;
80 traverseTree = [ &traverseTree, layer ]( QgsLayerTreeGroup * group ) -> bool
81 {
82 // is the group a layer group containing our target layer?
83 if ( group->groupLayer() && group->findLayer( layer ) )
84 {
85 return true;
86 }
87
88 const QList< QgsLayerTreeNode * > children = group->children();
89 for ( QgsLayerTreeNode *node : children )
90 {
91 if ( QgsLayerTreeGroup *childGroup = qobject_cast< QgsLayerTreeGroup * >( node ) )
92 {
93 if ( traverseTree( childGroup ) )
94 return true;
95 }
96 }
97 return false;
98 };
99 return traverseTree( project->layerTreeRoot() );
100}
101
103{
105 switch ( embeddedScriptMode )
106 {
108 {
109 // A user having changed the behavior to always allow is considered as determined
111 }
112
114 {
115 // A user having changed the behavior to always deny is considered as determined
117 }
118
123 {
124 QString absoluteFilePath;
125 QString absolutePath;
127 if ( storage )
128 {
129 if ( !storage->filePath( project->fileName() ).isEmpty() )
130 {
131 QFileInfo fileInfo( storage->filePath( project->fileName() ) );
132 absoluteFilePath = fileInfo.absoluteFilePath();
133 absolutePath = fileInfo.absolutePath();
134 }
135 else
136 {
137 // Match non-file based URIs too
138 absolutePath = project->fileName();
139 }
140 }
141 else
142 {
143 QFileInfo fileInfo( project->fileName() );
144 absoluteFilePath = fileInfo.absoluteFilePath();
145 absolutePath = fileInfo.absolutePath();
146 }
147
149 for ( const QString &path : untrustedProjectsFolders )
150 {
151 if ( absoluteFilePath == path || absolutePath == path )
152 {
154 }
155 }
156
158 for ( const QString &path : trustedProjectsFolders )
159 {
160 if ( absoluteFilePath == path || absolutePath == path )
161 {
163 }
164 }
165
167 }
168 }
169
171}
ProjectTrustStatus
Project trust status.
Definition qgis.h:452
@ Untrusted
The project has been determined by the user as trusted.
Definition qgis.h:455
@ Trusted
The project trust has not yet been determined by the user.
Definition qgis.h:454
EmbeddedScriptMode
Authorisation to run script embedded in projects.
Definition qgis.h:424
@ Always
Embedded scripts are always run.
Definition qgis.h:428
@ NotForThisSession
Embedded scripts will not be run for this session (only used prior to QGIS 4.0).
Definition qgis.h:429
@ Never
Embedded scripts never run.
Definition qgis.h:425
@ Ask
User is prompted before running scripts.
Definition qgis.h:426
@ NeverAsk
The user is never prompted, embedded scripts are only run on trusted projects and folders.
Definition qgis.h:430
@ SessionOnly
Only during this session (only used prior to QGIS 4.0).
Definition qgis.h:427
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 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:80
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 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:109
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:113
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.