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