From b1450802b99ac4a19b5ed4765066bcdf1e520556 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 14 Jun 2016 11:31:58 +1000 Subject: [PATCH] Fix joins are not recreated when loading project with bad layers (fix #10500) --- src/core/qgsproject.cpp | 31 ++++++++++++++++++++++++++++--- src/core/qgsproject.h | 5 +++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/core/qgsproject.cpp b/src/core/qgsproject.cpp index a614a8ea857..76e1ab8f5e2 100644 --- a/src/core/qgsproject.cpp +++ b/src/core/qgsproject.cpp @@ -578,6 +578,15 @@ QgsProjectVersion getVersion( const QDomDocument& doc ) return projectVersion; } +void QgsProject::processLayerJoins( QgsVectorLayer* layer ) +{ + if ( !layer ) + return; + + layer->createJoinCaches(); + layer->updateFields(); +} + bool QgsProject::_getMapLayers( const QDomDocument& doc, QList& brokenNodes ) { // Layer order is set by the restoring the legend settings from project file. @@ -641,8 +650,7 @@ bool QgsProject::_getMapLayers( const QDomDocument& doc, QList& broken QList< QPair< QgsVectorLayer*, QDomElement > >::iterator vIt = vLayerList.begin(); for ( ; vIt != vLayerList.end(); ++vIt ) { - vIt->first->createJoinCaches(); - vIt->first->updateFields(); + processLayerJoins( vIt->first ); } QSet notified; @@ -975,7 +983,24 @@ bool QgsProject::read( QDomNode &layerNode ) { QList brokenNodes; QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList; - return addLayer( layerNode.toElement(), brokenNodes, vectorLayerList ); + if ( addLayer( layerNode.toElement(), brokenNodes, vectorLayerList ) ) + { + // have to try to update joins for all layers now - a previously added layer may be dependant on this newly + // added layer for joins + QVector vectorLayers = QgsMapLayerRegistry::instance()->layers(); + Q_FOREACH ( QgsVectorLayer* layer, vectorLayers ) + { + processLayerJoins( layer ); + } + + if ( !vectorLayerList.isEmpty() ) + { + emit readMapLayer( vectorLayerList.at( 0 ).first, vectorLayerList.at( 0 ).second ); + } + + return true; + } + return false; } bool QgsProject::write( QFileInfo const &file ) diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index dad21815686..4cec867c8fc 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -491,6 +491,11 @@ class CORE_EXPORT QgsProject : public QObject */ bool _getMapLayers( QDomDocument const &doc, QList& brokenNodes ); + /** Processes any joins attached to a newly added layer. + * @param layer layer to process + */ + void processLayerJoins( QgsVectorLayer* layer ); + QString mErrorMessage; QgsProjectBadLayerHandler* mBadLayerHandler;