|
| 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 | + ConstraintStrengthNotSet, //!< Constraint is not set |
| 45 | + ConstraintStrengthHard, //!< Constraint must be honored before feature can be accepted |
| 46 | + ConstraintStrengthSoft, //!< User is warned if constraint is violated but feature can still be accepted |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor for QgsFieldConstraints. |
| 51 | + */ |
| 52 | + QgsFieldConstraints(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns any constraints which are present for the field. |
| 56 | + * @see setConstraints() |
| 57 | + * @see constraintOrigin() |
| 58 | + */ |
| 59 | + Constraints constraints() const; |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint |
| 63 | + * is not present on this field. |
| 64 | + * @see constraints() |
| 65 | + */ |
| 66 | + ConstraintOrigin constraintOrigin( Constraint constraint ) const; |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint |
| 70 | + * is not present on this field. |
| 71 | + * @see constraints() |
| 72 | + * @see setConstraintStrength() |
| 73 | + */ |
| 74 | + ConstraintStrength constraintStrength( Constraint constraint ) const; |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the strength of a constraint. Note that the strength of constraints which originate |
| 78 | + * from a provider cannot be changed. Constraints default to ConstraintStrengthHard unless |
| 79 | + * explicitly changed. |
| 80 | + * @see constraintStrength() |
| 81 | + */ |
| 82 | + void setConstraintStrength( Constraint constraint, ConstraintStrength strength ); |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets a constraint on the field. |
| 86 | + * @see constraints() |
| 87 | + * @see removeConstraint() |
| 88 | + */ |
| 89 | + void setConstraint( Constraint constraint, ConstraintOrigin origin = ConstraintOriginLayer ); |
| 90 | + |
| 91 | + /** |
| 92 | + * Removes a constraint from the field. |
| 93 | + * @see setConstraint() |
| 94 | + * @see constraints() |
| 95 | + */ |
| 96 | + void removeConstraint( Constraint constraint ); |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the constraint expression for the field, if set. |
| 100 | + * @see constraints() |
| 101 | + * @see constraintDescription() |
| 102 | + * @see setConstraintExpression() |
| 103 | + */ |
| 104 | + QString constraintExpression() const; |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the descriptive name for the constraint expression. |
| 108 | + * @see constraints() |
| 109 | + * @see constraintExpression() |
| 110 | + * @see setConstraintExpression() |
| 111 | + */ |
| 112 | + QString constraintDescription() const; |
| 113 | + |
| 114 | + /** |
| 115 | + * Set the constraint expression for the field. An optional descriptive name for the constraint |
| 116 | + * can also be set. Setting an empty expression will clear any existing expression constraint. |
| 117 | + * @see constraintExpression() |
| 118 | + * @see constraintDescription() |
| 119 | + * @see constraints() |
| 120 | + */ |
| 121 | + void setConstraintExpression( const QString& expression, const QString& description = QString() ); |
| 122 | + |
| 123 | + bool operator==( const QgsFieldConstraints& other ) const; |
| 124 | + |
| 125 | +}; |
| 126 | + |
| 127 | +QFlags<QgsFieldConstraints::Constraint> operator|(QgsFieldConstraints::Constraint f1, QFlags<QgsFieldConstraints::Constraint> f2); |
0 commit comments