Skip to content
Permalink
Browse files

Move constraint handling to QgsFieldConstraints

Avoids cluttering QgsField API
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent 003fe18 commit cb94b68d88a6c721c1c81596019265805949dda9
Showing with 651 additions and 473 deletions.
  1. +1 −0 python/core/core.sip
  2. +4 −73 python/core/qgsfield.sip
  3. +110 −0 python/core/qgsfieldconstraints.sip
  4. +1 −1 python/core/qgsvectordataprovider.sip
  5. +2 −2 python/core/qgsvectorlayer.sip
  6. +1 −1 python/core/qgsvectorlayerutils.sip
  7. +3 −3 src/app/qgsattributetypedialog.cpp
  8. +1 −1 src/app/qgsattributetypedialog.h
  9. +19 −17 src/app/qgsfieldsproperties.cpp
  10. +1 −1 src/app/qgsfieldsproperties.h
  11. +2 −0 src/core/CMakeLists.txt
  12. +22 −66 src/core/qgsfield.cpp
  13. +6 −75 src/core/qgsfield.h
  14. +4 −19 src/core/qgsfield_p.h
  15. +69 −0 src/core/qgsfieldconstraints.cpp
  16. +145 −0 src/core/qgsfieldconstraints.h
  17. +2 −2 src/core/qgsvectordataprovider.cpp
  18. +1 −1 src/core/qgsvectordataprovider.h
  19. +25 −18 src/core/qgsvectorlayer.cpp
  20. +3 −3 src/core/qgsvectorlayer.h
  21. +11 −9 src/core/qgsvectorlayerutils.cpp
  22. +1 −1 src/core/qgsvectorlayerutils.h
  23. +1 −1 src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
  24. +5 −5 src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
  25. +1 −1 src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
  26. +3 −3 src/gui/qgsattributeform.cpp
  27. +3 −1 src/providers/ogr/qgsogrprovider.cpp
  28. +4 −2 src/providers/postgres/qgspostgresprovider.cpp
  29. +4 −2 src/providers/spatialite/qgsspatialiteprovider.cpp
  30. +60 −32 tests/src/core/testqgsfield.cpp
  31. +10 −10 tests/src/python/test_provider_ogr_sqlite.py
  32. +34 −33 tests/src/python/test_provider_postgres.py
  33. +31 −31 tests/src/python/test_provider_spatialite.py
  34. +53 −52 tests/src/python/test_qgsvectorlayer.py
  35. +8 −7 tests/src/python/test_qgsvectorlayerutils.py
@@ -56,6 +56,7 @@
%Include qgsfeaturerequest.sip
%Include qgsfeedback.sip
%Include qgsfield.sip
%Include qgsfieldconstraints.sip
%Include qgsfields.sip
%Include qgsgeometrysimplifier.sip
%Include qgsgeometryvalidator.sip
@@ -15,29 +15,6 @@ class QgsField

public:

/**
* Constraints which may be present on a field.
* @note added in QGIS 3.0
*/
enum Constraint
{
ConstraintNotNull, //!< Field may not be null
ConstraintUnique, //!< Field must have a unique value
ConstraintExpression, //!< Field has an expression constraint set. See constraintExpression().
};
typedef QFlags<QgsField::Constraint> Constraints;

/**
* Origin of constraints.
* @note added in QGIS 3.0
*/
enum ConstraintOrigin
{
ConstraintOriginNotSet, //!< Constraint is not set
ConstraintOriginProvider, //!< Constraint was set at data provider
ConstraintOriginLayer, //!< Constraint was set by layer
};

/** Constructor. Constructs a new QgsField object.
* @param name Field name
* @param type Field variant type, currently supported: String / Int / Double
@@ -187,63 +164,18 @@ class QgsField
void setDefaultValueExpression( const QString& expression );

/**
* Returns any constraints which are present for the field.
* Returns constraints which are present for the field.
* @note added in QGIS 3.0
* @see setConstraints()
* @see constraintOrigin()
*/
Constraints constraints() const;

/**
* Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint
* is not present on this field.
* @note added in QGIS 3.0
* @see constraints()
*/
ConstraintOrigin constraintOrigin( Constraint constraint ) const;

/**
* Sets a constraint on the field.
* @note added in QGIS 3.0
* @see constraints()
* @see removeConstraint()
*/
void setConstraint( Constraint constraint, ConstraintOrigin origin = ConstraintOriginLayer );

/**
* Removes a constraint from the field.
* @see setConstraint()
* @see constraints()
*/
void removeConstraint( Constraint constraint );

/**
* Returns the constraint expression for the field, if set.
* @note added in QGIS 3.0
* @see constraints()
* @see constraintDescription()
* @see setConstraintExpression()
*/
QString constraintExpression() const;

