QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmapplylayerstyle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmapplylayerstyle.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2017 by Alexander Bruy
6 email : alexander dot bruy 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
19
21
22QString QgsApplyLayerStyleAlgorithm::name() const
23{
24 return QStringLiteral( "setlayerstyle" );
25}
26
27QString QgsApplyLayerStyleAlgorithm::displayName() const
28{
29 return QObject::tr( "Set layer style" );
30}
31
32QStringList QgsApplyLayerStyleAlgorithm::tags() const
33{
34 return QObject::tr( "change,layer,style,qml" ).split( ',' );
35}
36
37QString QgsApplyLayerStyleAlgorithm::group() const
38{
39 return QObject::tr( "Cartography" );
40}
41
42QString QgsApplyLayerStyleAlgorithm::groupId() const
43{
44 return QStringLiteral( "cartography" );
45}
46
47QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
48{
49 return QObject::tr( "This algorithm applies the style to a layer. The style must be defined as QML file." );
50}
51
52QString QgsApplyLayerStyleAlgorithm::shortDescription() const
53{
54 return QObject::tr( "Applies the style from a QML file to a layer." );
55}
56
57QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
58{
59 return new QgsApplyLayerStyleAlgorithm();
60}
61
62void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
63{
64 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
65 addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style file" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "qml" ) ) );
66 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Styled" ) ) );
67}
68
69bool QgsApplyLayerStyleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
70{
71 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
72 const QString style = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );
73
74 if ( !layer )
75 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
76
77 mLayerId = layer->id();
78
79 bool ok = false;
80 const QString msg = layer->loadNamedStyle( style, ok );
81 if ( !ok )
82 {
83 throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
84 }
85 layer->triggerRepaint();
86
87 return true;
88}
89
90QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
91{
92 Q_UNUSED( parameters );
93 Q_UNUSED( context );
94
95 QVariantMap results;
96 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
97 return results;
98}
99
@ File
Parameter is a single file.
Base class for all map layer types.
Definition qgsmaplayer.h:77
virtual QString loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories, Qgis::LoadStyleFlags flags=Qgis::LoadStyleFlags())
Loads a named style from file/local db/datasource db.
QString id
Definition qgsmaplayer.h:80
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A map layer output for processing algorithms, where layers may be either vector or raster.
An input file or folder parameter for processing algorithms.
A map layer parameter for processing algorithms.