QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "qgis_core.h"
20#include "qgis_sip.h"
21#include "qgsattributes.h"
22#include "qgsreadwritecontext.h"
23#include "qgsrelationcontext.h"
24
25#include <QDomNode>
26#include <QList>
27#include <QPair>
28
31class QgsVectorLayer;
32class QgsRelationPrivate;
34
41class CORE_EXPORT QgsRelation
42{
43 Q_GADGET
44
45 Q_PROPERTY( QString id READ id WRITE setId )
48 Q_PROPERTY( QList<int> referencingFields READ referencedFields )
51 Q_PROPERTY( QList<int> referencedFields READ referencedFields )
52 Q_PROPERTY( QString name READ name WRITE setName )
53 Q_PROPERTY( bool isValid READ isValid )
54 Q_PROPERTY( Qgis::RelationshipStrength strength READ strength WRITE setStrength )
57
58 public:
59
60#ifndef SIP_RUN
61
71 class FieldPair : public QPair< QString, QString >
72 {
73 public:
75 FieldPair() = default;
76
78 FieldPair( const QString &referencingField, const QString &referencedField )
79 : QPair< QString, QString >( referencingField, referencedField ) {}
80
82 QString referencingField() const { return first; }
84 QString referencedField() const { return second; }
85
86 bool operator==( const FieldPair &other ) const { return first == other.first && second == other.second; }
87 };
88#endif
89
95
99 QgsRelation( const QgsRelationContext &context );
100
106 QgsRelation( const QgsRelation &other );
108
114 QgsRelation &operator=( const QgsRelation &other );
116
126 static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext = QgsRelationContext() );
127
134 void writeXml( QDomNode &node, QDomDocument &doc ) const;
135
139 void setId( const QString &id );
140
144 void setName( const QString &name );
145
150
154 void setReferencingLayer( const QString &id );
155
159 void setReferencedLayer( const QString &id );
160
169 Q_INVOKABLE void addFieldPair( const QString &referencingField, const QString &referencedField );
170
179 void addFieldPair( const FieldPair &fieldPair ) SIP_SKIP;
180
191 QgsFeatureIterator getRelatedFeatures( const QgsFeature &feature ) const;
192
204
213 Q_INVOKABLE QString getRelatedFeaturesFilter( const QgsFeature &feature ) const;
214
224
234
244 Q_INVOKABLE QgsFeature getReferencedFeature( const QgsFeature &feature ) const;
245
253 QString name() const;
254
261
267 QString id() const;
268
272 void generateId();
273
280 QString referencingLayerId() const;
281
289
295 QString referencedLayerId() const;
296
303
311#ifndef SIP_RUN
312 QList< QgsRelation::FieldPair > fieldPairs() const;
313#else
314 QMap< QString, QString > fieldPairs() const;
315 % MethodCode
316 const QList< QgsRelation::FieldPair > &pairs = sipCpp->fieldPairs();
317 sipRes = new QMap< QString, QString >();
318 for ( const QgsRelation::FieldPair &pair : pairs )
319 {
320 sipRes->insert( pair.first, pair.second );
321 }
322 % End
323#endif
324
331 QgsAttributeList referencedFields() const;
332
339 QgsAttributeList referencingFields() const;
340
345 bool referencingFieldsAllowNull() const;
346
355 bool isValid() const;
356
365 QString validationError() const;
366
373 bool hasEqualDefinition( const QgsRelation &other ) const;
374
379 Q_INVOKABLE QString resolveReferencedField( const QString &referencingField ) const;
380
385 Q_INVOKABLE QString resolveReferencingField( const QString &referencedField ) const;
386
393 void updateRelationStatus();
394
399 void setPolymorphicRelationId( const QString &polymorphicRelationId );
400
405 QString polymorphicRelationId() const;
406
411 QgsPolymorphicRelation polymorphicRelation() const;
412
417 Qgis::RelationshipType type() const;
418
424 static QString cardinalityToDisplayString( Qgis::RelationshipCardinality cardinality );
425
431 static QString strengthToDisplayString( Qgis::RelationshipStrength strength );
432
433 private:
434
435 mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
436
437 QgsRelationContext mContext;
438};
439
440// Register QgsRelation for usage with QVariant
442
443#endif // QGSRELATION_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
RelationshipStrength
Relationship strength.
Definition qgis.h:4405
RelationshipType
Relationship types.
Definition qgis.h:4391
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4417
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
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 relation where the referenced (parent) layer is calculated based on fields from the referencing (ch...
A container for the context for various read/write operations on objects.
Context for relations.
Defines a relation between matching fields of the two involved tables of a relation.
Definition qgsrelation.h:72
QString referencingField() const
Gets the name of the referencing (child) field.
Definition qgsrelation.h:82
QString referencedField() const
Gets the name of the referenced (parent) field.
Definition qgsrelation.h:84
FieldPair(const QString &referencingField, const QString &referencedField)
Constructor which takes two fields.
Definition qgsrelation.h:78
bool operator==(const FieldPair &other) const
Definition qgsrelation.h:86
FieldPair()=default
Default constructor: NULL strings.
Represents a relationship between two vector layers.
Definition qgsrelation.h:42
QgsFeatureRequest getReferencedFeatureRequest(const QgsAttributes &attributes) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
QList< int > referencedFields
Definition qgsrelation.h:51
QString name
Definition qgsrelation.h:52
Q_INVOKABLE QgsFeature getReferencedFeature(const QgsFeature &feature) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
void setId(const QString &id)
Set an id for this relation.
QgsFeatureIterator getRelatedFeatures(const QgsFeature &feature) const
Creates an iterator which returns all the features on the referencing (child) layer which have a fore...
QgsRelation()
Default constructor.
void setReferencedLayer(const QString &id)
Set the referenced (parent) layer id.
void setPolymorphicRelationId(const QString &polymorphicRelationId)
Sets the parent polymorphic relation id.
QgsRelation & operator=(const QgsRelation &other)
Copies a relation.
void generateId()
Generate a (project-wide) unique id for this relation.
static QgsRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
QString polymorphicRelationId
Definition qgsrelation.h:55
QgsVectorLayer * referencedLayer
Definition qgsrelation.h:50
void setStrength(Qgis::RelationshipStrength strength)
Set a strength for this relation.
QString id
Definition qgsrelation.h:45
Q_INVOKABLE void addFieldPair(const QString &referencingField, const QString &referencedField)
Add a field pair which is part of this relation The first element of each pair are the field names of...
void setReferencingLayer(const QString &id)
Set the referencing (child) layer id.
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
QString referencedLayerId
Definition qgsrelation.h:49
QgsPolymorphicRelation polymorphicRelation
Definition qgsrelation.h:56
Qgis::RelationshipStrength strength
Definition qgsrelation.h:54
QgsVectorLayer * referencingLayer
Definition qgsrelation.h:47
QString referencingLayerId
Definition qgsrelation.h:46
void setName(const QString &name)
Set a name for this relation.
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
Q_INVOKABLE QString getRelatedFeaturesFilter(const QgsFeature &feature) const
Returns a filter expression which returns all the features on the referencing (child) layer which hav...
QList< int > referencingFields
Definition qgsrelation.h:48
QgsFeatureRequest getRelatedFeaturesRequest(const QgsFeature &feature) const
Creates a request to return all the features on the referencing (child) layer which have a foreign ke...
Represents a vector layer which manages a vector based dataset.
#define SIP_SKIP
Definition qgis_sip.h:134
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QList< int > QgsAttributeList
Definition qgsfield.h:28