コード例 #1
0
QgsMapLayer* QgsServerProjectParser::createLayerFromElement( const QDomElement& elem, bool useCache ) const
{
  if ( elem.isNull() || !mXMLDoc )
  {
    return nullptr;
  }

  addJoinLayersForElement( elem );
  addGetFeatureLayers( elem );

  QDomElement dataSourceElem = elem.firstChildElement( "datasource" );
  QString uri = dataSourceElem.text();
  QString absoluteUri;
  // If QgsProject instance fileName is set,
  // Is converting relative pathes to absolute still relevant ?
  if ( !dataSourceElem.isNull() )
  {
    //convert relative pathes to absolute ones if necessary
    if ( uri.startsWith( "dbname" ) ) //database
    {
      QgsDataSourceUri dsUri( uri );
      if ( dsUri.host().isEmpty() ) //only convert path for file based databases
      {
        QString dbnameUri = dsUri.database();
        QString dbNameUriAbsolute = convertToAbsolutePath( dbnameUri );
        if ( dbnameUri != dbNameUriAbsolute )
        {
          dsUri.setDatabase( dbNameUriAbsolute );
          absoluteUri = dsUri.uri();
          QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
          dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
        }
      }
    }
    else if ( uri.startsWith( "file:" ) ) //a file based datasource in url notation (e.g. delimited text layer)
    {
      QString filePath = uri.mid( 5, uri.indexOf( "?" ) - 5 );
      QString absoluteFilePath = convertToAbsolutePath( filePath );
      if ( filePath != absoluteFilePath )
      {
        QUrl destUrl = QUrl::fromEncoded( uri.toAscii() );
        destUrl.setScheme( "file" );
        destUrl.setPath( absoluteFilePath );
        absoluteUri = destUrl.toEncoded();
        QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
        dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
      }
      else
      {
        absoluteUri = uri;
      }
    }
    else //file based data source
    {
      absoluteUri = convertToAbsolutePath( uri );
      if ( uri != absoluteUri )
      {
        QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
        dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
      }
    }
  }

  QString id = layerId( elem );
  QgsMapLayer* layer = nullptr;
  if ( useCache )
  {
    layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id, mProjectPath );
  }

  if ( layer )
  {
    if ( !QgsMapLayerRegistry::instance()->mapLayer( id ) )
      QgsMapLayerRegistry::instance()->addMapLayer( layer, false, false );
    if ( layer->type() == QgsMapLayer::VectorLayer )
      addValueRelationLayersForLayer( dynamic_cast<QgsVectorLayer *>( layer ) );

    return layer;
  }

  QString type = elem.attribute( "type" );
  if ( type == "vector" )
  {
    layer = new QgsVectorLayer();
  }
  else if ( type == "raster" )
  {
    layer = new QgsRasterLayer();
  }
  else if ( elem.attribute( "embedded" ) == "1" ) //layer is embedded from another project file
  {
    QString project = convertToAbsolutePath( elem.attribute( "project" ) );
    QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );

    QgsServerProjectParser* otherConfig = QgsConfigCache::instance()->serverConfiguration( project );
    if ( !otherConfig )
    {
      return nullptr;
    }
    return otherConfig->mapLayerFromLayerId( elem.attribute( "id" ), useCache );
  }

  if ( layer )
  {
    if ( layer->type() == QgsMapLayer::VectorLayer )
    {
      // see QgsEditorWidgetRegistry::mapLayerAdded()
      QObject::connect( layer, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), QgsEditorWidgetRegistry::instance(), SLOT( readSymbology( const QDomElement&, QString& ) ) );
    }

    layer->readLayerXml( const_cast<QDomElement&>( elem ) ); //should be changed to const in QgsMapLayer
    //layer->setLayerName( layerName( elem ) );

    // Insert layer in registry and cache before addValueRelationLayersForLayer
    if ( !QgsMapLayerRegistry::instance()->mapLayer( id ) )
      QgsMapLayerRegistry::instance()->addMapLayer( layer, false, false );
    if ( useCache )
    {
      QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer, mProjectPath );
    }
    else
    {
      //todo: fixme
      //mLayersToRemove.push_back( layer );
    }

    if ( layer->type() == QgsMapLayer::VectorLayer )
    {
      addValueRelationLayersForLayer( dynamic_cast<QgsVectorLayer *>( layer ) );
    }
  }
  return layer;
}
コード例 #2
0
QgsMapLayer* QgsServerProjectParser::createLayerFromElement( const QDomElement& elem, bool useCache ) const
{
    if ( elem.isNull() || !mXMLDoc )
    {
        return 0;
    }

    addJoinLayersForElement( elem, useCache );
    addValueRelationLayersForElement( elem, useCache );

    QDomElement dataSourceElem = elem.firstChildElement( "datasource" );
    QString uri = dataSourceElem.text();
    QString absoluteUri;
    if ( !dataSourceElem.isNull() )
    {
        //convert relative pathes to absolute ones if necessary
        if ( uri.startsWith( "dbname" ) ) //database
        {
            QgsDataSourceURI dsUri( uri );
            if ( dsUri.host().isEmpty() ) //only convert path for file based databases
            {
                QString dbnameUri = dsUri.database();
                QString dbNameUriAbsolute = convertToAbsolutePath( dbnameUri );
                if ( dbnameUri != dbNameUriAbsolute )
                {
                    dsUri.setDatabase( dbNameUriAbsolute );
                    absoluteUri = dsUri.uri();
                    QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
                    dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
                }
            }
        }
        else if ( uri.startsWith( "file:" ) ) //a file based datasource in url notation (e.g. delimited text layer)
        {
            QString filePath = uri.mid( 5, uri.indexOf( "?" ) - 5 );
            QString absoluteFilePath = convertToAbsolutePath( filePath );
            if ( filePath != absoluteFilePath )
            {
                QUrl destUrl = QUrl::fromEncoded( uri.toAscii() );
                destUrl.setScheme( "file" );
                destUrl.setPath( absoluteFilePath );
                absoluteUri = destUrl.toEncoded();
                QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
                dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
            }
            else
            {
                absoluteUri = uri;
            }
        }
        else //file based data source
        {
            absoluteUri = convertToAbsolutePath( uri );
            if ( uri != absoluteUri )
            {
                QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
                dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
            }
        }
    }

    QString id = layerId( elem );
    QgsMapLayer* layer = 0;
    if ( useCache )
    {
        layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
    }

    if ( layer )
    {
        return layer;
    }

    QString type = elem.attribute( "type" );
    if ( type == "vector" )
    {
        layer = new QgsVectorLayer();
    }
    else if ( type == "raster" )
    {
        layer = new QgsRasterLayer();
    }
    else if ( elem.attribute( "embedded" ) == "1" ) //layer is embedded from another project file
    {
        QString project = convertToAbsolutePath( elem.attribute( "project" ) );
        QgsDebugMsg( QString( "Project path: %1" ).arg( project ) );

        QgsServerProjectParser* otherConfig = QgsConfigCache::instance()->serverConfiguration( project );
        if ( !otherConfig )
        {
            return 0;
        }
        return otherConfig->mapLayerFromLayerId( elem.attribute( "id" ), useCache );
    }

    if ( layer )
    {
        layer->readLayerXML( const_cast<QDomElement&>( elem ) ); //should be changed to const in QgsMapLayer
        layer->setLayerName( layerName( elem ) );
        if ( useCache )
        {
            QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer, mProjectPath );
        }
        else
        {
            //todo: fixme
            //mLayersToRemove.push_back( layer );
        }
    }
    return layer;
}