Skip to content

Commit 1221353

Browse files
committed
Refactor code around constraints
1 parent 7be7f7d commit 1221353

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

app/attributes/fieldvalidator.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "featurelayerpair.h"
1313
#include "mixedattributevalue.h"
1414

15-
#include "qgsfield.h"
16-
#include "qgsvectorlayerutils.h"
15+
#include <qgsfield.h>
16+
#include <qgsvectorlayerutils.h>
1717

1818
#include <QRegularExpression>
1919
#include <QLocale>
@@ -56,7 +56,7 @@ FieldValidator::ValidationStatus FieldValidator::validate( const FeatureLayerPai
5656
if ( value.userType() == qMetaTypeId<MixedAttributeValue>() )
5757
return Valid;
5858

59-
bool isNumeric = item.editorWidgetType() == QStringLiteral( "Range" ) || field.isNumeric();
59+
const bool isNumeric = item.editorWidgetType() == QStringLiteral( "Range" ) || field.isNumeric();
6060
if ( isNumeric )
6161
{
6262
state = validateNumericField( item, value, validationMessage );
@@ -76,7 +76,7 @@ FieldValidator::ValidationStatus FieldValidator::validate( const FeatureLayerPai
7676
// Continue to check hard and soft QGIS constraints
7777
QStringList errors;
7878

79-
bool hardConstraintSatisfied = QgsVectorLayerUtils::validateAttribute( pair.layer(), pair.feature(), item.fieldIndex(), errors, QgsFieldConstraints::ConstraintStrengthHard );
79+
const bool hardConstraintSatisfied = QgsVectorLayerUtils::validateAttribute( pair.layer(), pair.feature(), item.fieldIndex(), errors, QgsFieldConstraints::ConstraintStrengthHard );
8080
if ( !hardConstraintSatisfied )
8181
{
8282
validationMessage = constructConstraintValidationMessage( item, errors );
@@ -85,7 +85,7 @@ FieldValidator::ValidationStatus FieldValidator::validate( const FeatureLayerPai
8585

8686
errors.clear();
8787

88-
bool softConstraintSatisfied = QgsVectorLayerUtils::validateAttribute( pair.layer(), pair.feature(), item.fieldIndex(), errors, QgsFieldConstraints::ConstraintStrengthSoft );
88+
const bool softConstraintSatisfied = QgsVectorLayerUtils::validateAttribute( pair.layer(), pair.feature(), item.fieldIndex(), errors, QgsFieldConstraints::ConstraintStrengthSoft );
8989
if ( !softConstraintSatisfied )
9090
{
9191
validationMessage = constructConstraintValidationMessage( item, errors );
@@ -102,7 +102,7 @@ FieldValidator::ValidationStatus FieldValidator::validateTextField( const FormIt
102102
// Check if the text is not too long for the field
103103
if ( field.length() > 0 )
104104
{
105-
const int vLength = value.toString().length();
105+
const int vLength = static_cast<int>( value.toString().length() );
106106

107107
if ( vLength > field.length() )
108108
{
@@ -131,7 +131,7 @@ FieldValidator::ValidationStatus FieldValidator::validateNumericField( const For
131131

132132
// in Qt 6 isNull() does not return true for true if the variant contained an object
133133
// of a builtin type with an isNull() method that returned true for that object.
134-
// So isNull() for QVariant( QString() ) will return false and we need to handle this
134+
// So isNull() for QVariant( QString() ) will return false, and we need to handle this
135135
// separately.
136136
if ( value.userType() == QVariant::String && value.toString().isEmpty() )
137137
{
@@ -140,7 +140,7 @@ FieldValidator::ValidationStatus FieldValidator::validateNumericField( const For
140140

141141
QString errorMessage;
142142

143-
bool containsDecimals = value.toString().contains( QLocale().decimalPoint() ) || value.toString().contains( "." );
143+
const bool containsDecimals = value.toString().contains( QLocale().decimalPoint() ) || value.toString().contains( "." );
144144

145145
if ( !field.convertCompatible( value, &errorMessage ) )
146146
{
@@ -165,15 +165,15 @@ FieldValidator::ValidationStatus FieldValidator::validateNumericField( const For
165165
return Error;
166166
}
167167

168-
bool isRangeEditable = item.editorWidgetType() == QStringLiteral( "Range" ) &&
169-
item.editorWidgetConfig().value( QStringLiteral( "Style" ) ) == QStringLiteral( "SpinBox" );
168+
const bool isRangeEditable = item.editorWidgetType() == QStringLiteral( "Range" ) &&
169+
item.editorWidgetConfig().value( QStringLiteral( "Style" ) ) == QStringLiteral( "SpinBox" );
170170

171171
// Check min/max range
172172
if ( isRangeEditable )
173173
{
174-
double min = item.editorWidgetConfig().value( "Min" ).toDouble();
175-
double max = item.editorWidgetConfig().value( "Max" ).toDouble();
176-
double val = value.toDouble();
174+
const double min = item.editorWidgetConfig().value( "Min" ).toDouble();
175+
const double max = item.editorWidgetConfig().value( "Max" ).toDouble();
176+
const double val = value.toDouble();
177177

178178
if ( val < min )
179179
{
@@ -211,15 +211,15 @@ QString FieldValidator::constructConstraintValidationMessage( const FormItem &it
211211
*/
212212

213213
const QgsField field = item.field();
214-
QgsFieldConstraints fldCons = field.constraints();
214+
const QgsFieldConstraints &fldCons = field.constraints();
215215
QStringList validationMessages;
216216

217-
bool hasNotNullConstraint = fldCons.constraints() & QgsFieldConstraints::ConstraintNotNull;
218-
bool notNullViolated = unmetConstraints.contains( QStringLiteral( "value is NULL" ) );
217+
const bool hasNotNullConstraint = fldCons.constraints() & QgsFieldConstraints::ConstraintNotNull;
218+
const bool notNullViolated = unmetConstraints.contains( QStringLiteral( "value is NULL" ) );
219219

220220
if ( hasNotNullConstraint && notNullViolated )
221221
{
222-
QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintNotNull );
222+
const QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintNotNull );
223223

224224
if ( strength == QgsFieldConstraints::ConstraintStrengthHard )
225225
{
@@ -231,12 +231,12 @@ QString FieldValidator::constructConstraintValidationMessage( const FormItem &it
231231
}
232232
}
233233

234-
bool hasUniqueConstraint = fldCons.constraints() & QgsFieldConstraints::ConstraintUnique;
235-
bool uniqueViolated = unmetConstraints.contains( QStringLiteral( "value is not unique" ) );
234+
const bool hasUniqueConstraint = fldCons.constraints() & QgsFieldConstraints::ConstraintUnique;
235+
const bool uniqueViolated = unmetConstraints.contains( QStringLiteral( "value is not unique" ) );
236236

237237
if ( hasUniqueConstraint && uniqueViolated )
238238
{
239-
QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintUnique );
239+
const QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintUnique );
240240

241241
if ( strength == QgsFieldConstraints::ConstraintStrengthHard )
242242
{
@@ -248,13 +248,13 @@ QString FieldValidator::constructConstraintValidationMessage( const FormItem &it
248248
}
249249
}
250250

251-
bool hasExpressionConstrain = fldCons.constraints() & QgsFieldConstraints::ConstraintExpression;
252-
bool expressionViolated = unmetConstraints.filter( QRegularExpression( "(parser error|evaluation error|check failed)" ) ).size() > 0;
251+
const bool hasExpressionConstrain = fldCons.constraints() & QgsFieldConstraints::ConstraintExpression;
252+
const bool expressionViolated = !unmetConstraints.filter( QRegularExpression( "(parser error|evaluation error|check failed)" ) ).empty();
253253

254254
if ( hasExpressionConstrain && expressionViolated )
255255
{
256-
QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintExpression );
257-
bool containsDescription = !fldCons.constraintDescription().isEmpty();
256+
const QgsFieldConstraints::ConstraintStrength strength = fldCons.constraintStrength( QgsFieldConstraints::ConstraintExpression );
257+
const bool containsDescription = !fldCons.constraintDescription().isEmpty();
258258

259259
if ( containsDescription )
260260
{
@@ -273,10 +273,10 @@ QString FieldValidator::constructConstraintValidationMessage( const FormItem &it
273273
}
274274
}
275275

276-
if ( validationMessages.size() )
276+
if ( !validationMessages.empty() )
277277
{
278278
return validationMessages.join( QStringLiteral( "\n" ) ); // each message on new line
279279
}
280280

281-
return QString();
281+
return {};
282282
}

app/attributes/fieldvalidator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef FIELDVALIDATOR_H
1111
#define FIELDVALIDATOR_H
1212

13-
#include <QString>
1413
#include <QVariant>
1514
#include <QObject>
1615

0 commit comments

Comments
 (0)