23 #include <QDomElement>
36 QList<QgsVectorLayer*> lst;
47 if ( mark.value( n ) == 1 )
49 if ( mark.value( n ) == 0 )
65 mVectorJoins.push_back( joinInfo );
69 QHash<QgsVectorLayer*, int> markDFS;
73 mVectorJoins.pop_back();
80 cacheJoinLayer( mVectorJoins.last() );
88 connect( vl, SIGNAL( updatedFields() ),
this, SLOT( joinedLayerUpdatedFields() ), Qt::UniqueConnection );
97 for (
int i = 0; i < mVectorJoins.size(); ++i )
99 if ( mVectorJoins.at( i ).joinLayerId == joinLayerId )
101 mVectorJoins.removeAt( i );
106 disconnect( vl, SIGNAL( updatedFields() ),
this, SLOT( joinedLayerUpdatedFields() ) );
139 QVector<int> subsetIndices;
146 if ( !cacheLayerAttrs.contains( joinFieldIndex ) )
147 cacheLayerAttrs.append( joinFieldIndex );
156 QString key = attrs[joinFieldIndex].toString();
160 for (
int i = 0; i < subsetIndices.count(); ++i )
161 subsetAttrs[i] = attrs[ subsetIndices[i] ];
167 attrs2.remove( joinFieldIndex );
177 QVector<int> subsetIndices;
179 for (
int i = 0; i < joinFieldsSubset.count(); ++i )
181 QString joinedFieldName = joinFieldsSubset.
at( i );
185 subsetIndices.append( index );
189 QgsDebugMsg(
"Join layer subset field not found: " + joinedFieldName );
193 return subsetIndices;
200 QList< QgsVectorJoinInfo>::const_iterator joinIt = mVectorJoins.constBegin();
201 for (
int joinIdx = 0 ; joinIt != mVectorJoins.constEnd(); ++joinIt, ++joinIdx )
210 QString joinFieldName;
211 if ( joinIt->joinFieldName.isEmpty() && joinIt->joinFieldIndex >= 0 && joinIt->joinFieldIndex < joinFields.
count() )
212 joinFieldName = joinFields.
field( joinIt->joinFieldIndex ).
name();
214 joinFieldName = joinIt->joinFieldName;
216 QSet<QString> subset;
217 bool hasSubset =
false;
218 if ( joinIt->joinFieldNamesSubset() )
221 subset = QSet<QString>::fromList( *joinIt->joinFieldNamesSubset() );
224 if ( joinIt->prefix.isNull() )
226 prefix = joinLayer->
name() +
"_";
230 prefix = joinIt->prefix;
233 for (
int idx = 0; idx < joinFields.
count(); ++idx )
236 if ( hasSubset && !subset.contains( joinFields[idx].name() ) )
240 if ( joinFields[idx].name() != joinFieldName )
252 QList< QgsVectorJoinInfo >::iterator joinIt = mVectorJoins.begin();
253 for ( ; joinIt != mVectorJoins.end(); ++joinIt )
255 cacheJoinLayer( *joinIt );
259 connect( vl, SIGNAL( updatedFields() ),
this, SLOT( joinedLayerUpdatedFields() ), Qt::UniqueConnection );
266 QDomElement vectorJoinsElem = document.createElement(
"vectorjoins" );
267 layer_node.appendChild( vectorJoinsElem );
268 QList< QgsVectorJoinInfo >::const_iterator joinIt = mVectorJoins.constBegin();
269 for ( ; joinIt != mVectorJoins.constEnd(); ++joinIt )
271 QDomElement joinElem = document.createElement(
"join" );
273 if ( joinIt->targetFieldName.isEmpty() )
274 joinElem.setAttribute(
"targetField", joinIt->targetFieldIndex );
276 joinElem.setAttribute(
"targetFieldName", joinIt->targetFieldName );
278 joinElem.setAttribute(
"joinLayerId", joinIt->joinLayerId );
279 if ( joinIt->joinFieldName.isEmpty() )
280 joinElem.setAttribute(
"joinField", joinIt->joinFieldIndex );
282 joinElem.setAttribute(
"joinFieldName", joinIt->joinFieldName );
284 joinElem.setAttribute(
"memoryCache", !joinIt->cachedAttributes.isEmpty() );
286 if ( joinIt->joinFieldNamesSubset() )
288 QDomElement subsetElem = document.createElement(
"joinFieldsSubset" );
289 foreach ( QString fieldName, *joinIt->joinFieldNamesSubset() )
291 QDomElement fieldElem = document.createElement(
"field" );
292 fieldElem.setAttribute(
"name", fieldName );
293 subsetElem.appendChild( fieldElem );
296 joinElem.appendChild( subsetElem );
299 if ( !joinIt->prefix.isNull() )
301 joinElem.setAttribute(
"customPrefix", joinIt->prefix );
302 joinElem.setAttribute(
"hasCustomPrefix", 1 );
305 vectorJoinsElem.appendChild( joinElem );
311 mVectorJoins.clear();
312 QDomElement vectorJoinsElem = layer_node.firstChildElement(
"vectorjoins" );
313 if ( !vectorJoinsElem.isNull() )
315 QDomNodeList joinList = vectorJoinsElem.elementsByTagName(
"join" );
316 for (
int i = 0; i < joinList.size(); ++i )
318 QDomElement infoElem = joinList.at( i ).toElement();
321 info.
joinLayerId = infoElem.attribute(
"joinLayerId" );
323 info.
memoryCache = infoElem.attribute(
"memoryCache" ).toInt();
328 QDomElement subsetElem = infoElem.firstChildElement(
"joinFieldsSubset" );
329 if ( !subsetElem.isNull() )
331 QStringList* fieldNames =
new QStringList;
332 QDomNodeList fieldNodes = infoElem.elementsByTagName(
"field" );
333 for (
int i = 0; i < fieldNodes.count(); ++i )
334 *fieldNames << fieldNodes.at( i ).toElement().attribute(
"name" );
338 if ( infoElem.attribute(
"hasCustomPrefix" ).toInt() )
339 info.
prefix = infoElem.attribute(
"customPrefix" );
341 info.
prefix = QString::null;
353 int joinIndex = mVectorJoins.indexOf( *info );
354 if ( joinIndex == -1 )
357 for (
int i = 0; i < fields.
count(); ++i )
374 int sourceJoinIndex = originIndex / 1000;
375 sourceFieldIndex = originIndex % 1000;
377 if ( sourceJoinIndex < 0 || sourceJoinIndex >= mVectorJoins.count() )
380 return &( mVectorJoins[sourceJoinIndex] );
386 cloned->mVectorJoins = mVectorJoins;
390 void QgsVectorLayerJoinBuffer::joinedLayerUpdatedFields()
393 Q_ASSERT( joinedLayer );
396 for ( QgsVectorJoinList::iterator it = mVectorJoins.begin(); it != mVectorJoins.end(); ++it )
398 if ( joinedLayer->
id() == it->joinLayerId )
400 it->cachedAttributes.clear();
401 cacheJoinLayer( *it );