QGIS API Documentation 4.1.0-Master (60fea48833c)
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
38 : d( new QgsRelationPrivate() )
39 , mContext( context )
40{}
41
43
45 : d( other.d )
46 , mContext( other.mContext )
47{}
48
50 : d( std::move( other.d ) )
51 , mContext( std::move( other.mContext ) )
52{}
53
55{
56 if ( &other == this )
57 return *this;
58
59 d = other.d;
60 mContext = other.mContext;
61 return *this;
62}
63
65{
66 if ( &other == this )
67 return *this;
68
69 d = std::move( other.d );
70 mContext = std::move( other.mContext );
71 return *this;
72}
73
74QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext )
75{
76 QDomElement elem = node.toElement();
77
78 if ( elem.tagName() != "relation"_L1 )
79 {
80 QgsLogger::warning( QApplication::translate( "QgsRelation", "Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
81 }
82
83 QgsRelation relation( relationContext );
84
85 QString referencingLayerId = elem.attribute( u"referencingLayer"_s );
86 QString referencedLayerId = elem.attribute( u"referencedLayer"_s );
87 QString id = elem.attribute( u"id"_s );
88 QString name = context.projectTranslator()->translate( u"project:relations"_s, elem.attribute( u"name"_s ) );
89 QString strength = elem.attribute( u"strength"_s );
90
91 QMap<QString, QgsMapLayer *> mapLayers = relationContext.project()->mapLayers();
92
95
96 if ( !referencingLayer )
97 {
98 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which does not exist." ).arg( referencingLayerId ) );
99 }
100 else if ( Qgis::LayerType::Vector != referencingLayer->type() )
101 {
102 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencingLayerId ) );
103 }
104
105 if ( !referencedLayer )
106 {
107 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which does not exist." ).arg( referencedLayerId ) );
108 }
109 else if ( Qgis::LayerType::Vector != referencedLayer->type() )
110 {
111 QgsLogger::warning( QApplication::translate( "QgsRelation", "Relation defined for layer '%1' which is not of type VectorLayer." ).arg( referencedLayerId ) );
112 }
113
114 relation.d->mReferencingLayerId = referencingLayerId;
115 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( referencingLayer );
116 relation.d->mReferencedLayerId = referencedLayerId;
117 relation.d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer );
118 relation.d->mRelationId = id;
119 relation.d->mRelationName = name;
121
122 QDomNodeList references = elem.elementsByTagName( u"fieldRef"_s );
123 for ( int i = 0; i < references.size(); ++i )
124 {
125 QDomElement refEl = references.at( i ).toElement();
126
127 QString referencingField = refEl.attribute( u"referencingField"_s );
128 QString referencedField = refEl.attribute( u"referencedField"_s );
129
130 relation.addFieldPair( referencingField, referencedField );
131 }
132
133 relation.updateRelationStatus();
134
135 return relation;
136}
137
138void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
139{
140 QDomElement elem = doc.createElement( u"relation"_s );
141 elem.setAttribute( u"id"_s, d->mRelationId );
142 elem.setAttribute( u"name"_s, d->mRelationName );
143 elem.setAttribute( u"referencingLayer"_s, d->mReferencingLayerId );
144 elem.setAttribute( u"referencedLayer"_s, d->mReferencedLayerId );
145 elem.setAttribute( u"strength"_s, qgsEnumValueToKey<Qgis::RelationshipStrength>( d->mRelationStrength ) );
146
147 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
148 {
149 QDomElement referenceElem = doc.createElement( u"fieldRef"_s );
150 referenceElem.setAttribute( u"referencingField"_s, pair.first );
151 referenceElem.setAttribute( u"referencedField"_s, pair.second );
152 elem.appendChild( referenceElem );
153 }
154
155 node.appendChild( elem );
156}
157
158void QgsRelation::setId( const QString &id )
159{
160 d.detach();
161 d->mRelationId = id;
162
164}
165
166void QgsRelation::setName( const QString &name )
167{
168 d.detach();
169 d->mRelationName = name;
170}
171
172
174{
175 d.detach();
176 d->mRelationStrength = strength;
177}
178
179void QgsRelation::setReferencingLayer( const QString &id )
180{
181 d.detach();
182 d->mReferencingLayerId = id;
183
185}
186
187void QgsRelation::setReferencedLayer( const QString &id )
188{
189 d.detach();
190 d->mReferencedLayerId = id;
191
193}
194
195void QgsRelation::addFieldPair( const QString &referencingField, const QString &referencedField )
196{
197 d.detach();
198 d->mFieldPairs << FieldPair( referencingField, referencedField );
200}
201
202void QgsRelation::addFieldPair( const FieldPair &fieldPair )
203{
204 d.detach();
205 d->mFieldPairs << fieldPair;
207}
208
213
215{
216 QString filter = getRelatedFeaturesFilter( feature );
217 QgsDebugMsgLevel( u"Filter conditions: '%1'"_s.arg( filter ), 2 );
218
219 QgsFeatureRequest myRequest;
220 myRequest.setFilterExpression( filter );
221 return myRequest;
222}
223
225{
226 QStringList conditions;
227
228 if ( !d->mPolymorphicRelationId.isEmpty() )
229 {
231 if ( polyRel.isValid() )
232 {
234 }
235 else
236 {
237 QgsDebugError( "The polymorphic relation is invalid" );
238 conditions << u" FALSE "_s;
239 }
240 }
241
242 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
243 {
244 QVariant val( feature.attribute( pair.referencedField() ) );
245 int referencingIdx = referencingLayer()->fields().lookupField( pair.referencingField() );
246 if ( referencingIdx >= 0 )
247 {
248 QMetaType::Type fieldType = referencingLayer()->fields().at( referencingIdx ).type();
249 conditions << QgsExpression::createFieldEqualityExpression( pair.referencingField(), val, fieldType );
250 }
251 else
252 {
253 conditions << QgsExpression::createFieldEqualityExpression( pair.referencingField(), val );
254 }
255 }
256
257 return conditions.join( " AND "_L1 );
258}
259
261{
262 QStringList conditions;
263
264 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
265 {
266 int referencedIdx = referencedLayer()->fields().lookupField( pair.referencedField() );
267 int referencingIdx = referencingLayer()->fields().lookupField( pair.referencingField() );
268 if ( referencedIdx >= 0 )
269 {
270 QMetaType::Type fieldType = referencedLayer()->fields().at( referencedIdx ).type();
271 conditions << QgsExpression::createFieldEqualityExpression( pair.referencedField(), attributes.at( referencingIdx ), fieldType );
272 }
273 else
274 {
275 conditions << QgsExpression::createFieldEqualityExpression( pair.referencedField(), attributes.at( referencingIdx ) );
276 }
277 }
278
279 QgsFeatureRequest myRequest;
280
281 QgsDebugMsgLevel( u"Filter conditions: '%1'"_s.arg( conditions.join( " AND " ) ), 2 );
282
283 myRequest.setFilterExpression( conditions.join( " AND "_L1 ) );
284
285 return myRequest;
286}
287
292
294{
296
297 QgsFeature f;
298 ( void ) d->mReferencedLayer->getFeatures( request ).nextFeature( f );
299 return f;
300}
301
302QString QgsRelation::name() const
303{
304 return d->mRelationName;
305}
306
308{
309 return d->mRelationStrength;
310}
311
312QString QgsRelation::id() const
313{
314 return d->mRelationId;
315}
316
318{
319 if ( !d->mFieldPairs.isEmpty() )
320 {
321 const QgsRelation::FieldPair fieldPair = d->mFieldPairs.at( 0 );
322 d->mRelationId = u"%1_%2_%3_%4"_s.arg( referencingLayerId(), fieldPair.referencingField(), referencedLayerId(), fieldPair.referencedField() );
323 }
325}
326
328{
329 return d->mReferencingLayerId;
330}
331
333{
334 return d->mReferencingLayer;
335}
336
338{
339 return d->mReferencedLayerId;
340}
341
343{
344 return d->mReferencedLayer;
345}
346
347QList<QgsRelation::FieldPair> QgsRelation::fieldPairs() const
348{
349 return d->mFieldPairs;
350}
351
353{
354 QgsAttributeList attrs;
355 attrs.reserve( d->mFieldPairs.size() );
356 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
357 {
358 attrs << d->mReferencedLayer->fields().lookupField( pair.second );
359 }
360 return attrs;
361}
362
364{
365 QgsAttributeList attrs;
366
367 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
368 {
369 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
370 }
371 return attrs;
372}
373
375{
376 if ( !referencingLayer() )
377 {
378 return false;
379 }
380
381 const auto fields = referencingFields();
382
383 return std::find_if(
384 fields.constBegin(),
385 fields.constEnd(),
386 [&]( const auto &fieldIdx ) {
387 if ( !referencingLayer()->fields().exists( fieldIdx ) )
388 {
389 return false;
390 }
391 const QgsField field = referencingLayer()->fields().field( fieldIdx );
392 return field.constraints().constraints().testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull );
393 }
394 )
395 == fields.constEnd();
396}
397
399{
400 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayer.isNull() && d->mReferencingLayer.data()->isValid() && d->mReferencedLayer.data()->isValid();
401}
402
404{
405 if ( isValid() )
406 return QString();
407
408 if ( d->mReferencingLayer.isNull() )
409 {
410 if ( d->mReferencingLayerId.isEmpty() )
411 return QObject::tr( "Referencing layer not set" );
412 else
413 return QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
414 }
415 else if ( !d->mReferencingLayer.data()->isValid() )
416 return QObject::tr( "Referencing layer %1 is not valid" ).arg( d->mReferencingLayerId );
417 else if ( d->mReferencedLayer.isNull() )
418 {
419 if ( d->mReferencedLayerId.isEmpty() )
420 return QObject::tr( "Referenced layer not set" );
421 else
422 return QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
423 }
424 else if ( !d->mReferencedLayer.data()->isValid() )
425 return QObject::tr( "Referenced layer %1 is not valid" ).arg( d->mReferencedLayerId );
426 else
427 return d->mValidationError;
428}
429
431{
432 return d->mReferencedLayerId == other.d->mReferencedLayerId && d->mReferencingLayerId == other.d->mReferencingLayerId && d->mFieldPairs == other.d->mFieldPairs;
433}
434
435QString QgsRelation::resolveReferencedField( const QString &referencingField ) const
436{
437 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
438 {
439 if ( pair.first == referencingField )
440 return pair.second;
441 }
442 return QString();
443}
444
445QString QgsRelation::resolveReferencingField( const QString &referencedField ) const
446{
447 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
448 {
449 if ( pair.second == referencedField )
450 return pair.first;
451 }
452 return QString();
453}
454
456{
457 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.project()->mapLayers();
458
459 d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] );
460 d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencedLayerId] );
461
462 d->mValid = true;
463
464 if ( d->mRelationId.isEmpty() )
465 {
466 QgsDebugError( u"Invalid relation: no ID"_s );
467 d->mValidationError = QObject::tr( "Relationship has no ID" );
468 d->mValid = false;
469 }
470 else
471 {
472 if ( !d->mReferencedLayer )
473 {
474 QgsDebugMsgLevel( u"Invalid relation: referenced layer does not exist. ID: %1"_s.arg( d->mReferencedLayerId ), 4 );
475 d->mValidationError = QObject::tr( "Referenced layer %1 does not exist" ).arg( d->mReferencedLayerId );
476 d->mValid = false;
477 }
478 else if ( !d->mReferencingLayer )
479 {
480 QgsDebugMsgLevel( u"Invalid relation: referencing layer does not exist. ID: %2"_s.arg( d->mReferencingLayerId ), 4 );
481 d->mValidationError = QObject::tr( "Referencing layer %1 does not exist" ).arg( d->mReferencingLayerId );
482 d->mValid = false;
483 }
484 else
485 {
486 if ( d->mFieldPairs.count() < 1 )
487 {
488 QgsDebugMsgLevel( u"Invalid relation: no pair of field is specified."_s, 4 );
489 d->mValidationError = QObject::tr( "No fields specified for relationship" );
490 d->mValid = false;
491 }
492
493 for ( const FieldPair &pair : std::as_const( d->mFieldPairs ) )
494 {
495 if ( -1 == d->mReferencingLayer->fields().lookupField( pair.first ) )
496 {
497 QgsDebugError( u"Invalid relation: field %1 does not exist in referencing layer %2"_s.arg( pair.first, d->mReferencingLayer->name() ) );
498 d->mValidationError = QObject::tr( "Field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() );
499 d->mValid = false;
500 break;
501 }
502 else if ( -1 == d->mReferencedLayer->fields().lookupField( pair.second ) )
503 {
504 QgsDebugError( u"Invalid relation: field %1 does not exist in referenced layer %2"_s.arg( pair.second, d->mReferencedLayer->name() ) );
505 d->mValidationError = QObject::tr( "Field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayer->name() );
506 d->mValid = false;
507 break;
508 }
509 }
510 }
511 }
512}
513
515{
516 d.detach();
517 d->mPolymorphicRelationId = polymorphicRelationId;
518}
519
521{
522 return d->mPolymorphicRelationId;
523}
524
526{
527 if ( !mContext.project() || !mContext.project()->relationManager() )
528 return QgsPolymorphicRelation();
529
530 return mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );
531}
532
534{
535 if ( d->mPolymorphicRelationId.isNull() )
537 else
539}
540
542{
543 switch ( cardinality )
544 {
546 return QObject::tr( "One-to-one" );
548 return QObject::tr( "One-to-many" );
550 return QObject::tr( "Many-to-one" );
552 return QObject::tr( "Many-to-many" );
553 }
555}
556
558{
559 switch ( strength )
560 {
562 return QObject::tr( "Association" );
564 return QObject::tr( "Composition" );
565 }
567}
RelationshipStrength
Relationship strength.
Definition qgis.h:4549
@ Composition
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
Definition qgis.h:4551
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
Definition qgis.h:4550
RelationshipType
Relationship types.
Definition qgis.h:4535
@ Generated
A generated relation is a child of a polymorphic relation.
Definition qgis.h:4537
@ Normal
A normal relation.
Definition qgis.h:4536
@ Vector
Vector layer.
Definition qgis.h:207
RelationshipCardinality
Relationship cardinality.
Definition qgis.h:4561
@ ManyToMany
Many to many relationship.
Definition qgis.h:4565
@ ManyToOne
Many to one relationship.
Definition qgis.h:4564
@ OneToOne
One to one relationship.
Definition qgis.h:4562
@ OneToMany
One to many relationship.
Definition qgis.h:4563
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:71
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:7176
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157
#define BUILTIN_UNREACHABLE
Definition qgis.h:7540
QList< int > QgsAttributeList
Definition qgsfield.h:30
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59