QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsweakrelation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsweakrelation.cpp - QgsWeakRelation
3 
4  ---------------------
5  begin : 5.12.2019
6  copyright : (C) 2019 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #include "qgsweakrelation.h"
17 
18 
19 QgsWeakRelation::QgsWeakRelation( const QString &relationId, const QString &relationName, const QgsRelation::RelationStrength strength,
20  const QString &referencingLayerId, const QString &referencingLayerName, const QString &referencingLayerSource, const QString &referencingLayerProviderKey,
21  const QString &referencedLayerId, const QString &referencedLayerName, const QString &referencedLayerSource, const QString &referencedLayerProviderKey,
22  const QList<QgsRelation::FieldPair> &fieldPairs )
23  : mReferencingLayer( referencingLayerId, referencingLayerName, referencingLayerSource, referencingLayerProviderKey )
24  , mReferencedLayer( referencedLayerId, referencedLayerName, referencedLayerSource, referencedLayerProviderKey )
25  , mRelationId( relationId )
26  , mRelationName( relationName )
27  , mStrength( strength )
28  , mFieldPairs( fieldPairs )
29 {
30 }
31 
33 {
34  QgsRelation relation;
35  relation.setId( mRelationId );
36  relation.setName( mRelationName );
37  relation.setStrength( mStrength );
38  QgsVectorLayerRef referencedLayerRef { mReferencedLayer };
39  QgsMapLayer *referencedLayer { referencedLayerRef.resolveWeakly( project, matchType ) };
40  if ( referencedLayer )
41  {
42  relation.setReferencedLayer( referencedLayer->id() );
43  }
44  QgsVectorLayerRef referencingLayerRef { mReferencingLayer };
45  QgsMapLayer *referencingLayer { referencingLayerRef.resolveWeakly( project, matchType ) };
46  if ( referencingLayer )
47  {
49  }
50  for ( const auto &fp : qgis::as_const( mFieldPairs ) )
51  {
52  relation.addFieldPair( fp );
53  }
54  return relation;
55 }
56 
58 {
59  return mReferencingLayer;
60 }
61 
63 {
64  return mReferencedLayer;
65 }
66 
68 {
69  return mStrength;
70 }
71 
72 QList<QgsRelation::FieldPair> QgsWeakRelation::fieldPairs() const
73 {
74  return mFieldPairs;
75 }
QgsWeakRelation::strength
QgsRelation::RelationStrength strength() const
Returns the strength of the relation.
Definition: qgsweakrelation.cpp:67
_LayerRef::resolveWeakly
TYPE * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
Definition: qgsmaplayerref.h:210
qgsweakrelation.h
QgsRelation::addFieldPair
void addFieldPair(const QString &referencingField, const QString &referencedField)
Add a field pairs which is part of this relation The first element of each pair are the field names o...
Definition: qgsrelation.cpp:186
QgsWeakRelation::resolvedRelation
QgsRelation resolvedRelation(const QgsProject *project, QgsVectorLayerRef::MatchType matchType=QgsVectorLayerRef::MatchType::All) const
Resolves a weak relation in the given project returning a possibly invalid QgsRelation and without pe...
Definition: qgsweakrelation.cpp:32
QgsWeakRelation::QgsWeakRelation
QgsWeakRelation(const QString &relationId, const QString &relationName, const QgsRelation::RelationStrength strength, const QString &referencingLayerId, const QString &referencingLayerName, const QString &referencingLayerSource, const QString &referencingLayerProviderKey, const QString &referencedLayerId, const QString &referencedLayerName, const QString &referencedLayerSource, const QString &referencedLayerProviderKey, const QList< QgsRelation::FieldPair > &fieldPairs)
Creates a QgsWeakRelation.
Definition: qgsweakrelation.cpp:19
QgsRelation::setStrength
void setStrength(RelationStrength strength)
Set a strength for this relation.
Definition: qgsrelation.cpp:164
QgsProject
Definition: qgsproject.h:92
QgsRelation::setReferencingLayer
void setReferencingLayer(const QString &id)
Set the referencing (child) layer id.
Definition: qgsrelation.cpp:170
QgsRelation::setName
void setName(const QString &name)
Set a name for this relation.
Definition: qgsrelation.cpp:157
QgsRelation::setId
void setId(const QString &id)
Set an id for this relation.
Definition: qgsrelation.cpp:149
QgsWeakRelation::fieldPairs
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the list of field pairs.
Definition: qgsweakrelation.cpp:72
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsWeakRelation::referencingLayer
QgsVectorLayerRef referencingLayer() const
Returns a weak reference to the referencing layer.
Definition: qgsweakrelation.cpp:57
_LayerRef< QgsVectorLayer >::MatchType
MatchType
Flag for match type in weak resolution.
Definition: qgsmaplayerref.h:40
QgsWeakRelation::referencedLayer
QgsVectorLayerRef referencedLayer() const
Returns a weak reference to the referenced layer.
Definition: qgsweakrelation.cpp:62
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsRelation::setReferencedLayer
void setReferencedLayer(const QString &id)
Set the referenced (parent) layer id.
Definition: qgsrelation.cpp:178
QgsRelation
Definition: qgsrelation.h:41
QgsRelation::RelationStrength
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:57
_LayerRef< QgsVectorLayer >