QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
43class CORE_EXPORT QgsRelation
44{
45 Q_GADGET
46
47 Q_PROPERTY( QString id READ id WRITE setId )
48 Q_PROPERTY( QgsVectorLayer *referencingLayer READ referencingLayer )
49 Q_PROPERTY( QgsVectorLayer *referencedLayer READ referencedLayer )
50 Q_PROPERTY( QString name READ name WRITE setName )
51 Q_PROPERTY( bool isValid READ isValid )
52 Q_PROPERTY( QString polymorphicRelationId READ polymorphicRelationId WRITE setPolymorphicRelationId )
53 Q_PROPERTY( QgsPolymorphicRelation polymorphicRelation READ polymorphicRelation )
54
55 public:
56
57#ifndef SIP_RUN
58
68 class FieldPair : public QPair< QString, QString >
69 {
70 public:
72 FieldPair() = default;
73
75 FieldPair( const QString &referencingField, const QString &referencedField )
76 : QPair< QString, QString >( referencingField, referencedField ) {}
77
79 QString referencingField() const { return first; }
81 QString referencedField() const { return second; }
82
83 bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
84 };
85#endif
86
92
96 QgsRelation( const QgsRelationContext &context );
97
103 QgsRelation( const QgsRelation &other );
104
110 QgsRelation &operator=( const QgsRelation &other );
111
121 static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext = QgsRelationContext() );
122
129 void writeXml( QDomNode &node, QDomDocument &doc ) const;
130
134 void setId( const QString &id );
135
139 void setName( const QString &name );
140
144 void setStrength( Qgis::RelationshipStrength strength );
145
149 void setReferencingLayer( const QString &id );
150
154 void setReferencedLayer( const QString &id );
155
164 void addFieldPair( const QString &referencingField, const QString &referencedField );
165
174 void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
175
186 QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
187
198 QgsFeatureRequest getRelatedFeaturesRequest( const QgsFeature &feature ) const;
199
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
254 Qgis::RelationshipStrength strength() const;
255
261 QString id() const;
262
266 void generateId();
267
274 QString referencingLayerId() const;
275
282 QgsVectorLayer *referencingLayer() const;
283
289 QString referencedLayerId() const;
290
296 QgsVectorLayer *referencedLayer() const;
297
305#ifndef SIP_RUN
306 QList< QgsRelation::FieldPair > fieldPairs() const;
307#else
308 QMap< QString, QString > fieldPairs() const;
309 % MethodCode
310 const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
311 sipRes = new QMap< QString, QString >();
312 for ( const QgsRelation::FieldPair &pair : pairs )
313 {
314 sipRes->insert( pair.first, pair.second );
315 }
316 % End
317#endif
318
325 QgsAttributeList referencedFields() const;
326
333 QgsAttributeList referencingFields() const;
334
339 bool referencingFieldsAllowNull() const;
340
349 bool isValid() const;
350
359 QString validationError() const;
360
367 bool hasEqualDefinition( const QgsRelation &other ) const;
368
373 Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
374
379 Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
380
387 void updateRelationStatus();
388
393 void setPolymorphicRelationId( const QString &polymorphicRelationId );
394
399 QString polymorphicRelationId() const;
400
405 QgsPolymorphicRelation polymorphicRelation() const;
406
411 Qgis::RelationshipType type() const;
412
418 static QString cardinalityToDisplayString( Qgis::RelationshipCardinality cardinality );
419
425 static QString strengthToDisplayString( Qgis::RelationshipStrength strength );
426
427 private:
428
429 mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
430
431 QgsRelationContext mContext;
432};
433
434// Register QgsRelation for usage with QVariant
436
437#endif // QGSRELATION_H
RelationshipStrength
Relationship strength.
Definition qgis.h:4108
RelationshipType
Relationship types.
Definition qgis.h:4094
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4120
A vector of attributes.
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:58
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:69
QString referencingField() const
Gets the name of the referencing (child) field.
Definition qgsrelation.h:79
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition qgsrelation.h:81
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition qgsrelation.h:75
bool operator==(const FieldPair &other) const
Definition qgsrelation.h:83
FieldPair()=default
Default constructor: NULL strings.
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
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:27