QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
24
25QString QgsSetLayerEncodingAlgorithm::name() const
26{
27 return QStringLiteral( "setlayerencoding" );
28}
29
30QString QgsSetLayerEncodingAlgorithm::displayName() const
31{
32 return QObject::tr( "Set layer encoding" );
33}
34
35QStringList QgsSetLayerEncodingAlgorithm::tags() const
36{
37 return QObject::tr( "change,alter,attribute,codepage" ).split( ',' );
38}
39
40QString QgsSetLayerEncodingAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsSetLayerEncodingAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50QString QgsSetLayerEncodingAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "This algorithm sets the encoding used for reading a layer's attributes. No permanent changes "
53 "are made to the layer, rather it affects only how the layer is read during the current session.\n\n"
54 "Changing the encoding is only supported for some vector layer data sources." );
55}
56
57QString QgsSetLayerEncodingAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Sets the encoding used for reading a layer's attributes." );
60}
61
62QgsSetLayerEncodingAlgorithm *QgsSetLayerEncodingAlgorithm::createInstance() const
63{
64 return new QgsSetLayerEncodingAlgorithm();
65}
66
67void QgsSetLayerEncodingAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
70 addParameter( new QgsProcessingParameterString( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ) ) );
71
72 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ) ) );
73}
74
75bool QgsSetLayerEncodingAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76{
77 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
78
79 if ( !layer )
80 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
81
82 const QString encoding = parameterAsString( parameters, QStringLiteral( "ENCODING" ), context );
83
84 mOutputId = layer->id();
85 QgsVectorDataProvider *provider = layer->dataProvider();
86
88 {
89 layer->setProviderEncoding( encoding );
90 }
91 else
92 {
93 feedback->pushInfo( QObject::tr( "Layer's data provider does not support changing the attribute encoding" ) );
94 // we don't return false here -- rather we allow the algorithm to gracefully handle an attempt to be flexible to different layer sources,
95 // otherwise we potentially break model input flexibility!
96 }
97 return true;
98}
99
100QVariantMap QgsSetLayerEncodingAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
101{
102 QVariantMap outputs;
103 outputs.insert( QStringLiteral( "OUTPUT" ), mOutputId );
104 return outputs;
105}
106
@ SelectEncoding
Allows user to select encoding.
Definition qgis.h:509
QString id
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.
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.