QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "qgsreadwritecontext.h"
25#include "qgsrelationcontext.h"
26#include "qgsattributes.h"
27
28#include "qgis_sip.h"
29
31class QgsFeature;
33class QgsVectorLayer;
34class QgsRelationPrivate;
36
41class 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 Q_PROPERTY( QString polymorphicRelationId READ polymorphicRelationId WRITE setPolymorphicRelationId )
51 Q_PROPERTY( QgsPolymorphicRelation polymorphicRelation READ polymorphicRelation )
52
53 public:
54
55#ifndef SIP_RUN
56
66 class FieldPair : public QPair< QString, QString >
67 {
68 public:
70 FieldPair() = default;
71
73 FieldPair( const QString &referencingField, const QString &referencedField )
74 : QPair< QString, QString >( referencingField, referencedField ) {}
75
77 QString referencingField() const { return first; }
79 QString referencedField() const { return second; }
80
81 bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
82 };
83#endif
84
90
94 QgsRelation( const QgsRelationContext &context );
95
101 QgsRelation( const QgsRelation &other );
102
108 QgsRelation &operator=( const QgsRelation &other );
109
119 static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext = QgsRelationContext() );
120
127 void writeXml( QDomNode &node, QDomDocument &doc ) const;
128
132 void setId( const QString &id );
133
137 void setName( const QString &name );
138
143 void setStrength( Qgis::RelationshipStrength strength );
144
148 void setReferencingLayer( const QString &id );
149
153 void setReferencedLayer( const QString &id );
154
163 void addFieldPair( const QString &referencingField, const QString &referencedField );
164
173 void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
174
185 QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
186
197 QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
198
208 QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
209
218 QgsFeatureRequest getReferencedFeatureRequest( const QgsAttributes &attributes ) const;
219
228 QgsFeatureRequest getReferencedFeatureRequest( const QgsFeature &feature ) const;
229
238 QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
239
247 QString name() const;
248
255 Qgis::RelationshipStrength strength() const;
256
262 QString id() const;
263
268 void generateId();
269
276 QString referencingLayerId() const;
277
284 QgsVectorLayer *referencingLayer() const;
285
291 QString referencedLayerId() const;
292
298 QgsVectorLayer *referencedLayer() const;
299
307#ifndef SIP_RUN
308 QList< QgsRelation::FieldPair > fieldPairs() const;
309#else
310 QMap< QString, QString > fieldPairs() const;
311 % MethodCode
312 const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
313 sipRes = new QMap< QString, QString >();
314 for ( const QgsRelation::FieldPair &pair : pairs )
315 {
316 sipRes->insert( pair.first, pair.second );
317 }
318 % End
319#endif
320
327 QgsAttributeList referencedFields() const;
328
335 QgsAttributeList referencingFields() const;
336
345 bool isValid() const;
346
355 QString validationError() const;
356
364 bool hasEqualDefinition( const QgsRelation &other ) const;
365
371 Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
372
378 Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
379
386 void updateRelationStatus();
387
392 void setPolymorphicRelationId( const QString &polymorphicRelationId );
393
398 QString polymorphicRelationId() const;
399
404 QgsPolymorphicRelation polymorphicRelation() const;
405
410 Qgis::RelationshipType type() const;
411
417 static QString cardinalityToDisplayString( Qgis::RelationshipCardinality cardinality );
418
424 static QString strengthToDisplayString( Qgis::RelationshipStrength strength );
425
426 private:
427
428 mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
429
430 QgsRelationContext mContext;
431};
432
433// Register QgsRelation for usage with QVariant
435
436#endif // QGSRELATION_H
RelationshipStrength
Relationship strength.
Definition: qgis.h:2215
RelationshipType
Relationship types.
Definition: qgis.h:2201
RelationshipCardinality
Relationship cardinality.
Definition: qgis.h:2227
A vector of attributes.
Definition: qgsattributes.h:59
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
A polymorphic relation consists of the same properties like a normal relation except for the referenc...
The class is used as a container of context for various read/write operations on other objects.
Context for relations.
Defines a relation between matching fields of the two involved tables of a relation.
Definition: qgsrelation.h:67
QString referencingField() const
Gets the name of the referencing (child) field.
Definition: qgsrelation.h:77
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition: qgsrelation.h:79
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition: qgsrelation.h:73
bool operator==(const FieldPair &other) const
Definition: qgsrelation.h:81
FieldPair()=default
Default constructor: NULL strings.
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QList< int > QgsAttributeList
Definition: qgsfield.h:26