16
16
***************************************************************************/
17
17
18
18
#include " qgsprojectparser.h"
19
+ #include " qgsconfigcache.h"
19
20
#include " qgsepsgcache.h"
20
21
#include " qgsmslayercache.h"
21
22
#include " qgsmapserverlogger.h"
@@ -143,7 +144,43 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
143
144
titleElem.appendChild ( titleText );
144
145
layerElem.appendChild ( titleElem );
145
146
146
- addLayers ( doc, layerElem, currentChildElem, layerMap, nonIdentifiableLayers, mapExtent, mapCRS );
147
+ if ( currentChildElem.attribute (" embedded" ) == " 1" )
148
+ {
149
+ // add layers from other project files and embed into this group
150
+ QString project = convertToAbsolutePath ( currentChildElem.attribute (" project" ) );
151
+ QString embeddedGroupName = currentChildElem.attribute (" name" );
152
+ QgsProjectParser* p = dynamic_cast <QgsProjectParser*>( QgsConfigCache::instance ()->searchConfiguration ( project ) );
153
+ if ( p )
154
+ {
155
+ QStringList pIdDisabled = p->identifyDisabledLayers ();
156
+
157
+ QDomElement embeddedGroupElem;
158
+ QList<QDomElement> pLegendElems = p->legendGroupElements ();
159
+ QList<QDomElement>::const_iterator pLegendIt = pLegendElems.constBegin ();
160
+ for (; pLegendIt != pLegendElems.constEnd (); ++pLegendIt )
161
+ {
162
+ if ( pLegendIt->attribute (" name" ) == embeddedGroupName )
163
+ {
164
+ embeddedGroupElem = *pLegendIt;
165
+ break ;
166
+ }
167
+ }
168
+
169
+ QList<QDomElement> pLayerElems = p->projectLayerElements ();
170
+ QMap<QString, QgsMapLayer *> pLayerMap;
171
+ QList<QDomElement>::const_iterator pLayerIt = pLayerElems.constBegin ();
172
+ for (; pLayerIt != pLayerElems.constEnd (); ++pLayerIt )
173
+ {
174
+ pLayerMap.insert ( layerId ( *pLayerIt ), p->createLayerFromElement ( *pLayerIt ) );
175
+ }
176
+
177
+ p->addLayers ( doc, layerElem, embeddedGroupElem, pLayerMap, pIdDisabled, mapExtent, mapCRS);
178
+ }
179
+ }
180
+ else // normal (not embedded) legend group
181
+ {
182
+ addLayers ( doc, layerElem, currentChildElem, layerMap, nonIdentifiableLayers, mapExtent, mapCRS );
183
+ }
147
184
148
185
// combine bounding boxes of childs (groups/layers)
149
186
@@ -283,7 +320,6 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
283
320
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle ( const QString& lName, const QString& styleName, bool allowCaching ) const
284
321
{
285
322
QList<QgsMapLayer*> layerList;
286
- bool layerFound = false ;
287
323
288
324
// first assume lName refers to a leaf layer
289
325
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName ();
@@ -294,18 +330,11 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
294
330
if ( layer )
295
331
{
296
332
layerList.push_back ( layer );
333
+ return layerList;
297
334
}
298
- layerFound = true ;
299
- }
300
-
301
- if ( layerFound )
302
- {
303
- return layerList;
304
335
}
305
336
306
337
// Check if layer name refers to the top level group for the project.
307
- // The project group is not contained in the groupLayerRelationship list
308
- // because the list (and the qgis legend) does not support nested groups
309
338
if ( lName == projectTitle () )
310
339
{
311
340
QList<QDomElement> layerElemList = projectLayerElements ();
@@ -326,18 +355,110 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
326
355
{
327
356
if ( groupIt->attribute ( " name" ) == lName )
328
357
{
329
- QDomNodeList layerFileList = groupIt->elementsByTagName ( " legendlayerfile" );
330
- for ( int i = 0 ; i < layerFileList.size (); ++i )
358
+ if ( groupIt->attribute (" embedded" ) == " 1" ) // requested group is embedded from another project
331
359
{
332
- QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap. find ( layerFileList. at ( i ). toElement (). attribute ( " layerid " ) );
333
- if ( layerEntry != idLayerMap. constEnd () )
360
+ QgsProjectParser* p = dynamic_cast <QgsProjectParser*>( QgsConfigCache::instance ()-> searchConfiguration ( convertToAbsolutePath (groupIt-> attribute (" project " ) ) ) );
361
+ if ( p )
334
362
{
335
- layerList.push_back ( createLayerFromElement ( layerEntry.value () ) );
363
+ QList<QDomElement> pGroupElems = p->legendGroupElements ();
364
+ QList<QDomElement>::const_iterator pGroupIt = pGroupElems.constBegin ();
365
+ QDomElement embeddedGroupElem;
366
+
367
+ for (; pGroupIt != pGroupElems.constEnd (); ++pGroupIt )
368
+ {
369
+ if ( pGroupIt->attribute (" name" ) == lName )
370
+ {
371
+ embeddedGroupElem = *pGroupIt;
372
+ break ;
373
+ }
374
+ }
375
+
376
+ if ( !embeddedGroupElem.isNull () )
377
+ {
378
+ // add all the layers under the group
379
+ QMap< QString, QDomElement > pLayerElems = p->projectLayerElementsById ();
380
+ QDomNodeList pLayerNodes = embeddedGroupElem.elementsByTagName (" legendlayer" );
381
+ for ( int i = 0 ; i < pLayerNodes.size (); ++i )
382
+ {
383
+ QString pLayerId = pLayerNodes.at (i).toElement ().firstChildElement (" filegroup" ).firstChildElement (" legendlayerfile" ).attribute (" layerid" );
384
+ QgsMapLayer* pLayer = p->createLayerFromElement ( pLayerElems[pLayerId] );
385
+ if ( pLayer )
386
+ {
387
+ layerList.push_back ( pLayer );
388
+ }
389
+ }
390
+ }
336
391
}
337
392
}
393
+ else // normal (not embedded) group
394
+ {
395
+ QDomNodeList layerFileList = groupIt->elementsByTagName ( " legendlayerfile" );
396
+ for ( int i = 0 ; i < layerFileList.size (); ++i )
397
+ {
398
+ QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find ( layerFileList.at ( i ).toElement ().attribute ( " layerid" ) );
399
+ if ( layerEntry != idLayerMap.constEnd () )
400
+ {
401
+ layerList.push_back ( createLayerFromElement ( layerEntry.value () ) );
402
+ }
403
+ }
404
+ }
405
+ return layerList;
338
406
}
339
407
}
340
408
409
+ // maybe the layer is embedded from another project
410
+ QMap< QString, QDomElement >::const_iterator layerIt = idLayerMap.constBegin ();
411
+ for (; layerIt != idLayerMap.constEnd (); ++layerIt )
412
+ {
413
+ if ( layerIt.value ().attribute (" embedded" ) == " 1" )
414
+ {
415
+ QString id = layerIt.value ().attribute (" id" );
416
+ QString project = layerIt.value ().attribute (" project" );
417
+
418
+ // get config parser from cache
419
+ QgsProjectParser* otherParser = dynamic_cast <QgsProjectParser*>(QgsConfigCache::instance ()->searchConfiguration ( project ) );
420
+ if ( otherParser )
421
+ {
422
+ // get element by id
423
+ QMap< QString, QDomElement > otherLayerElems = otherParser->projectLayerElementsById ();
424
+ QMap< QString, QDomElement >::const_iterator otherLayerIt = otherLayerElems.find ( id );
425
+ if ( otherLayerIt != otherLayerElems.constEnd () )
426
+ {
427
+ if ( otherLayerIt.value ().firstChildElement (" layername" ).text () == lName )
428
+ {
429
+ layerList.push_back ( otherParser->createLayerFromElement ( otherLayerIt.value () ) );
430
+ return layerList;
431
+ }
432
+ }
433
+ }
434
+ }
435
+ }
436
+
437
+ // layer still not found. Check if it is a single layer contained in a embedded layer group
438
+ groupIt = legendGroups.constBegin ();
439
+ for (; groupIt != legendGroups.constEnd (); ++groupIt )
440
+ {
441
+ if ( groupIt->attribute (" embedded" ) == " 1" )
442
+ {
443
+ QgsProjectParser* p = dynamic_cast <QgsProjectParser*>(QgsConfigCache::instance ()->searchConfiguration ( convertToAbsolutePath (groupIt->attribute (" project" ) ) ) );
444
+ if ( p )
445
+ {
446
+ QMap< QString, QDomElement > pLayers = p->projectLayerElementsByName ();
447
+ QMap< QString, QDomElement >::const_iterator pLayerIt = pLayers.find ( lName );
448
+ if ( pLayerIt != pLayers.constEnd () )
449
+ {
450
+ QgsMapLayer* layer = p->createLayerFromElement ( pLayerIt.value () );
451
+ if ( layer )
452
+ {
453
+ layerList.push_back ( layer );
454
+ return layerList;
455
+ }
456
+ }
457
+ }
458
+ }
459
+ }
460
+
461
+ // layer not found, return empty list
341
462
return layerList;
342
463
}
343
464
@@ -728,27 +849,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
728
849
return 0 ;
729
850
}
730
851
852
+ QString uri;
853
+ QString absoluteUri;
731
854
QDomElement dataSourceElem = elem.firstChildElement ( " datasource" );
732
- if ( dataSourceElem.isNull () )
855
+ if ( ! dataSourceElem.isNull () )
733
856
{
734
- return 0 ;
735
- }
736
-
737
- // convert relative pathes to absolute ones if necessary
738
- QString uri = dataSourceElem.text ();
739
- QString absoluteUri = convertToAbsolutePath ( uri );
740
- if ( uri != absoluteUri )
741
- {
742
- QDomText absoluteTextNode = mXMLDoc ->createTextNode ( absoluteUri );
743
- dataSourceElem.replaceChild ( absoluteTextNode, dataSourceElem.firstChild () );
857
+ // convert relative pathes to absolute ones if necessary
858
+ uri = dataSourceElem.text ();
859
+ absoluteUri = convertToAbsolutePath ( uri );
860
+ if ( uri != absoluteUri )
861
+ {
862
+ QDomText absoluteTextNode = mXMLDoc ->createTextNode ( absoluteUri );
863
+ dataSourceElem.replaceChild ( absoluteTextNode, dataSourceElem.firstChild () );
864
+ }
744
865
}
745
866
746
867
QString id = layerId ( elem );
747
- if ( id.isNull () )
748
- {
749
- return 0 ;
750
- }
751
-
752
868
QgsMapLayer* layer = QgsMSLayerCache::instance ()->searchLayer ( absoluteUri, id );
753
869
if ( layer )
754
870
{
@@ -767,6 +883,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
767
883
{
768
884
layer = new QgsRasterLayer ();
769
885
}
886
+ else if ( elem.attribute (" embedded" ) == " 1" ) // layer is embedded from another project file
887
+ {
888
+ QgsProjectParser* otherConfig = dynamic_cast <QgsProjectParser*>( QgsConfigCache::instance ()->searchConfiguration ( convertToAbsolutePath ( elem.attribute ( " project" ) ) ) );
889
+ if ( !otherConfig )
890
+ {
891
+ return 0 ;
892
+ }
893
+
894
+ QMap< QString, QDomElement > layerMap = otherConfig->projectLayerElementsById ();
895
+ QMap< QString, QDomElement >::const_iterator layerIt = layerMap.find ( elem.attribute ( " id" ) );
896
+ if ( layerIt == layerMap.constEnd () )
897
+ {
898
+ return 0 ;
899
+ }
900
+ return otherConfig->createLayerFromElement ( layerIt.value () );
901
+ }
770
902
771
903
if ( layer )
772
904
{
0 commit comments