QGIS API Documentation  2.99.0-Master (ae4d26a)
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 
26 #include "qgis.h"
27 
28 class QgsVectorLayer;
29 class QgsFeatureIterator;
30 class QgsFeature;
31 class QgsFeatureRequest;
32 class QgsAttributes;
33 
38 class CORE_EXPORT QgsRelation
39 {
40  Q_GADGET
41 
42  Q_PROPERTY( QString id READ id WRITE setId )
43  Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
44  Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
45  Q_PROPERTY( QString name READ name WRITE setName )
46  Q_PROPERTY( bool isValid READ isValid )
47 
48 
49  public:
50 
51 #ifndef SIP_RUN
52 
61  class FieldPair : public QPair< QString, QString >
62  {
63  public:
65  FieldPair() {}
66 
68  FieldPair( const QString &referencingField, const QString &referencedField )
69  : QPair< QString, QString >( referencingField, referencedField ) {}
70 
72  QString referencingField() const { return first; }
74  QString referencedField() const { return second; }
75 
76  bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
77  };
78 #endif
79 
83  QgsRelation() = default;
84 
92  static QgsRelation createFromXml( const QDomNode &node );
93 
100  void writeXml( QDomNode &node, QDomDocument &doc ) const;
101 
105  void setId( const QString &id );
106 
110  void setName( const QString &name );
111 
115  void setReferencingLayer( const QString &id );
116 
120  void setReferencedLayer( const QString &id );
121 
130  void addFieldPair( const QString &referencingField, const QString &referencedField );
131 
140  void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
141 
152  QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
153 
164  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
165 
175  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
176 
186  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
187 
196  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
197 
206  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
207 
215  QString name() const;
216 
222  QString id() const;
223 
228  void generateId();
229 
236  QString referencingLayerId() const;
237 
244  QgsVectorLayer *referencingLayer() const;
245 
251  QString referencedLayerId() const;
252 
258  QgsVectorLayer *referencedLayer() const;
259 
267 #ifndef SIP_RUN
268  QList< QgsRelation::FieldPair > fieldPairs() const;
269 #else
270  QMap< QString, QString > fieldPairs() const;
271  % MethodCode
272  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
273  sipRes = new QMap< QString, QString >();
274  Q_FOREACH ( const QgsRelation::FieldPair &pair, pairs )
275  {
276  sipRes->insert( pair.first, pair.second );
277  }
278  % End
279 #endif
280 
287  QgsAttributeList referencedFields() const;
288 
295  QgsAttributeList referencingFields() const;
296 
302  bool isValid() const;
303 
311  bool hasEqualDefinition( const QgsRelation &other ) const;
312 
318  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
319 
325  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
326 
327  private:
328 
333  void updateRelationStatus();
334 
336  QString mRelationId;
338  QString mRelationName;
340  QString mReferencingLayerId;
342  QgsVectorLayer *mReferencingLayer = nullptr;
344  QString mReferencedLayerId;
346  QgsVectorLayer *mReferencedLayer = nullptr;
347 
353  QList< FieldPair > mFieldPairs;
354 
355  bool mValid = false;
356 };
357 
358 // Register QgsRelation for usage with QVariant
360 
361 #endif // QGSRELATION_H
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:62
Q_DECLARE_METATYPE(QModelIndex)
FieldPair()
Default constructor: NULL strings.
Definition: qgsrelation.h:65
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:61
#define SIP_SKIP
Definition: qgis_sip.h:119
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:76
QString referencedField() const
Get the name of the referenced (parent) field.
Definition: qgsrelation.h:74
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:68
QList< int > QgsAttributeList
Definition: qgsfield.h:27
A vector of attributes.
Definition: qgsattributes.h:58
Represents a vector layer which manages a vector based data sets.
QString referencingField() const
Get the name of the referencing (child) field.
Definition: qgsrelation.h:72