Exemplo n.º 1
0
void QgsServerProjectParser::layerFromLegendLayer( const QDomElement& legendLayerElem, QMap< int, QgsMapLayer*>& layers, bool useCache ) const
{
  QString id = legendLayerElem.firstChild().firstChild().toElement().attribute( "layerid" );
  int drawingOrder = updateLegendDrawingOrder() ? -1 : mCustomLayerOrder.indexOf( id );

  QHash< QString, QDomElement >::const_iterator layerIt = mProjectLayerElementsById.find( id );
  if ( layerIt != mProjectLayerElementsById.constEnd() )
  {
    QgsMapLayer* layer = createLayerFromElement( layerIt.value(), useCache );
    if ( layer )
    {
      layers.insertMulti( drawingOrder, layer );
    }
  }
}
Exemplo n.º 2
0
void QgsProjectParser::layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const
{
  QStringList nonIdentifiableLayers = identifyDisabledLayers();
  QMap<QString, QgsMapLayer *> layerMap;

  foreach ( const QDomElement &elem, mProjectLayerElements )
  {
    QgsMapLayer *layer = createLayerFromElement( elem );
    if ( layer )
    {
      QgsDebugMsg( QString( "add layer %1 to map" ).arg( layer->id() ) );
      layerMap.insert( layer->id(), layer );
    }
#if QGSMSDEBUG
    else
    {
      QString buf;
      QTextStream s( &buf );
      elem.save( s, 0 );
      QgsDebugMsg( QString( "layer %1 not found" ).arg( buf ) );
    }
#endif
  }
Exemplo n.º 3
0
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
{
  Q_UNUSED( styleName );
  Q_UNUSED( allowCaching );
  QList<QgsMapLayer*> layerList;

  //first assume lName refers to a leaf layer
  QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
  QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
  if ( layerElemIt != layerElemMap.constEnd() )
  {
    QgsMapLayer* layer = createLayerFromElement( layerElemIt.value() );
    if ( layer )
    {
      layerList.push_back( layer );
      return layerList;
    }
  }

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

  //maybe the layer is a goup. Check if lName is contained in the group list
  QMap< QString, QDomElement > idLayerMap = projectLayerElementsById();

  QList<QDomElement> legendGroups = legendGroupElements();
  QList<QDomElement>::const_iterator groupIt = legendGroups.constBegin();
  for ( ; groupIt != legendGroups.constEnd(); ++groupIt )
  {
    if ( groupIt->attribute( "name" ) == lName )
    {
      if ( groupIt->attribute( "embedded" ) == "1" ) //requested group is embedded from another project
      {
        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> pGroupElems = p->legendGroupElements();
          QList<QDomElement>::const_iterator pGroupIt = pGroupElems.constBegin();
          QDomElement embeddedGroupElem;

          for ( ; pGroupIt != pGroupElems.constEnd(); ++pGroupIt )
          {
            if ( pGroupIt->attribute( "name" ) == lName )
            {
              embeddedGroupElem = *pGroupIt;
              break;
            }
          }

          if ( !embeddedGroupElem.isNull() )
          {
            //add all the layers under the group
            QMap< QString, QDomElement > pLayerElems = p->projectLayerElementsById();
            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( pLayerElems[pLayerId] );
              if ( pLayer )
              {
                layerList.push_back( pLayer );
              }
            }
          }
        }
      }
      else //normal (not embedded) group
      {
        QDomNodeList layerFileList = groupIt->elementsByTagName( "legendlayerfile" );
        for ( int i = 0; i < layerFileList.size(); ++i )
        {
          QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
          if ( layerEntry != idLayerMap.constEnd() )
          {
            layerList.push_back( createLayerFromElement( layerEntry.value() ) );
          }
        }
      }
      return layerList;
    }
  }

  //maybe the layer is embedded from another project
  QMap< QString, QDomElement >::const_iterator layerIt = idLayerMap.constBegin();
  for ( ; layerIt != idLayerMap.constEnd(); ++layerIt )
  {
    if ( layerIt.value().attribute( "embedded" ) == "1" )
    {
      QString id = layerIt.value().attribute( "id" );
      QString project = layerIt.value().attribute( "project" );
      QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );

      //get config parser from cache
      QgsProjectParser* otherParser = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
      if ( otherParser )
      {
        //get element by id
        QMap< QString, QDomElement > otherLayerElems = otherParser->projectLayerElementsById();
        QMap< QString, QDomElement >::const_iterator otherLayerIt = otherLayerElems.find( id );
        if ( otherLayerIt != otherLayerElems.constEnd() )
        {
          if ( otherLayerIt.value().firstChildElement( "layername" ).text() == lName )
          {
            layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value() ) );
            return layerList;
          }
        }
      }
    }
  }

  //layer still not found. Check if it is a single layer contained in a embedded layer group
  groupIt = legendGroups.constBegin();
  for ( ; groupIt != legendGroups.constEnd(); ++groupIt )
  {
    if ( groupIt->attribute( "embedded" ) == "1" )
    {
      QString project = convertToAbsolutePath( groupIt->attribute( "project" ) );
      QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );
      QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
      if ( p )
      {
        QMap< QString, QDomElement > pLayers = p->projectLayerElementsByName();
        QMap< QString, QDomElement >::const_iterator pLayerIt = pLayers.find( lName );
        if ( pLayerIt != pLayers.constEnd() )
        {
          QgsMapLayer* layer = p->createLayerFromElement( pLayerIt.value() );
          if ( layer )
          {
            layerList.push_back( layer );
            return layerList;
          }
        }
      }
    }
  }

  //layer not found, return empty list
  return layerList;
}