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 "Specifying an optional modulus value will restart the count to START whenever the field value reaches the modulus value.\n\n"
40 "Optionally, grouping fields can be specified. If group fields are present, then the field value will "
41 "be reset for each combination of these group field values.\n\n"
42 "The sort order for features may be specified, if so, then the incremental field will respect "
46 QStringList QgsAddIncrementalFieldAlgorithm::tags()
const
48 return QObject::tr(
"add,create,serial,primary,key,unique,fields" ).split(
',' );
51 QString QgsAddIncrementalFieldAlgorithm::group()
const
53 return QObject::tr(
"Vector table" );
56 QString QgsAddIncrementalFieldAlgorithm::groupId()
const
58 return QStringLiteral(
"vectortable" );
61 QString QgsAddIncrementalFieldAlgorithm::outputName()
const
63 return QObject::tr(
"Incremented" );
66 QList<int> QgsAddIncrementalFieldAlgorithm::inputLayerTypes()
const
71 QgsAddIncrementalFieldAlgorithm *QgsAddIncrementalFieldAlgorithm::createInstance()
const
73 return new QgsAddIncrementalFieldAlgorithm();
81 void QgsAddIncrementalFieldAlgorithm::initParameters(
const QVariantMap & )
83 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Field name" ), QStringLiteral(
"AUTO" ) ) );
92 std::unique_ptr< QgsProcessingParameterExpression > sortExp = std::make_unique< QgsProcessingParameterExpression >( QStringLiteral(
"SORT_EXPRESSION" ), QObject::tr(
"Sort expression" ), QVariant(), QStringLiteral(
"INPUT" ),
true );
94 addParameter( sortExp.release() );
95 std::unique_ptr< QgsProcessingParameterBoolean > sortAscending = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"SORT_ASCENDING" ), QObject::tr(
"Sort ascending" ),
true );
97 addParameter( sortAscending.release() );
98 std::unique_ptr< QgsProcessingParameterBoolean > sortNullsFirst = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"SORT_NULLS_FIRST" ), QObject::tr(
"Sort nulls first" ),
false );
100 addParameter( sortNullsFirst.release() );
103 QgsFields QgsAddIncrementalFieldAlgorithm::outputFields(
const QgsFields &inputFields )
const
113 mStartValue = parameterAsInt( parameters, QStringLiteral(
"START" ), context );
114 mValue = mStartValue;
115 mModulusValue = parameterAsInt( parameters, QStringLiteral(
"MODULUS" ), context );
116 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
117 mGroupedFieldNames = parameterAsFields( parameters, QStringLiteral(
"GROUP_FIELDS" ), context );
119 mSortExpressionString = parameterAsExpression( parameters, QStringLiteral(
"SORT_EXPRESSION" ), context );
120 mSortAscending = parameterAsBoolean( parameters, QStringLiteral(
"SORT_ASCENDING" ), context );
121 mSortNullsFirst = parameterAsBoolean( parameters, QStringLiteral(
"SORT_NULLS_FIRST" ), context );
128 if ( mSortExpressionString.isEmpty() )
136 if ( !mGroupedFieldNames.empty() && mGroupedFields.empty() )
138 for (
const QString &
field : std::as_const( mGroupedFieldNames ) )
140 int idx = mFields.lookupField(
field );
142 mGroupedFields << idx;
148 if ( mGroupedFields.empty() )
150 attributes.append( mValue );
152 if ( mModulusValue != 0 && ( mValue % mModulusValue ) == 0 )
153 mValue = mStartValue;
158 groupAttributes.reserve( mGroupedFields.size() );
159 for (
int index : std::as_const( mGroupedFields ) )
163 long long value = mGroupedValues.value( groupAttributes, mStartValue );
164 attributes.append( value );
166 if ( mModulusValue != 0 && ( value % mModulusValue ) == 0 )
168 mGroupedValues[ groupAttributes ] = value;
174 bool QgsAddIncrementalFieldAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const