Better handling of invalid relations ...

- do not add them to the manager dialog
- do not add relations to not existent layers (but keep relations to invalid layers)
This commit is contained in:
Alessandro Pasotti 2018-11-03 11:25:05 +01:00
parent 2d1a521fe4
commit 7a259c6ea5
7 changed files with 13 additions and 0 deletions

View File

@ -273,6 +273,7 @@ Returns a list of attributes used to form the referencing fields
bool isValid() const; bool isValid() const;
%Docstring %Docstring
Returns the validity of this relation. Don't use the information if it's not valid. Returns the validity of this relation. Don't use the information if it's not valid.
A relation is considered valid if both referenced and referencig layers are valid.
:return: true if the relation is valid :return: true if the relation is valid
%End %End

View File

@ -45,6 +45,8 @@ Gets access to the relations managed by this class.
void addRelation( const QgsRelation &relation ); void addRelation( const QgsRelation &relation );
%Docstring %Docstring
Add a relation. Add a relation.
Invalid relations are added only if both referencing layer and referenced
layer exist.
:param relation: The relation to add. :param relation: The relation to add.
%End %End

View File

@ -49,6 +49,9 @@ void QgsRelationManagerDialog::setLayers( const QList< QgsVectorLayer * > &layer
void QgsRelationManagerDialog::addRelation( const QgsRelation &rel ) void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
{ {
if ( ! rel.isValid() )
return;
mRelationsTable->setSortingEnabled( false ); mRelationsTable->setSortingEnabled( false );
int row = mRelationsTable->rowCount(); int row = mRelationsTable->rowCount();
mRelationsTable->insertRow( row ); mRelationsTable->insertRow( row );

View File

@ -338,6 +338,7 @@ class CORE_EXPORT QgsRelation
/** /**
* Returns the validity of this relation. Don't use the information if it's not valid. * Returns the validity of this relation. Don't use the information if it's not valid.
* A relation is considered valid if both referenced and referencig layers are valid.
* *
* \returns true if the relation is valid * \returns true if the relation is valid
*/ */

View File

@ -50,6 +50,10 @@ QMap<QString, QgsRelation> QgsRelationManager::relations() const
void QgsRelationManager::addRelation( const QgsRelation &relation ) void QgsRelationManager::addRelation( const QgsRelation &relation )
{ {
// Do not add relations to layers that do not exist
if ( !( relation.referencingLayer() && relation.referencedLayer() ) )
return;
mRelations.insert( relation.id(), relation ); mRelations.insert( relation.id(), relation );
if ( mProject ) if ( mProject )

View File

@ -59,6 +59,8 @@ class CORE_EXPORT QgsRelationManager : public QObject
/** /**
* Add a relation. * Add a relation.
* Invalid relations are added only if both referencing layer and referenced
* layer exist.
* *
* \param relation The relation to add. * \param relation The relation to add.
*/ */

Binary file not shown.