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