20
20
#include " qgslogger.h"
21
21
#include " qgsproject.h"
22
22
#include " qgsvectorlayer.h"
23
+ #include " qgsrelation_p.h"
24
+
25
+ QgsRelation::QgsRelation ()
26
+ {
27
+ }
28
+
29
+ QgsRelation::~QgsRelation ()
30
+ {
31
+ }
32
+
33
+ QgsRelation::QgsRelation ( const QgsRelation &other )
34
+ : d( other.d )
35
+ {
36
+ }
37
+
38
+ QgsRelation &QgsRelation::operator =( const QgsRelation &other )
39
+ {
40
+ d = other.d ;
41
+ return *this ;
42
+ }
23
43
24
44
QgsRelation QgsRelation::createFromXml ( const QDomNode &node, QgsReadWriteContext &context )
25
45
{
@@ -61,19 +81,19 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContex
61
81
QgsLogger::warning ( QApplication::translate ( " QgsRelation" , " Relation defined for layer '%1' which is not of type VectorLayer." ).arg ( referencedLayerId ) );
62
82
}
63
83
64
- relation .mReferencingLayerId = referencingLayerId;
65
- relation .mReferencingLayer = qobject_cast<QgsVectorLayer *>( referencingLayer );
66
- relation .mReferencedLayerId = referencedLayerId;
67
- relation .mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer );
68
- relation .mRelationId = id;
69
- relation .mRelationName = name;
70
- if ( strength == " Composition" )
84
+ relation .d -> mReferencingLayerId = referencingLayerId;
85
+ relation .d -> mReferencingLayer = qobject_cast<QgsVectorLayer *>( referencingLayer );
86
+ relation .d -> mReferencedLayerId = referencedLayerId;
87
+ relation .d -> mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer );
88
+ relation .d -> mRelationId = id;
89
+ relation .d -> mRelationName = name;
90
+ if ( strength == QLatin1String ( " Composition" ) )
71
91
{
72
- relation .mRelationStrength = RelationStrength::Composition;
92
+ relation .d -> mRelationStrength = RelationStrength::Composition;
73
93
}
74
94
else
75
95
{
76
- relation .mRelationStrength = RelationStrength::Association;
96
+ relation .d -> mRelationStrength = RelationStrength::Association;
77
97
}
78
98
79
99
QDomNodeList references = elem.elementsByTagName ( QStringLiteral ( " fieldRef" ) );
@@ -95,11 +115,11 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContex
95
115
void QgsRelation::writeXml ( QDomNode &node, QDomDocument &doc ) const
96
116
{
97
117
QDomElement elem = doc.createElement ( QStringLiteral ( " relation" ) );
98
- elem.setAttribute ( QStringLiteral ( " id" ), mRelationId );
99
- elem.setAttribute ( QStringLiteral ( " name" ), mRelationName );
100
- elem.setAttribute ( QStringLiteral ( " referencingLayer" ), mReferencingLayerId );
101
- elem.setAttribute ( QStringLiteral ( " referencedLayer" ), mReferencedLayerId );
102
- if ( mRelationStrength == RelationStrength::Composition )
118
+ elem.setAttribute ( QStringLiteral ( " id" ), d-> mRelationId );
119
+ elem.setAttribute ( QStringLiteral ( " name" ), d-> mRelationName );
120
+ elem.setAttribute ( QStringLiteral ( " referencingLayer" ), d-> mReferencingLayerId );
121
+ elem.setAttribute ( QStringLiteral ( " referencedLayer" ), d-> mReferencedLayerId );
122
+ if ( d-> mRelationStrength == RelationStrength::Composition )
103
123
{
104
124
elem.setAttribute ( QStringLiteral ( " strength" ), QStringLiteral ( " Composition" ) );
105
125
}
@@ -108,7 +128,7 @@ void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
108
128
elem.setAttribute ( QStringLiteral ( " strength" ), QStringLiteral ( " Association" ) );
109
129
}
110
130
111
- Q_FOREACH ( const FieldPair &fields, mFieldPairs )
131
+ Q_FOREACH ( const FieldPair &fields, d-> mFieldPairs )
112
132
{
113
133
QDomElement referenceElem = doc.createElement ( QStringLiteral ( " fieldRef" ) );
114
134
referenceElem.setAttribute ( QStringLiteral ( " referencingField" ), fields.first );
@@ -121,45 +141,52 @@ void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
121
141
122
142
void QgsRelation::setId ( const QString &id )
123
143
{
124
- mRelationId = id;
144
+ d.detach ();
145
+ d->mRelationId = id;
125
146
126
147
updateRelationStatus ();
127
148
}
128
149
129
150
void QgsRelation::setName ( const QString &name )
130
151
{
131
- mRelationName = name;
152
+ d.detach ();
153
+ d->mRelationName = name;
132
154
}
133
155
134
156
135
157
void QgsRelation::setStrength ( RelationStrength strength )
136
158
{
137
- mRelationStrength = strength;
159
+ d.detach ();
160
+ d->mRelationStrength = strength;
138
161
}
139
162
140
163
void QgsRelation::setReferencingLayer ( const QString &id )
141
164
{
142
- mReferencingLayerId = id;
165
+ d.detach ();
166
+ d->mReferencingLayerId = id;
143
167
144
168
updateRelationStatus ();
145
169
}
146
170
147
171
void QgsRelation::setReferencedLayer ( const QString &id )
148
172
{
149
- mReferencedLayerId = id;
173
+ d.detach ();
174
+ d->mReferencedLayerId = id;
150
175
151
176
updateRelationStatus ();
152
177
}
153
178
154
179
void QgsRelation::addFieldPair ( const QString &referencingField, const QString &referencedField )
155
180
{
156
- mFieldPairs << FieldPair ( referencingField, referencedField );
181
+ d.detach ();
182
+ d->mFieldPairs << FieldPair ( referencingField, referencedField );
157
183
updateRelationStatus ();
158
184
}
159
185
160
186
void QgsRelation::addFieldPair ( const FieldPair &fieldPair )
161
187
{
162
- mFieldPairs << fieldPair;
188
+ d.detach ();
189
+ d->mFieldPairs << fieldPair;
163
190
updateRelationStatus ();
164
191
}
165
192
@@ -182,7 +209,7 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const
182
209
{
183
210
QStringList conditions;
184
211
185
- Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mFieldPairs )
212
+ Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, d-> mFieldPairs )
186
213
{
187
214
QVariant val ( feature.attribute ( fieldPair.referencedField () ) );
188
215
conditions << QgsExpression::createFieldEqualityExpression ( fieldPair.referencingField (), val );
@@ -195,7 +222,7 @@ QgsFeatureRequest QgsRelation::getReferencedFeatureRequest( const QgsAttributes
195
222
{
196
223
QStringList conditions;
197
224
198
- Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mFieldPairs )
225
+ Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, d-> mFieldPairs )
199
226
{
200
227
int referencingIdx = referencingLayer ()->fields ().indexFromName ( fieldPair.referencingField () );
201
228
conditions << QgsExpression::createFieldEqualityExpression ( fieldPair.referencedField (), attributes.at ( referencingIdx ) );
@@ -220,67 +247,67 @@ QgsFeature QgsRelation::getReferencedFeature( const QgsFeature &feature ) const
220
247
QgsFeatureRequest request = getReferencedFeatureRequest ( feature );
221
248
222
249
QgsFeature f;
223
- mReferencedLayer ->getFeatures ( request ).nextFeature ( f );
250
+ d-> mReferencedLayer ->getFeatures ( request ).nextFeature ( f );
224
251
return f;
225
252
}
226
253
227
254
QString QgsRelation::name () const
228
255
{
229
- return mRelationName ;
256
+ return d-> mRelationName ;
230
257
}
231
258
232
259
QgsRelation::RelationStrength QgsRelation::strength () const
233
260
{
234
- return mRelationStrength ;
261
+ return d-> mRelationStrength ;
235
262
}
236
263
237
264
QString QgsRelation::id () const
238
265
{
239
- return mRelationId ;
266
+ return d-> mRelationId ;
240
267
}
241
268
242
269
void QgsRelation::generateId ()
243
270
{
244
- mRelationId = QStringLiteral ( " %1_%2_%3_%4" )
245
- .arg ( referencingLayerId (),
246
- mFieldPairs .at ( 0 ).referencingField (),
247
- referencedLayerId (),
248
- mFieldPairs .at ( 0 ).referencedField () );
271
+ d-> mRelationId = QStringLiteral ( " %1_%2_%3_%4" )
272
+ .arg ( referencingLayerId (),
273
+ d-> mFieldPairs .at ( 0 ).referencingField (),
274
+ referencedLayerId (),
275
+ d-> mFieldPairs .at ( 0 ).referencedField () );
249
276
updateRelationStatus ();
250
277
}
251
278
252
279
QString QgsRelation::referencingLayerId () const
253
280
{
254
- return mReferencingLayerId ;
281
+ return d-> mReferencingLayerId ;
255
282
}
256
283
257
284
QgsVectorLayer *QgsRelation::referencingLayer () const
258
285
{
259
- return mReferencingLayer ;
286
+ return d-> mReferencingLayer ;
260
287
}
261
288
262
289
QString QgsRelation::referencedLayerId () const
263
290
{
264
- return mReferencedLayerId ;
291
+ return d-> mReferencedLayerId ;
265
292
}
266
293
267
294
QgsVectorLayer *QgsRelation::referencedLayer () const
268
295
{
269
- return mReferencedLayer ;
296
+ return d-> mReferencedLayer ;
270
297
}
271
298
272
299
QList<QgsRelation::FieldPair> QgsRelation::fieldPairs () const
273
300
{
274
- return mFieldPairs ;
301
+ return d-> mFieldPairs ;
275
302
}
276
303
277
304
QgsAttributeList QgsRelation::referencedFields () const
278
305
{
279
306
QgsAttributeList attrs;
280
307
281
- Q_FOREACH ( const FieldPair &pair, mFieldPairs )
308
+ Q_FOREACH ( const FieldPair &pair, d-> mFieldPairs )
282
309
{
283
- attrs << mReferencedLayer ->fields ().lookupField ( pair.second );
310
+ attrs << d-> mReferencedLayer ->fields ().lookupField ( pair.second );
284
311
}
285
312
return attrs;
286
313
}
@@ -289,27 +316,27 @@ QgsAttributeList QgsRelation::referencingFields() const
289
316
{
290
317
QgsAttributeList attrs;
291
318
292
- Q_FOREACH ( const FieldPair &pair, mFieldPairs )
319
+ Q_FOREACH ( const FieldPair &pair, d-> mFieldPairs )
293
320
{
294
- attrs << mReferencingLayer ->fields ().lookupField ( pair.first );
321
+ attrs << d-> mReferencingLayer ->fields ().lookupField ( pair.first );
295
322
}
296
323
return attrs;
297
324
298
325
}
299
326
300
327
bool QgsRelation::isValid () const
301
328
{
302
- return mValid ;
329
+ return d-> mValid ;
303
330
}
304
331
305
332
bool QgsRelation::hasEqualDefinition ( const QgsRelation &other ) const
306
333
{
307
- return mReferencedLayerId == other.mReferencedLayerId && mReferencingLayerId == other.mReferencingLayerId && mFieldPairs == other.mFieldPairs ;
334
+ return d-> mReferencedLayerId == other.d -> mReferencedLayerId && d-> mReferencingLayerId == other.d -> mReferencingLayerId && d-> mFieldPairs == other.d -> mFieldPairs ;
308
335
}
309
336
310
337
QString QgsRelation::resolveReferencedField ( const QString &referencingField ) const
311
338
{
312
- Q_FOREACH ( const FieldPair &pair, mFieldPairs )
339
+ Q_FOREACH ( const FieldPair &pair, d-> mFieldPairs )
313
340
{
314
341
if ( pair.first == referencingField )
315
342
return pair.second ;
@@ -319,7 +346,7 @@ QString QgsRelation::resolveReferencedField( const QString &referencingField ) c
319
346
320
347
QString QgsRelation::resolveReferencingField ( const QString &referencedField ) const
321
348
{
322
- Q_FOREACH ( const FieldPair &pair, mFieldPairs )
349
+ Q_FOREACH ( const FieldPair &pair, d-> mFieldPairs )
323
350
{
324
351
if ( pair.second == referencedField )
325
352
return pair.first ;
@@ -331,48 +358,48 @@ void QgsRelation::updateRelationStatus()
331
358
{
332
359
const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance ()->mapLayers ();
333
360
334
- mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[mReferencingLayerId ] );
335
- mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[mReferencedLayerId ] );
361
+ d-> mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d-> mReferencingLayerId ] );
362
+ d-> mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d-> mReferencedLayerId ] );
336
363
337
- mValid = true ;
364
+ d-> mValid = true ;
338
365
339
- if ( mRelationId .isEmpty () )
366
+ if ( d-> mRelationId .isEmpty () )
340
367
{
341
368
QgsDebugMsg ( " Invalid relation: no ID" );
342
- mValid = false ;
369
+ d-> mValid = false ;
343
370
}
344
371
else
345
372
{
346
- if ( !mReferencedLayer )
373
+ if ( !d-> mReferencedLayer )
347
374
{
348
- QgsDebugMsgLevel ( QStringLiteral ( " Invalid relation: referenced layer does not exist. ID: %1" ).arg ( mReferencedLayerId ), 4 );
349
- mValid = false ;
375
+ QgsDebugMsgLevel ( QStringLiteral ( " Invalid relation: referenced layer does not exist. ID: %1" ).arg ( d-> mReferencedLayerId ), 4 );
376
+ d-> mValid = false ;
350
377
}
351
- else if ( !mReferencingLayer )
378
+ else if ( !d-> mReferencingLayer )
352
379
{
353
- QgsDebugMsgLevel ( QStringLiteral ( " Invalid relation: referencing layer does not exist. ID: %2" ).arg ( mReferencingLayerId ), 4 );
354
- mValid = false ;
380
+ QgsDebugMsgLevel ( QStringLiteral ( " Invalid relation: referencing layer does not exist. ID: %2" ).arg ( d-> mReferencingLayerId ), 4 );
381
+ d-> mValid = false ;
355
382
}
356
383
else
357
384
{
358
- if ( mFieldPairs .count () < 1 )
385
+ if ( d-> mFieldPairs .count () < 1 )
359
386
{
360
387
QgsDebugMsgLevel ( " Invalid relation: no pair of field is specified." , 4 );
361
- mValid = false ;
388
+ d-> mValid = false ;
362
389
}
363
390
364
- Q_FOREACH ( const FieldPair &fieldPair, mFieldPairs )
391
+ Q_FOREACH ( const FieldPair &fieldPair, d-> mFieldPairs )
365
392
{
366
- if ( -1 == mReferencingLayer ->fields ().lookupField ( fieldPair.first ) )
393
+ if ( -1 == d-> mReferencingLayer ->fields ().lookupField ( fieldPair.first ) )
367
394
{
368
- QgsDebugMsg ( QStringLiteral ( " Invalid relation: field %1 does not exist in referencing layer %2" ).arg ( fieldPair.first , mReferencingLayer ->name () ) );
369
- mValid = false ;
395
+ QgsDebugMsg ( QStringLiteral ( " Invalid relation: field %1 does not exist in referencing layer %2" ).arg ( fieldPair.first , d-> mReferencingLayer ->name () ) );
396
+ d-> mValid = false ;
370
397
break ;
371
398
}
372
- else if ( -1 == mReferencedLayer ->fields ().lookupField ( fieldPair.second ) )
399
+ else if ( -1 == d-> mReferencedLayer ->fields ().lookupField ( fieldPair.second ) )
373
400
{
374
- QgsDebugMsg ( QStringLiteral ( " Invalid relation: field %1 does not exist in referencedg layer %2" ).arg ( fieldPair.second , mReferencedLayer ->name () ) );
375
- mValid = false ;
401
+ QgsDebugMsg ( QStringLiteral ( " Invalid relation: field %1 does not exist in referencedg layer %2" ).arg ( fieldPair.second , d-> mReferencedLayer ->name () ) );
402
+ d-> mValid = false ;
376
403
break ;
377
404
}
378
405
}
0 commit comments