-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] Add a model-only algorithm for renaming layers
This is required for algorithms with behaviour which depends on the layer names (e.g. the package algorithm uses the layer name as the table name in the geopackage). We need a way for models to be able to explicitly specify a layer name for this algorithm to be useful in models, otherwise the auto-generated temporary layer names are used (which are not very nice!)
- Loading branch information
1 parent
aef0d33
commit 6283ca6
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/*************************************************************************** | ||
qgsalgorithmrenamelayer.cpp | ||
--------------------- | ||
begin : November 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmrenamelayer.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsRenameLayerAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "renamelayer" ); | ||
} | ||
|
||
QgsProcessingAlgorithm::Flags QgsRenameLayerAlgorithm::flags() const | ||
{ | ||
return FlagHideFromToolbox; | ||
} | ||
|
||
QString QgsRenameLayerAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Rename layer" ); | ||
} | ||
|
||
QStringList QgsRenameLayerAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "change,layer,name" ).split( ',' ); | ||
} | ||
|
||
QString QgsRenameLayerAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Modeler tool" ); | ||
} | ||
|
||
QString QgsRenameLayerAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm renames a layer." ); | ||
} | ||
|
||
QgsRenameLayerAlgorithm *QgsRenameLayerAlgorithm::createInstance() const | ||
{ | ||
return new QgsRenameLayerAlgorithm(); | ||
} | ||
|
||
void QgsRenameLayerAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) ); | ||
addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "New name" ) ) ); | ||
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer" ) ) ); | ||
} | ||
|
||
QVariantMap QgsRenameLayerAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ); | ||
QString name = parameterAsString( parameters, QStringLiteral( "NAME" ), context ); | ||
|
||
if ( !layer ) | ||
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) ); | ||
|
||
if ( name.isEmpty() ) | ||
throw QgsProcessingException( QObject::tr( "Invalid (empty) layer name" ) ); | ||
|
||
bool parameterWasLayerName = parameters.value( QStringLiteral( "INPUT" ) ).toString() == layer->name(); | ||
|
||
layer->setName( name ); | ||
QVariantMap results; | ||
if ( parameterWasLayerName ) | ||
results.insert( QStringLiteral( "OUTPUT" ), name ); | ||
else | ||
results.insert( QStringLiteral( "OUTPUT" ), parameters.value( QStringLiteral( "INPUT" ) ) ); | ||
|
||
return results; | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*************************************************************************** | ||
qgsalgorithmrenamelayer.h | ||
--------------------- | ||
begin : November 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMRENAMELAYER_H | ||
#define QGSALGORITHMRENAMELAYER_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native rename layer algorithm. | ||
*/ | ||
class QgsRenameLayerAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
public: | ||
QgsRenameLayerAlgorithm() = default; | ||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
Flags flags() const override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
virtual QStringList tags() const override; | ||
QString group() const override; | ||
QString shortHelpString() const override; | ||
QgsRenameLayerAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback * ) override; | ||
|
||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMRENAMELAYER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters