Skip to content

Commit

Permalink
Avoid unnecessary handling of active layer changed when we are
Browse files Browse the repository at this point in the history
adding many layers at once, or removing many (e.g. due to project
clear)

Saves a lot of unnecessary and potentially expensive processing

(cherry-picked from 59dbe0e)
  • Loading branch information
nyalldawson committed Aug 17, 2018
1 parent 5d6ea60 commit 9995238
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -250,7 +250,7 @@ QgsGpsInformationWidget::QgsGpsInformationWidget( QgsMapCanvas *thepCanvas, QWid
//SLM - added functionality
mLogFile = nullptr;

connect( QgisApp::instance()->layerTreeView(), &QgsLayerTreeView::currentLayerChanged,
connect( QgisApp::instance(), &QgisApp::activeLayerChanged,
this, &QgsGpsInformationWidget::updateCloseFeatureButton );

mStackedWidget->setCurrentIndex( 3 ); // force to Options
Expand Down
31 changes: 25 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -566,9 +566,13 @@ void QgisApp::layerTreeViewDoubleClicked( const QModelIndex &index )
}
}

void QgisApp::activeLayerChanged( QgsMapLayer *layer )
void QgisApp::onActiveLayerChanged( QgsMapLayer *layer )
{
Q_FOREACH ( QgsMapCanvas *canvas, mapCanvases() )
if ( mBlockActiveLayerChanged )
return;

const QList< QgsMapCanvas * > canvases = mapCanvases();
for ( QgsMapCanvas *canvas : canvases )
canvas->setCurrentLayer( layer );

if ( mUndoWidget )
Expand All @@ -583,6 +587,8 @@ void QgisApp::activeLayerChanged( QgsMapLayer *layer )
}
updateUndoActions();
}

emit activeLayerChanged( layer );
}

/*
Expand Down Expand Up @@ -1684,6 +1690,10 @@ QVector<QPointer<QgsLayoutCustomDropHandler> > QgisApp::customLayoutDropHandlers

void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
{
// avoid unnecessary work when adding lots of layers at once - defer emitting the active layer changed signal until we've
// added all layers, and only emit the signal once for the final layer added
mBlockActiveLayerChanged = true;

// insert items in reverse order as each one is inserted on top of previous one
for ( int i = lst.size() - 1 ; i >= 0 ; i-- )
{
Expand Down Expand Up @@ -1724,8 +1734,10 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
openFile( u.uri, QStringLiteral( "project" ) );
}
}
}

mBlockActiveLayerChanged = false;
emit activeLayerChanged( activeLayer() );
}

bool QgisApp::event( QEvent *event )
{
Expand Down Expand Up @@ -3299,9 +3311,9 @@ void QgisApp::setupConnections()
} );

// connect legend signals
connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged,
connect( this, &QgisApp::activeLayerChanged,
this, &QgisApp::activateDeactivateLayerRelatedActions );
connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged,
connect( this, &QgisApp::activeLayerChanged,
this, &QgisApp::setMapStyleDockLayer );

connect( mLayerTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
Expand Down Expand Up @@ -3779,7 +3791,7 @@ void QgisApp::initLayerTreeView()
setupLayerTreeViewFromSettings();

connect( mLayerTreeView, &QAbstractItemView::doubleClicked, this, &QgisApp::layerTreeViewDoubleClicked );
connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged, this, &QgisApp::activeLayerChanged );
connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged, this, &QgisApp::onActiveLayerChanged );
connect( mLayerTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgisApp::updateNewLayerInsertionPoint );
connect( QgsProject::instance()->layerTreeRegistryBridge(), &QgsLayerTreeRegistryBridge::addedLayersToLayerTree,
this, &QgisApp::autoSelectAddedLayer );
Expand Down Expand Up @@ -10983,7 +10995,14 @@ void QgisApp::closeProject()
mMapCanvas->clearCache();
mOverviewCanvas->setLayers( QList<QgsMapLayer *>() );
mMapCanvas->freeze( false );

// Avoid unnecessary layer changed handling for each layer removed - instead,
// defer the handling until we've removed all layers
mBlockActiveLayerChanged = true;
QgsProject::instance()->clear();
mBlockActiveLayerChanged = false;

emit activeLayerChanged( activeLayer() );
}


Expand Down
12 changes: 11 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -710,7 +710,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void updateNewLayerInsertionPoint();
//! connected to layer tree registry bridge, selects first of the newly added map layers
void autoSelectAddedLayer( QList<QgsMapLayer *> layers );
void activeLayerChanged( QgsMapLayer *layer );

//! Zoom to full extent
void zoomFull();
//! Zoom to the previous extent
Expand Down Expand Up @@ -1647,6 +1647,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! handle project crs changes
void projectCrsChanged();

void onActiveLayerChanged( QgsMapLayer *layer );

signals:

/**
Expand Down Expand Up @@ -1720,6 +1722,11 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
void layerSavedAs( QgsMapLayer *l, const QString &path );

/**
* Emitted when the active layer is changed.
*/
void activeLayerChanged( QgsMapLayer *layer );

private:
void startProfile( const QString &name );
void endProfile();
Expand Down Expand Up @@ -2256,6 +2263,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QMap< QString, QString > mProjectPropertiesPagesMap;
QMap< QString, QString > mSettingPagesMap;

//! True if we are blocking the activeLayerChanged signal from being emitted
bool mBlockActiveLayerChanged = false;

friend class TestQgisAppPython;
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisappinterface.cpp
Expand Up @@ -53,7 +53,7 @@ QgisAppInterface::QgisAppInterface( QgisApp *_qgis )
, pluginManagerIface( _qgis->pluginManager() )
{
// connect signals
connect( qgis->layerTreeView(), &QgsLayerTreeView::currentLayerChanged,
connect( qgis, &QgisApp::activeLayerChanged,
this, &QgisInterface::currentLayerChanged );
connect( qgis, &QgisApp::currentThemeChanged,
this, &QgisAppInterface::currentThemeChanged );
Expand Down

0 comments on commit 9995238

Please sign in to comment.