Skip to content

Commit

Permalink
Better implementation of QgsProjectParser::mapLayerFromStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
marco committed Nov 16, 2011
1 parent 3b7e731 commit 59a5fc6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 100 deletions.
204 changes: 104 additions & 100 deletions src/mapserver/qgsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
QString embeddedGroupName = currentChildElem.attribute( "name" );
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
QList<QDomElement> embededGroupElements = p->mLegendGroupElements;
if ( p )
{
QStringList pIdDisabled = p->identifyDisabledLayers();

QDomElement embeddedGroupElem;
QList<QDomElement>::const_iterator pLegendIt = mLegendGroupElements.constBegin();
for ( ; pLegendIt != mLegendGroupElements.constEnd(); ++pLegendIt )
QList<QDomElement>::const_iterator pLegendIt = embededGroupElements.constBegin();
for ( ; pLegendIt != embededGroupElements.constEnd(); ++pLegendIt )
{
if ( pLegendIt->attribute( "name" ) == embeddedGroupName )
{
Expand All @@ -189,8 +190,9 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
}

QMap<QString, QgsMapLayer *> pLayerMap;
QList<QDomElement>::const_iterator pLayerIt = mProjectLayerElements.constBegin();
for ( ; pLayerIt != mProjectLayerElements.constEnd(); ++pLayerIt )
QList<QDomElement> embededProjectLayerElements = p->mProjectLayerElements;
QList<QDomElement>::const_iterator pLayerIt = embededProjectLayerElements.constBegin();
for ( ; pLayerIt != embededProjectLayerElements.constEnd(); ++pLayerIt )
{
pLayerMap.insert( layerId( *pLayerIt ), p->createLayerFromElement( *pLayerIt ) );
}
Expand Down Expand Up @@ -327,17 +329,10 @@ void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupEle
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool useCache ) const
{
Q_UNUSED( styleName );
Q_UNUSED( useCache );
QList<QgsMapLayer*> layerList;

//Check if layer name refers to the top level group for the project.
if ( lName == projectTitle() )
if ( !mXMLDoc )
{
QList<QDomElement>::const_iterator layerElemIt = mProjectLayerElements.constBegin();
for ( ; layerElemIt != mProjectLayerElements.constEnd(); ++layerElemIt )
{
layerList.push_back( createLayerFromElement( *layerElemIt, useCache ) );
}
return layerList;
}

Expand All @@ -353,118 +348,126 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
}
}

//maybe the layer is a goup. Check if lName is contained in the group list
QList<QDomElement>::const_iterator groupIt = mLegendGroupElements.constBegin();
for ( ; groupIt != mLegendGroupElements.constEnd(); ++groupIt )
//group or project name
QDomElement groupElement;
if ( lName == projectTitle() )
{
if ( groupIt->attribute( "name" ) == lName )
groupElement = mXMLDoc->documentElement().firstChildElement( "legend" );
}
else
{
QList<QDomElement>::const_iterator groupIt = mLegendGroupElements.constBegin();
for ( ; groupIt != mLegendGroupElements.constEnd(); ++groupIt )
{
if ( groupIt->attribute( "embedded" ) == "1" ) //requested group is embedded from another project
if ( groupIt->attribute( "name" ) == lName )
{
QString project = convertToAbsolutePath( groupIt->attribute( "project" ) );
QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
if ( p )
{
QList<QDomElement>::const_iterator pGroupIt = mLegendGroupElements.constBegin();
QDomElement embeddedGroupElem;
groupElement = *groupIt;
break;
}
}
}

for ( ; pGroupIt != mLegendGroupElements.constEnd(); ++pGroupIt )
{
if ( pGroupIt->attribute( "name" ) == lName )
{
embeddedGroupElem = *pGroupIt;
break;
}
}
if ( !groupElement.isNull() )
{
//embedded group has no children in this project file
if ( groupElement.attribute( "embedded" ) == "1" )
{
addLayersFromGroup( groupElement, layerList, useCache );
return layerList;
}

if ( !embeddedGroupElem.isNull() )
{
//add all the layers under the group
QDomNodeList pLayerNodes = embeddedGroupElem.elementsByTagName( "legendlayer" );
for ( int i = 0; i < pLayerNodes.size(); ++i )
{
QString pLayerId = pLayerNodes.at( i ).toElement().firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
QgsMapLayer* pLayer = p->createLayerFromElement( mProjectLayerElementsById[pLayerId], useCache );
if ( pLayer )
{
layerList.push_back( pLayer );
}
}
}
}
//group element found, iterate children and call addLayersFromGroup / addLayerFromLegendLayer for each
QDomNodeList childList = groupElement.childNodes();
for ( uint i = 0; i < childList.length(); ++i )
{
QDomElement childElem = childList.at( i ).toElement();
if ( childElem.tagName() == "legendgroup" )
{
addLayersFromGroup( childElem, layerList, useCache );
}
else //normal (not embedded) group
else if ( childElem.tagName() == "legendlayer" )
{
QDomNodeList layerFileList = groupIt->elementsByTagName( "legendlayerfile" );
for ( int i = 0; i < layerFileList.size(); ++i )
{
QHash< QString, QDomElement >::const_iterator layerEntry = mProjectLayerElementsById.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
if ( layerEntry != mProjectLayerElementsById.constEnd() )
{
layerList.push_back( createLayerFromElement( layerEntry.value(), useCache ) );
}
}
addLayerFromLegendLayer( childElem, layerList, useCache );
}
return layerList;
}
return layerList;
}

