QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
202  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
203 
212  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
213 
222  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
223 
231  QString name() const;
232 
239  RelationStrength strength() const;
240 
246  QString id() const;
247 
252  void generateId();
253 
260  QString referencingLayerId() const;
261 
268  QgsVectorLayer *referencingLayer() const;
269 
275  QString referencedLayerId() const;
276 
282  QgsVectorLayer *referencedLayer() const;
283 
291 #ifndef SIP_RUN
292  QList< QgsRelation::FieldPair > fieldPairs() const;
293 #else
294  QMap< QString, QString > fieldPairs() const;
295  % MethodCode
296  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
297  sipRes = new QMap< QString, QString >();
298  for ( const QgsRelation::FieldPair &pair : pairs )
299  {
300  sipRes->insert( pair.first, pair.second );
301  }
302  % End
303 #endif
304 
311  QgsAttributeList referencedFields() const;
312 
319  QgsAttributeList referencingFields() const;
320 
326  bool isValid() const;
327 
335  bool hasEqualDefinition( const QgsRelation &other ) const;
336 
342  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
343 
349  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
350 
351  private:
352 
357  void updateRelationStatus();
358 
360  QString mRelationId;
362  QString mRelationName;
364  QString mReferencingLayerId;
366  QgsVectorLayer *mReferencingLayer = nullptr;
368  QString mReferencedLayerId;
370  QgsVectorLayer *mReferencedLayer = nullptr;
371 
372  RelationStrength mRelationStrength = Association;
373 
379  QList< FieldPair > mFieldPairs;
380 
381  bool mValid = false;
382 };
383 
384 // Register QgsRelation for usage with QVariant
387 
388 #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
Gets 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
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:83