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