QGIS API Documentation  3.17.0-Master (a035f434f4)
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();
97  ~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
The class is used as a container of context for various read/write operations on other objects...
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition: qgsrelation.h:59
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:74
Context for relations.
#define SIP_SKIP
Definition: qgis_sip.h:126
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool operator==(const FieldPair &other) const
Definition: qgsrelation.h:89
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:57
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition: qgsrelation.h:87
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:81
QList< int > QgsAttributeList
Definition: qgsfield.h:26
A vector of attributes.
Definition: qgsattributes.h:57
Represents a vector layer which manages a vector based data sets.
QString referencingField() const
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:85