QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
22QString QgsRenameTableFieldAlgorithm::name() const
23{
24 return QStringLiteral( "renametablefield" );
25}
26
27QString QgsRenameTableFieldAlgorithm::displayName() const
28{
29 return QObject::tr( "Rename field" );
30}
31
32QString QgsRenameTableFieldAlgorithm::shortHelpString() const
33{
34 return QObject::tr( "This algorithm renames an existing field from a vector layer." );
35}
36
37QString QgsRenameTableFieldAlgorithm::shortDescription() const
38{
39 return QObject::tr( "Renames an existing field from a vector layer." );
40}
41
42QStringList QgsRenameTableFieldAlgorithm::tags() const
43{
44 return QObject::tr( "rename,attribute,fields,table,change" ).split( ',' );
45}
46
47QString QgsRenameTableFieldAlgorithm::group() const
48{
49 return QObject::tr( "Vector table" );
50}
51
52QString QgsRenameTableFieldAlgorithm::groupId() const
53{
54 return QStringLiteral( "vectortable" );
55}
56
57QString QgsRenameTableFieldAlgorithm::outputName() const
58{
59 return QObject::tr( "Renamed" );
60}
61
62QList<int> QgsRenameTableFieldAlgorithm::inputLayerTypes() const
63{
64 return QList<int>() << QgsProcessing::TypeVector;
65}
66
67QgsProcessingFeatureSource::Flag QgsRenameTableFieldAlgorithm::sourceFlags() const
68{
70}
71
72QgsRenameTableFieldAlgorithm *QgsRenameTableFieldAlgorithm::createInstance() const
73{
74 return new QgsRenameTableFieldAlgorithm();
75}
76
77void 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
83QgsFields 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
97bool 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
104QgsFeatureList QgsRenameTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
105{
106 return QgsFeatureList() << feature;
107}
108
109bool 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:349
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:922