QGIS API Documentation
4.3.0-Master (52aa6503bed)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
analysis
processing
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
18
#include "
qgsalgorithmapplylayerstyle.h
"
19
20
#include <QString>
21
22
using namespace
Qt::StringLiterals;
23
25
26
QString QgsApplyLayerStyleAlgorithm::name()
const
27
{
28
return
u
"setlayerstyle"
_s;
29
}
30
31
QString QgsApplyLayerStyleAlgorithm::displayName()
const
32
{
33
return
QObject::tr(
"Set layer style"
);
34
}
35
36
QStringList QgsApplyLayerStyleAlgorithm::tags()
const
37
{
38
return
QObject::tr(
"change,layer,style,qml"
).split(
','
);
39
}
40
41
QString QgsApplyLayerStyleAlgorithm::group()
const
42
{
43
return
QObject::tr(
"Cartography"
);
44
}
45
46
QString QgsApplyLayerStyleAlgorithm::groupId()
const
47
{
48
return
u
"cartography"
_s;
49
}
50
51
QString 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
56
QString QgsApplyLayerStyleAlgorithm::shortDescription()
const
57
{
58
return
QObject::tr(
"Applies the style from a QML file to a layer."
);
59
}
60
61
QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance()
const
62
{
63
return
new
QgsApplyLayerStyleAlgorithm();
64
}
65
66
void
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
73
bool
QgsApplyLayerStyleAlgorithm::prepareAlgorithm(
const
QVariantMap ¶meters,
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
96
QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm(
const
QVariantMap ¶meters,
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
Qgis::ProcessingFileParameterBehavior::File
@ File
Parameter is a single file.
Definition
qgis.h:4052
QgsMapLayer
Base class for all map layer types.
Definition
qgsmaplayer.h:83
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition
qgsprocessingcontext.h:45
QgsProcessingException
Custom exception class for processing related exceptions.
Definition
qgsexception.h:76
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition
qgsprocessingfeedback.h:40
QgsProcessingOutputMapLayer
A map layer output for processing algorithms, where layers may be either vector or raster.
Definition
qgsprocessingoutputs.h:212
QgsProcessingParameterFile
An input file or folder parameter for processing algorithms.
Definition
qgsprocessingparameters.h:2161
QgsProcessingParameterMapLayer
A map layer parameter for processing algorithms.
Definition
qgsprocessingparameters.h:3343
qgsalgorithmapplylayerstyle.h
Generated on
for QGIS API Documentation by
1.17.0