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
18 #include "qgsalgorithmrenamelayer.h"
19
20 ///@cond PRIVATE
21
name() const22 QString QgsRenameLayerAlgorithm::name() const
23 {
24 return QStringLiteral( "renamelayer" );
25 }
26
flags() const27 QgsProcessingAlgorithm::Flags QgsRenameLayerAlgorithm::flags() const
28 {
29 return FlagHideFromToolbox;
30 }
31
displayName() const32 QString QgsRenameLayerAlgorithm::displayName() const
33 {
34 return QObject::tr( "Rename layer" );
35 }
36
tags() const37 QStringList QgsRenameLayerAlgorithm::tags() const
38 {
39 return QObject::tr( "change,layer,name,title" ).split( ',' );
40 }
41
group() const42 QString QgsRenameLayerAlgorithm::group() const
43 {
44 return QObject::tr( "Modeler tools" );
45 }
46
groupId() const47 QString QgsRenameLayerAlgorithm::groupId() const
48 {
49 return QStringLiteral( "modelertools" );
50 }
51
shortHelpString() const52 QString QgsRenameLayerAlgorithm::shortHelpString() const
53 {
54 return QObject::tr( "This algorithm renames a layer." );
55 }
56
createInstance() const57 QgsRenameLayerAlgorithm *QgsRenameLayerAlgorithm::createInstance() const
58 {
59 return new QgsRenameLayerAlgorithm();
60 }
61
initAlgorithm(const QVariantMap &)62 void QgsRenameLayerAlgorithm::initAlgorithm( const QVariantMap & )
63 {
64 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
65 addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "New name" ) ) );
66 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer" ) ) );
67 }
68
processAlgorithm(const QVariantMap & parameters,QgsProcessingContext & context,QgsProcessingFeedback *)69 QVariantMap QgsRenameLayerAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
70 {
71 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
72 const 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 const bool parameterWasLayerName = parameters.value( QStringLiteral( "INPUT" ) ).toString() == layer->name();
81
82 layer->setName( name );
83 QVariantMap results;
84 if ( parameterWasLayerName )
85 results.insert( QStringLiteral( "OUTPUT" ), name );
86 else
87 results.insert( QStringLiteral( "OUTPUT" ), parameters.value( QStringLiteral( "INPUT" ) ) );
88
89 return results;
90 }
91
92 ///@endcond
93