QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmaddtablefield.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmaddtablefield.cpp
3 -----------------------------------
4 begin : November 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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 "qgsvariantutils.h"
20
22
23QString QgsAddTableFieldAlgorithm::name() const
24{
25 return QStringLiteral( "addfieldtoattributestable" );
26}
27
28QString QgsAddTableFieldAlgorithm::displayName() const
29{
30 return QObject::tr( "Add field to attributes table" );
31}
32
33QString QgsAddTableFieldAlgorithm::shortHelpString() const
34{
35 return QObject::tr( "This algorithm adds a new attribute to a vector layer.\n\n"
36 "The name and characteristics of the attribute are defined as parameters. The new attribute "
37 "is not added to the input layer but a new layer is generated instead." );
38}
39
40QString QgsAddTableFieldAlgorithm::shortDescription() const
41{
42 return QObject::tr( "Adds a new attribute to a vector layer." );
43}
44
45QStringList QgsAddTableFieldAlgorithm::tags() const
46{
47 return QObject::tr( "add,create,new,attribute,fields" ).split( ',' );
48}
49
50QString QgsAddTableFieldAlgorithm::group() const
51{
52 return QObject::tr( "Vector table" );
53}
54
55QString QgsAddTableFieldAlgorithm::groupId() const
56{
57 return QStringLiteral( "vectortable" );
58}
59
60QString QgsAddTableFieldAlgorithm::outputName() const
61{
62 return QObject::tr( "Added" );
63}
64
65QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const
66{
67 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
68}
69
70Qgis::ProcessingFeatureSourceFlags QgsAddTableFieldAlgorithm::sourceFlags() const
71{
73}
74
75QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const
76{
77 return new QgsAddTableFieldAlgorithm();
78}
79
80void QgsAddTableFieldAlgorithm::initParameters( const QVariantMap & )
81{
82 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ) ) );
83
84 QStringList typeStrings;
85 QVariantList icons;
86 typeStrings.reserve( 11 );
87 icons.reserve( 11 );
88 for ( const auto &type :
89 std::vector<std::pair<QMetaType::Type, QMetaType::Type>> {
90 { QMetaType::Type::Int, QMetaType::Type::UnknownType },
91 { QMetaType::Type::Double, QMetaType::Type::UnknownType },
92 { QMetaType::Type::QString, QMetaType::Type::UnknownType },
93 { QMetaType::Type::Bool, QMetaType::Type::UnknownType },
94 { QMetaType::Type::QDate, QMetaType::Type::UnknownType },
95 { QMetaType::Type::QTime, QMetaType::Type::UnknownType },
96 { QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
97 { QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
98 { QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
99 { QMetaType::Type::QVariantList, QMetaType::Type::Int },
100 { QMetaType::Type::QVariantList, QMetaType::Type::Double }
101 } )
102 {
103 typeStrings << QgsVariantUtils::typeToDisplayString( type.first, type.second );
104 icons << QgsFields::iconForFieldType( type.first, type.second );
105 }
106
107 auto fieldTypes = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ), typeStrings, false, 0 );
108 fieldTypes->setMetadata(
109 { QVariantMap( { { QStringLiteral( "widget_wrapper" ), QVariantMap( { { QStringLiteral( "icons" ), icons } } ) } } )
110 }
111 );
112 addParameter( fieldTypes.release() );
113 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ), Qgis::ProcessingNumberParameterType::Integer, 10, false, 1, 255 ) );
114 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ), Qgis::ProcessingNumberParameterType::Integer, 0, false, 0, 10 ) );
115
116 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_ALIAS" ), QObject::tr( "Field alias" ), QVariant(), false, true ) );
117 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_COMMENT" ), QObject::tr( "Field comment" ), QVariant(), false, true ) );
118}
119
120QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
121{
122 QgsFields outFields = inputFields;
123 outFields.append( QgsField( mField ) );
124 return outFields;
125}
126
127bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
128{
129 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
130 const QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
131 const int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
132 const int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
133 const int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
134 const QString alias = parameterAsString( parameters, QStringLiteral( "FIELD_ALIAS" ), context );
135 const QString comment = parameterAsString( parameters, QStringLiteral( "FIELD_COMMENT" ), context );
136
137 if ( source->fields().lookupField( name ) >= 0 )
138 {
139 throw QgsProcessingException( QObject::tr( "A field with the same name (%1) already exists" ).arg( name ) );
140 }
141
142 mField.setName( name );
143 mField.setLength( length );
144 mField.setPrecision( precision );
145 mField.setAlias( alias );
146 mField.setComment( comment );
147
148 switch ( type )
149 {
150 case 0: // Integer
151 mField.setType( QMetaType::Type::Int );
152 break;
153 case 1: // Float
154 mField.setType( QMetaType::Type::Double );
155 break;
156 case 2: // String
157 mField.setType( QMetaType::Type::QString );
158 break;
159 case 3: // Boolean
160 mField.setType( QMetaType::Type::Bool );
161 break;
162 case 4: // Date
163 mField.setType( QMetaType::Type::QDate );
164 break;
165 case 5: // Time
166 mField.setType( QMetaType::Type::QTime );
167 break;
168 case 6: // DateTime
169 mField.setType( QMetaType::Type::QDateTime );
170 break;
171 case 7: // Binary
172 mField.setType( QMetaType::Type::QByteArray );
173 break;
174 case 8: // StringList
175 mField.setType( QMetaType::Type::QStringList );
176 mField.setSubType( QMetaType::Type::QString );
177 break;
178 case 9: // IntegerList
179 mField.setType( QMetaType::Type::QVariantList );
180 mField.setSubType( QMetaType::Type::Int );
181 break;
182 case 10: // DoubleList
183 mField.setType( QMetaType::Type::QVariantList );
184 mField.setSubType( QMetaType::Type::Double );
185 break;
186 }
187
188 return true;
189}
190
191QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
192{
193 QgsFeature f = feature;
194 QgsAttributes attributes = f.attributes();
195 attributes.append( QVariant() );
196 f.setAttributes( attributes );
197 return QgsFeatureList() << f;
198}
199
200bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
201{
202 Q_UNUSED( layer )
203 return false;
204}
205
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Base class for all map layer types.
Definition qgsmaplayer.h:77
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 numeric parameter for processing algorithms.
A string parameter for processing algorithms.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
QList< QgsFeature > QgsFeatureList
int precision