Skip to content

Commit

Permalink
Move constraint handling to QgsFieldConstraints
Browse files Browse the repository at this point in the history
Avoids cluttering QgsField API
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent 003fe18 commit cb94b68
Show file tree
Hide file tree
Showing 35 changed files with 651 additions and 473 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 4 additions & 73 deletions python/core/qgsfield.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -338,4 +270,3 @@ class QgsField
const QgsEditorWidgetSetup& editorWidgetSetup() const;
}; // class QgsField

QFlags<QgsField::Constraint> operator|(QgsField::Constraint f1, QFlags<QgsField::Constraint> f2);
110 changes: 110 additions & 0 deletions python/core/qgsfieldconstraints.sip
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion python/core/qgsvectordataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayerutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

};
6 changes: 3 additions & 3 deletions src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetypedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 19 additions & 17 deletions src/app/qgsfieldsproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ SET(QGIS_CORE_SRCS
qgsfeaturerequest.cpp
qgsfeaturestore.cpp
qgsfield.cpp
qgsfieldconstraints.cpp
qgsfields.cpp
qgsfontutils.cpp
qgsgeometrycache.cpp
Expand Down Expand Up @@ -459,6 +460,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsfeature.h
qgsfeedback.h
qgsfield.h
qgsfieldconstraints.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
Expand Down
Loading

0 comments on commit cb94b68

Please sign in to comment.