23QString QgsAddTableFieldAlgorithm::name()
 const 
   25  return QStringLiteral( 
"addfieldtoattributestable" );
 
   28QString QgsAddTableFieldAlgorithm::displayName()
 const 
   30  return QObject::tr( 
"Add field to attributes table" );
 
   33QString QgsAddTableFieldAlgorithm::shortHelpString()
 const 
   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." );
 
   40QString QgsAddTableFieldAlgorithm::shortDescription()
 const 
   42  return QObject::tr( 
"Adds a new attribute to a vector layer." );
 
   45QStringList QgsAddTableFieldAlgorithm::tags()
 const 
   47  return QObject::tr( 
"add,create,new,attribute,fields" ).split( 
',' );
 
   50QString QgsAddTableFieldAlgorithm::group()
 const 
   52  return QObject::tr( 
"Vector table" );
 
   55QString QgsAddTableFieldAlgorithm::groupId()
 const 
   57  return QStringLiteral( 
"vectortable" );
 
   60QString QgsAddTableFieldAlgorithm::outputName()
 const 
   62  return QObject::tr( 
"Added" );
 
   65QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes()
 const 
   75QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance()
 const 
   77  return new QgsAddTableFieldAlgorithm();
 
   80void QgsAddTableFieldAlgorithm::initParameters( 
const QVariantMap & )
 
   84  QStringList typeStrings;
 
   86  typeStrings.reserve( 11 );
 
   88  for ( 
const auto &type :
 
   89        std::vector<std::pair<QMetaType::Type, QMetaType::Type>> {
 
   90          { QMetaType::Type::Int, QMetaType::Type::UnknownType },
 
   91          { QMetaType::Type::Double, QMetaType::Type::UnknownType },
 
   92          { QMetaType::Type::QString, QMetaType::Type::UnknownType },
 
   93          { QMetaType::Type::Bool, QMetaType::Type::UnknownType },
 
   94          { QMetaType::Type::QDate, QMetaType::Type::UnknownType },
 
   95          { QMetaType::Type::QTime, QMetaType::Type::UnknownType },
 
   96          { QMetaType::Type::QDateTime, QMetaType::Type::UnknownType },
 
   97          { QMetaType::Type::QByteArray, QMetaType::Type::UnknownType },
 
   98          { QMetaType::Type::QStringList, QMetaType::Type::UnknownType },
 
   99          { QMetaType::Type::QVariantList, QMetaType::Type::Int },
 
  100          { QMetaType::Type::QVariantList, QMetaType::Type::Double }
 
  107  auto fieldTypes = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( 
"FIELD_TYPE" ), QObject::tr( 
"Field type" ), typeStrings, 
false, 0 );
 
  108  fieldTypes->setMetadata(
 
  109    { QVariantMap( { { QStringLiteral( 
"widget_wrapper" ), QVariantMap( { { QStringLiteral( 
"icons" ), icons } } ) } } )
 
  112  addParameter( fieldTypes.release() );
 
  116  addParameter( 
new QgsProcessingParameterString( QStringLiteral( 
"FIELD_ALIAS" ), QObject::tr( 
"Field alias" ), QVariant(), 
false, 
true ) );
 
  117  addParameter( 
new QgsProcessingParameterString( QStringLiteral( 
"FIELD_COMMENT" ), QObject::tr( 
"Field comment" ), QVariant(), 
false, 
true ) );
 
  120QgsFields QgsAddTableFieldAlgorithm::outputFields( 
const QgsFields &inputFields )
 const 
  129  std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( 
"INPUT" ), context ) );
 
  130  const QString name = parameterAsString( parameters, QStringLiteral( 
"FIELD_NAME" ), context );
 
  131  const int type = parameterAsInt( parameters, QStringLiteral( 
"FIELD_TYPE" ), context );
 
  132  const int length = parameterAsInt( parameters, QStringLiteral( 
"FIELD_LENGTH" ), context );
 
  133  const int precision = parameterAsInt( parameters, QStringLiteral( 
"FIELD_PRECISION" ), context );
 
  134  const QString alias = parameterAsString( parameters, QStringLiteral( 
"FIELD_ALIAS" ), context );
 
  135  const QString comment = parameterAsString( parameters, QStringLiteral( 
"FIELD_COMMENT" ), context );
 
  137  if ( source->fields().lookupField( name ) >= 0 )
 
  139    throw QgsProcessingException( QObject::tr( 
"A field with the same name (%1) already exists" ).arg( name ) );
 
  142  mField.setName( name );
 
  143  mField.setLength( length );
 
  145  mField.setAlias( alias );
 
  146  mField.setComment( comment );
 
  151      mField.setType( QMetaType::Type::Int );
 
  154      mField.setType( QMetaType::Type::Double );
 
  157      mField.setType( QMetaType::Type::QString );
 
  160      mField.setType( QMetaType::Type::Bool );
 
  163      mField.setType( QMetaType::Type::QDate );
 
  166      mField.setType( QMetaType::Type::QTime );
 
  169      mField.setType( QMetaType::Type::QDateTime );
 
  172      mField.setType( QMetaType::Type::QByteArray );
 
  175      mField.setType( QMetaType::Type::QStringList );
 
  176      mField.setSubType( QMetaType::Type::QString );
 
  179      mField.setType( QMetaType::Type::QVariantList );
 
  180      mField.setSubType( QMetaType::Type::Int );
 
  183      mField.setType( QMetaType::Type::QVariantList );
 
  184      mField.setSubType( QMetaType::Type::Double );
 
  195  attributes.append( QVariant() );
 
  200bool 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