Skip to content

Commit

Permalink
Fix application hangs when attempting to load qlr file for
Browse files Browse the repository at this point in the history
non vector/raster/vector-tile layer types

Refs #42112
  • Loading branch information
nyalldawson committed Mar 10, 2021
1 parent ebdb7ba commit 10c3a1a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgslayerdefinition.sip.in
Expand Up @@ -116,6 +116,7 @@ Whether some dependency is missing
%End

};

};

/************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -7290,7 +7290,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 );
Expand Down Expand Up @@ -10819,7 +10819,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 );
}
Expand Down
31 changes: 19 additions & 12 deletions src/core/qgslayerdefinition.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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 );

Expand All @@ -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 )
Expand Down Expand Up @@ -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" ) );
Expand All @@ -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" ) )
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgslayerdefinition.h
Expand Up @@ -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 10c3a1a

Please sign in to comment.