Skip to content

Commit

Permalink
Merge pull request #57217 from signedav/duplicatepolicy
Browse files Browse the repository at this point in the history
Duplicate policy on QgsFields when duplicate features
  • Loading branch information
signedav authored May 8, 2024
2 parents 5a22063 + cfe7197 commit d0d2023
Show file tree
Hide file tree
Showing 24 changed files with 475 additions and 28 deletions.
7 changes: 7 additions & 0 deletions python/PyQt6/core/auto_additions/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3215,6 +3215,13 @@
# --
Qgis.FieldDomainMergePolicy.baseClass = Qgis
# monkey patching scoped based enum
Qgis.FieldDuplicatePolicy.DefaultValue.__doc__ = "Use default field value"
Qgis.FieldDuplicatePolicy.Duplicate.__doc__ = "Duplicate original value"
Qgis.FieldDuplicatePolicy.UnsetField.__doc__ = "Clears the field value so that the data provider backend will populate using any backend triggers or similar logic (since QGIS 3.30)"
Qgis.FieldDuplicatePolicy.__doc__ = "Duplicate policy for fields.\n\nWhen a feature is duplicated, defines how the value of attributes are computed.\n\n.. versionadded:: 3.38\n\n" + '* ``DefaultValue``: ' + Qgis.FieldDuplicatePolicy.DefaultValue.__doc__ + '\n' + '* ``Duplicate``: ' + Qgis.FieldDuplicatePolicy.Duplicate.__doc__ + '\n' + '* ``UnsetField``: ' + Qgis.FieldDuplicatePolicy.UnsetField.__doc__
# --
Qgis.FieldDuplicatePolicy.baseClass = Qgis
# monkey patching scoped based enum
Qgis.FieldDomainType.Coded.__doc__ = "Coded field domain"
Qgis.FieldDomainType.Range.__doc__ = "Numeric range field domain (min/max)"
Qgis.FieldDomainType.Glob.__doc__ = "Glob string pattern field domain"
Expand Down
7 changes: 7 additions & 0 deletions python/PyQt6/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,13 @@ The development version
GeometryWeighted,
};

enum class FieldDuplicatePolicy /BaseType=IntEnum/
{
DefaultValue,
Duplicate,
UnsetField,
};

enum class FieldDomainType /BaseType=IntEnum/
{
Coded,
Expand Down
20 changes: 20 additions & 0 deletions python/PyQt6/core/auto_generated/qgsfield.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,26 @@ be handled during a split operation.
.. seealso:: :py:func:`splitPolicy`

.. versionadded:: 3.30
%End

Qgis::FieldDuplicatePolicy duplicatePolicy() const;
%Docstring
Returns the field's duplicate policy, which indicates how field values should
be handled during a duplicate operation.

.. seealso:: :py:func:`setDuplicatePolicy`

.. versionadded:: 3.38
%End

void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy );
%Docstring
Sets the field's duplicate ``policy``, which indicates how field values should
be handled during a duplicate operation.

.. seealso:: :py:func:`duplicatePolicy`

.. versionadded:: 3.38
%End

SIP_PYOBJECT __repr__();
Expand Down
21 changes: 21 additions & 0 deletions python/PyQt6/core/auto_generated/vector/qgsvectorlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,27 @@ Sets a split ``policy`` for the field with the specified index.
}
%End

void setFieldDuplicatePolicy( int index, Qgis::FieldDuplicatePolicy policy );
%Docstring
Sets a duplicate ``policy`` for the field with the specified index.

:raises KeyError: if no field with the specified index exists

.. versionadded:: 3.38
%End

