Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[processing] Add a model-only "load layer to project" algorithm
This can be used to force loading a layer into the current project. The primary use case here is to load a preset layer as part of a model, but it's also useful for loading results from the 'package layers' algorithm into the project.
- Loading branch information
Showing
with
188 additions
and 1 deletion.
@@ -0,0 +1,83 @@ | ||
/*************************************************************************** | ||
qgsalgorithmloadlayer.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 "qgsalgorithmloadlayer.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsLoadLayerAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "loadlayer" ); | ||
} | ||
|
||
QgsProcessingAlgorithm::Flags QgsLoadLayerAlgorithm::flags() const | ||
{ | ||
return FlagHideFromToolbox; | ||
} | ||
|
||
QString QgsLoadLayerAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Load layer into project" ); | ||
} | ||
|
||
QStringList QgsLoadLayerAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "load,open,layer,raster,vector,project" ).split( ',' ); | ||
} | ||
|
||
QString QgsLoadLayerAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Modeler tool" ); | ||
} | ||
|
||
QString QgsLoadLayerAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm loads a layer to the current project." ); | ||
} | ||
|
||
QgsLoadLayerAlgorithm *QgsLoadLayerAlgorithm::createInstance() const | ||
{ | ||
return new QgsLoadLayerAlgorithm(); | ||
} | ||
|
||
void QgsLoadLayerAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) ); | ||
addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "Loaded layer name" ) ) ); | ||
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer" ) ) ); | ||
} | ||
|
||
QVariantMap QgsLoadLayerAlgorithm::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" ) ); | ||
|
||
layer->setName( name ); | ||
context.addLayerToLoadOnCompletion( layer->id(), QgsProcessingContext::LayerDetails( name, context.project(), name ) ); | ||
|
||
QVariantMap results; | ||
results.insert( QStringLiteral( "OUTPUT" ), layer->id() ); | ||
return results; | ||
} | ||
|
||
///@endcond |
@@ -0,0 +1,53 @@ | ||
/*************************************************************************** | ||
qgsalgorithmloadlayer.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 QGSALGORITHMLOADLAYER_H | ||
#define QGSALGORITHMLOADLAYER_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native rename layer algorithm. | ||
*/ | ||
class QgsLoadLayerAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
public: | ||
QgsLoadLayerAlgorithm() = 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; | ||
QgsLoadLayerAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback * ) override; | ||
|
||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMLOADLAYER_H |