Skip to content
Permalink
Browse files

Avoid unnecessary handling of active layer changed when we are

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 9995238e003cac1a4343a3c028ae4e19db90462b
Showing with 38 additions and 9 deletions.
  1. +1 −1 src/app/gps/qgsgpsinformationwidget.cpp
  2. +25 −6 src/app/qgisapp.cpp
  3. +11 −1 src/app/qgisapp.h
  4. +1 −1 src/app/qgisappinterface.cpp
@@ -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
@@ -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 )
@@ -583,6 +587,8 @@ void QgisApp::activeLayerChanged( QgsMapLayer *layer )
}
updateUndoActions();
}

emit activeLayerChanged( layer );
}

/*
@@ -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-- )
{
@@ -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 )
{
@@ -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,
@@ -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 );
@@ -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() );
}


@@ -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
@@ -1647,6 +1647,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! handle project crs changes
void projectCrsChanged();

void onActiveLayerChanged( QgsMapLayer *layer );

signals:

/**
@@ -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();
@@ -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;
};

@@ -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 );

0 comments on commit 9995238

Please sign in to comment.
You can’t perform that action at this time.