24QString QgsAddTableFieldAlgorithm::name()
const
26 return QStringLiteral(
"addfieldtoattributestable" );
29QString QgsAddTableFieldAlgorithm::displayName()
const
31 return QObject::tr(
"Add field to attributes table" );
34QString QgsAddTableFieldAlgorithm::shortHelpString()
const
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." );
41QString QgsAddTableFieldAlgorithm::shortDescription()
const
43 return QObject::tr(
"Adds a new attribute to a vector layer." );
46QStringList QgsAddTableFieldAlgorithm::tags()
const
48 return QObject::tr(
"add,create,new,attribute,fields" ).split(
',' );
51QString QgsAddTableFieldAlgorithm::group()
const
53 return QObject::tr(
"Vector table" );
56QString QgsAddTableFieldAlgorithm::groupId()
const
58 return QStringLiteral(
"vectortable" );
61QString QgsAddTableFieldAlgorithm::outputName()
const
63 return QObject::tr(
"Added" );
66QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes()
const
76QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance()
const
78 return new QgsAddTableFieldAlgorithm();
81void QgsAddTableFieldAlgorithm::initParameters(
const QVariantMap & )
85 QStringList typeStrings;
87 typeStrings.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 }
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 } } ) } } )
113 addParameter( fieldTypes.release() );
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 ) );
121QgsFields QgsAddTableFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
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 );
138 if ( source->fields().lookupField( name ) >= 0 )
140 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( name ) );
143 mField.setName( name );
144 mField.setLength( length );
145 mField.setPrecision( precision );
146 mField.setAlias( alias );
147 mField.setComment( comment );
152 mField.setType( QMetaType::Type::Int );
155 mField.setType( QMetaType::Type::Double );
158 mField.setType( QMetaType::Type::QString );
161 mField.setType( QMetaType::Type::Bool );
164 mField.setType( QMetaType::Type::QDate );
167 mField.setType( QMetaType::Type::QTime );
170 mField.setType( QMetaType::Type::QDateTime );
173 mField.setType( QMetaType::Type::QByteArray );
176 mField.setType( QMetaType::Type::QStringList );
177 mField.setSubType( QMetaType::Type::QString );
180 mField.setType( QMetaType::Type::QVariantList );
181 mField.setSubType( QMetaType::Type::Int );
184 mField.setType( QMetaType::Type::QVariantList );
185 mField.setSubType( QMetaType::Type::Double );
196 attributes.append( QVariant() );
201bool 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