QGIS API Documentation 3.43.0-Master (6c62b930b02)
qgsprocessingguiutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingguiutils.cpp
3 ------------------------
4 Date : June 2025
5 Copyright : (C) 2025 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgslayertreelayer.h"
18#include "qgslayertree.h"
19#include "qgslayertreeview.h"
21#include <optional>
22
24{
25 const QgsMapLayer *layer = layerTreeLayer->layer();
26 if ( layer && layer->type() == Qgis::LayerType::Vector )
27 {
28 // post-process vector layer
29 QgsSettings settings;
30 if ( settings.value( QStringLiteral( "Processing/Configuration/VECTOR_FEATURE_COUNT" ), false ).toBool() )
31 {
32 layerTreeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), true );
33 }
34 }
35}
36
38{
39 QgsProject *destinationProject = layerDetails.project ? layerDetails.project : context.project();
40 if ( !destinationProject )
41 return nullptr;
42
43 QgsLayerTreeGroup *resultsGroup = nullptr;
44
45 // if a specific results group is specified in Processing settings,
46 // respect it (and create if necessary)
47 QgsSettings settings;
48 const QString resultsGroupName = settings.value( QStringLiteral( "Processing/Configuration/RESULTS_GROUP_NAME" ), QString() ).toString();
49
50 if ( !resultsGroupName.isEmpty() )
51 {
52 resultsGroup = destinationProject->layerTreeRoot()->findGroup( resultsGroupName );
53 if ( !resultsGroup )
54 {
55 resultsGroup = destinationProject->layerTreeRoot()->insertGroup(
56 0, resultsGroupName
57 );
58 resultsGroup->setExpanded( true );
59 }
60 }
61
62 // if this particular output layer has a specific output group assigned,
63 // find or create it now
64 QgsLayerTreeGroup *group = nullptr;
65 if ( !layerDetails.groupName.isEmpty() )
66 {
67 if ( !resultsGroup )
68 {
69 resultsGroup = destinationProject->layerTreeRoot();
70 }
71
72 group = resultsGroup->findGroup( layerDetails.groupName );
73 if ( !group )
74 {
75 group = resultsGroup->insertGroup( 0, layerDetails.groupName );
76 group->setExpanded( true );
77 }
78 }
79 else
80 {
81 group = resultsGroup;
82 }
83
84 return group;
85}
86
87void QgsProcessingGuiUtils::addResultLayers( const QVector<ResultLayerDetails> &layers, const QgsProcessingContext &context, QgsLayerTreeView *view )
88{
89 // sort added layer tree layers
90 QVector<ResultLayerDetails> sortedLayers = layers;
91 std::sort(
92 sortedLayers.begin(), sortedLayers.end(), []( const ResultLayerDetails &a, const ResultLayerDetails &b ) {
93 return a.sortKey < b.sortKey;
94 }
95 );
96
97 bool haveSetActiveLayer = false;
98 QgsLayerTreeNode *currentSelectedNode = nullptr;
99 if ( view )
100 {
101 currentSelectedNode = view->currentNode();
102 }
103 QgsLayerTreeGroup *defaultTargetGroup = nullptr;
104 int defaultTargetGroupIndex = 0;
105 if ( auto currentSelectedLayer = qobject_cast< QgsLayerTreeLayer * >( currentSelectedNode ) )
106 {
107 defaultTargetGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedLayer->parent() );
108 if ( defaultTargetGroup )
109 defaultTargetGroupIndex = defaultTargetGroup->children().indexOf( currentSelectedNode );
110 }
111 if ( auto currentSelectedGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedNode ) )
112 {
113 defaultTargetGroup = currentSelectedGroup;
114 }
115
116 for ( const ResultLayerDetails &layerDetails : std::as_const( sortedLayers ) )
117 {
118 QgsProject *project = layerDetails.destinationProject;
119 if ( !project )
120 project = context.project();
121
122 // store the current insertion point to restore it later
123 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > previousInsertionPoint;
124 if ( project )
125 {
126 previousInsertionPoint.emplace( project->layerTreeRegistryBridge()->layerInsertionPoint() );
127 }
128
129 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > insertionPoint;
130 if ( layerDetails.targetLayerTreeGroup )
131 {
132 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint( layerDetails.targetLayerTreeGroup, 0 ) );
133 }
134 else
135 {
136 // no destination group for this layer, so should be placed
137 // above the current layer if one was selected, or at top of group if a group was selected
138 if ( defaultTargetGroup )
139 {
140 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint(
141 defaultTargetGroup, defaultTargetGroupIndex
142 ) );
143 }
144 else if ( project )
145 {
146 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint(
147 project->layerTreeRoot(), 0
148 ) );
149 }
150 }
151
152 if ( project && insertionPoint.has_value() )
153 {
154 project->layerTreeRegistryBridge()->setLayerInsertionPoint( *insertionPoint );
155 }
156
157 if ( project )
158 {
159 project->addMapLayer( layerDetails.layer );
160 QgsLayerTreeLayer *layerTreeLayer = project->layerTreeRoot()->findLayer( layerDetails.layer );
161 configureResultLayerTreeLayer( layerTreeLayer );
162 }
163
164 if ( !haveSetActiveLayer && view )
165 {
166 view->setCurrentLayer( layerDetails.layer );
167 haveSetActiveLayer = true;
168 }
169
170 // reset to the previous insertion point
171 if ( project && previousInsertionPoint.has_value() )
172 {
174 *previousInsertionPoint
175 );
176 }
177 }
178}
@ Vector
Vector layer.
Layer tree group node serves as a container for layers and further groups.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
QgsLayerTreeGroup * insertGroup(int index, const QString &name)
Insert a new group node with given name at specified position.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Base class for nodes in a layer tree.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
Q_DECL_DEPRECATED void setLayerInsertionPoint(QgsLayerTreeGroup *parentGroup, int index)
Set where the new layers should be inserted - can be used to follow current selection.
InsertionPoint layerInsertionPoint() const
Returns the insertion point used to add layers to the tree.
Extends QTreeView and provides additional functionality when working with a layer tree.
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
QgsLayerTreeNode * currentNode() const
Gets current node. May be nullptr.
Base class for all map layer types.
Definition qgsmaplayer.h:77
Qgis::LayerType type
Definition qgsmaplayer.h:87
Details for layers to load into projects.
QString groupName
Optional name for a layer tree group under which to place the layer when loading it into a project.
QgsProject * project
Destination project.
Contains information about the context in which a processing algorithm is executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Contains details of a layer result from running an algorithm.
static void addResultLayers(const QVector< QgsProcessingGuiUtils::ResultLayerDetails > &layers, const QgsProcessingContext &context, QgsLayerTreeView *view=nullptr)
Responsible for adding layers created by an algorithm to a project and the project's layer tree in th...
static void configureResultLayerTreeLayer(QgsLayerTreeLayer *layerTreeLayer)
Applies post-processing steps to the QgsLayerTreeLayer created for an algorithm's output.
static QgsLayerTreeGroup * layerTreeResultsGroup(const QgsProcessingContext::LayerDetails &layerDetails, const QgsProcessingContext &context)
Returns the destination layer tree group to store results in, or nullptr if there is no specific dest...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
QgsLayerTreeRegistryBridge * layerTreeRegistryBridge() const
Returns pointer to the helper class that synchronizes map layer registry with layer tree.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QgsMapLayer * addMapLayer(QgsMapLayer *mapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
Stores settings for use within QGIS.
Definition qgssettings.h:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
A structure to define the insertion point to the layer tree.