QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmrenamelayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrenamelayer.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 QgsRenameLayerAlgorithm::name() const
27{
28 return u"renamelayer"_s;
29}
30
31Qgis::ProcessingAlgorithmFlags QgsRenameLayerAlgorithm::flags() const
32{
34}
35
36QString QgsRenameLayerAlgorithm::displayName() const
37{
38 return QObject::tr( "Rename layer" );
39}
40
41QStringList QgsRenameLayerAlgorithm::tags() const
42{
43 return QObject::tr( "change,layer,name,title" ).split( ',' );
44}
45
46QString QgsRenameLayerAlgorithm::group() const
47{
48 return QObject::tr( "Modeler tools" );
49}
50
51QString QgsRenameLayerAlgorithm::groupId() const
52{
53 return u"modelertools"_s;
54}
55
56QString QgsRenameLayerAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm renames a layer." );
59}
60
61QString QgsRenameLayerAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Renames a layer." );
64}
65
66QgsRenameLayerAlgorithm *QgsRenameLayerAlgorithm::createInstance() const
67{
68 return new QgsRenameLayerAlgorithm();
69}
70
71void QgsRenameLayerAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterMapLayer( u"INPUT"_s, QObject::tr( "Layer" ) ) );
74 addParameter( new QgsProcessingParameterString( u"NAME"_s, QObject::tr( "New name" ) ) );
75 addOutput( new QgsProcessingOutputMapLayer( u"OUTPUT"_s, QObject::tr( "Layer" ) ) );
76}
77
78QVariantMap QgsRenameLayerAlgorithm::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 const bool parameterWasLayerName = parameters.value( u"INPUT"_s ).toString() == layer->name();
90
91 layer->setName( name );
92 QVariantMap results;
93 if ( parameterWasLayerName )
94 results.insert( u"OUTPUT"_s, name );
95 else
96 results.insert( u"OUTPUT"_s, parameters.value( u"INPUT"_s ) );
97
98 return results;
99}
100
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3654
Base class for all map layer types.
Definition qgsmaplayer.h:83
QString name
Definition qgsmaplayer.h:87
void setName(const QString &name)
Set the display name of the layer.
Contains information about the context in which a processing algorithm is 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.