QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsRenameTableFieldAlgorithm::name() const
27{
28 return u"renametablefield"_s;
29}
30
31QString QgsRenameTableFieldAlgorithm::displayName() const
32{
33 return QObject::tr( "Rename field" );
34}
35
36QString QgsRenameTableFieldAlgorithm::shortHelpString() const
37{
38 return QObject::tr( "This algorithm renames an existing field from a vector layer." );
39}
40
41QString QgsRenameTableFieldAlgorithm::shortDescription() const
42{
43 return QObject::tr( "Renames an existing field from a vector layer." );
44}
45
46QStringList QgsRenameTableFieldAlgorithm::tags() const
47{
48 return QObject::tr( "rename,attribute,fields,table,change" ).split( ',' );
49}
50
51QString QgsRenameTableFieldAlgorithm::group() const
52{
53 return QObject::tr( "Vector table" );
54}
55
56QString QgsRenameTableFieldAlgorithm::groupId() const
57{
58 return u"vectortable"_s;
59}
60
61QString QgsRenameTableFieldAlgorithm::outputName() const
62{
63 return QObject::tr( "Renamed" );
64}
65
66QList<int> QgsRenameTableFieldAlgorithm::inputLayerTypes() const
67{
68 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
69}
70
71Qgis::ProcessingFeatureSourceFlags QgsRenameTableFieldAlgorithm::sourceFlags() const
72{
74}
75
76QgsRenameTableFieldAlgorithm *QgsRenameTableFieldAlgorithm::createInstance() const
77{
78 return new QgsRenameTableFieldAlgorithm();
79}
80
81void QgsRenameTableFieldAlgorithm::initParameters( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterField( u"FIELD"_s, QObject::tr( "Field to rename" ), QVariant(), u"INPUT"_s ) );
84 addParameter( new QgsProcessingParameterString( u"NEW_NAME"_s, QObject::tr( "New field name" ) ) );
85}
86
87QgsFields QgsRenameTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
88{
89 QgsFields outFields = inputFields;
90 const int index = outFields.lookupField( mOriginalName );
91 if ( index < 0 )
92 throw QgsProcessingException( QObject::tr( "Field %1 could not be found in input table" ).arg( mOriginalName ) );
93
94 if ( outFields.lookupField( mNewName ) >= 0 )
95 throw QgsProcessingException( QObject::tr( "A field already exists with the name %1" ).arg( mNewName ) );
96
97 outFields.rename( index, mNewName );
98 return outFields;
99}
100
101bool QgsRenameTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 mOriginalName = parameterAsString( parameters, u"FIELD"_s, context );
104 mNewName = parameterAsString( parameters, u"NEW_NAME"_s, context );
105 return true;
106}
107
108QgsFeatureList QgsRenameTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
109{
110 return QgsFeatureList() << feature;
111}
112
113bool QgsRenameTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
114{
115 Q_UNUSED( layer )
116 return false;
117}
118
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
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.
QList< QgsFeature > QgsFeatureList