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
40 return QObject::tr(
"This algorithm adds a new attribute to a vector layer.\n\n"
41 "The name and characteristics of the attribute are defined as parameters. The new attribute "
42 "is not added to the input layer but a new layer is generated instead." );
45QString QgsAddTableFieldAlgorithm::shortDescription()
const
47 return QObject::tr(
"Adds a new attribute to a vector layer." );
50QStringList QgsAddTableFieldAlgorithm::tags()
const
52 return QObject::tr(
"add,create,new,attribute,fields" ).split(
',' );
55QString QgsAddTableFieldAlgorithm::group()
const
57 return QObject::tr(
"Vector table" );
60QString QgsAddTableFieldAlgorithm::groupId()
const
62 return u
"vectortable"_s;
65QString QgsAddTableFieldAlgorithm::outputName()
const
67 return QObject::tr(
"Added" );
70QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes()
const
80QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance()
const
82 return new QgsAddTableFieldAlgorithm();
85void QgsAddTableFieldAlgorithm::initParameters(
const QVariantMap & )
89 QStringList typeStrings;
91 typeStrings.reserve( 11 );
93 for (
const auto &type :
94 std::vector<std::pair<QMetaType::Type, QMetaType::Type>> {
95 { QMetaType::Type::Int, QMetaType::Type::UnknownType },
96 { QMetaType::Type::Double, QMetaType::Type::UnknownType },
97 { QMetaType::Type::QString, QMetaType::Type::UnknownType },
98 { QMetaType::Type::Bool, QMetaType::Type::UnknownType },
99 { QMetaType::Type::QDate, QMetaType::Type::UnknownType },
100 { QMetaType::Type::QTime, QMetaType::Type::UnknownType },
101 { QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
102 { QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
103 { QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
104 { QMetaType::Type::QVariantList, QMetaType::Type::Int },
105 { QMetaType::Type::QVariantList, QMetaType::Type::Double }
112 auto fieldTypes = std::make_unique<QgsProcessingParameterEnum>( u
"FIELD_TYPE"_s, QObject::tr(
"Field type" ), typeStrings,
false, 0 );
113 fieldTypes->setMetadata(
114 { QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"icons"_s, icons } } ) } } )
117 addParameter( fieldTypes.release() );
125QgsFields QgsAddTableFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
134 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
135 const QString name = parameterAsString( parameters, u
"FIELD_NAME"_s, context );
136 const int type = parameterAsInt( parameters, u
"FIELD_TYPE"_s, context );
137 const int length = parameterAsInt( parameters, u
"FIELD_LENGTH"_s, context );
138 const int precision = parameterAsInt( parameters, u
"FIELD_PRECISION"_s, context );
139 const QString alias = parameterAsString( parameters, u
"FIELD_ALIAS"_s, context );
140 const QString comment = parameterAsString( parameters, u
"FIELD_COMMENT"_s, context );
142 if ( source->fields().lookupField( name ) >= 0 )
144 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( name ) );
147 mField.setName( name );
148 mField.setLength( length );
149 mField.setPrecision( precision );
150 mField.setAlias( alias );
151 mField.setComment( comment );
156 mField.setType( QMetaType::Type::Int );
159 mField.setType( QMetaType::Type::Double );
162 mField.setType( QMetaType::Type::QString );
165 mField.setType( QMetaType::Type::Bool );
168 mField.setType( QMetaType::Type::QDate );
171 mField.setType( QMetaType::Type::QTime );
174 mField.setType( QMetaType::Type::QDateTime );
177 mField.setType( QMetaType::Type::QByteArray );
180 mField.setType( QMetaType::Type::QStringList );
181 mField.setSubType( QMetaType::Type::QString );
184 mField.setType( QMetaType::Type::QVariantList );
185 mField.setSubType( QMetaType::Type::Int );
188 mField.setType( QMetaType::Type::QVariantList );
189 mField.setSubType( QMetaType::Type::Double );
200 attributes.append( QVariant() );
205bool 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