Skip to content

Commit cb94b68

Browse files
committedNov 2, 2016
Move constraint handling to QgsFieldConstraints
Avoids cluttering QgsField API
1 parent 003fe18 commit cb94b68

35 files changed

+651
-473
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
%Include qgsfeaturerequest.sip
5757
%Include qgsfeedback.sip
5858
%Include qgsfield.sip
59+
%Include qgsfieldconstraints.sip
5960
%Include qgsfields.sip
6061
%Include qgsgeometrysimplifier.sip
6162
%Include qgsgeometryvalidator.sip

‎python/core/qgsfield.sip

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,6 @@ class QgsField
1515

1616
public:
1717

18-
/**
19-
* Constraints which may be present on a field.
20-
* @note added in QGIS 3.0
21-
*/
22-
enum Constraint
23-
{
24-
ConstraintNotNull, //!< Field may not be null
25-
ConstraintUnique, //!< Field must have a unique value
26-
ConstraintExpression, //!< Field has an expression constraint set. See constraintExpression().
27-
};
28-
typedef QFlags<QgsField::Constraint> Constraints;
29-
30-
/**
31-
* Origin of constraints.
32-
* @note added in QGIS 3.0
33-
*/
34-
enum ConstraintOrigin
35-
{
36-
ConstraintOriginNotSet, //!< Constraint is not set
37-
ConstraintOriginProvider, //!< Constraint was set at data provider
38-
ConstraintOriginLayer, //!< Constraint was set by layer
39-
};
40-
4118
/** Constructor. Constructs a new QgsField object.
4219
* @param name Field name
4320
* @param type Field variant type, currently supported: String / Int / Double
@@ -187,63 +164,18 @@ class QgsField
187164
void setDefaultValueExpression( const QString& expression );
188165

189166
/**
190-
* Returns any constraints which are present for the field.
167+
* Returns constraints which are present for the field.
191168
* @note added in QGIS 3.0
192169
* @see setConstraints()
193-
* @see constraintOrigin()
194-
*/
195-
Constraints constraints() const;
196-
197-
/**
198-
* Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint
199-
* is not present on this field.
200-
* @note added in QGIS 3.0
201-
* @see constraints()
202-
*/
203-
ConstraintOrigin constraintOrigin( Constraint constraint ) const;
204-
205-
/**
206-
* Sets a constraint on the field.
207-
* @note added in QGIS 3.0
208-
* @see constraints()
209-
* @see removeConstraint()
210-
*/
211-
void setConstraint( Constraint constraint, ConstraintOrigin origin = ConstraintOriginLayer );
212-
213-
/**
214-
* Removes a constraint from the field.
215-
* @see setConstraint()
216-
* @see constraints()
217-
*/
218-
void removeConstraint( Constraint constraint );
219-
220-
/**
221-
* Returns the constraint expression for the field, if set.
222-
* @note added in QGIS 3.0
223-
* @see constraints()
224-
* @see constraintDescription()
225-
* @see setConstraintExpression()
226-
*/
227-
QString constraintExpression() const;
228-
229-
/**
230-
* Returns the descriptive name for the constraint expression.
231-
* @note added in QGIS 3.0
232-
* @see constraints()
233-
* @see constraintExpression()
234-
* @see setConstraintExpression()
235170
*/
236-
QString constraintDescription() const;
171+
const QgsFieldConstraints& constraints() const;
237172

238173
/**
239-
* Set the constraint expression for the field. An optional descriptive name for the constraint
240-
* can also be set. Setting an empty expression will clear any existing expression constraint.
174+
* Sets constraints which are present for the field.
241175
* @note added in QGIS 3.0
242-
* @see constraintExpression()
243-
* @see constraintDescription()
244176
* @see constraints()
245177
*/
246-
void setConstraintExpression( const QString& expression, const QString& description = QString() );
178+
void setConstraints( const QgsFieldConstraints& constraints );
247179

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

341-
QFlags<QgsField::Constraint> operator|(QgsField::Constraint f1, QFlags<QgsField::Constraint> f2);

