QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsrelation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrelation.cpp
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#include "qgsrelation.h"
17
18#include "qgsfeatureiterator.h"
19#include "qgslogger.h"
21#include "qgsproject.h"
22#include "qgsrelation_p.h"
23#include "qgsrelationmanager.h"
24#include "qgsvectorlayer.h"
25
26#include <QApplication>
27#include <QString>
28
29#include "moc_qgsrelation.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : d( new QgsRelationPrivate() )
35{
36}
37
39 : d( new QgsRelationPrivate() )
40 , mContext( context )
41{
42}
43
45
47 : d( other.d )
48 , mContext( other.mContext )
49{
50}
51
53 : d( std::move( other.d ) )
54 , mContext( std::move( other.mContext ) )
55{}
56
58{
59 if ( &other == this )
60 return *this;
61
62 d = other.d;
63 mContext = other.mContext;
64 return *this;
65}
66
68{
69 if ( &other == this )
70 return *this;
71
72 d = std::move( other.d );
73 mContext = std::move( other.mContext );
74 return *this;
75}
76
77QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext )
78{
79 QDomElement elem = node.toElement();
80
81 if ( elem.tagName() != "relation"_L1 )
82 {
83 QgsLogger::warning( QApplication::translate( "QgsRelation", "Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
84 }
85
86 QgsRelation relation( relationContext );
87
88 QString referencingLayerId = elem.attribute( u"referencingLayer"_s );
89 QString referencedLayerId = elem.attribute( u"referencedLayer"_s );
90 QString id = elem.attribute( u"id"_s );
91 QString name = context.projectTranslator()->translate( u"project:relations"_s, elem.attribute( u"name"_s ) );
92 QString strength = elem.attribute( u"strength"_s );
93
94 QMap<QString, QgsMapLayer *> mapLayers = relationContext.project()->mapLayers();
95
98
99 if ( !referencingLayer )
100 {
101 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which does not exist." ).arg( referencingLayerId ) );
102 }
103 else if ( Qgis::LayerType::Vector != referencingLayer->type() )
104 {
105 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencingLayerId ) );
106 }
107
108 if ( !referencedLayer )
109 {
110 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which does not exist." ).arg( referencedLayerId ) );
111 }
112 else if ( Qgis::LayerType::Vector != referencedLayer->type() )
113 {
114 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencedLayerId ) );
115 }
116
117 relation.d->mReferencingLayerId = referencingLayerId;
118 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( referencingLayer );
119 relation.d->mReferencedLayerId = referencedLayerId;
120 relation.d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer );
121 relation.d->mRelationId = id;
122 relation.d->mRelationName = name;
124
125 QDomNodeList references = elem.elementsByTagName( u"fieldRef"_s );
126 for ( int i = 0; i < references.size(); ++i )
127 {
128 QDomElement refEl = references.at( i ).toElement();
129
130 QString referencingField = refEl.attribute( u"referencingField"_s );
131 QString referencedField = refEl.attribute( u"referencedField"_s );
132
133 relation.addFieldPair( referencingField, referencedField );
134 }
135
136 relation.updateRelationStatus();
137
138 return relation;
139}
140
141void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
142{
143 QDomElement elem = doc.createElement( u"relation"_s );
144 elem.setAttribute( u"id"_s, d->mRelationId );
145 elem.setAttribute( u"name"_s, d->mRelationName );
146 elem.setAttribute( u"referencingLayer"_s, d->mReferencingLayerId );
147 elem.setAttribute( u"referencedLayer"_s, d->mReferencedLayerId );
148 elem.setAttribute( u"strength"_s, qgsEnumValueToKey<Qgis::RelationshipStrength>( d->mRelationStrength ) );
149
150 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
151 {
152 QDomElement referenceElem = doc.createElement( u"fieldRef"_s );
153 referenceElem.setAttribute( u"referencingField"_s, pair.first );
154 referenceElem.setAttribute( u"referencedField"_s, pair.second );
155 elem.appendChild( referenceElem );
156 }
157
158 node.appendChild( elem );
159}
160
161void QgsRelation::setId( const QString &id )
162{
163 d.detach();
164 d->mRelationId = id;
165
167}
168
169void QgsRelation::setName( const QString &name )
170{
171 d.detach();
172 d->mRelationName = name;
173}
174
175
177{
178 d.detach();
179 d->mRelationStrength = strength;
180}
181
182void QgsRelation::setReferencingLayer( const QString &id )
183{
184 d.detach();
185 d->mReferencingLayerId = id;
186
188}
189
190void QgsRelation::setReferencedLayer( const QString &id )
191{
192 d.detach();
193 d->mReferencedLayerId = id;
194
196}
197
198void QgsRelation::addFieldPair( const QString &referencingField, const QString &referencedField )
199{
200 d.detach();
201 d->mFieldPairs << FieldPair( referencingField, referencedField );
203}
204
205void QgsRelation::addFieldPair( const FieldPair &fieldPair )
206{
207 d.detach();
208 d->mFieldPairs << fieldPair;
210}
211
216
218{
219 QString filter = getRelatedFeaturesFilter( feature );
220 QgsDebugMsgLevel( u"Filter conditions: '%1'"_s.arg( filter ), 2 );
221
222 QgsFeatureRequest myRequest;
223 myRequest.setFilterExpression( filter );
224 return myRequest;
225}
226
228{
229 QStringList conditions;
230
231 if ( ! d->mPolymorphicRelationId.isEmpty() )
232 {
234 if ( polyRel.isValid() )
235 {
237 }
238 else
239 {
240 QgsDebugError( "The polymorphic relation is invalid" );
241 conditions << u" FALSE "_s;
242 }
243 }
244
245 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
246 {
247 QVariant val( feature.attribute( pair.referencedField() ) );
248 int referencingIdx = referencingLayer()->fields().lookupField( pair.referencingField() );
249 if ( referencingIdx >= 0 )
250 {
251 QMetaType::Type fieldType = referencingLayer()->fields().at( referencingIdx ).type();
252 conditions << QgsExpression::createFieldEqualityExpression( pair.referencingField(), val, fieldType );
253 }
254 else
255 {
256 conditions << QgsExpression::createFieldEqualityExpression( pair.referencingField(), val );
257 }
258 }
259
260 return conditions.join( " AND "_L1 );
261}
262
264{
265 QStringList conditions;
266
267 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
268 {
269 int referencedIdx = referencedLayer()->fields().lookupField( pair.referencedField() );
270 int referencingIdx = referencingLayer()->fields().lookupField( pair.referencingField() );
271 if ( referencedIdx >= 0 )
272 {
273 QMetaType::Type fieldType = referencedLayer()->fields().at( referencedIdx ).type();
274 conditions << QgsExpression::createFieldEqualityExpression( pair.referencedField(), attributes.at( referencingIdx ), fieldType );
275 }
276 else
277 {
278 conditions << QgsExpression::createFieldEqualityExpression( pair.referencedField(), attributes.at( referencingIdx ) );
279 }
280 }
281
282 QgsFeatureRequest myRequest;
283
284 QgsDebugMsgLevel( u"Filter conditions: '%1'"_s.arg( conditions.join( " AND " ) ), 2 );
285
286 myRequest.setFilterExpression( conditions.join( " AND "_L1 ) );
287
288 return myRequest;
289}
290
295
297{
299
300 QgsFeature f;
301 ( void )d->mReferencedLayer->getFeatures( request ).nextFeature( f );
302 return f;
303}
304
305QString QgsRelation::name() const
306{
307 return d->mRelationName;
308}
309
311{
312 return d->mRelationStrength;
313}
314
315QString QgsRelation::id() const
316{
317 return d->mRelationId;
318}
319
321{
322 if ( !d->mFieldPairs.isEmpty() )
323 {
324 const QgsRelation::FieldPair fieldPair = d->mFieldPairs.at( 0 );
325 d->mRelationId = u"%1_%2_%3_%4"_s
326 .arg( referencingLayerId(),
327 fieldPair.referencingField(),
329 fieldPair.referencedField() );
330 }
332}
333
335{
336 return d->mReferencingLayerId;
337}
338
340{
341 return d->mReferencingLayer;
342}
343
345{
346 return d->mReferencedLayerId;
347}
348
350{
351 return d->mReferencedLayer;
352}
353
354QList<QgsRelation::FieldPair> QgsRelation::fieldPairs() const
355{
356 return d->mFieldPairs;
357}
358
360{
361 QgsAttributeList attrs;
362 attrs.reserve( d->mFieldPairs.size() );
363 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
364 {
365 attrs << d->mReferencedLayer->fields().lookupField( pair.second );
366 }
367 return attrs;
368}
369
371{
372 QgsAttributeList attrs;
373
374 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
375 {
376 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
377 }
378 return attrs;
379
380}
381
383{
384 if ( ! referencingLayer() )
385 {
386 return false;
387 }
388
389 const auto fields = referencingFields();
390
391 return std::find_if( fields.constBegin(), fields.constEnd(), [&]( const auto & fieldIdx )
392 {
393 if ( !referencingLayer()->fields().exists( fieldIdx ) )
394 {
395 return false;
396 }
397 const QgsField field = referencingLayer()->fields().field( fieldIdx );
398 return field.constraints().constraints().testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull );
399 } ) == fields.constEnd();
400}
401
403{
404 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayer.isNull() && d->mReferencingLayer.data()->isValid() && d->mReferencedLayer.data()->isValid();
405}
406
408{
409 if ( isValid() )
410 return QString();
411
412 if ( d->mReferencingLayer.isNull() )
413 {
414 if ( d->mReferencingLayerId.isEmpty() )
415 return QObject::tr( "Referencing layer not set" );
416 else
417 return QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
418 }
419 else if ( !d->mReferencingLayer.data()->isValid() )
420 return QObject::tr( "Referencing layer %1 is not valid" ).arg( d->mReferencingLayerId );
421 else if ( d->mReferencedLayer.isNull() )
422 {
423 if ( d->mReferencedLayerId.isEmpty() )
424 return QObject::tr( "Referenced layer not set" );
425 else
426 return QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
427 }
428 else if ( !d->mReferencedLayer.data()->isValid() )
429 return QObject::tr( "Referenced layer %1 is not valid" ).arg( d->mReferencedLayerId );
430 else
431 return d->mValidationError;
432}
433
435{
436 return d->mReferencedLayerId == other.d->mReferencedLayerId && d->mReferencingLayerId == other.d->mReferencingLayerId && d->mFieldPairs == other.d->mFieldPairs;
437}
438
439QString QgsRelation::resolveReferencedField( const QString &referencingField ) const
440{
441 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
442 {
443 if ( pair.first == referencingField )
444 return pair.second;
445 }
446 return QString();
447}
448
449QString QgsRelation::resolveReferencingField( const QString &referencedField ) const
450{
451 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
452 {
453 if ( pair.second == referencedField )
454 return pair.first;
455 }
456 return QString();
457}
458
460{
461 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.project()->mapLayers();
462
463 d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] );
464 d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencedLayerId] );
465
466 d->mValid = true;
467
468 if ( d->mRelationId.isEmpty() )
469 {
470 QgsDebugError( u"Invalid relation: no ID"_s );
471 d->mValidationError = QObject::tr( "Relationship has no ID" );
472 d->mValid = false;
473 }
474 else
475 {
476 if ( !d->mReferencedLayer )
477 {
478 QgsDebugMsgLevel( u"Invalid relation: referenced layer does not exist. ID: %1"_s.arg( d->mReferencedLayerId ), 4 );
479 d->mValidationError = QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
480 d->mValid = false;
481 }
482 else if ( !d->mReferencingLayer )
483 {
484 QgsDebugMsgLevel( u"Invalid relation: referencing layer does not exist. ID: %2"_s.arg( d->mReferencingLayerId ), 4 );
485 d->mValidationError = QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
486 d->mValid = false;
487 }
488 else
489 {
490 if ( d->mFieldPairs.count() < 1 )
491 {
492 QgsDebugMsgLevel( u"Invalid relation: no pair of field is specified."_s, 4 );
493 d->mValidationError = QObject::tr( "No fields specified for relationship" );
494 d->mValid = false;
495 }
496
497 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
498 {
499 if ( -1 == d->mReferencingLayer->fields().lookupField( pair.first ) )
500 {
501 QgsDebugError( u"Invalid relation: field %1 does not exist in referencing layer %2"_s.arg( pair.first, d->mReferencingLayer->name() ) );
502 d->mValidationError = QObject::tr( "Field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() );
503 d->mValid = false;
504 break;
505 }
506 else if ( -1 == d->mReferencedLayer->fields().lookupField( pair.second ) )
507 {
508 QgsDebugError( u"Invalid relation: field %1 does not exist in referenced layer %2"_s.arg( pair.second, d->mReferencedLayer->name() ) );
509 d->mValidationError = QObject::tr( "Field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayer->name() );
510 d->mValid = false;
511 break;
512 }
513 }
514 }
515
516 }
517}
518
520{
521 d.detach();
522 d->mPolymorphicRelationId = polymorphicRelationId;
523}
524
526{
527 return d->mPolymorphicRelationId;
528}
529
531{
532 if ( ! mContext.project() || ! mContext.project()->relationManager() )
533 return QgsPolymorphicRelation();
534
535 return mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );
536}
537
539{
540 if ( d->mPolymorphicRelationId.isNull() )
542 else
544}
545
547{
548 switch ( cardinality )
549 {
551 return QObject::tr( "One-to-one" );
553 return QObject::tr( "One-to-many" );
555 return QObject::tr( "Many-to-one" );
557 return QObject::tr( "Many-to-many" );
558 }
560}
561
563{
564 switch ( strength )
565 {
567 return QObject::tr( "Association" );
569 return QObject::tr( "Composition" );
570 }
572}
RelationshipStrength
Relationship strength.
Definition qgis.h:4499
@ Composition
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
Definition qgis.h:4501
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition qgis.h:4500
RelationshipType
Relationship types.
Definition qgis.h:4485
@ Generated
A generated relation is a child of a polymorphic relation.
Definition qgis.h:4487
@ Normal
A normal relation.
Definition qgis.h:4486
@ Vector
Vector layer.
Definition qgis.h:194
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4511
@ ManyToMany
Many to many relationship.
Definition qgis.h:4515
@ ManyToOne
Many to one relationship.
Definition qgis.h:4514
@ OneToOne
One to one relationship.
Definition qgis.h:4512
@ OneToMany
One to many relationship.
Definition qgis.h:4513
A vector of attributes.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
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).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
@ ConstraintNotNull
Field may not be null.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QMetaType::Type type
Definition qgsfield.h:63
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static void warning(const QString &msg)
Goes to qWarning.
Base class for all map layer types.
Definition qgsmaplayer.h:83
A relation where the referenced (parent) layer is calculated based on fields from the referencing (ch...
QString layerRepresentation(const QgsVectorLayer *layer) const
Returns layer representation as evaluated string.
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
Translates a string using the Qt QTranslator mechanism.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
A container for the context for various read/write operations on objects.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
Context for relations.
const QgsProject * project() const
Gets the associated project.
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
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
static QString cardinalityToDisplayString(Qgis::RelationshipCardinality cardinality)
Returns a user-friendly translated string representing a relationship cardinality.
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.
Q_INVOKABLE QString resolveReferencingField(const QString &referencedField) const
Gets the referencing field counterpart given a referenced field.
bool hasEqualDefinition(const QgsRelation &other) const
Compares the two QgsRelation, ignoring the name and the ID.
static QgsRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
QString validationError() const
Returns a user-friendly explanation for why the relationship is invalid.
Q_INVOKABLE QString resolveReferencedField(const QString &referencingField) const
Gets the referenced field counterpart given a referencing field.
QString polymorphicRelationId
Definition qgsrelation.h:55
QgsVectorLayer * referencedLayer
Definition qgsrelation.h:50
Qgis::RelationshipType type() const
Returns the type of the relation.
void setStrength(Qgis::RelationshipStrength strength)
Set a strength for this relation.
static QString strengthToDisplayString(Qgis::RelationshipStrength strength)
Returns a user-friendly translated string representing a relationship strength.
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
bool referencingFieldsAllowNull() const
Returns true if none of the referencing fields has a NOT NULL constraint.
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...
void updateRelationStatus()
Updates the validity status of this relation.
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7145
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7126
#define BUILTIN_UNREACHABLE
Definition qgis.h:7524
QList< int > QgsAttributeList
Definition qgsfield.h:30
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59