/**
* Returns the descriptive name for the constraint expression.
* @note added in QGIS 3.0
* @see constraints()
* @see constraintExpression()
* @see setConstraintExpression()
*/
QString constraintDescription() const;
const QgsFieldConstraints& constraints() const;

/**
* Set the constraint expression for the field. An optional descriptive name for the constraint
* can also be set. Setting an empty expression will clear any existing expression constraint.
* Sets constraints which are present for the field.
* @note added in QGIS 3.0
* @see constraintExpression()
* @see constraintDescription()
* @see constraints()
*/
void setConstraintExpression( const QString& expression, const QString& description = QString() );
void setConstraints( const QgsFieldConstraints& constraints );

/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
@@ -338,4 +270,3 @@ class QgsField
const QgsEditorWidgetSetup& editorWidgetSetup() const;
}; // class QgsField

QFlags<QgsField::Constraint> operator|(QgsField::Constraint f1, QFlags<QgsField::Constraint> f2);
@@ -0,0 +1,110 @@
/**
* \class QgsFieldConstraints
* \ingroup core
* Stores information about constraints which may be present on a field.
* \note added in QGIS 3.0
*/


class QgsFieldConstraints
{

%TypeHeaderCode
#include <qgsfieldconstraints.h>
%End

public:

/**
* Constraints which may be present on a field.
*/
enum Constraint
{
ConstraintNotNull, //!< Field may not be null
ConstraintUnique, //!< Field must have a unique value
ConstraintExpression, //!< Field has an expression constraint set. See constraintExpression().
};
typedef QFlags<QgsFieldConstraints::Constraint> Constraints;

/**
* Origin of constraints.
*/
enum ConstraintOrigin
{
ConstraintOriginNotSet, //!< Constraint is not set
ConstraintOriginProvider, //!< Constraint was set at data provider
ConstraintOriginLayer, //!< Constraint was set by layer
};

/**
* Strength of constraints.
*/
enum ConstraintStrength
{
ConstraintHard, //!< Constraint must be honored before feature can be accepted
ConstraintSoft, //!< User is warned if constraint is violated but feature can still be accepted
};

/**
* Constructor for QgsFieldConstraints.
*/
QgsFieldConstraints();

/**
* Returns any constraints which are present for the field.
* @see setConstraints()
* @see constraintOrigin()
*/
Constraints constraints() const;

/**
* Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint
* is not present on this field.
* @see constraints()
*/
ConstraintOrigin constraintOrigin( Constraint constraint ) const;

/**
* Sets a constraint on the field.
* @see constraints()
* @see removeConstraint()
*/
void setConstraint( Constraint constraint, ConstraintOrigin origin = ConstraintOriginLayer );

/**
* Removes a constraint from the field.
* @see setConstraint()
* @see constraints()
*/
void removeConstraint( Constraint constraint );

/**
* Returns the constraint expression for the field, if set.
* @see constraints()
* @see constraintDescription()
* @see setConstraintExpression()
*/
QString constraintExpression() const;

/**
* Returns the descriptive name for the constraint expression.
* @see constraints()
* @see constraintExpression()
* @see setConstraintExpression()
*/
QString constraintDescription() const;

/**
* Set the constraint expression for the field. An optional descriptive name for the constraint
* can also be set. Setting an empty expression will clear any existing expression constraint.
* @see constraintExpression()
* @see constraintDescription()
* @see constraints()
*/
void setConstraintExpression( const QString& expression, const QString& description = QString() );

bool operator==( const QgsFieldConstraints& other ) const;

};

QFlags<QgsFieldConstraints::Constraint> operator|(QgsFieldConstraints::Constraint f1, QFlags<QgsFieldConstraints::Constraint> f2);
@@ -235,7 +235,7 @@ class QgsVectorDataProvider : QgsDataProvider
* field index.
* @note added in QGIS 3.0
*/
QgsField::Constraints fieldConstraints( int fieldIndex ) const;
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;

/**
* Changes geometries of existing features
@@ -1264,7 +1264,7 @@ class QgsVectorLayer : QgsMapLayer
* @note added in QGIS 3.0
* @see setFieldConstraints()
*/
QgsField::Constraints fieldConstraints( int fieldIndex ) const;
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;

/**
* Sets the constraints for a specified field index. Any constraints inherited from the layer's
@@ -1273,7 +1273,7 @@ class QgsVectorLayer : QgsMapLayer
* @note added in QGIS 3.0
* @see fieldConstraints()
*/
void setFieldConstraints( int index, QgsField::Constraints constraints );
void setFieldConstraints( int index, QgsFieldConstraints::Constraints constraints );

