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