QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsApplyLayerStyleAlgorithm::name() const
27{
28 return u"setlayerstyle"_s;
29}
30
31QString QgsApplyLayerStyleAlgorithm::displayName() const
32{
33 return QObject::tr( "Set layer style" );
34}
35
36QStringList QgsApplyLayerStyleAlgorithm::tags() const
37{
38 return QObject::tr( "change,layer,style,qml" ).split( ',' );
39}
40
41QString QgsApplyLayerStyleAlgorithm::group() const
42{
43 return QObject::tr( "Cartography" );
44}
45
46QString QgsApplyLayerStyleAlgorithm::groupId() const
47{
48 return u"cartography"_s;
49}
50
51QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "This algorithm applies the style to a layer. The style must be defined as QML file." );
54}
55
56QString QgsApplyLayerStyleAlgorithm::shortDescription() const
57{
58 return QObject::tr( "Applies the style from a QML file to a layer." );
59}
60
61QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
62{
63 return new QgsApplyLayerStyleAlgorithm();
64}
65
66void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
67{
68 addParameter( new QgsProcessingParameterMapLayer( u"INPUT"_s, QObject::tr( "Layer" ) ) );
69 addParameter( new QgsProcessingParameterFile( u"STYLE"_s, QObject::tr( "Style file" ), Qgis::ProcessingFileParameterBehavior::File, u"qml"_s ) );
70 addOutput( new QgsProcessingOutputMapLayer( u"OUTPUT"_s, QObject::tr( "Styled" ) ) );
71}
72
73bool QgsApplyLayerStyleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
74{
75 QGS_MARK_ALGORITHM_SOURCE
76
77 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
78 const QString style = parameterAsFile( parameters, u"STYLE"_s, context );
79
80 if ( !layer )
81 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
82
83 mLayerId = layer->id();
84
85 bool ok = false;
86 const QString msg = layer->loadNamedStyle( style, ok );
87 if ( !ok )
88 {
89 throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
90 }
91 layer->triggerRepaint();
92
93 return true;
94}
95
96QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
97{
98 Q_UNUSED( parameters );
99 Q_UNUSED( context );
100
101 QVariantMap results;
102 results.insert( u"OUTPUT"_s, mLayerId );
103 return results;
104}
105
@ File
Parameter is a single file.
Definition qgis.h:4008
Base class for all map layer types.
Definition qgsmaplayer.h:83
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.