Skip to content

Commit

Permalink
Expose QgsRelation to QML
Browse files Browse the repository at this point in the history
API changes for consistency:
  - setRelationName has been renamed to QgsRelation::setName
  - setRelationId has been renamed to QgsRelation::setId
  • Loading branch information
m-kuhn committed Feb 20, 2017
1 parent bcc35b3 commit 7e6695b
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 78 deletions.
6 changes: 4 additions & 2 deletions doc/api_break.dox
Expand Up @@ -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}
Expand All @@ -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.


Expand Down
20 changes: 2 additions & 18 deletions python/core/qgsrelation.sip
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
};
6 changes: 3 additions & 3 deletions src/app/qgsrelationmanagerdialog.cpp
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -153,7 +153,7 @@ QList< QgsRelation > QgsRelationManagerDialog::relations()
{
QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value<QgsRelation>();
// 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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -518,6 +518,7 @@ SET(QGIS_CORE_MOC_HDRS
qgspointlocator.h
qgsproject.h
qgsrelationmanager.h
qgsrelation.h
qgsrunprocess.h
qgssnappingconfig.h
qgssnappingutils.h
Expand Down Expand Up @@ -743,7 +744,6 @@ SET(QGIS_CORE_HDRS
qgsproviderregistry.h
qgspythonrunner.h
qgsrectangle.h
qgsrelation.h
qgsrenderchecker.h
qgsrendercontext.h
qgsruntimeprofiler.h
Expand Down
24 changes: 22 additions & 2 deletions src/core/qgsrelation.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<QString, QgsMapLayer*>& mapLayers = QgsProject::instance()->mapLayers();
Expand Down
38 changes: 26 additions & 12 deletions src/core/qgsrelation.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -291,15 +292,28 @@ 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.
* Will be called internally whenever a member is changed.
*/
void updateRelationStatus();

private:
//! Unique Id
QString mRelationId;
//! Human redable name
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrelationmanager.h
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -4174,7 +4174,7 @@ QList<QgsRelation> 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 );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -5486,7 +5486,7 @@ QList<QgsRelation> 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 );
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgscomposerhtml.cpp
Expand Up @@ -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" ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgscomposertablev2.cpp
Expand Up @@ -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" ) );
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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" ) );
Expand Down
4 changes: 2 additions & 2 deletions tests/src/gui/testqgseditorwidgetregistry.cpp
Expand Up @@ -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" );
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -133,8 +133,8 @@ def test_representValue(self):
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')
Expand Down Expand Up @@ -184,8 +184,8 @@ def test_representValue(self):

# 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())
Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_qgsjsonutils.py
Expand Up @@ -451,8 +451,8 @@ def testExportFeatureRelations(self):
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')
Expand Down
16 changes: 8 additions & 8 deletions tests/src/python/test_qgsrelation.py
Expand Up @@ -81,10 +81,10 @@ def test_isValid(self):
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())
Expand All @@ -99,8 +99,8 @@ def test_isValid(self):
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')
Expand All @@ -114,8 +114,8 @@ def test_getRelatedFeatures(self):

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')
Expand All @@ -130,8 +130,8 @@ def test_getReferencedFeature(self):
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')
Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_qgsrelationeditwidget.py
Expand Up @@ -66,15 +66,15 @@ def setUpClass(cls):
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)

cls.rel_b = QgsRelation()
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)

Expand Down

0 comments on commit 7e6695b

Please sign in to comment.