QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
57 "This algorithm sets the encoding used for reading a layer's attributes. No permanent changes "
58 "are made to the layer, rather it affects only how the layer is read during the current session.\n\n"
59 "Changing the encoding is only supported for some vector layer data sources."
60 );
61}
62
63QString QgsSetLayerEncodingAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Sets the encoding used for reading a layer's attributes." );
66}
67
68QgsSetLayerEncodingAlgorithm *QgsSetLayerEncodingAlgorithm::createInstance() const
69{
70 return new QgsSetLayerEncodingAlgorithm();
71}
72
73void QgsSetLayerEncodingAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterVectorLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
76 addParameter( new QgsProcessingParameterString( u"ENCODING"_s, QObject::tr( "Encoding" ) ) );
77
78 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Output layer" ) ) );
79}
80
81bool QgsSetLayerEncodingAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
82{
83 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u"INPUT"_s, context );
84
85 if ( !layer )
86 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( "INPUT"_L1 ) );
87
88 const QString encoding = parameterAsString( parameters, u"ENCODING"_s, context );
89
90 mOutputId = layer->id();
91 QgsVectorDataProvider *provider = layer->dataProvider();
92
94 {
95 layer->setProviderEncoding( encoding );
96 }
97 else
98 {
99 feedback->pushInfo( QObject::tr( "Layer's data provider does not support changing the attribute encoding" ) );
100 // we don't return false here -- rather we allow the algorithm to gracefully handle an attempt to be flexible to different layer sources,
101 // otherwise we potentially break model input flexibility!
102 }
103 return true;
104}
105
106QVariantMap QgsSetLayerEncodingAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
107{
108 QVariantMap outputs;
109 outputs.insert( u"OUTPUT"_s, mOutputId );
110 return outputs;
111}
112
@ SelectEncoding
Allows user to select encoding.
Definition qgis.h:535
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.