From 7e6695b0a0fb7a09e47c9ae31ef7804c50faba9b Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Fri, 17 Feb 2017 16:50:43 +0100 Subject: [PATCH] Expose QgsRelation to QML API changes for consistency: - setRelationName has been renamed to QgsRelation::setName - setRelationId has been renamed to QgsRelation::setId --- doc/api_break.dox | 6 ++- python/core/qgsrelation.sip | 20 +--------- src/app/qgsrelationmanagerdialog.cpp | 6 +-- src/core/CMakeLists.txt | 2 +- src/core/qgsrelation.cpp | 24 +++++++++++- src/core/qgsrelation.h | 38 +++++++++++++------ src/core/qgsrelationmanager.h | 2 +- .../postgres/qgspostgresprovider.cpp | 2 +- .../spatialite/qgsspatialiteprovider.cpp | 2 +- tests/src/core/testqgscomposerhtml.cpp | 4 +- tests/src/core/testqgscomposertablev2.cpp | 2 +- tests/src/core/testqgsexpression.cpp | 4 +- tests/src/gui/testqgseditorwidgetregistry.cpp | 4 +- tests/src/python/test_qgsfieldformatters.py | 8 ++-- tests/src/python/test_qgsjsonutils.py | 4 +- tests/src/python/test_qgsrelation.py | 16 ++++---- .../src/python/test_qgsrelationeditwidget.py | 4 +- tests/src/python/test_qgsrelationmanager.py | 28 +++++++------- 18 files changed, 98 insertions(+), 78 deletions(-) diff --git a/doc/api_break.dox b/doc/api_break.dox index d7ff1331434..be4e430436b 100644 --- a/doc/api_break.dox +++ b/doc/api_break.dox @@ -1651,7 +1651,9 @@ QgsRasterRenderer QgsRelation {#qgis_api_break_3_0_QgsRelation} ----------- -- createFromXML() has been renamed to createFromXml() +- `createFromXML()` has been renamed to `QgsRelation::createFromXml()` +- `setRelationName()` has been renamed to `QgsRelation::setName()` +- `setRelationId()` has been renamed to `QgsRelation::setId()` QgsRenderChecker {#qgis_api_break_3_0_QgsRenderChecker} @@ -1664,7 +1666,7 @@ setExcludeAttributesWms() setExcludeAttributesWfs() - editorWidgetV2() and editorWidgetV2Config() have been removed and QgsEditorWidgetRegistry::instance()->findBest() must be used instead. - setEditorWidgetV2(), setEditorWidgetV2Config() have been removed and their equivalent in editFormConfig() must be used instead. -- setCheckedState() is removed. Use editFormConfig()->setWidgetConfig()` instead. +- setCheckedState() is removed. Use `editFormConfig()->setWidgetConfig()` instead. - valueMap(), valueRelation(), dateFormat(), widgetSize() have been removed. Use QgsEditorWidgetRegistry::instance()->findBest().config() instead. diff --git a/python/core/qgsrelation.sip b/python/core/qgsrelation.sip index fa723329cf7..02656157b6b 100644 --- a/python/core/qgsrelation.sip +++ b/python/core/qgsrelation.sip @@ -42,19 +42,9 @@ class QgsRelation */ void writeXml( QDomNode& node, QDomDocument& doc ) const; - /** - * Set an id for this relation - * - * @param id - */ - void setRelationId( const QString& id ); + void setId( const QString& id ); - /** - * Set a name for this relation - * - * @param name - */ - void setRelationName( const QString& name ); + void setName( const QString& name ); /** * Set the referencing (child) layer id. This layer will be searched in the registry. @@ -256,10 +246,4 @@ class QgsRelation */ bool hasEqualDefinition( const QgsRelation& other ) const; - protected: - /** - * Updates the validity status of this relation. - * Will be called internally whenever a member is changed. - */ - void updateRelationStatus(); }; diff --git a/src/app/qgsrelationmanagerdialog.cpp b/src/app/qgsrelationmanagerdialog.cpp index e77a9450d12..dc9fea48e1f 100644 --- a/src/app/qgsrelationmanagerdialog.cpp +++ b/src/app/qgsrelationmanagerdialog.cpp @@ -114,9 +114,9 @@ void QgsRelationManagerDialog::on_mBtnAddRelation_clicked() relationId = tempId.arg( suffix ); ++suffix; } - relation.setRelationId( relationId ); + relation.setId( relationId ); relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second ); - relation.setRelationName( addDlg.relationName() ); + relation.setName( addDlg.relationName() ); addRelation( relation ); } @@ -153,7 +153,7 @@ QList< QgsRelation > QgsRelationManagerDialog::relations() { QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value(); // The name can be editted in the table, so apply this one - relation.setRelationName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() ); + relation.setName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() ); relations << relation; } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 93563093744..a2775522776 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -518,6 +518,7 @@ SET(QGIS_CORE_MOC_HDRS qgspointlocator.h qgsproject.h qgsrelationmanager.h + qgsrelation.h qgsrunprocess.h qgssnappingconfig.h qgssnappingutils.h @@ -743,7 +744,6 @@ SET(QGIS_CORE_HDRS qgsproviderregistry.h qgspythonrunner.h qgsrectangle.h - qgsrelation.h qgsrenderchecker.h qgsrendercontext.h qgsruntimeprofiler.h diff --git a/src/core/qgsrelation.cpp b/src/core/qgsrelation.cpp index 5961bd19229..05f09774ea3 100644 --- a/src/core/qgsrelation.cpp +++ b/src/core/qgsrelation.cpp @@ -109,14 +109,14 @@ void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const node.appendChild( elem ); } -void QgsRelation::setRelationId( const QString& id ) +void QgsRelation::setId( const QString& id ) { mRelationId = id; updateRelationStatus(); } -void QgsRelation::setRelationName( const QString& name ) +void QgsRelation::setName( const QString& name ) { mRelationName = name; } @@ -316,6 +316,26 @@ bool QgsRelation::hasEqualDefinition( const QgsRelation& other ) const return mReferencedLayerId == other.mReferencedLayerId && mReferencingLayerId == other.mReferencingLayerId && mFieldPairs == other.mFieldPairs; } +QString QgsRelation::resolveReferencedField( const QString& referencingField ) const +{ + Q_FOREACH ( const FieldPair& pair, mFieldPairs ) + { + if ( pair.first == referencingField ) + return pair.second; + } + return QString(); +} + +QString QgsRelation::resolveReferencingField( const QString& referencedField ) const +{ + Q_FOREACH ( const FieldPair& pair, mFieldPairs ) + { + if ( pair.second == referencedField ) + return pair.first; + } + return QString(); +} + void QgsRelation::updateRelationStatus() { const QMap& mapLayers = QgsProject::instance()->mapLayers(); diff --git a/src/core/qgsrelation.h b/src/core/qgsrelation.h index c319570075b..5089270132f 100644 --- a/src/core/qgsrelation.h +++ b/src/core/qgsrelation.h @@ -34,6 +34,15 @@ class QgsAttributes; */ class CORE_EXPORT QgsRelation { + Q_GADGET + + Q_PROPERTY( QString id READ id WRITE setId ) + Q_PROPERTY( QgsVectorLayer* referencingLayer READ referencingLayer ) + Q_PROPERTY( QgsVectorLayer* referencedLayer READ referencedLayer ) + Q_PROPERTY( QString name READ name WRITE setName ) + Q_PROPERTY( bool isValid READ isValid ) + + public: /** @@ -87,29 +96,21 @@ class CORE_EXPORT QgsRelation /** * Set an id for this relation - * - * @param id */ - void setRelationId( const QString& id ); + void setId( const QString& id ); /** * Set a name for this relation - * - * @param name */ - void setRelationName( const QString& name ); + void setName( const QString& name ); /** * Set the referencing (child) layer id. This layer will be searched in the registry. - * - * @param id */ void setReferencingLayer( const QString& id ); /** * Set the referenced (parent) layer id. This layer will be searched in the registry. - * - * @param id */ void setReferencedLayer( const QString& id ); @@ -291,7 +292,21 @@ class CORE_EXPORT QgsRelation */ bool hasEqualDefinition( const QgsRelation& other ) const; - protected: + /** + * Get the referenced field counterpart given a referencing field. + * + * @note Added in QGIS 3.0 + */ + Q_INVOKABLE QString resolveReferencedField( const QString& referencingField ) const; + + /** + * Get the referencing field counterpart given a referenced field. + * + * @note Added in QGIS 3.0 + */ + Q_INVOKABLE QString resolveReferencingField( const QString& referencedField ) const; + + private: /** * Updates the validity status of this relation. @@ -299,7 +314,6 @@ class CORE_EXPORT QgsRelation */ void updateRelationStatus(); - private: //! Unique Id QString mRelationId; //! Human redable name diff --git a/src/core/qgsrelationmanager.h b/src/core/qgsrelationmanager.h index c17c71e39c5..291c0495e62 100644 --- a/src/core/qgsrelationmanager.h +++ b/src/core/qgsrelationmanager.h @@ -84,7 +84,7 @@ class CORE_EXPORT QgsRelationManager : public QObject * @return A relation. Invalid if not found. * @see relationsByName() */ - QgsRelation relation( const QString& id ) const; + Q_INVOKABLE QgsRelation relation( const QString& id ) const; /** Returns a list of relations with matching names. * @param name relation name to search for. Searching is case insensitive. diff --git a/src/providers/postgres/qgspostgresprovider.cpp b/src/providers/postgres/qgspostgresprovider.cpp index 5e1f0c9c15b..a2b63d1c185 100644 --- a/src/providers/postgres/qgspostgresprovider.cpp +++ b/src/providers/postgres/qgspostgresprovider.cpp @@ -4174,7 +4174,7 @@ QList QgsPostgresProvider::discoverRelations( const QgsVectorLayer* Q_FOREACH ( const QgsVectorLayer* foundLayer, foundLayers ) { QgsRelation relation; - relation.setRelationName( name ); + relation.setName( name ); relation.setReferencingLayer( self->id() ); relation.setReferencedLayer( foundLayer->id() ); relation.addFieldPair( fkColumn, refColumn ); diff --git a/src/providers/spatialite/qgsspatialiteprovider.cpp b/src/providers/spatialite/qgsspatialiteprovider.cpp index 37277acc42a..7051cb74518 100644 --- a/src/providers/spatialite/qgsspatialiteprovider.cpp +++ b/src/providers/spatialite/qgsspatialiteprovider.cpp @@ -5486,7 +5486,7 @@ QList QgsSpatiaLiteProvider::discoverRelations( const QgsVectorLaye Q_FOREACH ( const QgsVectorLayer* foundLayer, foundLayers ) { QgsRelation relation; - relation.setRelationName( name ); + relation.setName( name ); relation.setReferencingLayer( self->id() ); relation.setReferencedLayer( foundLayer->id() ); relation.addFieldPair( fkColumn, refColumn ); diff --git a/tests/src/core/testqgscomposerhtml.cpp b/tests/src/core/testqgscomposerhtml.cpp index 58fbcf49dbf..f8ce73f8152 100644 --- a/tests/src/core/testqgscomposerhtml.cpp +++ b/tests/src/core/testqgscomposerhtml.cpp @@ -283,8 +283,8 @@ void TestQgsComposerHtml::javascriptSetFeature() mComposition->atlasComposition().setEnabled( true ); QgsRelation rel; - rel.setRelationId( QStringLiteral( "rel1" ) ); - rel.setRelationName( QStringLiteral( "relation one" ) ); + rel.setId( QStringLiteral( "rel1" ) ); + rel.setName( QStringLiteral( "relation one" ) ); rel.setReferencingLayer( childLayer->id() ); rel.setReferencedLayer( parentLayer->id() ); rel.addFieldPair( QStringLiteral( "y" ), QStringLiteral( "foreignkey" ) ); diff --git a/tests/src/core/testqgscomposertablev2.cpp b/tests/src/core/testqgscomposertablev2.cpp index 3e867097fa2..4ff4340251e 100644 --- a/tests/src/core/testqgscomposertablev2.cpp +++ b/tests/src/core/testqgscomposertablev2.cpp @@ -493,7 +493,7 @@ void TestQgsComposerTableV2::attributeTableRelationSource() //create a relation QgsRelation relation; - relation.setRelationId( QStringLiteral( "testrelation" ) ); + relation.setId( QStringLiteral( "testrelation" ) ); relation.setReferencedLayer( atlasLayer->id() ); relation.setReferencingLayer( mVectorLayer->id() ); relation.addFieldPair( QStringLiteral( "Class" ), QStringLiteral( "Class" ) ); diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index d29d4c34bf6..79423e07ad5 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -181,8 +181,8 @@ class TestQgsExpression: public QObject QgsProject::instance()->addMapLayer( mChildLayer ); QgsRelation rel; - rel.setRelationId( QStringLiteral( "my_rel" ) ); - rel.setRelationName( QStringLiteral( "relation name" ) ); + rel.setId( QStringLiteral( "my_rel" ) ); + rel.setName( QStringLiteral( "relation name" ) ); rel.setReferencedLayer( mAggregatesLayer->id() ); rel.setReferencingLayer( mChildLayer->id() ); rel.addFieldPair( QStringLiteral( "parent" ), QStringLiteral( "col1" ) ); diff --git a/tests/src/gui/testqgseditorwidgetregistry.cpp b/tests/src/gui/testqgseditorwidgetregistry.cpp index 5b31be0ba54..f7ff1ee6ee9 100644 --- a/tests/src/gui/testqgseditorwidgetregistry.cpp +++ b/tests/src/gui/testqgseditorwidgetregistry.cpp @@ -121,8 +121,8 @@ class TestQgsEditorWidgetRegistry: public QObject //create a relation between them QgsRelation relation; - relation.setRelationId( QStringLiteral( "vl1->vl2" ) ); - relation.setRelationName( QStringLiteral( "vl1->vl2" ) ); + relation.setId( QStringLiteral( "vl1->vl2" ) ); + relation.setName( QStringLiteral( "vl1->vl2" ) ); relation.setReferencingLayer( vl1.id() ); relation.setReferencedLayer( vl2.id() ); relation.addFieldPair( "fk", "pk" ); diff --git a/tests/src/python/test_qgsfieldformatters.py b/tests/src/python/test_qgsfieldformatters.py index df0a5ee2fc2..b8b8f34c7bb 100644 --- a/tests/src/python/test_qgsfieldformatters.py +++ b/tests/src/python/test_qgsfieldformatters.py @@ -133,8 +133,8 @@ class TestQgsRelationReferenceFieldFormatter(unittest.TestCase): fieldFormatter = QgsRelationReferenceFieldFormatter() rel = QgsRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') rel.setReferencingLayer(first_layer.id()) rel.setReferencedLayer(second_layer.id()) rel.addFieldPair('foreign_key', 'pkid') @@ -184,8 +184,8 @@ class TestQgsRelationReferenceFieldFormatter(unittest.TestCase): # Invalid relation rel = QgsRelation() - rel.setRelationId('rel2') - rel.setRelationName('Relation Number Two') + rel.setId('rel2') + rel.setName('Relation Number Two') rel.setReferencingLayer(first_layer.id()) rel.addFieldPair('foreign_key', 'pkid') self.assertFalse(rel.isValid()) diff --git a/tests/src/python/test_qgsjsonutils.py b/tests/src/python/test_qgsjsonutils.py index a68c829e3b5..efcf540b2be 100644 --- a/tests/src/python/test_qgsjsonutils.py +++ b/tests/src/python/test_qgsjsonutils.py @@ -451,8 +451,8 @@ class TestQgsJSONUtils(unittest.TestCase): QgsProject.instance().addMapLayers([child, parent]) rel = QgsRelation() - rel.setRelationId('rel1') - rel.setRelationName('relation one') + rel.setId('rel1') + rel.setName('relation one') rel.setReferencingLayer(child.id()) rel.setReferencedLayer(parent.id()) rel.addFieldPair('y', 'foreignkey') diff --git a/tests/src/python/test_qgsrelation.py b/tests/src/python/test_qgsrelation.py index 6380e1698cc..1d506e7186e 100644 --- a/tests/src/python/test_qgsrelation.py +++ b/tests/src/python/test_qgsrelation.py @@ -81,10 +81,10 @@ class TestQgsRelation(unittest.TestCase): rel = QgsRelation() assert not rel.isValid() - rel.setRelationId('rel1') + rel.setId('rel1') assert not rel.isValid() - rel.setRelationName('Relation Number One') + rel.setName('Relation Number One') assert not rel.isValid() rel.setReferencingLayer(self.referencingLayer.id()) @@ -99,8 +99,8 @@ class TestQgsRelation(unittest.TestCase): def test_getRelatedFeatures(self): rel = QgsRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') rel.setReferencingLayer(self.referencingLayer.id()) rel.setReferencedLayer(self.referencedLayer.id()) rel.addFieldPair('foreignkey', 'y') @@ -114,8 +114,8 @@ class TestQgsRelation(unittest.TestCase): def test_getReferencedFeature(self): rel = QgsRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') rel.setReferencingLayer(self.referencingLayer.id()) rel.setReferencedLayer(self.referencedLayer.id()) rel.addFieldPair('foreignkey', 'y') @@ -130,8 +130,8 @@ class TestQgsRelation(unittest.TestCase): def test_fieldPairs(self): rel = QgsRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') rel.setReferencingLayer(self.referencingLayer.id()) rel.setReferencedLayer(self.referencedLayer.id()) rel.addFieldPair('foreignkey', 'y') diff --git a/tests/src/python/test_qgsrelationeditwidget.py b/tests/src/python/test_qgsrelationeditwidget.py index 21e81790db5..587820b4a85 100644 --- a/tests/src/python/test_qgsrelationeditwidget.py +++ b/tests/src/python/test_qgsrelationeditwidget.py @@ -66,7 +66,7 @@ class TestQgsRelationEditWidget(unittest.TestCase): cls.rel_a.setReferencingLayer(cls.vl_link.id()) cls.rel_a.setReferencedLayer(cls.vl_a.id()) cls.rel_a.addFieldPair('fk_author', 'pk') - cls.rel_a.setRelationId('rel_a') + cls.rel_a.setId('rel_a') assert(cls.rel_a.isValid()) cls.relMgr.addRelation(cls.rel_a) @@ -74,7 +74,7 @@ class TestQgsRelationEditWidget(unittest.TestCase): cls.rel_b.setReferencingLayer(cls.vl_link.id()) cls.rel_b.setReferencedLayer(cls.vl_b.id()) cls.rel_b.addFieldPair('fk_book', 'pk') - cls.rel_b.setRelationId('rel_b') + cls.rel_b.setId('rel_b') assert(cls.rel_b.isValid()) cls.relMgr.addRelation(cls.rel_b) diff --git a/tests/src/python/test_qgsrelationmanager.py b/tests/src/python/test_qgsrelationmanager.py index f203357af0f..21ac98af5a7 100644 --- a/tests/src/python/test_qgsrelationmanager.py +++ b/tests/src/python/test_qgsrelationmanager.py @@ -62,8 +62,8 @@ class TestQgsRelationManager(unittest.TestCase): self.assertEqual(len(relations), 0) rel = self.createRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') assert rel.isValid() manager.addRelation(rel) @@ -73,8 +73,8 @@ class TestQgsRelationManager(unittest.TestCase): self.assertEqual(relations['rel1'].id(), 'rel1') rel = self.createRelation() - rel.setRelationId('rel2') - rel.setRelationName('Relation Number Two') + rel.setId('rel2') + rel.setName('Relation Number Two') assert rel.isValid() manager.addRelation(rel) @@ -93,13 +93,13 @@ class TestQgsRelationManager(unittest.TestCase): # add two relations rel = self.createRelation() - rel.setRelationId('rel1') - rel.setRelationName('Relation Number One') + rel.setId('rel1') + rel.setName('Relation Number One') assert rel.isValid() manager.addRelation(rel) rel = self.createRelation() - rel.setRelationId('rel2') - rel.setRelationName('Relation Number Two') + rel.setId('rel2') + rel.setName('Relation Number Two') assert rel.isValid() manager.addRelation(rel) @@ -121,18 +121,18 @@ class TestQgsRelationManager(unittest.TestCase): # add some relations rel = self.createRelation() - rel.setRelationId('rel1') - rel.setRelationName('my relation') + rel.setId('rel1') + rel.setName('my relation') assert rel.isValid() manager.addRelation(rel) rel = self.createRelation() - rel.setRelationId('rel2') - rel.setRelationName('dupe name') + rel.setId('rel2') + rel.setName('dupe name') assert rel.isValid() manager.addRelation(rel) rel = self.createRelation() - rel.setRelationId('rel3') - rel.setRelationName('dupe name') + rel.setId('rel3') + rel.setName('dupe name') assert rel.isValid() manager.addRelation(rel)