QGIS API Documentation  2.99.0-Master (08c2e66)
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 "qgis_core.h"
20 #include <QList>
21 #include <QDomNode>
22 #include <QPair>
23 
24 #include "qgsfields.h"
25 
26 class QgsVectorLayer;
27 class QgsFeatureIterator;
28 class QgsFeature;
29 class QgsFeatureRequest;
30 class QgsAttributes;
31 
35 class CORE_EXPORT QgsRelation
36 {
37  Q_GADGET
38 
39  Q_PROPERTY( QString id READ id WRITE setId )
40  Q_PROPERTY( QgsVectorLayer* referencingLayer READ referencingLayer )
41  Q_PROPERTY( QgsVectorLayer* referencedLayer READ referencedLayer )
42  Q_PROPERTY( QString name READ name WRITE setName )
43  Q_PROPERTY( bool isValid READ isValid )
44 
45 
46  public:
47 
56  class FieldPair : public QPair< QString, QString >
57  {
58  public:
61  : QPair< QString, QString >() {}
62 
64  FieldPair( const QString& referencingField, const QString& referencedField )
65  : QPair< QString, QString >( referencingField, referencedField ) {}
66 
68  QString referencingField() const { return first; }
70  QString referencedField() const { return second; }
71 
72  bool operator==( const FieldPair& other ) const { return first == other.first && second == other.second; }
73  };
74 
78  QgsRelation();
79 
87  static QgsRelation createFromXml( const QDomNode& node );
88 
95  void writeXml( QDomNode& node, QDomDocument& doc ) const;
96 
100  void setId( const QString& id );
101 
105  void setName( const QString& name );
106 
110  void setReferencingLayer( const QString& id );
111 
115  void setReferencedLayer( const QString& id );
116 
125  void addFieldPair( const QString& referencingField, const QString& referencedField );
126 
135  void addFieldPair( const FieldPair& fieldPair );
136 
147  QgsFeatureIterator getRelatedFeatures( const QgsFeature& feature ) const;
148 
159  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature& feature ) const;
160 
169  QString getRelatedFeaturesFilter( const QgsFeature& feature ) const;
170 
180  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes& attributes ) const;
181 
190  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature& feature ) const;
191 
200  QgsFeature getReferencedFeature( const QgsFeature& feature ) const;
201 
209  QString name() const;
210 
216  QString id() const;
217 
222  void generateId();
223 
230  QString referencingLayerId() const;
231 
238  QgsVectorLayer* referencingLayer() const;
239 
245  QString referencedLayerId() const;
246 
252  QgsVectorLayer* referencedLayer() const;
253 
261  QList< FieldPair > fieldPairs() const;
262 
269  QgsAttributeList referencedFields() const;
270 
277  QgsAttributeList referencingFields() const;
278 
284  bool isValid() const;
285 
293  bool hasEqualDefinition( const QgsRelation& other ) const;
294 
300  Q_INVOKABLE QString resolveReferencedField( const QString& referencingField ) const;
301 
307  Q_INVOKABLE QString resolveReferencingField( const QString& referencedField ) const;
308 
309  private:
310 
315  void updateRelationStatus();
316 
318  QString mRelationId;
320  QString mRelationName;
322  QString mReferencingLayerId;
324  QgsVectorLayer* mReferencingLayer = nullptr;
326  QString mReferencedLayerId;
328  QgsVectorLayer* mReferencedLayer = nullptr;
329 
334  QList< FieldPair > mFieldPairs;
335 
336  bool mValid;
337 };
338 
339 // Register QgsRelation for usage with QVariant
341 
342 #endif // QGSRELATION_H
Wrapper for iterator of features from vector data provider or vector layer.
QString referencedField() const
Get the name of the referenced (parent) field.
Definition: qgsrelation.h:70
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
FieldPair()
Default constructor: NULL strings.
Definition: qgsrelation.h:60
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:56
QString referencingField() const
Get the name of the referencing (child) field.
Definition: qgsrelation.h:68
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:64
A vector of attributes.
Definition: qgsfeature.h:56
Represents a vector layer which manages a vector based data sets.
bool operator==(const FieldPair &other) const
Definition: qgsrelation.h:72