[processing][needs-docs] replace set raster style and set vector style

Python algorithms with generics set layer style C++ algorithm
This commit is contained in:
Alexander Bruy 2019-12-06 13:48:55 +02:00
parent b85fe90955
commit c974125fd9
6 changed files with 149 additions and 2 deletions

View File

@ -48,7 +48,7 @@ class SetRasterStyle(QgisAlgorithm):
super().__init__()
def flags(self):
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagHideFromToolbox
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,

View File

@ -44,7 +44,7 @@ class SetVectorStyle(QgisAlgorithm):
super().__init__()
def flags(self):
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagHideFromToolbox
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,

View File

@ -24,6 +24,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmaddincrementalfield.cpp
processing/qgsalgorithmaddtablefield.cpp
processing/qgsalgorithmaddxyfields.cpp
processing/qgsalgorithmapplylayerstyle.cpp
processing/qgsalgorithmarraytranslatedfeatures.cpp
processing/qgsalgorithmaspect.cpp
processing/qgsalgorithmassignprojection.cpp

View File

@ -0,0 +1,90 @@
/***************************************************************************
qgsalgorithmapplylayerstyle.cpp
---------------------
begin : December 2019
copyright : (C) 2017 by Alexander Bruy
email : alexander dot bruy 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 "qgsalgorithmapplylayerstyle.h"
///@cond PRIVATE
QString QgsApplyLayerStyleAlgorithm::name() const
{
return QStringLiteral( "setlayerstyle" );
}
QString QgsApplyLayerStyleAlgorithm::displayName() const
{
return QObject::tr( "Set layer style" );
}
QStringList QgsApplyLayerStyleAlgorithm::tags() const
{
return QObject::tr( "change,layer,style,qml" ).split( ',' );
}
QString QgsApplyLayerStyleAlgorithm::group() const
{
return QObject::tr( "Cartography" );
}
QString QgsApplyLayerStyleAlgorithm::groupId() const
{
return QStringLiteral( "cartography" );
}
QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm renames a layer." );
}
QgsProcessingAlgorithm::Flags QgsApplyLayerStyleAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
}
QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
{
return new QgsApplyLayerStyleAlgorithm();
}
void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style file" ), QgsProcessingParameterFile::File, QStringLiteral( "qml" ) ) );
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Styled" ) ) );
}
QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
QString style = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );
if ( !layer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
bool ok = false;
QString msg = layer->loadNamedStyle( style, ok );
if ( !ok )
{
throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
}
layer->triggerRepaint();
QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
return results;
}
///@endcond

View File

@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmapplylayerstyle.h
---------------------
begin : December 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy 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 QGSALGORITHMAPPLYLAYERSTYLE_H
#define QGSALGORITHMAPPLYLAYERSTYLE_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native apply layer style algorithm.
*/
class QgsApplyLayerStyleAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsApplyLayerStyleAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QgsApplyLayerStyleAlgorithm *createInstance() const override SIP_FACTORY;
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback * ) override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMAPPLYLAYERSTYLE_H

View File

@ -19,6 +19,7 @@
#include "qgsalgorithmaddincrementalfield.h"
#include "qgsalgorithmaddtablefield.h"
#include "qgsalgorithmaddxyfields.h"
#include "qgsalgorithmapplylayerstyle.h"
#include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h"
#include "qgsalgorithmassignprojection.h"
@ -192,6 +193,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsAddTableFieldAlgorithm() );
addAlgorithm( new QgsAddXYFieldsAlgorithm() );
addAlgorithm( new QgsAddUniqueValueIndexAlgorithm() );
addAlgorithm( new QgsApplyLayerStyleAlgorithm() );
addAlgorithm( new QgsArrayTranslatedFeaturesAlgorithm() );
addAlgorithm( new QgsAspectAlgorithm() );
addAlgorithm( new QgsAssignProjectionAlgorithm() );