Skip to content
Permalink
Browse files

[FEATURE] Constraints are updated on joined fields when dynamic form …

…option is enabled
  • Loading branch information
pblottiere committed Jul 10, 2017
1 parent dcec6bb commit 1811a396541f498d0ddf66550118543a36709f62
@@ -416,10 +416,11 @@ QgsFeature QgsVectorLayerJoinBuffer::joinedFeatureOf( const QgsVectorLayerJoinIn

if ( info->joinLayer() )
{
joinedFeature.setFields( info->joinLayer()->fields() );

QString joinFieldName = info->joinFieldName();
const QVariant targetValue = feature.attribute( info->targetFieldName() );
QString fieldRef = QgsExpression::quotedColumnRef( info->joinFieldName() );
QString quotedVal = QgsExpression::quotedValue( targetValue.toString() );
const QString filter = QStringLiteral( "%1 = %2" ).arg( fieldRef, quotedVal );
QString filter = QgsExpression::createFieldEqualityExpression( joinFieldName, targetValue );

QgsFeatureRequest request;
request.setFilterExpression( filter );
@@ -18,6 +18,8 @@
#include "qgsvectordataprovider.h"
#include "qgsfields.h"
#include "qgsvectorlayerutils.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsvectorlayerjoininfo.h"

#include <QTableView>

@@ -118,9 +120,14 @@ void QgsEditorWidgetWrapper::updateConstraintWidgetStatus( ConstraintResult cons
}

void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft, QgsFieldConstraints::ConstraintOrigin constraintOrigin )
{
updateConstraint( layer(), mFieldIdx, ft, constraintOrigin );
}

void QgsEditorWidgetWrapper::updateConstraint( const QgsVectorLayer *layer, int index, const QgsFeature &ft, QgsFieldConstraints::ConstraintOrigin constraintOrigin )
{
bool toEmit( false );
QgsField field = layer()->fields().at( mFieldIdx );
QgsField field = layer->fields().at( index );

QString expression = field.constraints().constraintExpression();
QStringList expressions, descriptions;
@@ -161,10 +168,10 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft, QgsFieldCon
}

QStringList errors;
bool hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer(), ft, mFieldIdx, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );
bool hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );

QStringList softErrors;
bool softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer(), ft, mFieldIdx, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
bool softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
errors << softErrors;

mValidConstraint = hardConstraintsOk && softConstraintsOk;
@@ -138,6 +138,18 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
void updateConstraint( const QgsFeature &featureContext, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet );

/**
* Update constraint on a feature coming from a specific layer.
* \param layer The vector layer where the feature is defined
* \param index The index of the field to check
* \param feature The feature to use to evaluate the constraint
* \param constraintOrigin Optional origin for constraints to check. This
* can be used to limit the constraints tested to only provider or layer
* based constraints.
* \since QGIS 3.0
*/
void updateConstraint( const QgsVectorLayer *layer, int index, const QgsFeature &feature, QgsFieldConstraints::ConstraintOrigin constraintOrigin = QgsFieldConstraints::ConstraintOriginNotSet );

/**
* Get the current constraint status.
* \returns true if the constraint is valid or if there's no constraint,
@@ -720,17 +720,15 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )
// to test, but they are unlikely to have any control over provider-side constraints
// 2. the provider has already accepted the value, so presumably it doesn't violate the constraint
// and there's no point rechecking!
QgsFieldConstraints::ConstraintOrigin constraintOrigin = mLayer->isEditable() ? QgsFieldConstraints::ConstraintOriginNotSet
: QgsFieldConstraints::ConstraintOriginLayer;

// update eww constraint
eww->updateConstraint( ft, constraintOrigin );
updateConstraint( ft, eww );

// update eww dependencies constraint
QList<QgsEditorWidgetWrapper *> deps = constraintDependencies( eww );

Q_FOREACH ( QgsEditorWidgetWrapper *depsEww, deps )
depsEww->updateConstraint( ft, constraintOrigin );
updateConstraint( ft, depsEww );

// sync OK button status
synchronizeEnabledState();
@@ -745,6 +743,26 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )
}
}

void QgsAttributeForm::updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww )
{
QgsFieldConstraints::ConstraintOrigin constraintOrigin = mLayer->isEditable() ? QgsFieldConstraints::ConstraintOriginNotSet : QgsFieldConstraints::ConstraintOriginLayer;

if ( eww->layer()->fields().fieldOrigin( QgsFields::OriginJoin ) )
{
int srcFieldIdx;
const QgsVectorLayerJoinInfo *info = eww->layer()->joinBuffer()->joinForFieldIndex( eww->fieldIdx(), eww->layer()->fields(), srcFieldIdx );

if ( info && info->joinLayer() && mJoinedFeatures.contains( info ) && info->isDynamicFormEnabled() )
{
eww->updateConstraint( info->joinLayer(), srcFieldIdx, mJoinedFeatures[info], constraintOrigin );
return;
}
}

// default constraint update
eww->updateConstraint( ft, constraintOrigin );
}

bool QgsAttributeForm::currentFormFeature( QgsFeature &feature )
{
bool rc = true;
@@ -765,7 +783,7 @@ bool QgsAttributeForm::currentFormFeature( QgsFeature &feature )
QVariant srcVar = eww->value();
// need to check dstVar.isNull() != srcVar.isNull()
// otherwise if dstVar=NULL and scrVar=0, then dstVar = srcVar
if ( ( dstVar != srcVar || dstVar.isNull() != srcVar.isNull() ) && srcVar.isValid() && !mLayer->editFormConfig().readOnly( eww->fieldIdx() ) )
if ( ( dstVar != srcVar || dstVar.isNull() != srcVar.isNull() ) && srcVar.isValid() )
dst[eww->fieldIdx()] = srcVar;
}
else
@@ -1954,6 +1972,9 @@ void QgsAttributeForm::updateJoinedFields( const QgsEditorWidgetWrapper &eww )

QgsFeature joinFeature = mLayer->joinBuffer()->joinedFeatureOf( info, formFeature );

if ( joinFeature.isValid() )
mJoinedFeatures[info] = joinFeature;

QStringList *subsetFields = info->joinFieldNamesSubset();
if ( subsetFields )
{
@@ -318,6 +318,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
//! constraints management
void updateAllConstraints();
void updateConstraints( QgsEditorWidgetWrapper *w );
void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
bool currentFormFeature( QgsFeature &feature );
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
@@ -340,6 +341,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
QList<QgsAttributeFormInterface *> mInterfaces;
QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
QgsExpressionContext mExpressionContext;
QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;

struct ContainerInformation
{

0 comments on commit 1811a39

Please sign in to comment.
You can’t perform that action at this time.