Skip to content

Commit 0224b8e

Browse files
committedJun 12, 2019
If the first layers loaded into a project is non-valid (e.g. qlrs
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
1 parent a4eb6b0 commit 0224b8e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
 

‎src/gui/layertree/qgslayertreemapcanvasbridge.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge( QgsLayerTree *root, Qg
3737
connect( root, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged );
3838
connect( root, &QgsLayerTree::layerOrderChanged, this, &QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers );
3939

40+
connect( QgsProject::instance(), &QgsProject::layersAdded, this, &QgsLayerTreeMapCanvasBridge::layersAdded );
41+
4042
setCanvasLayers();
4143
}
4244

@@ -70,21 +72,27 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
7072

7173
const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
7274
int currentSpatialLayerCount = 0;
75+
int currentValidSpatialLayerCount = 0;
7376
for ( QgsLayerTreeLayer *layerNode : layerNodes )
7477
{
7578
if ( layerNode->layer() && layerNode->layer()->isSpatial() )
79+
{
7680
currentSpatialLayerCount++;
81+
if ( layerNode->layer()->isValid() )
82+
currentValidSpatialLayerCount++;
83+
}
7784
}
7885

7986
bool firstLayers = mAutoSetupOnFirstLayer && !mHasLayersLoaded && currentSpatialLayerCount != 0;
87+
bool firstValidLayers = mAutoSetupOnFirstLayer && !mHasValidLayersLoaded && currentValidSpatialLayerCount != 0;
8088

8189
mCanvas->setLayers( canvasLayers );
8290
if ( mOverviewCanvas )
8391
mOverviewCanvas->setLayers( overviewLayers );
8492

85-
if ( firstLayers )
93+
if ( firstValidLayers )
8694
{
87-
// if we are moving from zero to non-zero layers, let's zoom to those data
95+
// if we are moving from zero to non-zero layers, let's zoom to those data (only consider valid layers here!)
8896
mCanvas->zoomToFullExtent();
8997
}
9098

@@ -116,6 +124,7 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
116124
}
117125

118126
mHasLayersLoaded = currentSpatialLayerCount;
127+
mHasValidLayersLoaded = currentValidSpatialLayerCount;
119128
if ( currentSpatialLayerCount == 0 )
120129
mFirstCRS = QgsCoordinateReferenceSystem();
121130

@@ -164,3 +173,22 @@ void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged( QgsLayerTreeNode *n
164173
if ( key == QLatin1String( "overview" ) )
165174
deferredSetCanvasLayers();
166175
}
176+
177+
void QgsLayerTreeMapCanvasBridge::layersAdded( const QList<QgsMapLayer *> &layers )
178+
{
179+
for ( QgsMapLayer *l : layers )
180+
{
181+
if ( l )
182+
{
183+
connect( l, &QgsMapLayer::dataSourceChanged, this, [ this, l ]
184+
{
185+
if ( l->isValid() && l->isSpatial() && mAutoSetupOnFirstLayer && !mHasValidLayersLoaded )
186+
{
187+
mHasValidLayersLoaded = true;
188+
// if we are moving from zero valid layers to non-zero VALID layers, let's zoom to those data
189+
mCanvas->zoomToFullExtent();
190+
}
191+
} );
192+
}
193+
}
194+
}

‎src/gui/layertree/qgslayertreemapcanvasbridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class GUI_EXPORT QgsLayerTreeMapCanvasBridge : public QObject
102102
private slots:
103103
void nodeVisibilityChanged();
104104
void nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
105+
void layersAdded( const QList<QgsMapLayer *> &layers );
105106

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

121122
bool mHasFirstLayer;
122123
bool mHasLayersLoaded;
124+
bool mHasValidLayersLoaded = false;
123125
bool mUpdatingProjectLayerOrder = false;
124126

125127
QgsCoordinateReferenceSystem mFirstCRS;

0 commit comments

Comments
 (0)
Please sign in to comment.