QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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>() << static_cast< int >( Qgis::ProcessingSourceType::Vector );
63}
64
65Qgis::ProcessingFeatureSourceFlags 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< QMetaType::Type, QMetaType::Type > >
85{
86 {QMetaType::Type::Int, QMetaType::Type::UnknownType },
87 {QMetaType::Type::Double, QMetaType::Type::UnknownType },
88 {QMetaType::Type::QString, QMetaType::Type::UnknownType },
89 {QMetaType::Type::Bool, QMetaType::Type::UnknownType },
90 {QMetaType::Type::QDate, QMetaType::Type::UnknownType },
91 {QMetaType::Type::QTime, QMetaType::Type::UnknownType },
92 {QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
93 {QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
94 {QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
95 {QMetaType::Type::QVariantList, QMetaType::Type::Int },
96 {QMetaType::Type::QVariantList, QMetaType::Type::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" ),
119 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ),
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( QMetaType::Type::Int );
152 break;
153 case 1: // Float
154 mField.setType( QMetaType::Type::Double );
155 break;
156 case 2: // String
157 mField.setType( QMetaType::Type::QString );
158 break;
159 case 3: // Boolean
160 mField.setType( QMetaType::Type::Bool );
161 break;
162 case 4: // Date
163 mField.setType( QMetaType::Type::QDate );
164 break;
165 case 5: // Time
166 mField.setType( QMetaType::Type::QTime );
167 break;
168 case 6: // DateTime
169 mField.setType( QMetaType::Type::QDateTime );
170 break;
171 case 7: // Binary
172 mField.setType( QMetaType::Type::QByteArray );
173 break;
174 case 8: // StringList
175 mField.setType( QMetaType::Type::QStringList );
176 mField.setSubType( QMetaType::Type::QString );
177 break;
178 case 9: // IntegerList
179 mField.setType( QMetaType::Type::QVariantList );
180 mField.setSubType( QMetaType::Type::Int );
181 break;
182 case 10: // DoubleList
183 mField.setType( QMetaType::Type::QVariantList );
184 mField.setSubType( QMetaType::Type::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
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3436
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:53
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:69
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:76
Contains information about the context in which a processing algorithm is executed.
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
int precision