QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
95  int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
96  int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
97  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 
QgsProcessingParameterNumber
Definition: qgsprocessingparameters.h:1838
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
qgsalgorithmaddtablefield.h
QgsFields
Definition: qgsfields.h:44
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsFields::append
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
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsProcessingParameterEnum
Definition: qgsprocessingparameters.h:2134
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsField
Definition: qgsfield.h:49