Skip to content
Permalink
Browse files

Fix application hangs when attempting to load qlr file for

non vector/raster/vector-tile layer types

Refs #42112
  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent d8b6060 commit 2884e07718d24d9127bb3c47a06e1cda3d0459d9
@@ -116,6 +116,7 @@ Whether some dependency is missing
%End

};

};

/************************************************************************
@@ -7440,7 +7440,7 @@ void QgisApp::openLayerDefinition( const QString &path )
{
QString errorMessage;
bool loaded = QgsLayerDefinition::loadLayerDefinition( path, QgsProject::instance(), QgsProject::instance()->layerTreeRoot(), errorMessage );
if ( !loaded )
if ( !loaded || !errorMessage.isEmpty() )
{
QgsDebugMsg( errorMessage );
visibleMessageBar()->pushMessage( tr( "Error loading layer definition" ), errorMessage, Qgis::Warning );
@@ -10956,7 +10956,7 @@ void QgisApp::pasteLayer()
bool loaded = QgsLayerDefinition::loadLayerDefinition( doc, QgsProject::instance(), root,
errorMessage, readWriteContext );

if ( !loaded )
if ( !loaded || !errorMessage.isEmpty() )
{
visibleMessageBar()->pushMessage( tr( "Error pasting layer" ), errorMessage, Qgis::Warning );
}
@@ -60,7 +60,7 @@ bool QgsLayerDefinition::loadLayerDefinition( const QString &path, QgsProject *p

bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *project, QgsLayerTreeGroup *rootGroup, QString &errorMessage, QgsReadWriteContext &context )
{
Q_UNUSED( errorMessage )
errorMessage.clear();

QgsLayerTreeGroup *root = new QgsLayerTreeGroup();

@@ -172,7 +172,7 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
loadInLegend = false;
}

QList<QgsMapLayer *> layers = QgsLayerDefinition::loadLayerDefinitionLayers( doc, context );
QList<QgsMapLayer *> layers = QgsLayerDefinition::loadLayerDefinitionLayersInternal( doc, context, errorMessage );

project->addMapLayers( layers, loadInLegend );

@@ -195,7 +195,6 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
rootGroup->insertChildNodes( -1, nodes );

return true;

}

bool QgsLayerDefinition::exportLayerDefinition( QString path, const QList<QgsLayerTreeNode *> &selectedTreeNodes, QString &errorMessage )
@@ -275,6 +274,12 @@ QDomDocument QgsLayerDefinition::exportLayerDefinitionLayers( const QList<QgsMap
}

QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( QDomDocument &document, QgsReadWriteContext &context )
{
QString errorMessage;
return loadLayerDefinitionLayersInternal( document, context, errorMessage );
}

QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayersInternal( QDomDocument &document, QgsReadWriteContext &context, QString &errorMessage )
{
QList<QgsMapLayer *> layers;
QDomElement layerElem = document.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) ).firstChildElement( QStringLiteral( "maplayer" ) );
@@ -287,7 +292,6 @@ QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( QDomDocument
while ( ! layerElem.isNull() )
{
const QString type = layerElem.attribute( QStringLiteral( "type" ) );
QgsDebugMsg( type );
QgsMapLayer *layer = nullptr;

if ( type == QLatin1String( "vector" ) )
@@ -307,14 +311,18 @@ QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( QDomDocument
QString typeName = layerElem.attribute( QStringLiteral( "name" ) );
layer = QgsApplication::pluginLayerRegistry()->createLayer( typeName );
}
else
{
errorMessage = QObject::tr( "Unsupported layer type: %1" ).arg( type );
}

if ( !layer )
continue;

// always add the layer, even if the source is invalid -- this allows users to fix the source
// at a later stage and still retain all the layer properties intact
layer->readLayerXml( layerElem, context );
layers << layer;
if ( layer )
{
// always add the layer, even if the source is invalid -- this allows users to fix the source
// at a later stage and still retain all the layer properties intact
layer->readLayerXml( layerElem, context );
layers << layer;
}
layerElem = layerElem.nextSiblingElement( QStringLiteral( "maplayer" ) );
}
return layers;
@@ -342,7 +350,6 @@ QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( const QStrin
return QgsLayerDefinition::loadLayerDefinitionLayers( doc, context );
}


void QgsLayerDefinition::DependencySorter::init( const QDomDocument &doc )
{
// Determine a loading order of layers based on a graph of dependencies
@@ -115,6 +115,12 @@ class CORE_EXPORT QgsLayerDefinition
bool mHasMissingDependency;
void init( const QDomDocument &doc );
};

private:

static QList<QgsMapLayer *> loadLayerDefinitionLayersInternal( QDomDocument &document, QgsReadWriteContext &context, QString &errorMessage );


};

#endif // QGSLAYERDEFINITION_H

0 comments on commit 2884e07

Please sign in to comment.