Skip to content

Commit

Permalink
If the first layers loaded into a project is non-valid (e.g. qlrs
Browse files Browse the repository at this point in the history
pointing to a broken path), then defer the default zoom to the layer's extent
until AFTER the layer path is fixed

Avoids the situation where a user:
- loads a broken qlr
- canvas goes to a invalid extent, since the first loaded layer has an invalid extent
- user fixes the layer path, but there's no visible changes -- because the canvas
is pointing to some random location
  • Loading branch information
nyalldawson committed Jun 12, 2019
1 parent a4eb6b0 commit 0224b8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge( QgsLayerTree *root, Qg
connect( root, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged );
connect( root, &QgsLayerTree::layerOrderChanged, this, &QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers );

connect( QgsProject::instance(), &QgsProject::layersAdded, this, &QgsLayerTreeMapCanvasBridge::layersAdded );

setCanvasLayers();
}

Expand Down Expand Up @@ -70,21 +72,27 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()

const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
int currentSpatialLayerCount = 0;
int currentValidSpatialLayerCount = 0;
for ( QgsLayerTreeLayer *layerNode : layerNodes )
{
if ( layerNode->layer() && layerNode->layer()->isSpatial() )
{
currentSpatialLayerCount++;
if ( layerNode->layer()->isValid() )
currentValidSpatialLayerCount++;
}
}

bool firstLayers = mAutoSetupOnFirstLayer && !mHasLayersLoaded && currentSpatialLayerCount != 0;
bool firstValidLayers = mAutoSetupOnFirstLayer && !mHasValidLayersLoaded && currentValidSpatialLayerCount != 0;

mCanvas->setLayers( canvasLayers );
if ( mOverviewCanvas )
mOverviewCanvas->setLayers( overviewLayers );

if ( firstLayers )
if ( firstValidLayers )
{
// if we are moving from zero to non-zero layers, let's zoom to those data
// if we are moving from zero to non-zero layers, let's zoom to those data (only consider valid layers here!)
mCanvas->zoomToFullExtent();
}

Expand Down Expand Up @@ -116,6 +124,7 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
}

mHasLayersLoaded = currentSpatialLayerCount;
mHasValidLayersLoaded = currentValidSpatialLayerCount;
if ( currentSpatialLayerCount == 0 )
mFirstCRS = QgsCoordinateReferenceSystem();

Expand Down Expand Up @@ -164,3 +173,22 @@ void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged( QgsLayerTreeNode *n
if ( key == QLatin1String( "overview" ) )
deferredSetCanvasLayers();
}

void QgsLayerTreeMapCanvasBridge::layersAdded( const QList<QgsMapLayer *> &layers )
{
for ( QgsMapLayer *l : layers )
{
if ( l )
{
connect( l, &QgsMapLayer::dataSourceChanged, this, [ this, l ]
{
if ( l->isValid() && l->isSpatial() && mAutoSetupOnFirstLayer && !mHasValidLayersLoaded )
{
mHasValidLayersLoaded = true;
// if we are moving from zero valid layers to non-zero VALID layers, let's zoom to those data
mCanvas->zoomToFullExtent();
}
} );
}
}
}
2 changes: 2 additions & 0 deletions src/gui/layertree/qgslayertreemapcanvasbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class GUI_EXPORT QgsLayerTreeMapCanvasBridge : public QObject
private slots:
void nodeVisibilityChanged();
void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
void layersAdded( const QList<QgsMapLayer *> &layers );

private:
//! Fill canvasLayers and overviewLayers lists from node and its descendants
Expand All @@ -120,6 +121,7 @@ class GUI_EXPORT QgsLayerTreeMapCanvasBridge : public QObject

bool mHasFirstLayer;
bool mHasLayersLoaded;
bool mHasValidLayersLoaded = false;
bool mUpdatingProjectLayerOrder = false;

QgsCoordinateReferenceSystem mFirstCRS;
Expand Down

0 comments on commit 0224b8e

Please sign in to comment.