%MethodCode
if ( a0 < 0 || a0 >= sipCpp->fields().count() )
{
PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
else
{
sipCpp->setFieldDuplicatePolicy( a0, a1 );
}
%End

QSet<QString> excludeAttributesWms() const /Deprecated/;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_additions/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,13 @@
# --
Qgis.FieldDomainMergePolicy.baseClass = Qgis
# monkey patching scoped based enum
Qgis.FieldDuplicatePolicy.DefaultValue.__doc__ = "Use default field value"
Qgis.FieldDuplicatePolicy.Duplicate.__doc__ = "Duplicate original value"
Qgis.FieldDuplicatePolicy.UnsetField.__doc__ = "Clears the field value so that the data provider backend will populate using any backend triggers or similar logic (since QGIS 3.30)"
Qgis.FieldDuplicatePolicy.__doc__ = "Duplicate policy for fields.\n\nWhen a feature is duplicated, defines how the value of attributes are computed.\n\n.. versionadded:: 3.38\n\n" + '* ``DefaultValue``: ' + Qgis.FieldDuplicatePolicy.DefaultValue.__doc__ + '\n' + '* ``Duplicate``: ' + Qgis.FieldDuplicatePolicy.Duplicate.__doc__ + '\n' + '* ``UnsetField``: ' + Qgis.FieldDuplicatePolicy.UnsetField.__doc__
# --
Qgis.FieldDuplicatePolicy.baseClass = Qgis
# monkey patching scoped based enum
Qgis.FieldDomainType.Coded.__doc__ = "Coded field domain"
Qgis.FieldDomainType.Range.__doc__ = "Numeric range field domain (min/max)"
Qgis.FieldDomainType.Glob.__doc__ = "Glob string pattern field domain"
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,13 @@ The development version
GeometryWeighted,
};

enum class FieldDuplicatePolicy
{
DefaultValue,
Duplicate,
UnsetField,
};

enum class FieldDomainType
{
Coded,
Expand Down
20 changes: 20 additions & 0 deletions python/core/auto_generated/qgsfield.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,26 @@ be handled during a split operation.
.. seealso:: :py:func:`splitPolicy`

.. versionadded:: 3.30
%End

Qgis::FieldDuplicatePolicy duplicatePolicy() const;
%Docstring
Returns the field's duplicate policy, which indicates how field values should
be handled during a duplicate operation.

.. seealso:: :py:func:`setDuplicatePolicy`

.. versionadded:: 3.38
%End

void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy );
%Docstring
Sets the field's duplicate ``policy``, which indicates how field values should
be handled during a duplicate operation.

.. seealso:: :py:func:`duplicatePolicy`

.. versionadded:: 3.38
%End

SIP_PYOBJECT __repr__();
Expand Down
21 changes: 21 additions & 0 deletions python/core/auto_generated/vector/qgsvectorlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,27 @@ Sets a split ``policy`` for the field with the specified index.
}
%End

void setFieldDuplicatePolicy( int index, Qgis::FieldDuplicatePolicy policy );
%Docstring
Sets a duplicate ``policy`` for the field with the specified index.

:raises KeyError: if no field with the specified index exists

.. versionadded:: 3.38
%End

%MethodCode
if ( a0 < 0 || a0 >= sipCpp->fields().count() )
{
PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
sipIsErr = 1;
}
else
{
sipCpp->setFieldDuplicatePolicy( a0, a1 );
}
%End

QSet<QString> excludeAttributesWms() const /Deprecated/;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -3218,6 +3218,21 @@ class CORE_EXPORT Qgis
};
Q_ENUM( FieldDomainMergePolicy )

/**
* Duplicate policy for fields.
*
* When a feature is duplicated, defines how the value of attributes are computed.
*
* \since QGIS 3.38
*/
enum class FieldDuplicatePolicy : int
{
DefaultValue, //!< Use default field value
Duplicate, //!< Duplicate original value
UnsetField, //!< Clears the field value so that the data provider backend will populate using any backend triggers or similar logic (since QGIS 3.30)
};
Q_ENUM( FieldDuplicatePolicy )

