QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsProcessingOutputVectorLayer
A vector layer output for processing algorithms.
Definition: qgsprocessingoutputs.h:179
QgsVectorDataProvider::capabilities
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:208
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
qgsvectordataprovider.h
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:169
QgsProcessingParameterVectorLayer
A vector layer (with or without geometry) parameter for processing algorithms. Consider using the mor...
Definition: qgsprocessingparameters.h:2827
qgsvectorlayer.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:58
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsVectorLayer::setProviderEncoding
void setProviderEncoding(const QString &encoding)
Sets the text encoding of the data provider.
Definition: qgsvectorlayer.cpp:703
QgsVectorDataProvider::SelectEncoding
@ SelectEncoding
Allows user to select encoding.
Definition: qgsvectordataprovider.h:83
qgsalgorithmsetlayerencoding.h