Skip to content
Permalink
Browse files

Expose QgsRelation to QML

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 7e6695b0a0fb7a09e47c9ae31ef7804c50faba9b
@@ -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.


@@ -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();
};
@@ -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<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;
}

@@ -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
@@ -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<QString, QgsMapLayer*>& mapLayers = QgsProject::instance()->mapLayers();
@@ -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,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
@@ -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.
@@ -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 );
@@ -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 );
@@ -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" ) );
@@ -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" ) );
@@ -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" ) );
@@ -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" );
@@ -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')
@@ -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())
@@ -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')
@@ -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())
@@ -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')
@@ -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')
@@ -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')
@@ -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)

0 comments on commit 7e6695b

Please sign in to comment.
You can’t perform that action at this time.