QGIS API Documentation  3.0.2-Girona (307d082)
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 
56  {
58  Composition
59 
60  };
61 
62 #ifndef SIP_RUN
63 
72  class FieldPair : public QPair< QString, QString >
73  {
74  public:
76  FieldPair() = default;
77 
79  FieldPair( const QString &referencingField, const QString &referencedField )
80  : QPair< QString, QString >( referencingField, referencedField ) {}
81 
83  QString referencingField() const { return first; }
85  QString referencedField() const { return second; }
86 
87  bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
88  };
89 #endif
90 
94  QgsRelation() = default;
95 
103  static QgsRelation createFromXml( const QDomNode &node );
104 
111  void writeXml( QDomNode &node, QDomDocument &doc ) const;
112 
116  void setId( const QString &id );
117 
121  void setName( const QString &name );
122 
127  void setStrength( RelationStrength strength );
128 
132  void setReferencingLayer( const QString &id );
133 
137  void setReferencedLayer( const QString &id );
138 
147  void addFieldPair( const QString &referencingField, const QString &referencedField );
148 
157  void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
158 
169  QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
170 
181  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
182 
192  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
193 
203  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
204 
213  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
214 
223  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
224 
232  QString name() const;
233 
240  RelationStrength strength() const;
241 
247  QString id() const;
248 
253  void generateId();
254 
261  QString referencingLayerId() const;
262 
269  QgsVectorLayer *referencingLayer() const;
270 
276  QString referencedLayerId() const;
277 
283  QgsVectorLayer *referencedLayer() const;
284 
292 #ifndef SIP_RUN
293  QList< QgsRelation::FieldPair > fieldPairs() const;
294 #else
295  QMap< QString, QString > fieldPairs() const;
296  % MethodCode
297  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
298  sipRes = new QMap< QString, QString >();
299  Q_FOREACH ( const QgsRelation::FieldPair &pair, pairs )
300  {
301  sipRes->insert( pair.first, pair.second );
302  }
303  % End
304 #endif
305 
312  QgsAttributeList referencedFields() const;
313 
320  QgsAttributeList referencingFields() const;
321 
327  bool isValid() const;
328 
336  bool hasEqualDefinition( const QgsRelation &other ) const;
337 
343  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
344 
350  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
351 
352  private:
353 
358  void updateRelationStatus();
359 
361  QString mRelationId;
363  QString mRelationName;
365  QString mReferencingLayerId;
367  QgsVectorLayer *mReferencingLayer = nullptr;
369  QString mReferencedLayerId;
371  QgsVectorLayer *mReferencedLayer = nullptr;
372 
373  RelationStrength mRelationStrength = Association;
374 
380  QList< FieldPair > mFieldPairs;
381 
382  bool mValid = false;
383 };
384 
385 // Register QgsRelation for usage with QVariant
388 
389 #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)
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition: qgsrelation.h:57
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:72
#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:87
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:55
QString referencedField() const
Get the name of the referenced (parent) field.
Definition: qgsrelation.h:85
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:79
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:83