QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsrelation.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelation.h
3  --------------------------------------
4  Date : 29.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSRELATION_H
17 #define QGSRELATION_H
18 
19 #include <QList>
20 #include <QDomNode>
21 #include <QPair>
22 
23 #include "qgis_core.h"
24 #include "qgsfields.h"
25 #include "qgsreadwritecontext.h"
26 #include "qgsrelationcontext.h"
27 
28 #include "qgis_sip.h"
29 
30 class QgsFeatureIterator;
31 class QgsFeature;
32 class QgsFeatureRequest;
33 class QgsAttributes;
34 class QgsVectorLayer;
35 class QgsRelationPrivate;
36 
41 class CORE_EXPORT QgsRelation
42 {
43  Q_GADGET
44 
45  Q_PROPERTY( QString id READ id WRITE setId )
46  Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
47  Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
48  Q_PROPERTY( QString name READ name WRITE setName )
49  Q_PROPERTY( bool isValid READ isValid )
50 
51  public:
52 
58  {
60  Composition
61  };
62  Q_ENUM( RelationStrength )
63 
64 #ifndef SIP_RUN
65 
74  class FieldPair : public QPair< QString, QString >
75  {
76  public:
78  FieldPair() = default;
79 
81  FieldPair( const QString &referencingField, const QString &referencedField )
82  : QPair< QString, QString >( referencingField, referencedField ) {}
83 
85  QString referencingField() const { return first; }
87  QString referencedField() const { return second; }
88 
89  bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
90  };
91 #endif
92 
96  QgsRelation();
98 
102  QgsRelation( const QgsRelationContext &context );
103 
109  QgsRelation( const QgsRelation &other );
110 
116  QgsRelation &operator=( const QgsRelation &other );
117 
127  static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext = QgsRelationContext() );
128 
135  void writeXml( QDomNode &node, QDomDocument &doc ) const;
136 
140  void setId( const QString &id );
141 
145  void setName( const QString &name );
146 
151  void setStrength( RelationStrength strength );
152 
156  void setReferencingLayer( const QString &id );
157 
161  void setReferencedLayer( const QString &id );
162 
171  void addFieldPair( const QString &referencingField, const QString &referencedField );
172 
181  void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
182 
193  QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
194 
205  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
206 
216  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
217 
226  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
227 
236  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
237 
246  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
247 
255  QString name() const;
256 
263  RelationStrength strength() const;
264 
270  QString id() const;
271 
276  void generateId();
277 
284  QString referencingLayerId() const;
285 
292  QgsVectorLayer *referencingLayer() const;
293 
299  QString referencedLayerId() const;
300 
306  QgsVectorLayer *referencedLayer() const;
307 
315 #ifndef SIP_RUN
316  QList< QgsRelation::FieldPair > fieldPairs() const;
317 #else
318  QMap< QString, QString > fieldPairs() const;
319  % MethodCode
320  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
321  sipRes = new QMap< QString, QString >();
322  for ( const QgsRelation::FieldPair &pair : pairs )
323  {
324  sipRes->insert( pair.first, pair.second );
325  }
326  % End
327 #endif
328 
335  QgsAttributeList referencedFields() const;
336 
343  QgsAttributeList referencingFields() const;
344 
351  bool isValid() const;
352 
360  bool hasEqualDefinition( const QgsRelation &other ) const;
361 
367  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
368 
374  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
375 
382  void updateRelationStatus();
383 
384  private:
385 
386  mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
387 
388  QgsRelationContext mContext;
389 };
390 
391 // Register QgsRelation for usage with QVariant
394 
395 #endif // QGSRELATION_H
qgsfields.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgsreadwritecontext.h
QgsRelation::FieldPair::FieldPair
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:81
QgsRelation::FieldPair::referencingField
QString referencingField() const
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:85
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
QgsRelation::FieldPair::operator==
bool operator==(const FieldPair &other) const
Definition: qgsrelation.h:89
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
QgsRelation::Association
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition: qgsrelation.h:59
QgsRelation::FieldPair
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:75
QgsRelation::FieldPair::referencedField
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition: qgsrelation.h:87
QgsRelation::~QgsRelation
~QgsRelation()
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsRelation
Definition: qgsrelation.h:42
QgsRelation::FieldPair::FieldPair
FieldPair()=default
Default constructor: NULL strings.
QgsAttributes
A vector of attributes.
Definition: qgsattributes.h:58
QgsRelation::RelationStrength
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:58
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsRelationContext
Context for relations.
Definition: qgsrelationcontext.h:32
qgsrelationcontext.h
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265