QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmloadlayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmloadlayer.cpp
3 ---------------------
4 begin : November 2017
5 copyright : (C) 2017 by 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
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsLoadLayerAlgorithm::name() const
27{
28 return u"loadlayer"_s;
29}
30
31Qgis::ProcessingAlgorithmFlags QgsLoadLayerAlgorithm::flags() const
32{
34}
35
36QString QgsLoadLayerAlgorithm::displayName() const
37{
38 return QObject::tr( "Load layer into project" );
39}
40
41QStringList QgsLoadLayerAlgorithm::tags() const
42{
43 return QObject::tr( "load,open,layer,raster,vector,project" ).split( ',' );
44}
45
46QString QgsLoadLayerAlgorithm::group() const
47{
48 return QObject::tr( "Modeler tools" );
49}
50
51QString QgsLoadLayerAlgorithm::groupId() const
52{
53 return u"modelertools"_s;
54}
55
56QString QgsLoadLayerAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm loads a layer to the current project." );
59}
60
61QString QgsLoadLayerAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Loads a layer to the current project." );
64}
65
66QgsLoadLayerAlgorithm *QgsLoadLayerAlgorithm::createInstance() const
67{
68 return new QgsLoadLayerAlgorithm();
69}
70
71void QgsLoadLayerAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterMapLayer( u"INPUT"_s, QObject::tr( "Layer" ) ) );
74 addParameter( new QgsProcessingParameterString( u"NAME"_s, QObject::tr( "Loaded layer name" ) ) );
75 addOutput( new QgsProcessingOutputMapLayer( u"OUTPUT"_s, QObject::tr( "Layer" ) ) );
76}
77
78QVariantMap QgsLoadLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
79{
80 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
81 const QString name = parameterAsString( parameters, u"NAME"_s, context );
82
83 if ( !layer )
84 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
85
86 if ( name.isEmpty() )
87 throw QgsProcessingException( QObject::tr( "Invalid (empty) layer name" ) );
88
89 layer->setName( name );
90 QgsProcessingContext::LayerDetails details( name, context.project(), name );
91 details.forceName = true;
92 context.addLayerToLoadOnCompletion( layer->id(), details );
93
94 QVariantMap results;
95 results.insert( u"OUTPUT"_s, layer->id() );
96 return results;
97}
98
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ NotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
Definition qgis.h:3666
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3654
Base class for all map layer types.
Definition qgsmaplayer.h:83
QString id
Definition qgsmaplayer.h:86
void setName(const QString &name)
Set the display name of the layer.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A map layer output for processing algorithms, where layers may be either vector or raster.
A map layer parameter for processing algorithms.
A string parameter for processing algorithms.