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