QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 { QMetaType::Type::Int, QMetaType::Type::UnknownType },
86 { QMetaType::Type::Double, QMetaType::Type::UnknownType },
87 { QMetaType::Type::QString, QMetaType::Type::UnknownType },
88 { QMetaType::Type::Bool, QMetaType::Type::UnknownType },
89 { QMetaType::Type::QDate, QMetaType::Type::UnknownType },
90 { QMetaType::Type::QTime, QMetaType::Type::UnknownType },
91 { QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
92 { QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
93 { QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
94 { QMetaType::Type::QVariantList, QMetaType::Type::Int },
95 { QMetaType::Type::QVariantList, QMetaType::Type::Double }
96 } )
97 {
98 typeStrings << QgsVariantUtils::typeToDisplayString( type.first, type.second );
99 icons << QgsFields::iconForFieldType( type.first, type.second );
100 }
101
102 std::unique_ptr<QgsProcessingParameterEnum> fieldTypes = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ), typeStrings, false, 0 );
103 fieldTypes->setMetadata(
104 { QVariantMap( { { QStringLiteral( "widget_wrapper" ), QVariantMap( { { QStringLiteral( "icons" ), icons } } ) } } )
105 }
106 );
107 addParameter( fieldTypes.release() );
108 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ), Qgis::ProcessingNumberParameterType::Integer, 10, false, 1, 255 ) );
109 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ), Qgis::ProcessingNumberParameterType::Integer, 0, false, 0, 10 ) );
110
111 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_ALIAS" ), QObject::tr( "Field alias" ), QVariant(), false, true ) );
112 addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_COMMENT" ), QObject::tr( "Field comment" ), QVariant(), false, true ) );
113}
114
115QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
116{
117 QgsFields outFields = inputFields;
118 outFields.append( QgsField( mField ) );
119 return outFields;
120}
121
122bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
123{
124 const QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
125 const int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
126 const int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
127 const int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
128 const QString alias = parameterAsString( parameters, QStringLiteral( "FIELD_ALIAS" ), context );
129 const QString comment = parameterAsString( parameters, QStringLiteral( "FIELD_COMMENT" ), context );
130
131 mField.setName( name );
132 mField.setLength( length );
133 mField.setPrecision( precision );
134 mField.setAlias( alias );
135 mField.setComment( comment );
136
137 switch ( type )
138 {
139 case 0: // Integer
140 mField.setType( QMetaType::Type::Int );
141 break;
142 case 1: // Float
143 mField.setType( QMetaType::Type::Double );
144 break;
145 case 2: // String
146 mField.setType( QMetaType::Type::QString );
147 break;
148 case 3: // Boolean
149 mField.setType( QMetaType::Type::Bool );
150 break;
151 case 4: // Date
152 mField.setType( QMetaType::Type::QDate );
153 break;
154 case 5: // Time
155 mField.setType( QMetaType::Type::QTime );
156 break;
157 case 6: // DateTime
158 mField.setType( QMetaType::Type::QDateTime );
159 break;
160 case 7: // Binary
161 mField.setType( QMetaType::Type::QByteArray );
162 break;
163 case 8: // StringList
164 mField.setType( QMetaType::Type::QStringList );
165 mField.setSubType( QMetaType::Type::QString );
166 break;
167 case 9: // IntegerList
168 mField.setType( QMetaType::Type::QVariantList );
169 mField.setSubType( QMetaType::Type::Int );
170 break;
171 case 10: // DoubleList
172 mField.setType( QMetaType::Type::QVariantList );
173 mField.setSubType( QMetaType::Type::Double );
174 break;
175 }
176
177 return true;
178}
179
180QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
181{
182 QgsFeature f = feature;
183 QgsAttributes attributes = f.attributes();
184 attributes.append( QVariant() );
185 f.setAttributes( attributes );
186 return QgsFeatureList() << f;
187}
188
189bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
190{
191 Q_UNUSED( layer )
192 return false;
193}
194
@ 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:3489
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:70
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