24using namespace Qt::StringLiterals;
28QString QgsAddTableFieldAlgorithm::name()
const
30 return u
"addfieldtoattributestable"_s;
33QString QgsAddTableFieldAlgorithm::displayName()
const
35 return QObject::tr(
"Add field to attributes table" );
38QString QgsAddTableFieldAlgorithm::shortHelpString()
const
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."
47QString QgsAddTableFieldAlgorithm::shortDescription()
const
49 return QObject::tr(
"Adds a new attribute to a vector layer." );
52QStringList QgsAddTableFieldAlgorithm::tags()
const
54 return QObject::tr(
"add,create,new,attribute,fields" ).split(
',' );
57QString QgsAddTableFieldAlgorithm::group()
const
59 return QObject::tr(
"Vector table" );
62QString QgsAddTableFieldAlgorithm::groupId()
const
64 return u
"vectortable"_s;
67QString QgsAddTableFieldAlgorithm::outputName()
const
69 return QObject::tr(
"Added" );
72QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes()
const
82QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance()
const
84 return new QgsAddTableFieldAlgorithm();
87void QgsAddTableFieldAlgorithm::initParameters(
const QVariantMap & )
91 QStringList typeStrings;
93 typeStrings.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 }
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() );
123QgsFields QgsAddTableFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
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 );
140 if ( source->fields().lookupField( name ) >= 0 )
142 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( name ) );
145 mField.setName( name );
146 mField.setLength( length );
147 mField.setPrecision( precision );
148 mField.setAlias( alias );
149 mField.setComment( comment );
154 mField.setType( QMetaType::Type::Int );
157 mField.setType( QMetaType::Type::Double );
160 mField.setType( QMetaType::Type::QString );
163 mField.setType( QMetaType::Type::Bool );
166 mField.setType( QMetaType::Type::QDate );
169 mField.setType( QMetaType::Type::QTime );
172 mField.setType( QMetaType::Type::QDateTime );
175 mField.setType( QMetaType::Type::QByteArray );
178 mField.setType( QMetaType::Type::QStringList );
179 mField.setSubType( QMetaType::Type::QString );
182 mField.setType( QMetaType::Type::QVariantList );
183 mField.setSubType( QMetaType::Type::Int );
186 mField.setType( QMetaType::Type::QVariantList );
187 mField.setSubType( QMetaType::Type::Double );
198 attributes.append( QVariant() );
203bool QgsAddTableFieldAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
@ 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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
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