Skip to content

Commit

Permalink
Manage multiple layers selection for actions Zoom/Pan to Selection an…
Browse files Browse the repository at this point in the history
…d Zoom to Layers. Fixes #40647
  • Loading branch information
TurboGraphxBeige authored and nyalldawson committed Feb 1, 2021
1 parent ffff5e1 commit 34ef8e1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
Binary file modified resources/data/world_map.gpkg
Binary file not shown.
70 changes: 59 additions & 11 deletions src/app/qgisapp.cpp
Expand Up @@ -4266,9 +4266,10 @@ void QgisApp::setupConnections()
this, &QgisApp::activateDeactivateLayerRelatedActions );
connect( this, &QgisApp::activeLayerChanged,
this, &QgisApp::setMapStyleDockLayer );

connect( mLayerTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgisApp::legendLayerSelectionChanged );
connect( mLayerTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgisApp::activateDeactivateMultipleLayersRelatedActions );
connect( mLayerTreeView->layerTreeModel()->rootGroup(), &QgsLayerTreeNode::addedChildren,
this, &QgisApp::markDirty );
connect( mLayerTreeView->layerTreeModel()->rootGroup(), &QgsLayerTreeNode::addedChildren,
Expand Down Expand Up @@ -14537,6 +14538,8 @@ void QgisApp::selectionChanged( QgsMapLayer *layer )
{
activateDeactivateLayerRelatedActions( layer );
}

activateDeactivateMultipleLayersRelatedActions();
}

void QgisApp::legendLayerSelectionChanged()
Expand Down Expand Up @@ -14618,6 +14621,61 @@ void QgisApp::updateLabelToolButtons()
mActionChangeLabelProperties->setEnabled( enableChange );
}

bool QgisApp::selectedLayersHaveSelection()
{
QList<QgsMapLayer *> layers = mLayerTreeView->selectedLayers();

// If no selected layers, use active layer
if ( layers.size() == 0 )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( activeLayer() );

if ( layer && layer->selectedFeatureCount() == 0 )
return false;

if ( layer && layer->selectedFeatureCount() > 0 )
return true;
}

for ( QgsMapLayer *mapLayer : layers )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mapLayer );

if ( !layer || !layer->isSpatial() || layer->selectedFeatureCount() == 0 )
continue;

return true;
}

return false;
}

bool QgisApp::selectedLayersHaveSpatial()
{
QList<QgsMapLayer *> layers = mLayerTreeView->selectedLayers();

// If no selected layers, use active layer
if ( layers.size() == 0 )
return activeLayer()->isSpatial();

for ( QgsMapLayer *mapLayer : layers )
{
if ( !mapLayer || !mapLayer->isSpatial() )
continue;

return true;
}

return false;
}

void QgisApp::activateDeactivateMultipleLayersRelatedActions()
{
mActionZoomToLayers->setEnabled( selectedLayersHaveSpatial() );
mActionZoomToSelected->setEnabled( selectedLayersHaveSelection() );
mActionPanToSelected->setEnabled( selectedLayersHaveSelection() );
}

void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
{
updateLabelToolButtons();
Expand Down Expand Up @@ -14797,8 +14855,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
mActionDecreaseGamma->setEnabled( false );
mActionZoomActualSize->setEnabled( false );
mActionZoomToLayer->setEnabled( isSpatial );
mActionZoomToLayers->setEnabled( isSpatial );
mActionZoomToSelected->setEnabled( isSpatial );
mActionLabeling->setEnabled( isSpatial );
mActionDiagramProperties->setEnabled( isSpatial );
mActionReverseLine->setEnabled( false );
Expand Down Expand Up @@ -15053,8 +15109,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
mActionSelectRadius->setEnabled( false );
mActionZoomActualSize->setEnabled( true );
mActionZoomToLayer->setEnabled( true );
mActionZoomToLayers->setEnabled( true );
mActionZoomToSelected->setEnabled( false );
mActionOpenTable->setEnabled( false );
mActionSelectAll->setEnabled( false );
mActionReselect->setEnabled( false );
Expand Down Expand Up @@ -15166,8 +15220,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
mActionSelectRadius->setEnabled( false );
mActionZoomActualSize->setEnabled( false );
mActionZoomToLayer->setEnabled( true );
mActionZoomToLayers->setEnabled( true );
mActionZoomToSelected->setEnabled( false );
mActionOpenTable->setEnabled( false );
mActionSelectAll->setEnabled( false );
mActionReselect->setEnabled( false );
Expand Down Expand Up @@ -15233,8 +15285,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
mActionSelectRadius->setEnabled( false );
mActionZoomActualSize->setEnabled( false );
mActionZoomToLayer->setEnabled( true );
mActionZoomToLayers->setEnabled( true );
mActionZoomToSelected->setEnabled( false );
mActionOpenTable->setEnabled( false );
mActionSelectAll->setEnabled( false );
mActionReselect->setEnabled( false );
Expand Down Expand Up @@ -15300,8 +15350,6 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
mActionSelectRadius->setEnabled( false );
mActionZoomActualSize->setEnabled( false );
mActionZoomToLayer->setEnabled( true );
mActionZoomToLayers->setEnabled( true );
mActionZoomToSelected->setEnabled( false );
mActionOpenTable->setEnabled( false );
mActionSelectAll->setEnabled( false );
mActionReselect->setEnabled( false );
Expand Down
9 changes: 9 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -1727,6 +1727,15 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Update the label toolbar buttons
void updateLabelToolButtons();

//! Returns true if at least one selected layer in layer panel has a selection (returns false if none).
bool selectedLayersHaveSelection();

//! Returns true if at least one selected layer in layer panel is spatial (returns false if none).
bool selectedLayersHaveSpatial();

//! Activates or deactivates actions depending on the selected layers in the layer panel.
void activateDeactivateMultipleLayersRelatedActions();

/**
* Activates or deactivates actions depending on the current maplayer type.
* Is called from the legend when the current legend item has changed.
Expand Down

0 comments on commit 34ef8e1

Please sign in to comment.