28 , mFieldIdx( fieldIdx )
29 , mValidConstraint( true )
30 , mIsBlockingCommit( false )
42 if ( vl && mFieldIdx < vl->fields().count() )
65 wdg->setEnabled( enabled );
71 mFormFeature = feature;
82 if ( !mConstraintResultVisible )
84 widget()->setStyleSheet( QString() );
88 switch ( mConstraintResult )
91 widget()->setStyleSheet( QString() );
95 widget()->setStyleSheet( QStringLiteral(
"background-color: #FFE0B2;" ) );
99 widget()->setStyleSheet( QStringLiteral(
"background-color: #FFECB3;" ) );
107 return mFormFeature.
setAttribute( attributeName, attributeValue );
112 return mConstraintResult;
117 return mConstraintResultVisible;
122 if ( mConstraintResultVisible == constraintResultVisible )
140 QStringList softErrors;
141 QStringList expressions;
142 QStringList descriptions;
143 bool toEmit(
false );
144 bool hardConstraintsOk(
true );
145 bool softConstraintsOk(
true );
152 if ( ! expression.isEmpty() )
154 expressions << expression;
161 descriptions << tr(
"Not NULL" );
162 if ( !expression.isEmpty() )
164 expressions << field.
name() + QStringLiteral(
" IS NOT NULL" );
168 expressions << QStringLiteral(
"IS NOT NULL" );
175 descriptions << tr(
"Unique" );
176 if ( !expression.isEmpty() )
178 expressions << field.
name() + QStringLiteral(
" IS UNIQUE" );
182 expressions << QStringLiteral(
"IS UNIQUE" );
190 errors << softErrors;
194 if ( ! expression.isEmpty() )
196 hardConstraintsOk =
true;
197 softConstraintsOk =
false;
199 errors << QStringLiteral(
"Invalid feature" );
205 mValidConstraint = hardConstraintsOk && softConstraintsOk;
206 mIsBlockingCommit = !hardConstraintsOk;
208 mConstraintFailureReason = errors.join( QStringLiteral(
", " ) );
212 QString errStr = errors.isEmpty() ? tr(
"Constraint checks passed" ) : mConstraintFailureReason;
214 QString description = descriptions.join( QStringLiteral(
", " ) );
215 QString expressionDesc;
216 if ( expressions.size() > 1 )
217 expressionDesc =
"( " + expressions.join( QStringLiteral(
" ) AND ( " ) ) +
" )";
218 else if ( !expressions.isEmpty() )
219 expressionDesc = expressions.at( 0 );
224 mConstraintResult = result;
232 return mValidConstraint;
237 return mIsBlockingCommit;
242 return mConstraintFailureReason;
247 if ( !parent )
return false;
248 if ( qobject_cast<const QTableView *>( parent ) )
return true;
249 return isInTable( parent->parentWidget() );
254 widget()->setToolTip( hintText );
bool isValid() const
Returns the validity of this feature.
ConstraintOrigin
Origin of constraints.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static bool validateAttribute(const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin=QgsFieldConstraints::ConstraintOriginNotSet)
Tests an attribute value to check whether it passes all constraints which are present on the correspo...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QgsFields fields() const override
Returns the list of fields of this layer.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
Encapsulate a field in an attribute table or data source.
User is warned if constraint is violated but feature can still be accepted.
QgsFieldConstraints constraints
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Constraint must be honored before feature can be accepted.
Field must have a unique value.