Skip to content

Commit

Permalink
Merge pull request #35413 from suricactus/layer_toggle
Browse files Browse the repository at this point in the history
 FIX #33874 Turn on/off ALL selected layers with "Space" button
  • Loading branch information
m-kuhn committed Apr 3, 2020
2 parents 75ddf3c + 0892819 commit c6621a9
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -299,6 +299,7 @@
<file>themes/default/mActionHelpSponsors.png</file>
<file>themes/default/mActionHideAllLayers.svg</file>
<file>themes/default/mActionToggleAllLayers.svg</file>
<file>themes/default/mActionToggleSelectedLayers.svg</file>
<file>themes/default/mActionHideSelectedLayers.svg</file>
<file>themes/default/mActionHideDeselectedLayers.svg</file>
<file>themes/default/mActionHistory.svg</file>
Expand Down
126 changes: 126 additions & 0 deletions images/themes/default/mActionToggleSelectedLayers.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions python/gui/auto_generated/qgisinterface.sip.in
Expand Up @@ -578,6 +578,20 @@ 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 *actionToggleSelectedLayersIndependently() = 0;
%Docstring
Returns the Toggle Selected Layers Independently action.

.. versionadded:: 3.14
%End

virtual QAction *actionHideDeselectedLayers() = 0;
%Docstring
Returns the Hide Deselected Layers action.
Expand Down
31 changes: 31 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -2679,6 +2679,8 @@ 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( mActionToggleSelectedLayersIndependently, &QAction::triggered, this, &QgisApp::toggleSelectedLayersIndependently );
connect( mActionHideDeselectedLayers, &QAction::triggered, this, &QgisApp::hideDeselectedLayers );

// Plugin Menu Items
Expand Down Expand Up @@ -7592,6 +7594,35 @@ void QgisApp::hideSelectedLayers()
}
}

void QgisApp::toggleSelectedLayers()
{
QgsDebugMsg( QStringLiteral( "toggling selected layers!" ) );

const auto constSelectedNodes = mLayerTreeView->selectedNodes();
if ( ! constSelectedNodes.isEmpty() )
{
bool isFirstNodeChecked = constSelectedNodes[0]->itemVisibilityChecked();
for ( QgsLayerTreeNode *node : constSelectedNodes )
{
node->setItemVisibilityChecked( ! isFirstNodeChecked );
}
}
}

void QgisApp::toggleSelectedLayersIndependently()
{
QgsDebugMsg( QStringLiteral( "toggling selected layers independently!" ) );

const auto constSelectedNodes = mLayerTreeView->selectedNodes();
if ( ! constSelectedNodes.isEmpty() )
{
for ( QgsLayerTreeNode *node : constSelectedNodes )
{
node->setItemVisibilityChecked( ! node->itemVisibilityChecked() );
}
}
}

void QgisApp::hideDeselectedLayers()
{
QList<QgsLayerTreeLayer *> selectedLayerNodes = mLayerTreeView->selectedLayerNodes();
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -549,6 +549,8 @@ 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 *actionToggleSelectedLayersIndependently() { return mActionToggleSelectedLayersIndependently; }
QAction *actionHideDeselectedLayers() { return mActionHideDeselectedLayers; }
QAction *actionShowSelectedLayers() { return mActionShowSelectedLayers; }

Expand Down Expand Up @@ -1423,6 +1425,10 @@ 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 depending on the state of the first layer in selection (first clicked)
void toggleSelectedLayers();
//! Toggles the visibility of the selected layers independently
void toggleSelectedLayersIndependently();
//! Hides any layers which are not selected
void hideDeselectedLayers();
//reimplements method from base (gui) class
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisappinterface.cpp
Expand Up @@ -694,6 +694,8 @@ 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::actionToggleSelectedLayersIndependently() { return qgis->actionToggleSelectedLayersIndependently(); }
QAction *QgisAppInterface::actionHideDeselectedLayers() { return qgis->actionHideDeselectedLayers(); }
QAction *QgisAppInterface::actionShowSelectedLayers() { return qgis->actionShowSelectedLayers(); }

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisappinterface.h
Expand Up @@ -256,6 +256,8 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
QAction *actionHideAllLayers() override;
QAction *actionShowAllLayers() override;
QAction *actionHideSelectedLayers() override;
QAction *actionToggleSelectedLayers() override;
QAction *actionToggleSelectedLayersIndependently() override;
QAction *actionHideDeselectedLayers() override;
QAction *actionShowSelectedLayers() override;
QAction *actionManagePlugins() override;
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsmapthemes.cpp
Expand Up @@ -42,6 +42,8 @@ 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()->actionToggleSelectedLayersIndependently() );
mMenu->addAction( QgisApp::instance()->actionHideDeselectedLayers() );
mMenu->addSeparator();

Expand Down
17 changes: 17 additions & 0 deletions src/gui/layertree/qgslayertreeview.cpp
Expand Up @@ -507,6 +507,23 @@ void QgsLayerTreeView::mouseReleaseEvent( QMouseEvent *event )

void QgsLayerTreeView::keyPressEvent( QKeyEvent *event )
{
if ( event->key() == Qt::Key_Space )
{
const auto constSelectedNodes = selectedNodes();

if ( ! constSelectedNodes.isEmpty() )
{
bool isFirstNodeChecked = constSelectedNodes[0]->itemVisibilityChecked();
for ( QgsLayerTreeNode *node : constSelectedNodes )
{
node->setItemVisibilityChecked( ! isFirstNodeChecked );
}

// 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
12 changes: 12 additions & 0 deletions src/gui/qgisinterface.h
Expand Up @@ -512,6 +512,18 @@ 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 Toggle Selected Layers Independently action.
* \since QGIS 3.14
*/
virtual QAction *actionToggleSelectedLayersIndependently() = 0;

/**
* Returns the Hide Deselected Layers action.
* \since QGIS 3.0
Expand Down
16 changes: 16 additions & 0 deletions src/ui/qgisapp.ui
Expand Up @@ -151,6 +151,8 @@
<addaction name="mActionHideAllLayers"/>
<addaction name="mActionShowSelectedLayers"/>
<addaction name="mActionHideSelectedLayers"/>
<addaction name="mActionToggleSelectedLayers"/>
<addaction name="mActionToggleSelectedLayersIndependently"/>
<addaction name="mActionHideDeselectedLayers"/>
<addaction name="separator"/>
</widget>
Expand Down Expand Up @@ -2676,6 +2678,20 @@ Acts on the currently active layer only.</string>
<string>Hide Selected Layers</string>
</property>
</action>
<action name="mActionToggleSelectedLayers">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionToggleSelectedLayers.svg</normaloff>:/images/themes/default/mActionToggleSelectedLayers.svg</iconset>
</property>
<property name="text">
<string>Toggle Selected Layers</string>
</property>
</action>
<action name="mActionToggleSelectedLayersIndependently">
<property name="text">
<string>Toggle Selected Layers Independently</string>
</property>
</action>
<action name="mActionHideDeselectedLayers">
<property name="icon">
<iconset resource="../../images/images.qrc">
Expand Down

0 comments on commit c6621a9

Please sign in to comment.