QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsfieldconstraints.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldconstraints.cpp
3  -----------------------
4  Date : November 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsfieldconstraints.h"
16 
18 
20 {
21  if ( !( mConstraints & constraint ) )
23 
24  return mConstraintOrigins.value( constraint, ConstraintOriginNotSet );
25 }
26 
28 {
29  if ( !( mConstraints & constraint ) )
31 
32  // defaults to hard strength unless explicitly set
33  return mConstraintStrengths.value( constraint, ConstraintStrengthHard );
34 }
35 
37 {
38  if ( constraintOrigin( constraint ) == ConstraintOriginProvider )
39  {
40  // cannot be overwritten
41  return;
42  }
43  else if ( strength == ConstraintStrengthNotSet )
44  {
45  mConstraintStrengths.remove( constraint );
46  }
47  else
48  {
49  mConstraintStrengths.insert( constraint, strength );
50  }
51 }
52 
54 {
55  if ( origin == ConstraintOriginNotSet )
56  {
57  mConstraints &= ~constraint;
58  mConstraintOrigins.remove( constraint );
59  mConstraintStrengths.remove( constraint );
60  }
61  else
62  {
63  mConstraints |= constraint;
64  mConstraintOrigins.insert( constraint, origin );
65  if ( !mConstraintStrengths.contains( constraint ) || origin == ConstraintOriginProvider )
66  {
67  mConstraintStrengths.insert( constraint, ConstraintStrengthHard );
68  }
69  }
70 }
71 
73 {
74  return mConstraints & QgsFieldConstraints::ConstraintExpression ? mExpressionConstraint : QString();
75 }
76 
77 void QgsFieldConstraints::setConstraintExpression( const QString &expression, const QString &description )
78 {
79  if ( expression.isEmpty() )
81  else
82  {
85  }
86 
87  mExpressionConstraint = expression;
88  mExpressionConstraintDescription = description;
89 }
90 
92 {
93  return mConstraints == other.mConstraints && mConstraintOrigins == other.mConstraintOrigins
94  && mExpressionConstraint == other.mExpressionConstraint && mExpressionConstraintDescription == other.mExpressionConstraintDescription
95  && mConstraintStrengths == other.mConstraintStrengths;
96 }
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
Constraint
Constraints which may be present on a field.
ConstraintOrigin
Origin of constraints.
Stores information about constraints which may be present on a field.
Constraint was set at data provider.
Field has an expression constraint set. See constraintExpression().
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
ConstraintStrength
Strength of constraints.
bool operator==(const QgsFieldConstraints &other) const
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QgsFieldConstraints()
Constructor for QgsFieldConstraints.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
Constraint must be honored before feature can be accepted.