QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmsetlayerencoding.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsetlayerencoding.cpp
3 ------------------------------
4 begin : February 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson 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#include "qgsvectorlayer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsSetLayerEncodingAlgorithm::name() const
30{
31 return u"setlayerencoding"_s;
32}
33
34QString QgsSetLayerEncodingAlgorithm::displayName() const
35{
36 return QObject::tr( "Set layer encoding" );
37}
38
39QStringList QgsSetLayerEncodingAlgorithm::tags() const
40{
41 return QObject::tr( "change,alter,attribute,codepage" ).split( ',' );
42}
43
44QString QgsSetLayerEncodingAlgorithm::group() const
45{
46 return QObject::tr( "Vector general" );
47}
48
49QString QgsSetLayerEncodingAlgorithm::groupId() const
50{
51 return u"vectorgeneral"_s;
52}
53
54QString QgsSetLayerEncodingAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm sets the encoding used for reading a layer's attributes. No permanent changes "
57 "are made to the layer, rather it affects only how the layer is read during the current session.\n\n"
58 "Changing the encoding is only supported for some vector layer data sources." );
59}
60
61QString QgsSetLayerEncodingAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Sets the encoding used for reading a layer's attributes." );
64}
65
66QgsSetLayerEncodingAlgorithm *QgsSetLayerEncodingAlgorithm::createInstance() const
67{
68 return new QgsSetLayerEncodingAlgorithm();
69}
70
71void QgsSetLayerEncodingAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterVectorLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
74 addParameter( new QgsProcessingParameterString( u"ENCODING"_s, QObject::tr( "Encoding" ) ) );
75
76 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Output layer" ) ) );
77}
78
79bool QgsSetLayerEncodingAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
80{
81 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
82
83 if ( !layer )
84 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( "INPUT"_L1 ) );
85
86 const QString encoding = parameterAsString( parameters, u"ENCODING"_s, context );
87
88 mOutputId = layer->id();
89 QgsVectorDataProvider *provider = layer->dataProvider();
90
92 {
93 layer->setProviderEncoding( encoding );
94 }
95 else
96 {
97 feedback->pushInfo( QObject::tr( "Layer's data provider does not support changing the attribute encoding" ) );
98 // we don't return false here -- rather we allow the algorithm to gracefully handle an attempt to be flexible to different layer sources,
99 // otherwise we potentially break model input flexibility!
100 }
101 return true;
102}
103
104QVariantMap QgsSetLayerEncodingAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
105{
106 QVariantMap outputs;
107 outputs.insert( u"OUTPUT"_s, mOutputId );
108 return outputs;
109}
110
@ SelectEncoding
Allows user to select encoding.
Definition qgis.h:528
QString id
Definition qgsmaplayer.h:86
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.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A vector layer output for processing algorithms.
A string parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Base class for vector data providers.
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based dataset.
void setProviderEncoding(const QString &encoding)
Sets the text encoding of the data provider.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.