From 7a259c6ea5e69d04ba26f26f561b98a04b53d141 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Sat, 3 Nov 2018 11:25:05 +0100 Subject: [PATCH] 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) --- python/core/auto_generated/qgsrelation.sip.in | 1 + .../auto_generated/qgsrelationmanager.sip.in | 2 ++ src/app/qgsrelationmanagerdialog.cpp | 3 +++ src/core/qgsrelation.h | 1 + src/core/qgsrelationmanager.cpp | 4 ++++ src/core/qgsrelationmanager.h | 2 ++ tests/testdata/projects/bad_layers_test.gpkg | Bin 143360 -> 143360 bytes 7 files changed, 13 insertions(+) diff --git a/python/core/auto_generated/qgsrelation.sip.in b/python/core/auto_generated/qgsrelation.sip.in index c4361562b38..5f68c5e623c 100644 --- a/python/core/auto_generated/qgsrelation.sip.in +++ b/python/core/auto_generated/qgsrelation.sip.in @@ -273,6 +273,7 @@ Returns a list of attributes used to form the referencing fields bool isValid() const; %Docstring 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 %End diff --git a/python/core/auto_generated/qgsrelationmanager.sip.in b/python/core/auto_generated/qgsrelationmanager.sip.in index 255e029787d..3283d6ff27b 100644 --- a/python/core/auto_generated/qgsrelationmanager.sip.in +++ b/python/core/auto_generated/qgsrelationmanager.sip.in @@ -45,6 +45,8 @@ Gets access to the relations managed by this class. void addRelation( const QgsRelation &relation ); %Docstring Add a relation. +Invalid relations are added only if both referencing layer and referenced +layer exist. :param relation: The relation to add. %End diff --git a/src/app/qgsrelationmanagerdialog.cpp b/src/app/qgsrelationmanagerdialog.cpp index 4609308a6fd..e3a3df3aec0 100644 --- a/src/app/qgsrelationmanagerdialog.cpp +++ b/src/app/qgsrelationmanagerdialog.cpp @@ -49,6 +49,9 @@ void QgsRelationManagerDialog::setLayers( const QList< QgsVectorLayer * > &layer void QgsRelationManagerDialog::addRelation( const QgsRelation &rel ) { + if ( ! rel.isValid() ) + return; + mRelationsTable->setSortingEnabled( false ); int row = mRelationsTable->rowCount(); mRelationsTable->insertRow( row ); diff --git a/src/core/qgsrelation.h b/src/core/qgsrelation.h index 776b507e366..9c5b1c26346 100644 --- a/src/core/qgsrelation.h +++ b/src/core/qgsrelation.h @@ -338,6 +338,7 @@ class CORE_EXPORT QgsRelation /** * 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 */ diff --git a/src/core/qgsrelationmanager.cpp b/src/core/qgsrelationmanager.cpp index c6a936cc3a8..9e120990b38 100644 --- a/src/core/qgsrelationmanager.cpp +++ b/src/core/qgsrelationmanager.cpp @@ -50,6 +50,10 @@ QMap QgsRelationManager::relations() const 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 ); if ( mProject ) diff --git a/src/core/qgsrelationmanager.h b/src/core/qgsrelationmanager.h index e4d93123b79..4e675e8f9a2 100644 --- a/src/core/qgsrelationmanager.h +++ b/src/core/qgsrelationmanager.h @@ -59,6 +59,8 @@ class CORE_EXPORT QgsRelationManager : public QObject /** * Add a relation. + * Invalid relations are added only if both referencing layer and referenced + * layer exist. * * \param relation The relation to add. */ diff --git a/tests/testdata/projects/bad_layers_test.gpkg b/tests/testdata/projects/bad_layers_test.gpkg index 536b62b27943f7cea213c6f657e152fa01c0fa33..ed0a39349bb481c9775e26778dd1970953590c20 100644 GIT binary patch delta 25 gcmZp8z|ru4V}dlJ^+XwGM(f6e)&$0_2}}$00d`>skpKVy delta 25 gcmZp8z|ru4V}dlJ`9v9KM)Ssm)&$0_2}}$00d@5Vi2wiq