QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
23 QString QgsAddTableFieldAlgorithm::name() const
24 {
25  return QStringLiteral( "addfieldtoattributestable" );
26 }
27 
28 QString QgsAddTableFieldAlgorithm::displayName() const
29 {
30  return QObject::tr( "Add field to attributes table" );
31 }
32 
33 QString 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.\n\n" );
38 }
39 
40 QStringList QgsAddTableFieldAlgorithm::tags() const
41 {
42  return QObject::tr( "add,create,new,attribute,fields" ).split( ',' );
43 }
44 
45 QString QgsAddTableFieldAlgorithm::group() const
46 {
47  return QObject::tr( "Vector table" );
48 }
49 
50 QString QgsAddTableFieldAlgorithm::groupId() const
51 {
52  return QStringLiteral( "vectortable" );
53 }
54 
55 QString QgsAddTableFieldAlgorithm::outputName() const
56 {
57  return QObject::tr( "Added" );
58 }
59 
60 QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const
61 {
62  return QList<int>() << QgsProcessing::TypeVector;
63 }
64 
65 QgsProcessingFeatureSource::Flag QgsAddTableFieldAlgorithm::sourceFlags() const
66 {
68 }
69 
70 QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const
71 {
72  return new QgsAddTableFieldAlgorithm();
73 }
74 
75 void QgsAddTableFieldAlgorithm::initParameters( const QVariantMap & )
76 {
77  addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ) ) );
78 
79  QStringList typeStrings;
80  QVariantList icons;
81  typeStrings.reserve( 11 );
82  icons.reserve( 11 );
83  for ( const auto &type :
84  std::vector < std::pair< QVariant::Type, QVariant::Type > >
85 {
86  {QVariant::Int, QVariant::Invalid },
87  {QVariant::Double, QVariant::Invalid },
88  {QVariant::String, QVariant::Invalid },
89  {QVariant::Bool, QVariant::Invalid },
90  {QVariant::Date, QVariant::Invalid },
91  {QVariant::Time, QVariant::Invalid },
92  {QVariant::DateTime, QVariant::Invalid },
93  {QVariant::ByteArray, QVariant::Invalid },
94  {QVariant::StringList, QVariant::Invalid },
95  {QVariant::List, QVariant::Int },
96  {QVariant::List, QVariant::Double }
97 } )
98  {
99  typeStrings << QgsVariantUtils::typeToDisplayString( type.first, type.second );
100  icons << QgsFields::iconForFieldType( type.first, type.second );
101  }
102 
103  std::unique_ptr< QgsProcessingParameterEnum> fieldTypes = std::make_unique< QgsProcessingParameterEnum> ( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ),
104  typeStrings, false, 0 );
105  fieldTypes->setMetadata(
106  {
107  QVariantMap( {{
108  QStringLiteral( "widget_wrapper" ),
109  QVariantMap(
110  { {
111  QStringLiteral( "icons" ), icons
112  }}
113  )
114  }} )
115  } );
116  addParameter( fieldTypes.release() );
117  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ),
118  QgsProcessingParameterNumber::Integer, 10, false, 1, 255 ) );
119  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ),
120  QgsProcessingParameterNumber::Integer, 0, false, 0, 10 ) );
121 }
122 
123 QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
124 {
125  QgsFields outFields = inputFields;
126  outFields.append( QgsField( mField ) );
127  return outFields;
128 }
129 
130 bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
131 {
132  const QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
133  const int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
134  const int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
135  const int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
136 
137  mField.setName( name );
138  mField.setLength( length );
139  mField.setPrecision( precision );
140 
141  switch ( type )
142  {
143  case 0: // Integer
144  mField.setType( QVariant::Int );
145  break;
146  case 1: // Float
147  mField.setType( QVariant::Double );
148  break;
149  case 2: // String
150  mField.setType( QVariant::String );
151  break;
152  case 3: // Boolean
153  mField.setType( QVariant::Bool );
154  break;
155  case 4: // Date
156  mField.setType( QVariant::Date );
157  break;
158  case 5: // Time
159  mField.setType( QVariant::Time );
160  break;
161  case 6: // DateTime
162  mField.setType( QVariant::DateTime );
163  break;
164  case 7: // Binary
165  mField.setType( QVariant::ByteArray );
166  break;
167  case 8: // StringList
168  mField.setType( QVariant::StringList );
169  mField.setSubType( QVariant::String );
170  break;
171  case 9: // IntegerList
172  mField.setType( QVariant::List );
173  mField.setSubType( QVariant::Int );
174  break;
175  case 10: // DoubleList
176  mField.setType( QVariant::List );
177  mField.setSubType( QVariant::Double );
178  break;
179  }
180 
181  return true;
182 }
183 
184 QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
185 {
186  QgsFeature f = feature;
187  QgsAttributes attributes = f.attributes();
188  attributes.append( QVariant() );
189  f.setAttributes( attributes );
190  return QgsFeatureList() << f;
191 }
192 
193 bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
194 {
195  Q_UNUSED( layer )
196  return false;
197 }
198 
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
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns an icon corresponding to a field type.
Definition: qgsfields.cpp:294
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.
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
static QString typeToDisplayString(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
int precision