Skip to content
Permalink
Browse files
Support multi layers in QLR files
  • Loading branch information
NathanW2 committed May 18, 2014
1 parent 1b49a47 commit 317a5094c4288056f6e12aa907e949874cc7aa32
Showing with 54 additions and 46 deletions.
  1. +3 −3 python/core/qgsmaplayer.sip
  2. +6 −6 src/app/qgisapp.cpp
  3. +41 −33 src/core/qgsmaplayer.cpp
  4. +4 −4 src/core/qgsmaplayer.h
@@ -209,12 +209,12 @@ class QgsMapLayer : QObject

Layer definitions can be used to load a layer and styling all from a single file.
*/
QDomDocument asLayerDefinition( );
static QDomDocument asLayerDefinition( QList<QgsMapLayer*> layers );

/** Creates a new layer from a layer defininition document
*/
static QgsMapLayer* fromLayerDefinition( QDomDocument& document );
static QgsMapLayer* fromLayerDefinitionFile( const QString qlrfile );
static QList<QgsMapLayer*> fromLayerDefinition( QDomDocument& document );
static QList<QgsMapLayer*> fromLayerDefinitionFile( const QString qlrfile );

/** Set a custom property for layer. Properties are stored in a map and saved in project file.
* @note Added in v1.4 */
@@ -2462,9 +2462,8 @@ void QgisApp::addLayerDefinition()
if ( path.isEmpty() )
return;

QgsMapLayer* layer = QgsMapLayer::fromLayerDefinitionFile( path );
if ( layer && layer->isValid() )
QgsMapLayerRegistry::instance()->addMapLayer( layer );
QList<QgsMapLayer*> layers = QgsMapLayer::fromLayerDefinitionFile( path );
QgsMapLayerRegistry::instance()->addMapLayers( layers );
}

/**
@@ -4596,16 +4595,17 @@ void QgisApp::saveAsFile()

void QgisApp::saveAsLayerDefinition()
{
QgsMapLayer* layer = activeLayer();
if ( !layer )
QList<QgsMapLayer*> layers = mMapLegend->selectedLayers();

if ( layers.isEmpty() )
return;

QString path = QFileDialog::getSaveFileName( this, "Save as Layer Definition File", QDir::home().path(), "*.qlr" );
QgsDebugMsg( path );
if ( path.isEmpty() )
return;

QDomDocument doc = layer->asLayerDefinition();
QDomDocument doc = QgsMapLayer::asLayerDefinition( layers );
QFile file( path );
if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
{
@@ -596,61 +596,69 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume

} // bool QgsMapLayer::writeXML

QDomDocument QgsMapLayer::asLayerDefinition()
QDomDocument QgsMapLayer::asLayerDefinition( QList<QgsMapLayer *> layers )
{
QDomDocument doc( "qgis-layer-definition" );
QDomElement maplayer = doc.createElement( "maplayer" );
this->writeLayerXML( maplayer, doc );
maplayer.removeChild( maplayer.firstChildElement( "id" ) );
doc.appendChild( maplayer );
QDomElement layerselm = doc.createElement( "maplayers" );
foreach( QgsMapLayer* layer, layers)
{
QDomElement layerelm = doc.createElement( "maplayer" );
layer->writeLayerXML( layerelm, doc );
layerelm.removeChild( layerelm.firstChildElement( "id" ) );
layerselm.appendChild( layerelm );
}
doc.appendChild( layerselm );
return doc;
}

QgsMapLayer* QgsMapLayer::fromLayerDefinition( QDomDocument& document )
QList<QgsMapLayer*> QgsMapLayer::fromLayerDefinition( QDomDocument& document )
{
QDomNode layernode = document.elementsByTagName( "maplayer" ).at( 0 );
QDomElement layerElem = layernode.toElement();

QString type = layerElem.attribute( "type" );
QgsDebugMsg( type );
QgsMapLayer *layer = NULL;

if ( type == "vector" )
QList<QgsMapLayer*> layers;
QDomNodeList layernodes = document.elementsByTagName( "maplayer" );
for ( int i = 0; i < layernodes.size(); ++i )
{
layer = new QgsVectorLayer;
}
else if ( type == "raster" )
{
layer = new QgsRasterLayer;
}
else if ( type == "plugin" )
{
QString typeName = layerElem.attribute( "name" );
layer = QgsPluginLayerRegistry::instance()->createLayer( typeName );
}
QDomNode layernode = layernodes.at(i);
QDomElement layerElem = layernode.toElement();

bool ok = layer->readLayerXML( layerElem );
if ( ok )
return layer;
QString type = layerElem.attribute( "type" );
QgsDebugMsg( type );
QgsMapLayer *layer = NULL;

delete layer;
return 0;
if ( type == "vector" )
{
layer = new QgsVectorLayer;
}
else if ( type == "raster" )
{
layer = new QgsRasterLayer;
}
else if ( type == "plugin" )
{
QString typeName = layerElem.attribute( "name" );
layer = QgsPluginLayerRegistry::instance()->createLayer( typeName );
}

bool ok = layer->readLayerXML( layerElem );
if ( ok )
layers << layer;
}
return layers;
}

QgsMapLayer* QgsMapLayer::fromLayerDefinitionFile( const QString qlrfile )
QList<QgsMapLayer *> QgsMapLayer::fromLayerDefinitionFile( const QString qlrfile )
{
QFile file( qlrfile );
if ( !file.open( QIODevice::ReadOnly ) )
{
QgsDebugMsg( "Can't open file" );
return 0;
return QList<QgsMapLayer*>();
}

QDomDocument doc;
if ( !doc.setContent( &file ) )
{
QgsDebugMsg( "Can't set content" );
return 0;
return QList<QgsMapLayer*>();
}

return QgsMapLayer::fromLayerDefinition( doc );
@@ -218,17 +218,17 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
bool writeLayerXML( QDomElement& layerElement, QDomDocument& document );

/** Returns the layer as a layer definition document
/** Returns the given layer as a layer definition document
Layer definitions store the data source as well as styling and custom properties.
Layer definitions can be used to load a layer and styling all from a single file.
*/
QDomDocument asLayerDefinition( );
static QDomDocument asLayerDefinition( QList<QgsMapLayer*> layers );

/** Creates a new layer from a layer defininition document
*/
static QgsMapLayer* fromLayerDefinition( QDomDocument& document );
static QgsMapLayer* fromLayerDefinitionFile( const QString qlrfile );
static QList<QgsMapLayer*> fromLayerDefinition( QDomDocument& document );
static QList<QgsMapLayer*> fromLayerDefinitionFile( const QString qlrfile );

/** Set a custom property for layer. Properties are stored in a map and saved in project file.
* @note Added in v1.4 */

0 comments on commit 317a509

Please sign in to comment.