QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
22 QString QgsApplyLayerStyleAlgorithm::name() const
23 {
24  return QStringLiteral( "setlayerstyle" );
25 }
26 
27 QString QgsApplyLayerStyleAlgorithm::displayName() const
28 {
29  return QObject::tr( "Set layer style" );
30 }
31 
32 QStringList QgsApplyLayerStyleAlgorithm::tags() const
33 {
34  return QObject::tr( "change,layer,style,qml" ).split( ',' );
35 }
36 
37 QString QgsApplyLayerStyleAlgorithm::group() const
38 {
39  return QObject::tr( "Cartography" );
40 }
41 
42 QString QgsApplyLayerStyleAlgorithm::groupId() const
43 {
44  return QStringLiteral( "cartography" );
45 }
46 
47 QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
48 {
49  return QObject::tr( "Applies the style to a layer. The style must be defined as QML file." );
50 }
51 
52 QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
53 {
54  return new QgsApplyLayerStyleAlgorithm();
55 }
56 
57 void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
58 {
59  addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
60  addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style file" ), QgsProcessingParameterFile::File, QStringLiteral( "qml" ) ) );
61  addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Styled" ) ) );
62 }
63 
64 bool QgsApplyLayerStyleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
65 {
66  QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
67  QString style = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );
68 
69  if ( !layer )
70  throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
71 
72  mLayerId = layer->id();
73 
74  bool ok = false;
75  QString msg = layer->loadNamedStyle( style, ok );
76  if ( !ok )
77  {
78  throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
79  }
80  layer->triggerRepaint();
81 
82  return true;
83 }
84 
85 QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
86 {
87  Q_UNUSED( parameters );
88  Q_UNUSED( context );
89 
90  QVariantMap results;
91  results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
92  return results;
93 }
94 
Base class for all map layer types.
Definition: qgsmaplayer.h:70
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
virtual QString loadNamedStyle(const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Retrieve a named style for this layer if one exists (either as a .qml file on disk or as a record in ...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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.
@ File
Parameter is a single file.
A map layer parameter for processing algorithms.