QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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( QStringLiteral( "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:627
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:48
QgsProcessingOutputVectorLayer
Definition: qgsprocessingoutputs.h:179
QgsVectorDataProvider::capabilities
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:191
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
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:148
QgsProcessingParameterVectorLayer
Definition: qgsprocessingparameters.h:2382
qgsvectorlayer.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsVectorDataProvider
Definition: qgsvectordataprovider.h:58
QgsProcessingException
Definition: qgsexception.h:82
QgsVectorLayer::setProviderEncoding
void setProviderEncoding(const QString &encoding)
Sets the text encoding of the data provider.
Definition: qgsvectorlayer.cpp:642
QgsVectorDataProvider::SelectEncoding
@ SelectEncoding
Allows user to select encoding.
Definition: qgsvectordataprovider.h:83
qgsalgorithmsetlayerencoding.h