QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include "qgsreadwritecontext.h"
26 
27 #include "qgis_sip.h"
28 
29 class QgsFeatureIterator;
30 class QgsFeature;
31 class QgsFeatureRequest;
32 class QgsAttributes;
33 class QgsVectorLayer;
34 class QgsRelationPrivate;
35 
36 
41 class CORE_EXPORT QgsRelation
42 {
43  Q_GADGET
44 
45  Q_PROPERTY( QString id READ id WRITE setId )
46  Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
47  Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
48  Q_PROPERTY( QString name READ name WRITE setName )
49  Q_PROPERTY( bool isValid READ isValid )
50 
51  public:
52 
58  {
60  Composition
61 
62  };
63 
64 #ifndef SIP_RUN
65 
74  class FieldPair : public QPair< QString, QString >
75  {
76  public:
78  FieldPair() = default;
79 
81  FieldPair( const QString &referencingField, const QString &referencedField )
82  : QPair< QString, QString >( referencingField, referencedField ) {}
83 
85  QString referencingField() const { return first; }
87  QString referencedField() const { return second; }
88 
89  bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
90  };
91 #endif
92 
96  QgsRelation();
97  ~QgsRelation();
98 
104  QgsRelation( const QgsRelation &other );
105 
111  QgsRelation &operator=( const QgsRelation &other );
112 
121  static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context );
122 
129  void writeXml( QDomNode &node, QDomDocument &doc ) const;
130 
134  void setId( const QString &id );
135 
139  void setName( const QString &name );
140 
145  void setStrength( RelationStrength strength );
146 
150  void setReferencingLayer( const QString &id );
151 
155  void setReferencedLayer( const QString &id );
156 
165  void addFieldPair( const QString &referencingField, const QString &referencedField );
166 
175  void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
176 
187  QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
188 
199  QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
200 
210  QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
211 
220  QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
221 
230  QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
231 
240  QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
241 
249  QString name() const;
250 
257  RelationStrength strength() const;
258 
264  QString id() const;
265 
270  void generateId();
271 
278  QString referencingLayerId() const;
279 
286  QgsVectorLayer *referencingLayer() const;
287 
293  QString referencedLayerId() const;
294 
300  QgsVectorLayer *referencedLayer() const;
301 
309 #ifndef SIP_RUN
310  QList< QgsRelation::FieldPair > fieldPairs() const;
311 #else
312  QMap< QString, QString > fieldPairs() const;
313  % MethodCode
314  const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
315  sipRes = new QMap< QString, QString >();
316  for ( const QgsRelation::FieldPair &pair : pairs )
317  {
318  sipRes->insert( pair.first, pair.second );
319  }
320  % End
321 #endif
322 
329  QgsAttributeList referencedFields() const;
330 
337  QgsAttributeList referencingFields() const;
338 
345  bool isValid() const;
346 
354  bool hasEqualDefinition( const QgsRelation &other ) const;
355 
361  Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
362 
368  Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
369 
376  void updateRelationStatus();
377 
378  private:
379 
380  mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
381 };
382 
383 // Register QgsRelation for usage with QVariant
386 
387 #endif // QGSRELATION_H
The class is used as a container of context for various read/write operations on other objects...
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:55
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:59
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:74
#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:89
RelationStrength
enum for the relation strength Association, Composition
Definition: qgsrelation.h:57
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition: qgsrelation.h:87
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:81
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
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:85