Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #8612 from elpaso/vector-legend-toggle-items
Browse files Browse the repository at this point in the history
Add a toggle action to vector layer legend items [feature]
  • Loading branch information
elpaso committed Dec 7, 2018
2 parents 733d4ed + 4654790 commit 16922a3
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 3 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -277,6 +277,7 @@
<file>themes/default/mActionHelpContents.svg</file>
<file>themes/default/mActionHelpSponsors.png</file>
<file>themes/default/mActionHideAllLayers.svg</file>
<file>themes/default/mActionToggleAllLayers.svg</file>
<file>themes/default/mActionHideSelectedLayers.svg</file>
<file>themes/default/mActionHideDeselectedLayers.svg</file>
<file>themes/default/mActionHistory.svg</file>
Expand Down
165 changes: 165 additions & 0 deletions images/themes/default/mActionToggleAllLayers.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -269,6 +269,8 @@ Checks all items belonging to the same layer as this node.

.. seealso:: :py:func:`uncheckAllItems`

.. seealso:: :py:func:`toggleAllItems`

.. versionadded:: 2.14
%End

Expand All @@ -278,7 +280,20 @@ Unchecks all items belonging to the same layer as this node.

.. seealso:: :py:func:`checkAllItems`

.. seealso:: :py:func:`toggleAllItems`

.. versionadded:: 2.14
%End

void toggleAllItems();
%Docstring
Toggle all items belonging to the same layer as this node.

.. seealso:: :py:func:`checkAllItems`

.. seealso:: :py:func:`uncheckAllItems`

.. versionadded:: 3.6
%End

};
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -476,6 +476,8 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
// symbology item
if ( symbolNode->flags() & Qt::ItemIsUserCheckable )
{
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionToggleAllLayers.svg" ) ), tr( "&Toggle Items" ),
symbolNode, &QgsSymbolLegendNode::toggleAllItems );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) ), tr( "&Show All Items" ),
symbolNode, &QgsSymbolLegendNode::checkAllItems );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionHideAllLayers.svg" ) ), tr( "&Hide All Items" ),
Expand Down
22 changes: 19 additions & 3 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -227,6 +227,22 @@ void QgsSymbolLegendNode::uncheckAllItems()
checkAll( false );
}

void QgsSymbolLegendNode::toggleAllItems()
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
if ( !vlayer || !vlayer->renderer() )
return;

const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
for ( const auto &item : symbolList )
{
vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), ! vlayer->renderer()->legendSymbolItemChecked( item.ruleKey() ) );
}

emit dataChanged();
vlayer->triggerRepaint();
}

QgsRenderContext *QgsLayerTreeModelLegendNode::createTemporaryRenderContext() const
{
double scale = 0.0;
Expand All @@ -239,7 +255,7 @@ QgsRenderContext *QgsLayerTreeModelLegendNode::createTemporaryRenderContext() co
return nullptr;

// setup temporary render context
std::unique_ptr<QgsRenderContext> context( new QgsRenderContext );
std::unique_ptr<QgsRenderContext> context = qgis::make_unique<QgsRenderContext>( );
context->setScaleFactor( dpi / 25.4 );
context->setRendererScale( scale );
context->setMapToPixel( QgsMapToPixel( mupp ) );
Expand All @@ -252,8 +268,8 @@ void QgsSymbolLegendNode::checkAll( bool state )
if ( !vlayer || !vlayer->renderer() )
return;

QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
Q_FOREACH ( const QgsLegendSymbolItem &item, symbolList )
const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
for ( const auto &item : symbolList )
{
vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), state );
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -251,17 +251,27 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode
/**
* Checks all items belonging to the same layer as this node.
* \see uncheckAllItems()
* \see toggleAllItems()
* \since QGIS 2.14
*/
void checkAllItems();

/**
* Unchecks all items belonging to the same layer as this node.
* \see checkAllItems()
* \see toggleAllItems()
* \since QGIS 2.14
*/
void uncheckAllItems();

/**
* Toggle all items belonging to the same layer as this node.
* \see checkAllItems()
* \see uncheckAllItems()
* \since QGIS 3.6
*/
void toggleAllItems();

private:
void updateLabel();

Expand Down

0 comments on commit 16922a3

Please sign in to comment.