QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QgsRelation::RelationStrength strength() const
Returns the strength of the relation.
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...
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...
void setReferencingLayer(const QString &id)
Set the referencing (child) layer id.
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...
void setName(const QString &name)
Set a name for this relation.
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.
QgsVectorLayerRef referencingLayer() const
Returns a weak reference to the referencing layer.
void setId(const QString &id)
Set an id for this relation.
void setStrength(RelationStrength strength)
Set a strength for this relation.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:91
MatchType
Flag for match type in weak resolution.
void setReferencedLayer(const QString &id)
Set the referenced (parent) layer id.
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:57
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the list of field pairs.
QgsVectorLayerRef referencedLayer() const
Returns a weak reference to the referenced layer.