Skip to content

Commit

Permalink
[processing][needs-docs] replace set raster style and set vector style
Browse files Browse the repository at this point in the history
Python algorithms with generics set layer style C++ algorithm
  • Loading branch information
alexbruy committed Dec 9, 2019
1 parent b85fe90 commit c974125
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SetRasterStyle.py
Expand Up @@ -48,7 +48,7 @@ def __init__(self):
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,
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SetVectorStyle.py
Expand Up @@ -44,7 +44,7 @@ def __init__(self):
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,
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions src/analysis/processing/qgsalgorithmapplylayerstyle.cpp
@@ -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
54 changes: 54 additions & 0 deletions src/analysis/processing/qgsalgorithmapplylayerstyle.h
@@ -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
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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() );
Expand Down

0 comments on commit c974125

Please sign in to comment.