23 QString QgsAddIncrementalFieldAlgorithm::name()
const
25 return QStringLiteral(
"addautoincrementalfield" );
28 QString QgsAddIncrementalFieldAlgorithm::displayName()
const
30 return QObject::tr(
"Add autoincremental field" );
33 QString QgsAddIncrementalFieldAlgorithm::shortHelpString()
const
35 return QObject::tr(
"This algorithm adds a new integer field to a vector layer, with a sequential value for each feature.\n\n"
36 "This field can be used as a unique ID for features in the layer. The new attribute "
37 "is not added to the input layer but a new layer is generated instead.\n\n"
38 "The initial starting value for the incremental series can be specified.\n\n"
39 "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
40 "be reset for each combination of these group field values.\n\n"
41 "The sort order for features may be specified, if so, then the incremental field will respect "
45 QStringList QgsAddIncrementalFieldAlgorithm::tags()
const
47 return QObject::tr(
"add,create,serial,primary,key,unique,fields" ).split(
',' );
50 QString QgsAddIncrementalFieldAlgorithm::group()
const
52 return QObject::tr(
"Vector table" );
55 QString QgsAddIncrementalFieldAlgorithm::groupId()
const
57 return QStringLiteral(
"vectortable" );
60 QString QgsAddIncrementalFieldAlgorithm::outputName()
const
62 return QObject::tr(
"Incremented" );
65 QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes()
const
70 QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance()
const
72 return new QgsAddIncrementalFieldAlgorithm();
80 void QgsAddIncrementalFieldAlgorithm::initParameters(
const QVariantMap & )
82 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QStringLiteral(
"AUTO" ) ) );
89 std::unique_ptr< QgsProcessingParameterExpression > sortExp = qgis::make_unique< QgsProcessingParameterExpression >( QStringLiteral(
"SORT_EXPRESSION" ), QObject::tr(
"Sort expression" ), QVariant(), QStringLiteral(
"INPUT" ),
true );
91 addParameter( sortExp.release() );
92 std::unique_ptr< QgsProcessingParameterBoolean > sortAscending = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"SORT_ASCENDING" ), QObject::tr(
"Sort ascending" ),
true );
94 addParameter( sortAscending.release() );
95 std::unique_ptr< QgsProcessingParameterBoolean > sortNullsFirst = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"SORT_NULLS_FIRST" ), QObject::tr(
"Sort nulls first" ),
false );
97 addParameter( sortNullsFirst.release() );
100 QgsFields QgsAddIncrementalFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
110 mStartValue = parameterAsInt( parameters, QStringLiteral(
"START" ), context );
111 mValue = mStartValue;
112 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
113 mGroupedFieldNames = parameterAsFields( parameters, QStringLiteral(
"GROUP_FIELDS" ), context );
115 mSortExpressionString = parameterAsExpression( parameters, QStringLiteral(
"SORT_EXPRESSION" ), context );
116 mSortAscending = parameterAsBoolean( parameters, QStringLiteral(
"SORT_ASCENDING" ), context );
117 mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral(
"SORT_NULLS_FIRST" ), context );
124 if ( mSortExpressionString.isEmpty() )
132 if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
134 for (
const QString &field : qgis::as_const( mGroupedFieldNames ) )
136 int idx = mFields.lookupField( field );
138 mGroupedFields << idx;
144 if ( mGroupedFields.empty() )
146 attributes.append( mValue );
152 groupAttributes.reserve( mGroupedFields.size() );
153 for (
int index : qgis::as_const( mGroupedFields ) )
157 long long value = mGroupedValues.value( groupAttributes, mStartValue );
158 attributes.append( value );
160 mGroupedValues[ groupAttributes ] = value;
166 bool QgsAddIncrementalFieldAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const