/**
* Returns the constraint expression for for a specified field index, if set.
@@ -25,6 +25,6 @@ class QgsVectorLayerUtils
* If the origin parameter is set then only constraints with a matching origin will be checked.
*/
static bool validateAttribute( const QgsVectorLayer* layer, const QgsFeature& feature, int attributeIndex, QStringList& errors /Out/,
QgsField::ConstraintOrigin origin = QgsField::ConstraintOriginNotSet );
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );

};
@@ -176,16 +176,16 @@ bool QgsAttributeTypeDialog::fieldEditable() const
return isFieldEditableCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setProviderConstraints( QgsField::Constraints constraints )
void QgsAttributeTypeDialog::setProviderConstraints( QgsFieldConstraints::Constraints constraints )
{
if ( constraints & QgsField::ConstraintNotNull )
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
{
notNullCheckBox->setChecked( true );
notNullCheckBox->setEnabled( false );
notNullCheckBox->setToolTip( tr( "The provider for this layer has a NOT NULL constraint set on the field." ) );
}

if ( constraints & QgsField::ConstraintUnique )
if ( constraints & QgsFieldConstraints::ConstraintUnique )
{
mUniqueCheckBox->setChecked( true );
mUniqueCheckBox->setEnabled( false );
@@ -73,7 +73,7 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
/**
* Sets any provider side constraints which may affect this field's behaviour.
*/
void setProviderConstraints( QgsField::Constraints constraints );
void setProviderConstraints( QgsFieldConstraints::Constraints constraints );

/**
* Setter for checkbox for not null
@@ -558,16 +558,17 @@ void QgsFieldsProperties::attributeTypeDialog()

attributeTypeDialog.setFieldEditable( cfg.mEditable );
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsField::ConstraintNotNull );
attributeTypeDialog.setUnique( cfg.mConstraints & QgsField::ConstraintUnique );

QgsField::Constraints providerConstraints = 0;
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintNotNull ) == QgsField::ConstraintOriginProvider )
providerConstraints |= QgsField::ConstraintNotNull;
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintUnique ) == QgsField::ConstraintOriginProvider )
providerConstraints |= QgsField::ConstraintUnique;
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintExpression ) == QgsField::ConstraintOriginProvider )
providerConstraints |= QgsField::ConstraintExpression;
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull );
attributeTypeDialog.setUnique( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique );

QgsFieldConstraints constraints = mLayer->fields().at( index ).constraints();
QgsFieldConstraints::Constraints providerConstraints = 0;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintNotNull;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintUnique ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintUnique;
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintExpression ) == QgsFieldConstraints::ConstraintOriginProvider )
providerConstraints |= QgsFieldConstraints::ConstraintExpression;
attributeTypeDialog.setProviderConstraints( providerConstraints );

attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
@@ -584,13 +585,13 @@ void QgsFieldsProperties::attributeTypeDialog()
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();

cfg.mConstraints = 0;
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsField::ConstraintNotNull ) )
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsFieldConstraints::ConstraintNotNull ) )
{
cfg.mConstraints |= QgsField::ConstraintNotNull;
cfg.mConstraints |= QgsFieldConstraints::ConstraintNotNull;
}
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsField::ConstraintUnique ) )
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsFieldConstraints::ConstraintUnique ) )
{
cfg.mConstraints |= QgsField::ConstraintUnique;
cfg.mConstraints |= QgsFieldConstraints::ConstraintUnique;
}

cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
@@ -1060,9 +1061,10 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
mEditableEnabled = layer->fields().fieldOrigin( idx ) != QgsFields::OriginJoin
&& layer->fields().fieldOrigin( idx ) != QgsFields::OriginExpression;
mLabelOnTop = layer->editFormConfig().labelOnTop( idx );
mConstraints = layer->fields().at( idx ).constraints();
mConstraint = layer->fields().at( idx ).constraintExpression();
mConstraintDescription = layer->fields().at( idx ).constraintDescription();
QgsFieldConstraints constraints = layer->fields().at( idx ).constraints();
mConstraints = constraints.constraints();
mConstraint = constraints.constraintExpression();
mConstraintDescription = constraints.constraintDescription();
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer, layer->fields().field( idx ).name() );
mEditorWidgetType = setup.type();
mEditorWidgetConfig = setup.config();
@@ -122,7 +122,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
bool mEditable;
bool mEditableEnabled;
bool mLabelOnTop;
QgsField::Constraints mConstraints;
QgsFieldConstraints::Constraints mConstraints;
QString mConstraint;
QString mConstraintDescription;
QPushButton* mButton;
@@ -123,6 +123,7 @@ SET(QGIS_CORE_SRCS
qgsfeaturerequest.cpp
qgsfeaturestore.cpp
qgsfield.cpp
qgsfieldconstraints.cpp
qgsfields.cpp
qgsfontutils.cpp
qgsgeometrycache.cpp
@@ -459,6 +460,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsfeature.h
qgsfeedback.h
qgsfield.h
qgsfieldconstraints.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h

0 comments on commit cb94b68

Please sign in to comment.
You can’t perform that action at this time.