QGIS API Documentation  3.21.0-Master (5b68dc587e)
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;
37 
42 class CORE_EXPORT QgsRelation
43 {
44  Q_GADGET
45 
46  Q_PROPERTY( QString id READ id WRITE setId )
47  Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
48  Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
49  Q_PROPERTY( QString name READ name WRITE setName )
50  Q_PROPERTY( bool isValid READ isValid )
51  Q_PROPERTY( QString polymorphicRelationId READ polymorphicRelationId WRITE setPolymorphicRelationId )
52  Q_PROPERTY( QgsPolymorphicRelation polymorphicRelation READ polymorphicRelation )
53 
54  public:
55 
60  {
63  };
64  Q_ENUM( RelationType )
65 
66 
71  {
73  Composition
74  };
75  Q_ENUM( RelationStrength )
76 
77 #ifndef SIP_RUN
78 
88  class FieldPair : public QPair< QString, QString >
89  {
90  public:
92  FieldPair() = default;
93 
95  FieldPair( const QString &referencingField, const QString &referencedField )
96  : QPair< QString, QString >( referencingField, referencedField ) {}
97 
99  QString referencingField() const { return first; }
101  QString referencedField() const { return second; }
102 
103  bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
104  };
105 #endif
106 
110  QgsRelation();
112 
116  QgsRelation( const QgsRelationContext &context );
117 
123  QgsRelation( const QgsRelation &other );
124 
130  QgsRelation &operator=( const QgsRelation &other );
131 
141  static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext = QgsRelationContext() );
142 
149  void writeXml( QDomNode &node, QDomDocument &doc ) const;
150 
154  void setId( const QString &id );
155 
159  void setName( const QString &name );
160 
165  void setStrength( RelationStrength strength );
166 
170  void setReferencingLayer( const QString &id );
171 
175  void setReferencedLayer( const QString &id );
176 
185  void addFieldPair( const QString &referencingField, const QString &referencedField );
186 
195  void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
196 
207  QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
208 
219  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
220 
230  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
231 
240  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
241 
250  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
251 
260  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
261 
269  QString name() const;
270 
277  RelationStrength strength() const;
278 
284  QString id() const;
285 
290  void generateId();
291 
298  QString referencingLayerId() const;
299 
306  QgsVectorLayer *referencingLayer() const;
307 
313  QString referencedLayerId() const;
314 
320  QgsVectorLayer *referencedLayer() const;
321 
329 #ifndef SIP_RUN
330  QList< QgsRelation::FieldPair > fieldPairs() const;
331 #else
332  QMap< QString, QString > fieldPairs() const;
333  % MethodCode
334  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
335  sipRes = new QMap< QString, QString >();
336  for ( const QgsRelation::FieldPair &pair : pairs )
337  {
338  sipRes->insert( pair.first, pair.second );
339  }
340  % End
341 #endif
342 
349  QgsAttributeList referencedFields() const;
350 
357  QgsAttributeList referencingFields() const;
358 
365  bool isValid() const;
366 
374  bool hasEqualDefinition( const QgsRelation &other ) const;
375 
381  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
382 
388  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
389 
396  void updateRelationStatus();
397 
402  void setPolymorphicRelationId( const QString &polymorphicRelationId );
403 
408  QString polymorphicRelationId() const;
409 
414  QgsPolymorphicRelation polymorphicRelation() const;
415 
420  RelationType type() const;
421 
422  private:
423 
424  mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
425 
426  QgsRelationContext mContext;
427 };
428 
429 // Register QgsRelation for usage with QVariant
432 
433 #endif // QGSRELATION_H
A vector of attributes.
Definition: qgsattributes.h:58
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
A polymorphic relation consists of the same properties like a normal relation except for the referenc...
The class is used as a container of context for various read/write operations on other objects.
Context for relations.
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:89
QString referencingField() const
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:99
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition: qgsrelation.h:101
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:95
bool operator==(const FieldPair &other) const
Definition: qgsrelation.h:103
FieldPair()=default
Default constructor: NULL strings.
RelationType
Enum holding the relations type.
Definition: qgsrelation.h:60
@ Generated
A generated relation is a child of a polymorphic relation.
Definition: qgsrelation.h:62
@ Normal
A normal relation.
Definition: qgsrelation.h:61
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:71
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition: qgsrelation.h:72
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition: qgis_sip.h:126
QList< int > QgsAttributeList
Definition: qgsfield.h:26
Q_DECLARE_METATYPE(QgsMeshTimeSettings)