/**
* Types of field domain
*
Expand Down
20 changes: 19 additions & 1 deletion src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
return true;
}

QgsField::operator QVariant() const
{
return QVariant::fromValue( *this );
}

void QgsField::setEditorWidgetSetup( const QgsEditorWidgetSetup &v )
{
d->editorWidgetSetup = v;
Expand Down Expand Up @@ -736,6 +741,16 @@ void QgsField::setSplitPolicy( Qgis::FieldDomainSplitPolicy policy )
d->splitPolicy = policy;
}

Qgis::FieldDuplicatePolicy QgsField::duplicatePolicy() const
{
return d->duplicatePolicy;
}

void QgsField::setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy )
{
d->duplicatePolicy = policy;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
Expand Down Expand Up @@ -764,6 +779,7 @@ QDataStream &operator<<( QDataStream &out, const QgsField &field )
out << field.constraints().constraintDescription();
out << static_cast< quint32 >( field.subType() );
out << static_cast< int >( field.splitPolicy() );
out << static_cast< int >( field.duplicatePolicy() );
out << field.metadata();
return out;
}
Expand All @@ -782,6 +798,7 @@ QDataStream &operator>>( QDataStream &in, QgsField &field )
quint32 strengthUnique;
quint32 strengthExpression;
int splitPolicy;
int duplicatePolicy;

bool applyOnUpdate;

Expand All @@ -796,7 +813,7 @@ QDataStream &operator>>( QDataStream &in, QgsField &field )

in >> name >> type >> typeName >> length >> precision >> comment >> alias
>> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
constraintExpression >> constraintDescription >> subType >> splitPolicy >> metadata;
constraintExpression >> constraintDescription >> subType >> splitPolicy >> duplicatePolicy >> metadata;
field.setName( name );
field.setType( static_cast< QVariant::Type >( type ) );
field.setTypeName( typeName );
Expand All @@ -806,6 +823,7 @@ QDataStream &operator>>( QDataStream &in, QgsField &field )
field.setAlias( alias );
field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
field.setSplitPolicy( static_cast< Qgis::FieldDomainSplitPolicy >( splitPolicy ) );
field.setDuplicatePolicy( static_cast< Qgis::FieldDuplicatePolicy >( duplicatePolicy ) );
QgsFieldConstraints fieldConstraints;
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
{
Expand Down
25 changes: 21 additions & 4 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,7 @@ class CORE_EXPORT QgsField
#endif

//! Allows direct construction of QVariants from fields.
operator QVariant() const
{
return QVariant::fromValue( *this );
}
operator QVariant() const;

/**
* Set the editor widget setup for the field.
Expand Down Expand Up @@ -497,6 +494,26 @@ class CORE_EXPORT QgsField
*/
void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );

/**
* Returns the field's duplicate policy, which indicates how field values should
* be handled during a duplicate operation.
*
* \see setDuplicatePolicy()
*
* \since QGIS 3.38
*/
Qgis::FieldDuplicatePolicy duplicatePolicy() const;

/**
* Sets the field's duplicate \a policy, which indicates how field values should
* be handled during a duplicate operation.
*
* \see duplicatePolicy()
*
* \since QGIS 3.38
*/
void setDuplicatePolicy( Qgis::FieldDuplicatePolicy policy );

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsfield_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class QgsFieldPrivate : public QSharedData
, constraints( other.constraints )
, editorWidgetSetup( other.editorWidgetSetup )
, splitPolicy( other.splitPolicy )
, duplicatePolicy( other.duplicatePolicy )
, isReadOnly( other.isReadOnly )
{
}
Expand All @@ -99,6 +100,7 @@ class QgsFieldPrivate : public QSharedData
&& ( alias == other.alias ) && ( defaultValueDefinition == other.defaultValueDefinition )
&& ( constraints == other.constraints ) && ( flags == other.flags )
&& ( splitPolicy == other.splitPolicy )
&& ( duplicatePolicy == other.duplicatePolicy )
&& ( isReadOnly == other.isReadOnly )
&& ( editorWidgetSetup == other.editorWidgetSetup ) );
}
Expand Down Expand Up @@ -144,6 +146,9 @@ class QgsFieldPrivate : public QSharedData
//! Split policy
Qgis::FieldDomainSplitPolicy splitPolicy = Qgis::FieldDomainSplitPolicy::Duplicate;

//! Duplicate policy
Qgis::FieldDuplicatePolicy duplicatePolicy = Qgis::FieldDuplicatePolicy::Duplicate;

//! Read-only
bool isReadOnly = false;

Expand Down
Loading

0 comments on commit d0d2023

Please sign in to comment.