Skip to content

Commit

Permalink
FIX #33874 Turn on/off ALL selected layers with "Space" button
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Mar 27, 2020
1 parent c168e75 commit 1dc0800
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgisinterface.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QgsLayerTreeLayer *> selectedLayerNodes = mLayerTreeView->selectedLayerNodes();
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisappinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
1 change: 1 addition & 0 deletions src/app/qgisappinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsmapthemes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
16 changes: 16 additions & 0 deletions src/gui/layertree/qgslayertreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgisinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/ui/qgisapp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<addaction name="mActionHideAllLayers"/>
<addaction name="mActionShowSelectedLayers"/>
<addaction name="mActionHideSelectedLayers"/>
<addaction name="mActionToggleSelectedLayers"/>
<addaction name="mActionHideDeselectedLayers"/>
<addaction name="separator"/>
</widget>
Expand Down Expand Up @@ -2673,6 +2674,17 @@ Acts on the currently active layer only.</string>
<string>Hide Selected Layers</string>
</property>
</action>
<action name="mActionToggleSelectedLayers">
<property name="text">
<string>Toggle Selected Layers</string>
</property>
<property name="shortcut">
<string>Space</string>
</property>
<property name="shortcutContext">
<enum>Qt::WidgetShortcut</enum>
</property>
</action>
<action name="mActionHideDeselectedLayers">
<property name="icon">
<iconset resource="../../images/images.qrc">
Expand Down

0 comments on commit 1dc0800

Please sign in to comment.