Add method to get user friendly error when a relationship is not valid

This commit is contained in:
Nyall Dawson 2022-09-26 14:18:01 +10:00
parent c2b30d90f8
commit 765d89c675
4 changed files with 49 additions and 0 deletions

View File

@ -274,6 +274,19 @@ Returns the validity of this relation. Don't use the information if it's not val
A relation is considered valid if both referenced and referencig layers are valid.
:return: ``True`` if the relation is valid
.. seealso:: :py:func:`validationError`
%End
QString validationError() const;
%Docstring
Returns a user-friendly explanation for why the relationship is invalid.
Returns an empty string if the relationship :py:func:`~QgsRelation.isValid`.
.. seealso:: :py:func:`isValid`
.. versionadded:: 3.28
%End
bool hasEqualDefinition( const QgsRelation &other ) const;

View File

@ -360,6 +360,23 @@ bool QgsRelation::isValid() const
return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayer.isNull() && d->mReferencingLayer.data()->isValid() && d->mReferencedLayer.data()->isValid();
}
QString QgsRelation::validationError() const
{
if ( isValid() )
return QString();
if ( d->mReferencingLayer.isNull() )
return QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
else if ( !d->mReferencingLayer.data()->isValid() )
return QObject::tr( "Referencing layer %1 is not valid" ).arg( d->mReferencingLayerId );
else if ( d->mReferencedLayer.isNull() )
return QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
else if ( !d->mReferencedLayer.data()->isValid() )
return QObject::tr( "Referenced layer %1 is not valid" ).arg( d->mReferencedLayerId );
else
return d->mValidationError;
}
bool QgsRelation::hasEqualDefinition( const QgsRelation &other ) const
{
return d->mReferencedLayerId == other.d->mReferencedLayerId && d->mReferencingLayerId == other.d->mReferencingLayerId && d->mFieldPairs == other.d->mFieldPairs;
@ -397,6 +414,7 @@ void QgsRelation::updateRelationStatus()
if ( d->mRelationId.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "Invalid relation: no ID" ) );
d->mValidationError = QObject::tr( "Relationship has no ID" );
d->mValid = false;
}
else
@ -404,11 +422,13 @@ void QgsRelation::updateRelationStatus()
if ( !d->mReferencedLayer )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayerId ), 4 );
d->mValidationError = QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
d->mValid = false;
}
else if ( !d->mReferencingLayer )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: referencing layer does not exist. ID: %2" ).arg( d->mReferencingLayerId ), 4 );
d->mValidationError = QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
d->mValid = false;
}
else
@ -416,6 +436,7 @@ void QgsRelation::updateRelationStatus()
if ( d->mFieldPairs.count() < 1 )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: no pair of field is specified." ), 4 );
d->mValidationError = QObject::tr( "No fields specified for relationship" );
d->mValid = false;
}
@ -424,12 +445,14 @@ void QgsRelation::updateRelationStatus()
if ( -1 == d->mReferencingLayer->fields().lookupField( pair.first ) )
{
QgsDebugMsg( QStringLiteral( "Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
d->mValidationError = QObject::tr( "Field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() );
d->mValid = false;
break;
}
else if ( -1 == d->mReferencedLayer->fields().lookupField( pair.second ) )
{
QgsDebugMsg( QStringLiteral( "Invalid relation: field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayer->name() ) );
d->mValidationError = QObject::tr( "Field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayer->name() );
d->mValid = false;
break;
}

View File

@ -339,9 +339,21 @@ class CORE_EXPORT QgsRelation
* A relation is considered valid if both referenced and referencig layers are valid.
*
* \returns TRUE if the relation is valid
*
* \see validationError()
*/
bool isValid() const;
/**
* Returns a user-friendly explanation for why the relationship is invalid.
*
* Returns an empty string if the relationship isValid().
*
* \see isValid()
* \since QGIS 3.28
*/
QString validationError() const;
/**
* Compares the two QgsRelation, ignoring the name and the ID.
*

View File

@ -66,6 +66,7 @@ class QgsRelationPrivate : public QSharedData
QList< QgsRelation::FieldPair > mFieldPairs;
bool mValid = false;
QString mValidationError;
};
/// @endcond