28 , mValidConstraint( true )
29 , mIsBlockingCommit( false )
30 , mFieldIdx( fieldIdx )
42 if ( vl && mFieldIdx < vl->fields().count() )
65 wdg->setEnabled( enabled );
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 mConstraintResult;
112 return mConstraintResultVisible;
117 if ( mConstraintResultVisible == constraintResultVisible )
135 QStringList softErrors;
136 QStringList expressions;
137 QStringList descriptions;
138 bool toEmit(
false );
139 bool hardConstraintsOk(
true );
140 bool softConstraintsOk(
true );
147 if ( ! expression.isEmpty() )
149 expressions << expression;
156 descriptions << tr(
"Not NULL" );
157 if ( !expression.isEmpty() )
159 expressions << field.
name() + QStringLiteral(
" IS NOT NULL" );
163 expressions << QStringLiteral(
"IS NOT NULL" );
170 descriptions << tr(
"Unique" );
171 if ( !expression.isEmpty() )
173 expressions << field.
name() + QStringLiteral(
" IS UNIQUE" );
177 expressions << QStringLiteral(
"IS UNIQUE" );
185 errors << softErrors;
189 if ( ! expression.isEmpty() )
191 hardConstraintsOk =
true;
192 softConstraintsOk =
false;
194 errors << QStringLiteral(
"Invalid feature" );
200 mValidConstraint = hardConstraintsOk && softConstraintsOk;
201 mIsBlockingCommit = !hardConstraintsOk;
203 mConstraintFailureReason = errors.join( QStringLiteral(
", " ) );
207 QString errStr = errors.isEmpty() ? tr(
"Constraint checks passed" ) : mConstraintFailureReason;
209 QString description = descriptions.join( QStringLiteral(
", " ) );
210 QString expressionDesc;
211 if ( expressions.size() > 1 )
212 expressionDesc =
"( " + expressions.join( QStringLiteral(
" ) AND ( " ) ) +
" )";
213 else if ( !expressions.isEmpty() )
214 expressionDesc = expressions.at( 0 );
219 mConstraintResult = result;
227 return mValidConstraint;
232 return mIsBlockingCommit;
237 return mConstraintFailureReason;
242 if ( !parent )
return false;
243 if ( qobject_cast<const QTableView *>( parent ) )
return true;
244 return isInTable( parent->parentWidget() );
249 widget()->setToolTip( hintText );
bool isValid() const
Returns the validity of this feature.
ConstraintOrigin
Origin of constraints.
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
Get 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.