Skip to content

Commit 7e6695b

Browse files
committed
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

Lines changed: 98 additions & 78 deletions

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" ) );

0 commit comments

Comments
 (0)