QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
18 #include "qgsalgorithmloadlayer.h"
19 
21 
22 QString QgsLoadLayerAlgorithm::name() const
23 {
24  return QStringLiteral( "loadlayer" );
25 }
26 
27 QgsProcessingAlgorithm::Flags QgsLoadLayerAlgorithm::flags() const
28 {
29  return FlagHideFromToolbox;
30 }
31 
32 QString QgsLoadLayerAlgorithm::displayName() const
33 {
34  return QObject::tr( "Load layer into project" );
35 }
36 
37 QStringList QgsLoadLayerAlgorithm::tags() const
38 {
39  return QObject::tr( "load,open,layer,raster,vector,project" ).split( ',' );
40 }
41 
42 QString QgsLoadLayerAlgorithm::group() const
43 {
44  return QObject::tr( "Modeler tools" );
45 }
46 
47 QString QgsLoadLayerAlgorithm::groupId() const
48 {
49  return QStringLiteral( "modelertools" );
50 }
51 
52 QString QgsLoadLayerAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm loads a layer to the current project." );
55 }
56 
57 QgsLoadLayerAlgorithm *QgsLoadLayerAlgorithm::createInstance() const
58 {
59  return new QgsLoadLayerAlgorithm();
60 }
61 
62 void QgsLoadLayerAlgorithm::initAlgorithm( const QVariantMap & )
63 {
64  addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
65  addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "Loaded layer name" ) ) );
66  addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer" ) ) );
67 }
68 
69 QVariantMap QgsLoadLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
70 {
71  QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
72  QString name = parameterAsString( parameters, QStringLiteral( "NAME" ), context );
73 
74  if ( !layer )
75  throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
76 
77  if ( name.isEmpty() )
78  throw QgsProcessingException( QObject::tr( "Invalid (empty) layer name" ) );
79 
80  layer->setName( name );
81  context.addLayerToLoadOnCompletion( layer->id(), QgsProcessingContext::LayerDetails( name, context.project(), name ) );
82 
83  QVariantMap results;
84  results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
85  return results;
86 }
87 
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:99
qgsalgorithmloadlayer.h
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingParameterMapLayer
Definition: qgsprocessingparameters.h:2456
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsProcessingContext::LayerDetails
Details for layers to load into projects.
Definition: qgsprocessingcontext.h:158
QgsProcessingContext::addLayerToLoadOnCompletion
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.
Definition: qgsprocessingcontext.cpp:53
QgsMapLayer::setName
void setName(const QString &name)
Set the display name of the layer.
Definition: qgsmaplayer.cpp:153
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsProcessingOutputMapLayer
Definition: qgsprocessingoutputs.h:155
QgsProcessingException
Definition: qgsexception.h:82