QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18#include <optional>
19
20#include "qgslayertree.h"
21#include "qgslayertreelayer.h"
23#include "qgslayertreeview.h"
24
26{
27 const QgsMapLayer *layer = layerTreeLayer->layer();
28 if ( layer && layer->type() == Qgis::LayerType::Vector )
29 {
30 // post-process vector layer
31 QgsSettings settings;
32 if ( settings.value( QStringLiteral( "Processing/Configuration/VECTOR_FEATURE_COUNT" ), false ).toBool() )
33 {
34 layerTreeLayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), true );
35 }
36 }
37}
38
40{
41 QgsProject *destinationProject = layerDetails.project ? layerDetails.project : context.project();
42 if ( !destinationProject )
43 return nullptr;
44
45 QgsLayerTreeGroup *resultsGroup = nullptr;
46
47 // if a specific results group is specified in Processing settings,
48 // respect it (and create if necessary)
49 QgsSettings settings;
50 const QString resultsGroupName = settings.value( QStringLiteral( "Processing/Configuration/RESULTS_GROUP_NAME" ), QString() ).toString();
51
52 if ( !resultsGroupName.isEmpty() )
53 {
54 resultsGroup = destinationProject->layerTreeRoot()->findGroup( resultsGroupName );
55 if ( !resultsGroup )
56 {
57 resultsGroup = destinationProject->layerTreeRoot()->insertGroup(
58 0, resultsGroupName
59 );
60 resultsGroup->setExpanded( true );
61 }
62 }
63
64 // if this particular output layer has a specific output group assigned,
65 // find or create it now
66 QgsLayerTreeGroup *group = nullptr;
67 if ( !layerDetails.groupName.isEmpty() )
68 {
69 if ( !resultsGroup )
70 {
71 resultsGroup = destinationProject->layerTreeRoot();
72 }
73
74 group = resultsGroup->findGroup( layerDetails.groupName );
75 if ( !group )
76 {
77 group = resultsGroup->insertGroup( 0, layerDetails.groupName );
78 group->setExpanded( true );
79 }
80 }
81 else
82 {
83 group = resultsGroup;
84 }
85
86 return group;
87}
88
89void QgsProcessingGuiUtils::addResultLayers( const QVector<ResultLayerDetails> &layers, const QgsProcessingContext &context, QgsLayerTreeView *view )
90{
91 // sort added layer tree layers
92 QVector<ResultLayerDetails> sortedLayers = layers;
93 std::sort(
94 sortedLayers.begin(), sortedLayers.end(), []( const ResultLayerDetails &a, const ResultLayerDetails &b ) {
95 return a.sortKey < b.sortKey;
96 }
97 );
98
99 bool haveSetActiveLayer = false;
100 QgsLayerTreeNode *currentSelectedNode = nullptr;
101 if ( view )
102 {
103 currentSelectedNode = view->currentNode();
104 }
105 QgsLayerTreeGroup *defaultTargetGroup = nullptr;
106 int defaultTargetGroupIndex = 0;
107 if ( auto currentSelectedLayer = qobject_cast< QgsLayerTreeLayer * >( currentSelectedNode ) )
108 {
109 defaultTargetGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedLayer->parent() );
110 if ( defaultTargetGroup )
111 defaultTargetGroupIndex = defaultTargetGroup->children().indexOf( currentSelectedNode );
112 }
113 if ( auto currentSelectedGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedNode ) )
114 {
115 defaultTargetGroup = currentSelectedGroup;
116 }
117
118 for ( const ResultLayerDetails &layerDetails : std::as_const( sortedLayers ) )
119 {
120 QgsProject *project = layerDetails.destinationProject;
121 if ( !project )
122 project = context.project();
123
124 // store the current insertion point to restore it later
125 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > previousInsertionPoint;
126 if ( project )
127 {
128 previousInsertionPoint.emplace( project->layerTreeRegistryBridge()->layerInsertionPoint() );
129 }
130
131 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > insertionPoint;
132 if ( layerDetails.targetLayerTreeGroup )
133 {
134 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint( layerDetails.targetLayerTreeGroup, 0 ) );
135 }
136 else
137 {
138 // no destination group for this layer, so should be placed
139 // above the current layer if one was selected, or at top of group if a group was selected
140 if ( defaultTargetGroup )
141 {
142 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint(
143 defaultTargetGroup, defaultTargetGroupIndex
144 ) );
145 }
146 else if ( project )
147 {
148 insertionPoint.emplace( QgsLayerTreeRegistryBridge::InsertionPoint(
149 project->layerTreeRoot(), 0
150 ) );
151 }
152 }
153
154 if ( project && insertionPoint.has_value() )
155 {
156 project->layerTreeRegistryBridge()->setLayerInsertionPoint( *insertionPoint );
157 }
158
159 if ( project )
160 {
161 project->addMapLayer( layerDetails.layer );
162 QgsLayerTreeLayer *layerTreeLayer = project->layerTreeRoot()->findLayer( layerDetails.layer );
163 configureResultLayerTreeLayer( layerTreeLayer );
164 }
165
166 if ( !haveSetActiveLayer && view )
167 {
168 view->setCurrentLayer( layerDetails.layer );
169 haveSetActiveLayer = true;
170 }
171
172 // reset to the previous insertion point
173 if ( project && previousInsertionPoint.has_value() )
174 {
176 *previousInsertionPoint
177 );
178 }
179 }
180}
@ Vector
Vector layer.
Definition qgis.h:191
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.
QgsLayerTreeNode * currentNode() const
Returns the current node.
void setCurrentLayer(QgsMapLayer *layer)
Sets the currently selected layer.
Extends QTreeView and provides additional functionality when working with a layer tree.
Base class for all map layer types.
Definition qgsmaplayer.h:80
Qgis::LayerType type
Definition qgsmaplayer.h:90
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:109
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.