QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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.\n\n" );
38}
39
40QStringList QgsAddTableFieldAlgorithm::tags() const
41{
42 return QObject::tr( "add,create,new,attribute,fields" ).split( ',' );
43}
44
45QString QgsAddTableFieldAlgorithm::group() const
46{
47 return QObject::tr( "Vector table" );
48}
49
50QString QgsAddTableFieldAlgorithm::groupId() const
51{
52 return QStringLiteral( "vectortable" );
53}
54
55QString QgsAddTableFieldAlgorithm::outputName() const
56{
57 return QObject::tr( "Added" );
58}
59
60QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const
61{
62 return QList<int>() << QgsProcessing::TypeVector;
63}
64
65QgsProcessingFeatureSource::Flag QgsAddTableFieldAlgorithm::sourceFlags() const
66{
68}
69
70QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const
71{
72 return new QgsAddTableFieldAlgorithm();
73}
74
75void 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 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_ALIAS" ), QObject::tr( "Field alias" ), QVariant(), false, true ) );
123 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_COMMENT" ), QObject::tr( "Field comment" ), QVariant(), false, true ) );
124}
125
126QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
127{
128 QgsFields outFields = inputFields;
129 outFields.append( QgsField( mField ) );
130 return outFields;
131}
132
133bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
134{
135 const QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
136 const int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
137 const int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
138 const int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
139 const QString alias = parameterAsString( parameters, QStringLiteral( "FIELD_ALIAS" ), context );
140 const QString comment = parameterAsString( parameters, QStringLiteral( "FIELD_COMMENT" ), context );
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( QVariant::Int );
152 break;
153 case 1: // Float
154 mField.setType( QVariant::Double );
155 break;
156 case 2: // String
157 mField.setType( QVariant::String );
158 break;
159 case 3: // Boolean
160 mField.setType( QVariant::Bool );
161 break;
162 case 4: // Date
163 mField.setType( QVariant::Date );
164 break;
165 case 5: // Time
166 mField.setType( QVariant::Time );
167 break;
168 case 6: // DateTime
169 mField.setType( QVariant::DateTime );
170 break;
171 case 7: // Binary
172 mField.setType( QVariant::ByteArray );
173 break;
174 case 8: // StringList
175 mField.setType( QVariant::StringList );
176 mField.setSubType( QVariant::String );
177 break;
178 case 9: // IntegerList
179 mField.setType( QVariant::List );
180 mField.setSubType( QVariant::Int );
181 break;
182 case 10: // DoubleList
183 mField.setType( QVariant::List );
184 mField.setSubType( QVariant::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
A vector of attributes.
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.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
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, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Base class for all map layer types.
Definition qgsmaplayer.h:74
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...
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:920
int precision