QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
21 
22 QString QgsAddTableFieldAlgorithm::name() const
23 {
24  return QStringLiteral( "addfieldtoattributestable" );
25 }
26 
27 QString QgsAddTableFieldAlgorithm::displayName() const
28 {
29  return QObject::tr( "Add field to attributes table" );
30 }
31 
32 QString QgsAddTableFieldAlgorithm::shortHelpString() const
33 {
34  return QObject::tr( "This algorithm adds a new attribute to a vector layer.\n\n"
35  "The name and characteristics of the attribute are defined as parameters. The new attribute "
36  "is not added to the input layer but a new layer is generated instead.\n\n" );
37 }
38 
39 QStringList QgsAddTableFieldAlgorithm::tags() const
40 {
41  return QObject::tr( "add,create,new,attribute,fields" ).split( ',' );
42 }
43 
44 QString QgsAddTableFieldAlgorithm::group() const
45 {
46  return QObject::tr( "Vector table" );
47 }
48 
49 QString QgsAddTableFieldAlgorithm::groupId() const
50 {
51  return QStringLiteral( "vectortable" );
52 }
53 
54 QString QgsAddTableFieldAlgorithm::outputName() const
55 {
56  return QObject::tr( "Added" );
57 }
58 
59 QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const
60 {
61  return QList<int>() << QgsProcessing::TypeVector;
62 }
63 
64 QgsProcessingFeatureSource::Flag QgsAddTableFieldAlgorithm::sourceFlags() const
65 {
67 }
68 
69 QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const
70 {
71  return new QgsAddTableFieldAlgorithm();
72 }
73 
74 void QgsAddTableFieldAlgorithm::initParameters( const QVariantMap & )
75 {
76  addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ) ) );
77  addParameter( new QgsProcessingParameterEnum( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ),
78  QStringList() << QObject::tr( "Integer" ) << QObject::tr( "Float" ) << QObject::tr( "String" ), false, 0 ) );
79  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ),
80  QgsProcessingParameterNumber::Integer, 10, false, 1, 255 ) );
81  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ),
82  QgsProcessingParameterNumber::Integer, 0, false, 0, 10 ) );
83 }
84 
85 QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
86 {
87  QgsFields outFields = inputFields;
88  outFields.append( QgsField( mField ) );
89  return outFields;
90 }
91 
92 bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93 {
94  const QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
95  const int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
96  const int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
97  const int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
98 
99  mField.setName( name );
100  mField.setLength( length );
101  mField.setPrecision( precision );
102 
103  switch ( type )
104  {
105  case 0:
106  mField.setType( QVariant::Int );
107  break;
108  case 1:
109  mField.setType( QVariant::Double );
110  break;
111  case 2:
112  mField.setType( QVariant::String );
113  break;
114  }
115 
116  return true;
117 }
118 
119 QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
120 {
121  QgsFeature f = feature;
122  QgsAttributes attributes = f.attributes();
123  attributes.append( QVariant() );
124  f.setAttributes( attributes );
125  return QgsFeatureList() << f;
126 }
127 
128 bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
129 {
130  Q_UNUSED( layer )
131  return false;
132 }
133 
A vector of attributes.
Definition: qgsattributes.h:58
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:153
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Contains information about the context in which a processing algorithm is executed.
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.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A numeric 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
int precision