QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
239 QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
240
248 QString name() const;
249
255 Qgis::RelationshipStrength strength() const;
256
262 QString id() const;
263
267 void generateId();
268
275 QString referencingLayerId() const;
276
283 QgsVectorLayer *referencingLayer() const;
284
290 QString referencedLayerId() const;
291
297 QgsVectorLayer *referencedLayer() const;
298
306#ifndef SIP_RUN
307 QList< QgsRelation::FieldPair > fieldPairs() const;
308#else
309 QMap< QString, QString > fieldPairs() const;
310 % MethodCode
311 const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
312 sipRes = new QMap< QString, QString >();
313 for ( const QgsRelation::FieldPair &pair : pairs )
314 {
315 sipRes->insert( pair.first, pair.second );
316 }
317 % End
318#endif
319
326 QgsAttributeList referencedFields() const;
327
334 QgsAttributeList referencingFields() const;
335
340 bool referencingFieldsAllowNull() const;
341
350 bool isValid() const;
351
360 QString validationError() const;
361
368 bool hasEqualDefinition( const QgsRelation &other ) const;
369
374 Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
375
380 Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
381
388 void updateRelationStatus();
389
394 void setPolymorphicRelationId( const QString &polymorphicRelationId );
395
400 QString polymorphicRelationId() const;
401
406 QgsPolymorphicRelation polymorphicRelation() const;
407
412 Qgis::RelationshipType type() const;
413
419 static QString cardinalityToDisplayString( Qgis::RelationshipCardinality cardinality );
420
426 static QString strengthToDisplayString( Qgis::RelationshipStrength strength );
427
428 private:
429
430 mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
431
432 QgsRelationContext mContext;
433};
434
435// Register QgsRelation for usage with QVariant
437
438#endif // QGSRELATION_H
RelationshipStrength
Relationship strength.
Definition qgis.h:4153
RelationshipType
Relationship types.
Definition qgis.h:4139
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4165
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