Skip to content

Commit

Permalink
[FEATURE] Constraints are updated on joined fields when dynamic form …
Browse files Browse the repository at this point in the history
…option is enabled
  • Loading branch information
pblottiere committed Jul 24, 2017
1 parent dcec6bb commit 1811a39
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -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 );
Expand Down
13 changes: 10 additions & 3 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
Expand Up @@ -18,6 +18,8 @@
#include "qgsvectordataprovider.h"
#include "qgsfields.h"
#include "qgsvectorlayerutils.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsvectorlayerjoininfo.h"

#include <QTableView>

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
Expand Up @@ -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,
Expand Down
31 changes: 26 additions & 5 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 )
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -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 );
Expand All @@ -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
{
Expand Down

0 comments on commit 1811a39

Please sign in to comment.