‎python/core/qgsfieldconstraints.sip

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* \class QgsFieldConstraints
3+
* \ingroup core
4+
* Stores information about constraints which may be present on a field.
5+
* \note added in QGIS 3.0
6+
*/
7+
8+
9+
class QgsFieldConstraints
10+
{
11+
12+
%TypeHeaderCode
13+
#include <qgsfieldconstraints.h>
14+
%End
15+
16+
public:
17+
18+
/**
19+
* Constraints which may be present on a field.
20+
*/
21+
enum Constraint
22+
{
23+
ConstraintNotNull, //!< Field may not be null
24+
ConstraintUnique, //!< Field must have a unique value
25+
ConstraintExpression, //!< Field has an expression constraint set. See constraintExpression().
26+
};
27+
typedef QFlags<QgsFieldConstraints::Constraint> Constraints;
28+
29+
/**
30+
* Origin of constraints.
31+
*/
32+
enum ConstraintOrigin
33+
{
34+
ConstraintOriginNotSet, //!< Constraint is not set
35+
ConstraintOriginProvider, //!< Constraint was set at data provider
36+
ConstraintOriginLayer, //!< Constraint was set by layer
37+
};
38+
39+
/**
40+
* Strength of constraints.
41+
*/
42+
enum ConstraintStrength
43+
{
44+
ConstraintHard, //!< Constraint must be honored before feature can be accepted
45+
ConstraintSoft, //!< User is warned if constraint is violated but feature can still be accepted
46+
};
47+
48+
/**
49+
* Constructor for QgsFieldConstraints.
50+
*/
51+
QgsFieldConstraints();
52+
53+
/**
54+
* Returns any constraints which are present for the field.
55+
* @see setConstraints()
56+
* @see constraintOrigin()
57+
*/
58+
Constraints constraints() const;
59+
60+
/**
61+
* Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint
62+
* is not present on this field.
63+
* @see constraints()
64+
*/
65+
ConstraintOrigin constraintOrigin( Constraint constraint ) const;
66+
67+
/**
68+
* Sets a constraint on the field.
69+
* @see constraints()
70+
* @see removeConstraint()
71+
*/
72+
void setConstraint( Constraint constraint, ConstraintOrigin origin = ConstraintOriginLayer );
73+
74+
/**
75+
* Removes a constraint from the field.
76+
* @see setConstraint()
77+
* @see constraints()
78+
*/
79+
void removeConstraint( Constraint constraint );
80+
81+
/**
82+
* Returns the constraint expression for the field, if set.
83+
* @see constraints()
84+
* @see constraintDescription()
85+
* @see setConstraintExpression()
86+
*/
87+
QString constraintExpression() const;
88+
89+
/**
90+
* Returns the descriptive name for the constraint expression.
91+
* @see constraints()
92+
* @see constraintExpression()
93+
* @see setConstraintExpression()
94+
*/
95+
QString constraintDescription() const;
96+
97+
/**
98+
* Set the constraint expression for the field. An optional descriptive name for the constraint
99+
* can also be set. Setting an empty expression will clear any existing expression constraint.
100+
* @see constraintExpression()
101+
* @see constraintDescription()
102+
* @see constraints()
103+
*/
104+
void setConstraintExpression( const QString& expression, const QString& description = QString() );
105+
106+
bool operator==( const QgsFieldConstraints& other ) const;
107+
108+
};
109+
110+
QFlags<QgsFieldConstraints::Constraint> operator|(QgsFieldConstraints::Constraint f1, QFlags<QgsFieldConstraints::Constraint> f2);

‎python/core/qgsvectordataprovider.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class QgsVectorDataProvider : QgsDataProvider
235235
* field index.
236236
* @note added in QGIS 3.0
237237
*/
238-
QgsField::Constraints fieldConstraints( int fieldIndex ) const;
238+
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
239239

240240
/**
241241
* Changes geometries of existing features

‎python/core/qgsvectorlayer.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ class QgsVectorLayer : QgsMapLayer
12641264
* @note added in QGIS 3.0
12651265
* @see setFieldConstraints()
12661266
*/
1267-
QgsField::Constraints fieldConstraints( int fieldIndex ) const;
1267+
QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
12681268

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

12781278
/**
12791279
* Returns the constraint expression for for a specified field index, if set.

‎python/core/qgsvectorlayerutils.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class QgsVectorLayerUtils
2525
* If the origin parameter is set then only constraints with a matching origin will be checked.
2626
*/
2727
static bool validateAttribute( const QgsVectorLayer* layer, const QgsFeature& feature, int attributeIndex, QStringList& errors /Out/,
28-
QgsField::ConstraintOrigin origin = QgsField::ConstraintOriginNotSet );
28+
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );
2929

3030
};

‎src/app/qgsattributetypedialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,16 @@ bool QgsAttributeTypeDialog::fieldEditable() const
176176
return isFieldEditableCheckBox->isChecked();
177177
}
178178

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

