QGIS API Documentation 3.99.0-Master (357b655ed83)
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 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
76 const QString style = parameterAsFile( parameters, u"STYLE"_s, context );
77
78 if ( !layer )
79 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
80
81 mLayerId = layer->id();
82
83 bool ok = false;
84 const QString msg = layer->loadNamedStyle( style, ok );
85 if ( !ok )
86 {
87 throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
88 }
89 layer->triggerRepaint();
90
91 return true;
92}
93
94QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95{
96 Q_UNUSED( parameters );
97 Q_UNUSED( context );
98
99 QVariantMap results;
100 results.insert( u"OUTPUT"_s, mLayerId );
101 return results;
102}
103
@ File
Parameter is a single file.
Definition qgis.h:3860
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:86
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.