//maybe the layer is embedded from another project
QHash< QString, QDomElement >::const_iterator layerIt = mProjectLayerElementsById.constBegin();
for ( ; layerIt != mProjectLayerElementsById.constEnd(); ++layerIt )
//still not found. Check if it is a single embedded layer (embedded layers are not contained in mProjectLayerElementsByName)
QDomElement legendElement = mXMLDoc->documentElement().firstChildElement( "legend" );
QDomNodeList legendLayerList = legendElement.elementsByTagName( "legendlayer" );
for ( int i = 0; i < legendLayerList.size(); ++i )
{
if ( layerIt.value().attribute( "embedded" ) == "1" )
QDomElement legendLayerElem = legendLayerList.at( i ).toElement();
if ( legendLayerElem.attribute( "name" ) == lName )
{
QString id = layerIt.value().attribute( "id" );
QString project = layerIt.value().attribute( "project" );
QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
addLayerFromLegendLayer( legendLayerElem, layerList, useCache );
}
}

//get config parser from cache
QgsProjectParser* otherParser = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
if ( otherParser )
return layerList;
}

void QgsProjectParser::addLayersFromGroup( const QDomElement& legendGroupElem, QList<QgsMapLayer*>& layerList, bool useCache ) const
{
if ( legendGroupElem.attribute( "embedded" ) == "1" ) //embedded group
{
//get project parser
//get group elements from project parser, find the group
//iterate over layers and add them (embedding in embedded groups does not work)
QString groupName = legendGroupElem.attribute( "name" );
QString project = legendGroupElem.attribute( "project" );
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
if ( !p )
{
return;
}

QList<QDomElement> pLegendGroupElems = p->mLegendGroupElements;
QList<QDomElement>::const_iterator pGroupIt = pLegendGroupElems.constBegin();
for ( ; pGroupIt != pLegendGroupElems.constEnd(); ++pGroupIt )
{
if ( pGroupIt->attribute( "name" ) == groupName )
{
//get element by id
QHash< QString, QDomElement >::const_iterator otherLayerIt = otherParser->mProjectLayerElementsById.find( id );
if ( otherLayerIt != otherParser->mProjectLayerElementsById.constEnd() )
{
if ( otherLayerIt.value().firstChildElement( "layername" ).text() == lName )
{
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value(), useCache ) );
return layerList;
}
}
p->addLayersFromGroup( *pGroupIt, layerList, useCache );
return;
}
}
}

//layer still not found. Check if it is a single layer contained in a embedded layer group
groupIt = mLegendGroupElements.constBegin();
for ( ; groupIt != mLegendGroupElements.constEnd(); ++groupIt )
else //normal group
{
if ( groupIt->attribute( "embedded" ) == "1" )
QDomNodeList groupElemChildren = legendGroupElem.childNodes();
for ( int i = 0; i < groupElemChildren.size(); ++i )
{
QString project = convertToAbsolutePath( groupIt->attribute( "project" ) );
QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
if ( p )
QDomElement elem = groupElemChildren.at( i ).toElement();
if ( elem.tagName() == "legendgroup" )
{
QHash< QString, QDomElement >::const_iterator pLayerIt = mProjectLayerElementsByName.find( lName );
if ( pLayerIt != mProjectLayerElementsByName.constEnd() )
{
QgsMapLayer* layer = p->createLayerFromElement( pLayerIt.value() );
if ( layer )
{
layerList.push_back( layer );
return layerList;
}
}
addLayersFromGroup( elem, layerList, useCache );
}
else if ( elem.tagName() == "legendlayer" )
{
addLayerFromLegendLayer( elem, layerList, useCache );
}
}
}
}

//layer not found, return empty list
return layerList;
void QgsProjectParser::addLayerFromLegendLayer( const QDomElement& legendLayerElem, QList<QgsMapLayer*>& layerList, bool useCache ) const
{
//get layer id
//search dom element for <maplayer> element
//call createLayerFromElement()

QString id = legendLayerElem.firstChild().firstChild().toElement().attribute( "layerid" );
QHash< QString, QDomElement >::const_iterator layerIt = mProjectLayerElementsById.find( id );
if ( layerIt != mProjectLayerElementsById.constEnd() )
{
QgsMapLayer* layer = createLayerFromElement( layerIt.value(), useCache );
if ( layer )
{
layerList.append( layer );
}
}
}

int QgsProjectParser::layersAndStyles( QStringList& layers, QStringList& styles ) const
Expand Down Expand Up @@ -872,7 +875,8 @@ QString QgsProjectParser::layerId( const QDomElement& layerElem ) const
QDomElement idElem = layerElem.firstChildElement( "id" );
if ( idElem.isNull() )
{
return QString();
//embedded layer have id attribute instead of id child element
return layerElem.attribute( "id" );
}
return idElem.text();
}
Expand Down
3 changes: 3 additions & 0 deletions src/mapserver/qgsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class QgsProjectParser: public QgsConfigParser
/**Creates a maplayer object from <maplayer> element. The layer cash owns the maplayer, so don't delete it
@return the maplayer or 0 in case of error*/
QgsMapLayer* createLayerFromElement( const QDomElement& elem, bool useCache = true ) const;
/**Adds layers from a legend group to list (could be embedded or a normal group)*/
void addLayersFromGroup( const QDomElement& legendGroupElem, QList<QgsMapLayer*>& layerList, bool useCache = true ) const;
void addLayerFromLegendLayer( const QDomElement& legendLayerElem, QList<QgsMapLayer*>& layerList, bool useCache = true ) const;
/**Returns the text of the <id> element for a layer element
@return id or a null string in case of error*/
QString layerId( const QDomElement& layerElem ) const;
Expand Down

0 comments on commit 59a5fc6

Please sign in to comment.