188-
if ( constraints & QgsField::ConstraintUnique )
188+
if ( constraints & QgsFieldConstraints::ConstraintUnique )
189189
{
190190
mUniqueCheckBox->setChecked( true );
191191
mUniqueCheckBox->setEnabled( false );

‎src/app/qgsattributetypedialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
7373
/**
7474
* Sets any provider side constraints which may affect this field's behaviour.
7575
*/
76-
void setProviderConstraints( QgsField::Constraints constraints );
76+
void setProviderConstraints( QgsFieldConstraints::Constraints constraints );
7777

7878
/**
7979
* Setter for checkbox for not null

‎src/app/qgsfieldsproperties.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,17 @@ void QgsFieldsProperties::attributeTypeDialog()
558558

559559
attributeTypeDialog.setFieldEditable( cfg.mEditable );
560560
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
561-
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsField::ConstraintNotNull );
562-
attributeTypeDialog.setUnique( cfg.mConstraints & QgsField::ConstraintUnique );
563-
564-
QgsField::Constraints providerConstraints = 0;
565-
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintNotNull ) == QgsField::ConstraintOriginProvider )
566-
providerConstraints |= QgsField::ConstraintNotNull;
567-
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintUnique ) == QgsField::ConstraintOriginProvider )
568-
providerConstraints |= QgsField::ConstraintUnique;
569-
if ( mLayer->fields().at( index ).constraintOrigin( QgsField::ConstraintExpression ) == QgsField::ConstraintOriginProvider )
570-
providerConstraints |= QgsField::ConstraintExpression;
561+
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull );
562+
attributeTypeDialog.setUnique( cfg.mConstraints & QgsFieldConstraints::ConstraintUnique );
563+
564+
QgsFieldConstraints constraints = mLayer->fields().at( index ).constraints();
565+
QgsFieldConstraints::Constraints providerConstraints = 0;
566+
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) == QgsFieldConstraints::ConstraintOriginProvider )
567+
providerConstraints |= QgsFieldConstraints::ConstraintNotNull;
568+
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintUnique ) == QgsFieldConstraints::ConstraintOriginProvider )
569+
providerConstraints |= QgsFieldConstraints::ConstraintUnique;
570+
if ( constraints.constraintOrigin( QgsFieldConstraints::ConstraintExpression ) == QgsFieldConstraints::ConstraintOriginProvider )
571+
providerConstraints |= QgsFieldConstraints::ConstraintExpression;
571572
attributeTypeDialog.setProviderConstraints( providerConstraints );
572573

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

586587
cfg.mConstraints = 0;
587-
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsField::ConstraintNotNull ) )
588+
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsFieldConstraints::ConstraintNotNull ) )
588589
{
589-
cfg.mConstraints |= QgsField::ConstraintNotNull;
590+
cfg.mConstraints |= QgsFieldConstraints::ConstraintNotNull;
590591
}
591-
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsField::ConstraintUnique ) )
592+
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsFieldConstraints::ConstraintUnique ) )
592593
{
593-
cfg.mConstraints |= QgsField::ConstraintUnique;
594+
cfg.mConstraints |= QgsFieldConstraints::ConstraintUnique;
594595
}
595596

596597
cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
@@ -1060,9 +1061,10 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
10601061
mEditableEnabled = layer->fields().fieldOrigin( idx ) != QgsFields::OriginJoin
10611062
&& layer->fields().fieldOrigin( idx ) != QgsFields::OriginExpression;
10621063
mLabelOnTop = layer->editFormConfig().labelOnTop( idx );
1063-
mConstraints = layer->fields().at( idx ).constraints();
1064-
mConstraint = layer->fields().at( idx ).constraintExpression();
1065-
mConstraintDescription = layer->fields().at( idx ).constraintDescription();
1064+
QgsFieldConstraints constraints = layer->fields().at( idx ).constraints();
1065+
mConstraints = constraints.constraints();
1066+
mConstraint = constraints.constraintExpression();
1067+
mConstraintDescription = constraints.constraintDescription();
10661068
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer, layer->fields().field( idx ).name() );
10671069
mEditorWidgetType = setup.type();
10681070
mEditorWidgetConfig = setup.config();

‎src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
122122
bool mEditable;
123123
bool mEditableEnabled;
124124
bool mLabelOnTop;
125-
QgsField::Constraints mConstraints;
125+
QgsFieldConstraints::Constraints mConstraints;
126126
QString mConstraint;
127127
QString mConstraintDescription;
128128
QPushButton* mButton;

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ SET(QGIS_CORE_SRCS
123123
qgsfeaturerequest.cpp
124124
qgsfeaturestore.cpp
125125
qgsfield.cpp
126+
qgsfieldconstraints.cpp
126127
qgsfields.cpp
127128
qgsfontutils.cpp
128129
qgsgeometrycache.cpp
@@ -459,6 +460,7 @@ SET(QGIS_CORE_MOC_HDRS
459460
qgsfeature.h
460461
qgsfeedback.h
461462
qgsfield.h
463+
qgsfieldconstraints.h
462464
qgsgeometryvalidator.h
463465
qgsgml.h
464466
qgsgmlschema.h

0 commit comments

Comments
 (0)
Please sign in to comment.