QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
37 class CORE_EXPORT QgsRelation
38 {
39  Q_GADGET
40 
41  Q_PROPERTY( QString id READ id WRITE setId )
42  Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
43  Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
44  Q_PROPERTY( QString name READ name WRITE setName )
45  Q_PROPERTY( bool isValid READ isValid )
46 
47 
48  public:
49 
50 #ifndef SIP_RUN
51 
60  class FieldPair : public QPair< QString, QString >
61  {
62  public:
65  : QPair< QString, QString >() {}
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();
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 
174  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
175 
185  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
186 
195  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
196 
205  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
206 
214  QString name() const;
215 
221  QString id() const;
222 
227  void generateId();
228 
235  QString referencingLayerId() const;
236 
243  QgsVectorLayer *referencingLayer() const;
244 
250  QString referencedLayerId() const;
251 
257  QgsVectorLayer *referencedLayer() const;
258 
266 #ifndef SIP_RUN
267  QList< QgsRelation::FieldPair > fieldPairs() const;
268 #else
269  QMap< QString, QString > fieldPairs() const;
270  % MethodCode
271  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
272  sipRes = new QMap< QString, QString >();
273  Q_FOREACH ( const QgsRelation::FieldPair &pair, pairs )
274  {
275  sipRes->insert( pair.first, pair.second );
276  }
277  % End
278 #endif
279 
286  QgsAttributeList referencedFields() const;
287 
294  QgsAttributeList referencingFields() const;
295 
301  bool isValid() const;
302 
310  bool hasEqualDefinition( const QgsRelation &other ) const;
311 
317  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
318 
324  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
325 
326  private:
327 
332  void updateRelationStatus();
333 
335  QString mRelationId;
337  QString mRelationName;
339  QString mReferencingLayerId;
341  QgsVectorLayer *mReferencingLayer = nullptr;
343  QString mReferencedLayerId;
345  QgsVectorLayer *mReferencedLayer = nullptr;
346 
351  QList< FieldPair > mFieldPairs;
352 
353  bool mValid;
354 };
355 
356 // Register QgsRelation for usage with QVariant
358 
359 #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:61
Q_DECLARE_METATYPE(QModelIndex)
FieldPair()
Default constructor: NULL strings.
Definition: qgsrelation.h:64
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:60
#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:57
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