Skip to content

Commit 7e6695b

Browse files
committedFeb 20, 2017
Expose QgsRelation to QML
API changes for consistency: - setRelationName has been renamed to QgsRelation::setName - setRelationId has been renamed to QgsRelation::setId
1 parent bcc35b3 commit 7e6695b

18 files changed

+98
-78
lines changed
 

‎doc/api_break.dox

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,9 @@ QgsRasterRenderer
16511651
QgsRelation {#qgis_api_break_3_0_QgsRelation}
16521652
-----------
16531653

1654-
- createFromXML() has been renamed to createFromXml()
1654+
- `createFromXML()` has been renamed to `QgsRelation::createFromXml()`
1655+
- `setRelationName()` has been renamed to `QgsRelation::setName()`
1656+
- `setRelationId()` has been renamed to `QgsRelation::setId()`
16551657

16561658

16571659
QgsRenderChecker {#qgis_api_break_3_0_QgsRenderChecker}
@@ -1664,7 +1666,7 @@ setExcludeAttributesWms()
16641666
setExcludeAttributesWfs()
16651667
- editorWidgetV2() and editorWidgetV2Config() have been removed and QgsEditorWidgetRegistry::instance()->findBest() must be used instead.
16661668
- setEditorWidgetV2(), setEditorWidgetV2Config() have been removed and their equivalent in editFormConfig() must be used instead.
1667-
- setCheckedState() is removed. Use editFormConfig()->setWidgetConfig()` instead.
1669+
- setCheckedState() is removed. Use `editFormConfig()->setWidgetConfig()` instead.
16681670
- valueMap(), valueRelation(), dateFormat(), widgetSize() have been removed. Use QgsEditorWidgetRegistry::instance()->findBest().config() instead.
16691671

16701672

‎python/core/qgsrelation.sip

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,9 @@ class QgsRelation
4242
*/
4343
void writeXml( QDomNode& node, QDomDocument& doc ) const;
4444

45-
/**
46-
* Set an id for this relation
47-
*
48-
* @param id
49-
*/
50-
void setRelationId( const QString& id );
45+
void setId( const QString& id );
5146

52-
/**
53-
* Set a name for this relation
54-
*
55-
* @param name
56-
*/
57-
void setRelationName( const QString& name );
47+
void setName( const QString& name );
5848

5949
/**
6050
* Set the referencing (child) layer id. This layer will be searched in the registry.
@@ -256,10 +246,4 @@ class QgsRelation
256246
*/
257247
bool hasEqualDefinition( const QgsRelation& other ) const;
258248

259-
protected:
260-
/**
261-
* Updates the validity status of this relation.
262-
* Will be called internally whenever a member is changed.
263-
*/
264-
void updateRelationStatus();
265249
};

‎src/app/qgsrelationmanagerdialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ void QgsRelationManagerDialog::on_mBtnAddRelation_clicked()
114114
relationId = tempId.arg( suffix );
115115
++suffix;
116116
}
117-
relation.setRelationId( relationId );
117+
relation.setId( relationId );
118118
relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second );
119-
relation.setRelationName( addDlg.relationName() );
119+
relation.setName( addDlg.relationName() );
120120

121121
addRelation( relation );
122122
}
@@ -153,7 +153,7 @@ QList< QgsRelation > QgsRelationManagerDialog::relations()
153153
{
154154
QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value<QgsRelation>();
155155
// The name can be editted in the table, so apply this one
156-
relation.setRelationName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() );
156+
relation.setName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() );
157157
relations << relation;
158158
}
159159

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ SET(QGIS_CORE_MOC_HDRS
518518
qgspointlocator.h
519519
qgsproject.h
520520
qgsrelationmanager.h
521+
qgsrelation.h
521522
qgsrunprocess.h
522523
qgssnappingconfig.h
523524
qgssnappingutils.h
@@ -743,7 +744,6 @@ SET(QGIS_CORE_HDRS
743744
qgsproviderregistry.h
744745
qgspythonrunner.h
745746
qgsrectangle.h
746-
qgsrelation.h
747747
qgsrenderchecker.h
748748
qgsrendercontext.h
749749
qgsruntimeprofiler.h

‎src/core/qgsrelation.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
109109
node.appendChild( elem );
110110
}
111111

112-
void QgsRelation::setRelationId( const QString& id )
112+
void QgsRelation::setId( const QString& id )
113113
{
114114
mRelationId = id;
115115

116116
updateRelationStatus();
117117
}
118118

119-
void QgsRelation::setRelationName( const QString& name )
119+
void QgsRelation::setName( const QString& name )
120120
{
121121
mRelationName = name;
122122
}
@@ -316,6 +316,26 @@ bool QgsRelation::hasEqualDefinition( const QgsRelation& other ) const
316316
return mReferencedLayerId == other.mReferencedLayerId && mReferencingLayerId == other.mReferencingLayerId && mFieldPairs == other.mFieldPairs;
317317
}
318318

319+
QString QgsRelation::resolveReferencedField( const QString& referencingField ) const
320+
{
321+
Q_FOREACH ( const FieldPair& pair, mFieldPairs )
322+
{
323+
if ( pair.first == referencingField )
324+
return pair.second;
325+
}
326+
return QString();
327+
}
328+
329+
QString QgsRelation::resolveReferencingField( const QString& referencedField ) const
330+
{
331+
Q_FOREACH ( const FieldPair& pair, mFieldPairs )
332+
{
333+
if ( pair.second == referencedField )
334+
return pair.first;
335+
}
336+
return QString();
337+
}
338+
319339
void QgsRelation::updateRelationStatus()
320340
{
321341
const QMap<QString, QgsMapLayer*>& mapLayers = QgsProject::instance()->mapLayers();

‎src/core/qgsrelation.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ class QgsAttributes;
3434
*/
3535
class CORE_EXPORT QgsRelation
3636
{
37+
Q_GADGET
38+
39+
Q_PROPERTY( QString id READ id WRITE setId )
40+
Q_PROPERTY( QgsVectorLayer* referencingLayer READ referencingLayer )
41+
Q_PROPERTY( QgsVectorLayer* referencedLayer READ referencedLayer )
42+
Q_PROPERTY( QString name READ name WRITE setName )
43+
Q_PROPERTY( bool isValid READ isValid )
44+
45+
3746
public:
3847

3948
/**
@@ -87,29 +96,21 @@ class CORE_EXPORT QgsRelation
8796

8897
/**
8998
* Set an id for this relation
90-
*
91-
* @param id
9299
*/
93-
void setRelationId( const QString& id );
100+
void setId( const QString& id );
94101

95102
/**
96103
* Set a name for this relation
97-
*
98-
* @param name
99104
*/
100-
void setRelationName( const QString& name );
105+
void setName( const QString& name );
101106

102107
/**
103108
* Set the referencing (child) layer id. This layer will be searched in the registry.
104-
*
105-
* @param id
106109
*/
107110
void setReferencingLayer( const QString& id );
108111

109112
/**
110113
* Set the referenced (parent) layer id. This layer will be searched in the registry.
111-
*
112-
* @param id
113114
*/
114115
void setReferencedLayer( const QString& id );
115116

@@ -291,15 +292,28 @@ class CORE_EXPORT QgsRelation
291292
*/
292293
bool hasEqualDefinition( const QgsRelation& other ) const;
293294

294-
protected:
295+
/**
296+
* Get the referenced field counterpart given a referencing field.
297+
*
298+
* @note Added in QGIS 3.0
299+
*/
300+
Q_INVOKABLE QString resolveReferencedField( const QString& referencingField ) const;
301+
302+
/**
303+
* Get the referencing field counterpart given a referenced field.
304+
*
305+
* @note Added in QGIS 3.0
306+
*/
307+
Q_INVOKABLE QString resolveReferencingField( const QString& referencedField ) const;
308+
309+
private:
295310

296311
/**
297312
* Updates the validity status of this relation.
298313
* Will be called internally whenever a member is changed.
299314
*/
300315
void updateRelationStatus();
301316

302-
private:
303317
//! Unique Id
304318
QString mRelationId;
305319
//! Human redable name

‎src/core/qgsrelationmanager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CORE_EXPORT QgsRelationManager : public QObject
8484
* @return A relation. Invalid if not found.
8585
* @see relationsByName()
8686
*/
87-
QgsRelation relation( const QString& id ) const;
87+
Q_INVOKABLE QgsRelation relation( const QString& id ) const;
8888

8989
/** Returns a list of relations with matching names.
9090
* @param name relation name to search for. Searching is case insensitive.

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4174,7 +4174,7 @@ QList<QgsRelation> QgsPostgresProvider::discoverRelations( const QgsVectorLayer*
41744174
Q_FOREACH ( const QgsVectorLayer* foundLayer, foundLayers )
41754175
{
41764176
QgsRelation relation;
4177-
relation.setRelationName( name );
4177+
relation.setName( name );
41784178
relation.setReferencingLayer( self->id() );
41794179
relation.setReferencedLayer( foundLayer->id() );
41804180
relation.addFieldPair( fkColumn, refColumn );

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5486,7 +5486,7 @@ QList<QgsRelation> QgsSpatiaLiteProvider::discoverRelations( const QgsVectorLaye
54865486
Q_FOREACH ( const QgsVectorLayer* foundLayer, foundLayers )
54875487
{
54885488
QgsRelation relation;
5489-
relation.setRelationName( name );
5489+
relation.setName( name );
54905490
relation.setReferencingLayer( self->id() );
54915491
relation.setReferencedLayer( foundLayer->id() );
54925492
relation.addFieldPair( fkColumn, refColumn );

‎tests/src/core/testqgscomposerhtml.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ void TestQgsComposerHtml::javascriptSetFeature()
283283
mComposition->atlasComposition().setEnabled( true );
284284

285285
QgsRelation rel;
286-
rel.setRelationId( QStringLiteral( "rel1" ) );
287-
rel.setRelationName( QStringLiteral( "relation one" ) );
286+
rel.setId( QStringLiteral( "rel1" ) );
287+
rel.setName( QStringLiteral( "relation one" ) );
288288
rel.setReferencingLayer( childLayer->id() );
289289
rel.setReferencedLayer( parentLayer->id() );
290290
rel.addFieldPair( QStringLiteral( "y" ), QStringLiteral( "foreignkey" ) );

‎tests/src/core/testqgscomposertablev2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ void TestQgsComposerTableV2::attributeTableRelationSource()
493493

494494
//create a relation
495495
QgsRelation relation;
496-
relation.setRelationId( QStringLiteral( "testrelation" ) );
496+
relation.setId( QStringLiteral( "testrelation" ) );
497497
relation.setReferencedLayer( atlasLayer->id() );
498498
relation.setReferencingLayer( mVectorLayer->id() );
499499
relation.addFieldPair( QStringLiteral( "Class" ), QStringLiteral( "Class" ) );

‎tests/src/core/testqgsexpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class TestQgsExpression: public QObject
181181
QgsProject::instance()->addMapLayer( mChildLayer );
182182

183183
QgsRelation rel;
184-
rel.setRelationId( QStringLiteral( "my_rel" ) );
185-
rel.setRelationName( QStringLiteral( "relation name" ) );
184+
rel.setId( QStringLiteral( "my_rel" ) );
185+
rel.setName( QStringLiteral( "relation name" ) );
186186
rel.setReferencedLayer( mAggregatesLayer->id() );
187187
rel.setReferencingLayer( mChildLayer->id() );
188188
rel.addFieldPair( QStringLiteral( "parent" ), QStringLiteral( "col1" ) );

‎tests/src/gui/testqgseditorwidgetregistry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class TestQgsEditorWidgetRegistry: public QObject
121121

122122
//create a relation between them
123123
QgsRelation relation;
124-
relation.setRelationId( QStringLiteral( "vl1->vl2" ) );
125-
relation.setRelationName( QStringLiteral( "vl1->vl2" ) );
124+
relation.setId( QStringLiteral( "vl1->vl2" ) );
125+
relation.setName( QStringLiteral( "vl1->vl2" ) );
126126
relation.setReferencingLayer( vl1.id() );
127127
relation.setReferencedLayer( vl2.id() );
128128
relation.addFieldPair( "fk", "pk" );

‎tests/src/python/test_qgsfieldformatters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def test_representValue(self):
133133
fieldFormatter = QgsRelationReferenceFieldFormatter()
134134

135135
rel = QgsRelation()
136-
rel.setRelationId('rel1')
137-
rel.setRelationName('Relation Number One')
136+
rel.setId('rel1')
137+
rel.setName('Relation Number One')
138138
rel.setReferencingLayer(first_layer.id())
139139
rel.setReferencedLayer(second_layer.id())
140140
rel.addFieldPair('foreign_key', 'pkid')
@@ -184,8 +184,8 @@ def test_representValue(self):
184184

185185
# Invalid relation
186186
rel = QgsRelation()
187-
rel.setRelationId('rel2')
188-
rel.setRelationName('Relation Number Two')
187+
rel.setId('rel2')
188+
rel.setName('Relation Number Two')
189189
rel.setReferencingLayer(first_layer.id())
190190
rel.addFieldPair('foreign_key', 'pkid')
191191
self.assertFalse(rel.isValid())

‎tests/src/python/test_qgsjsonutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ def testExportFeatureRelations(self):
451451
QgsProject.instance().addMapLayers([child, parent])
452452

453453
rel = QgsRelation()
454-
rel.setRelationId('rel1')
455-
rel.setRelationName('relation one')
454+
rel.setId('rel1')
455+
rel.setName('relation one')
456456
rel.setReferencingLayer(child.id())
457457
rel.setReferencedLayer(parent.id())
458458
rel.addFieldPair('y', 'foreignkey')

‎tests/src/python/test_qgsrelation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def test_isValid(self):
8181
rel = QgsRelation()
8282
assert not rel.isValid()
8383

84-
rel.setRelationId('rel1')
84+
rel.setId('rel1')
8585
assert not rel.isValid()
8686

87-
rel.setRelationName('Relation Number One')
87+
rel.setName('Relation Number One')
8888
assert not rel.isValid()
8989

9090
rel.setReferencingLayer(self.referencingLayer.id())
@@ -99,8 +99,8 @@ def test_isValid(self):
9999
def test_getRelatedFeatures(self):
100100
rel = QgsRelation()
101101

102-
rel.setRelationId('rel1')
103-
rel.setRelationName('Relation Number One')
102+
rel.setId('rel1')
103+
rel.setName('Relation Number One')
104104
rel.setReferencingLayer(self.referencingLayer.id())
105105
rel.setReferencedLayer(self.referencedLayer.id())
106106
rel.addFieldPair('foreignkey', 'y')
@@ -114,8 +114,8 @@ def test_getRelatedFeatures(self):
114114

115115
def test_getReferencedFeature(self):
116116
rel = QgsRelation()
117-
rel.setRelationId('rel1')
118-
rel.setRelationName('Relation Number One')
117+
rel.setId('rel1')
118+
rel.setName('Relation Number One')
119119
rel.setReferencingLayer(self.referencingLayer.id())
120120
rel.setReferencedLayer(self.referencedLayer.id())
121121
rel.addFieldPair('foreignkey', 'y')
@@ -130,8 +130,8 @@ def test_getReferencedFeature(self):
130130
def test_fieldPairs(self):
131131
rel = QgsRelation()
132132

133-
rel.setRelationId('rel1')
134-
rel.setRelationName('Relation Number One')
133+
rel.setId('rel1')
134+
rel.setName('Relation Number One')
135135
rel.setReferencingLayer(self.referencingLayer.id())
136136
rel.setReferencedLayer(self.referencedLayer.id())
137137
rel.addFieldPair('foreignkey', 'y')

‎tests/src/python/test_qgsrelationeditwidget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ def setUpClass(cls):
6666
cls.rel_a.setReferencingLayer(cls.vl_link.id())
6767
cls.rel_a.setReferencedLayer(cls.vl_a.id())
6868
cls.rel_a.addFieldPair('fk_author', 'pk')
69-
cls.rel_a.setRelationId('rel_a')
69+
cls.rel_a.setId('rel_a')
7070
assert(cls.rel_a.isValid())
7171
cls.relMgr.addRelation(cls.rel_a)
7272

7373
cls.rel_b = QgsRelation()
7474
cls.rel_b.setReferencingLayer(cls.vl_link.id())
7575
cls.rel_b.setReferencedLayer(cls.vl_b.id())
7676
cls.rel_b.addFieldPair('fk_book', 'pk')
77-
cls.rel_b.setRelationId('rel_b')
77+
cls.rel_b.setId('rel_b')
7878
assert(cls.rel_b.isValid())
7979
cls.relMgr.addRelation(cls.rel_b)
8080

0 commit comments

Comments
 (0)