QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsalgorithmrenametablefield.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmrenametablefield.cpp
3  -----------------------------------
4  begin : January 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 
22 QString QgsRenameTableFieldAlgorithm::name() const
23 {
24  return QStringLiteral( "renametablefield" );
25 }
26 
27 QString QgsRenameTableFieldAlgorithm::displayName() const
28 {
29  return QObject::tr( "Rename field" );
30 }
31 
32 QString QgsRenameTableFieldAlgorithm::shortHelpString() const
33 {
34  return QObject::tr( "This algorithm renames an existing field from a vector layer." );
35 }
36 
37 QString QgsRenameTableFieldAlgorithm::shortDescription() const
38 {
39  return QObject::tr( "Renames an existing field from a vector layer." );
40 }
41 
42 QStringList QgsRenameTableFieldAlgorithm::tags() const
43 {
44  return QObject::tr( "rename,attribute,fields,table,change" ).split( ',' );
45 }
46 
47 QString QgsRenameTableFieldAlgorithm::group() const
48 {
49  return QObject::tr( "Vector table" );
50 }
51 
52 QString QgsRenameTableFieldAlgorithm::groupId() const
53 {
54  return QStringLiteral( "vectortable" );
55 }
56 
57 QString QgsRenameTableFieldAlgorithm::outputName() const
58 {
59  return QObject::tr( "Renamed" );
60 }
61 
62 QList<int> QgsRenameTableFieldAlgorithm::inputLayerTypes() const
63 {
64  return QList<int>() << QgsProcessing::TypeVector;
65 }
66 
67 QgsProcessingFeatureSource::Flag QgsRenameTableFieldAlgorithm::sourceFlags() const
68 {
70 }
71 
72 QgsRenameTableFieldAlgorithm *QgsRenameTableFieldAlgorithm::createInstance() const
73 {
74  return new QgsRenameTableFieldAlgorithm();
75 }
76 
77 void QgsRenameTableFieldAlgorithm::initParameters( const QVariantMap & )
78 {
79  addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Field to rename" ), QVariant(), QStringLiteral( "INPUT" ) ) );
80  addParameter( new QgsProcessingParameterString( QStringLiteral( "NEW_NAME" ), QObject::tr( "New field name" ) ) );
81 }
82 
83 QgsFields QgsRenameTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
84 {
85  QgsFields outFields = inputFields;
86  const int index = outFields.lookupField( mOriginalName );
87  if ( index < 0 )
88  throw QgsProcessingException( QObject::tr( "Field %1 could not be found in input table" ).arg( mOriginalName ) );
89 
90  if ( outFields.lookupField( mNewName ) >= 0 )
91  throw QgsProcessingException( QObject::tr( "A field already exists with the name %1" ).arg( mNewName ) );
92 
93  outFields.rename( index, mNewName );
94  return outFields;
95 }
96 
97 bool QgsRenameTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
98 {
99  mOriginalName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
100  mNewName = parameterAsString( parameters, QStringLiteral( "NEW_NAME" ), context );
101  return true;
102 }
103 
104 QgsFeatureList QgsRenameTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
105 {
106  return QgsFeatureList() << feature;
107 }
108 
109 bool QgsRenameTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
110 {
111  Q_UNUSED( layer )
112  return false;
113 }
114 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:344
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
Definition: qgsfields.cpp:72
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882