diff --git a/python/gui/auto_generated/qgisinterface.sip.in b/python/gui/auto_generated/qgisinterface.sip.in index 06c7c2dbffb5..33ea2af29d2f 100644 --- a/python/gui/auto_generated/qgisinterface.sip.in +++ b/python/gui/auto_generated/qgisinterface.sip.in @@ -578,6 +578,13 @@ Statistical summary action. virtual QAction *actionShowAllLayers() = 0; virtual QAction *actionHideSelectedLayers() = 0; + virtual QAction *actionToggleSelectedLayers() = 0; +%Docstring +Returns the Toggle Selected Layers action. + +.. versionadded:: 3.14 +%End + virtual QAction *actionHideDeselectedLayers() = 0; %Docstring Returns the Hide Deselected Layers action. diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 8a0bbc4afb12..4025b3f6dcf0 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -2650,6 +2650,7 @@ void QgisApp::createActions() connect( mActionHideAllLayers, &QAction::triggered, this, &QgisApp::hideAllLayers ); connect( mActionShowSelectedLayers, &QAction::triggered, this, &QgisApp::showSelectedLayers ); connect( mActionHideSelectedLayers, &QAction::triggered, this, &QgisApp::hideSelectedLayers ); + connect( mActionToggleSelectedLayers, &QAction::triggered, this, &QgisApp::toggleSelectedLayers ); connect( mActionHideDeselectedLayers, &QAction::triggered, this, &QgisApp::hideDeselectedLayers ); // Plugin Menu Items @@ -7554,6 +7555,18 @@ void QgisApp::hideSelectedLayers() } } +//reimplements method from base (gui) class +void QgisApp::toggleSelectedLayers() +{ + QgsDebugMsg( QStringLiteral( "toggling selected layers!" ) ); + + const auto constSelectedNodes = mLayerTreeView->selectedNodes(); + for ( QgsLayerTreeNode *node : constSelectedNodes ) + { + node->setItemVisibilityChecked( ! node->isVisible() ); + } +} + void QgisApp::hideDeselectedLayers() { QList selectedLayerNodes = mLayerTreeView->selectedLayerNodes(); diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h index 0244c88f764a..39fdc8d621fb 100644 --- a/src/app/qgisapp.h +++ b/src/app/qgisapp.h @@ -544,6 +544,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow QAction *actionHideAllLayers() { return mActionHideAllLayers; } QAction *actionShowAllLayers() { return mActionShowAllLayers; } QAction *actionHideSelectedLayers() { return mActionHideSelectedLayers; } + QAction *actionToggleSelectedLayers() { return mActionToggleSelectedLayers; } QAction *actionHideDeselectedLayers() { return mActionHideDeselectedLayers; } QAction *actionShowSelectedLayers() { return mActionShowSelectedLayers; } @@ -1412,6 +1413,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow void showAllLayers(); //reimplements method from base (gui) class void hideSelectedLayers(); + //! Toggles the visibility of the selected layers + void toggleSelectedLayers(); //! Hides any layers which are not selected void hideDeselectedLayers(); //reimplements method from base (gui) class diff --git a/src/app/qgisappinterface.cpp b/src/app/qgisappinterface.cpp index d2fe3c92a166..24227de5fcc8 100644 --- a/src/app/qgisappinterface.cpp +++ b/src/app/qgisappinterface.cpp @@ -684,6 +684,7 @@ QAction *QgisAppInterface::actionRemoveAllFromOverview() { return qgis->actionRe QAction *QgisAppInterface::actionHideAllLayers() { return qgis->actionHideAllLayers(); } QAction *QgisAppInterface::actionShowAllLayers() { return qgis->actionShowAllLayers(); } QAction *QgisAppInterface::actionHideSelectedLayers() { return qgis->actionHideSelectedLayers(); } +QAction *QgisAppInterface::actionToggleSelectedLayers() { return qgis->actionToggleSelectedLayers(); } QAction *QgisAppInterface::actionHideDeselectedLayers() { return qgis->actionHideDeselectedLayers(); } QAction *QgisAppInterface::actionShowSelectedLayers() { return qgis->actionShowSelectedLayers(); } diff --git a/src/app/qgisappinterface.h b/src/app/qgisappinterface.h index 1468d0fe0172..dd36ecec46f2 100644 --- a/src/app/qgisappinterface.h +++ b/src/app/qgisappinterface.h @@ -254,6 +254,7 @@ class APP_EXPORT QgisAppInterface : public QgisInterface QAction *actionHideAllLayers() override; QAction *actionShowAllLayers() override; QAction *actionHideSelectedLayers() override; + QAction *actionToggleSelectedLayers() override; QAction *actionHideDeselectedLayers() override; QAction *actionShowSelectedLayers() override; QAction *actionManagePlugins() override; diff --git a/src/app/qgsmapthemes.cpp b/src/app/qgsmapthemes.cpp index 687cb3bd97e1..c3006ad2dd1d 100644 --- a/src/app/qgsmapthemes.cpp +++ b/src/app/qgsmapthemes.cpp @@ -42,6 +42,7 @@ QgsMapThemes::QgsMapThemes() mMenu->addAction( QgisApp::instance()->actionHideAllLayers() ); mMenu->addAction( QgisApp::instance()->actionShowSelectedLayers() ); mMenu->addAction( QgisApp::instance()->actionHideSelectedLayers() ); + mMenu->addAction( QgisApp::instance()->actionToggleSelectedLayers() ); mMenu->addAction( QgisApp::instance()->actionHideDeselectedLayers() ); mMenu->addSeparator(); diff --git a/src/gui/layertree/qgslayertreeview.cpp b/src/gui/layertree/qgslayertreeview.cpp index 7fe243af10da..68af1ac50034 100644 --- a/src/gui/layertree/qgslayertreeview.cpp +++ b/src/gui/layertree/qgslayertreeview.cpp @@ -507,6 +507,22 @@ void QgsLayerTreeView::mouseReleaseEvent( QMouseEvent *event ) void QgsLayerTreeView::keyPressEvent( QKeyEvent *event ) { + if ( event->key() == Qt::Key_Space ) + { + const auto constSelectedNodes = selectedNodes(); + + if ( ! constSelectedNodes.isEmpty() ) + { + for ( QgsLayerTreeNode *node : constSelectedNodes ) + { + node->setItemVisibilityChecked( ! node->isVisible() ); + } + + // if we call the original keyPress handler, the current item will be checked to the original state yet again + return; + } + } + const QgsLayerTreeModel::Flags oldFlags = layerTreeModel()->flags(); if ( event->modifiers() & Qt::ControlModifier ) layerTreeModel()->setFlags( oldFlags | QgsLayerTreeModel::ActionHierarchical ); diff --git a/src/gui/qgisinterface.h b/src/gui/qgisinterface.h index c748e3aa6bf3..78a329c23b10 100644 --- a/src/gui/qgisinterface.h +++ b/src/gui/qgisinterface.h @@ -511,6 +511,12 @@ class GUI_EXPORT QgisInterface : public QObject virtual QAction *actionShowAllLayers() = 0; virtual QAction *actionHideSelectedLayers() = 0; + /** + * Returns the Toggle Selected Layers action. + * \since QGIS 3.14 + */ + virtual QAction *actionToggleSelectedLayers() = 0; + /** * Returns the Hide Deselected Layers action. * \since QGIS 3.0 diff --git a/src/ui/qgisapp.ui b/src/ui/qgisapp.ui index 421b1c70d803..408a8bc8c75c 100644 --- a/src/ui/qgisapp.ui +++ b/src/ui/qgisapp.ui @@ -151,6 +151,7 @@ + @@ -2673,6 +2674,17 @@ Acts on the currently active layer only. Hide Selected Layers + + + Toggle Selected Layers + + + Space + + + Qt::WidgetShortcut + +