Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only filter legend nodes that are checked.
Keep unchecked legend items when filtered by expression
  • Loading branch information
Hugo Mercier committed Mar 14, 2016
1 parent 5676430 commit f9c736c
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -4938,13 +4938,13 @@ void QgisApp::toggleFilterLegendByExpression( bool checked )

void QgisApp::updateFilterLegend()
{
if ( mActionFilterLegend->isChecked() )
bool hasExpressions = mLegendExpressionFilterButton->isChecked() && QgsLayerTreeUtils::hasLegendFilterExpression( *mLayerTreeView->layerTreeModel()->rootGroup() );
if ( mActionFilterLegend->isChecked() || hasExpressions )
{
layerTreeView()->layerTreeModel()->setLegendFilterByMap( &mMapCanvas->mapSettings() );
}
else if ( QgsLayerTreeUtils::hasLegendFilterExpression( *mLayerTreeView->layerTreeModel()->rootGroup() ) )
{
layerTreeView()->layerTreeModel()->setLegendFilter( &mMapCanvas->mapSettings(), /* useExtent */ false );
layerTreeView()->layerTreeModel()->setLegendFilter( &mMapCanvas->mapSettings(),
/* useExtent */ mActionFilterLegend->isChecked(),
/* polygon */ QgsGeometry(),
hasExpressions );
}
else
{
Expand Down
9 changes: 6 additions & 3 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -39,6 +39,7 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
, mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
, mAutoCollapseLegendNodesCount( -1 )
, mLegendFilterByScale( 0 )
, mLegendFilterUsesExtent( false )
, mLegendMapViewMupp( 0 )
, mLegendMapViewDpi( 0 )
, mLegendMapViewScale( 0 )
Expand Down Expand Up @@ -590,6 +591,7 @@ void QgsLayerTreeModel::setLegendFilter( const QgsMapSettings* settings, bool us
mLegendFilterMapSettings.reset( new QgsMapSettings( *settings ) );
mLegendFilterMapSettings->setLayerStyleOverrides( mLayerStyleOverrides );
QgsMapHitTest::LayerFilterExpression exprs;
mLegendFilterUsesExtent = useExtent;
// collect expression filters
if ( useExpressions )
{
Expand Down Expand Up @@ -1098,8 +1100,9 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::filterLegendNodes( const
{
Q_FOREACH ( QgsLayerTreeModelLegendNode* node, nodes )
{
QgsSymbolV2* symbolKey = reinterpret_cast< QgsSymbolV2* >( node->data( QgsSymbolV2LegendNode::SymbolV2LegacyRuleKeyRole ).value<void*>() );
if ( symbolKey )
QgsSymbolV2* ruleKey = reinterpret_cast< QgsSymbolV2* >( node->data( QgsSymbolV2LegendNode::SymbolV2LegacyRuleKeyRole ).value<void*>() );
bool checked = mLegendFilterUsesExtent || node->data( Qt::CheckStateRole ).toInt() == Qt::Checked;
if ( ruleKey && checked )
{
QString ruleKey = node->data( QgsSymbolV2LegendNode::RuleKeyRole ).toString();
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( node->layerNode()->layer() ) )
Expand All @@ -1108,7 +1111,7 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::filterLegendNodes( const
filtered << node;
}
}
else // unknown node type
else // unknown node type or unchecked
filtered << node;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreemodel.h
Expand Up @@ -337,6 +337,9 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
QScopedPointer<QgsMapSettings> mLegendFilterMapSettings;
QScopedPointer<QgsMapHitTest> mLegendFilterHitTest;

//! whether to use map filtering
bool mLegendFilterUsesExtent;

double mLegendMapViewMupp;
int mLegendMapViewDpi;
double mLegendMapViewScale;
Expand Down
12 changes: 8 additions & 4 deletions tests/src/core/testqgslegendrenderer.cpp
Expand Up @@ -458,9 +458,11 @@ void TestQgsLegendRenderer::testFilterByPolygon()
// again with useExtent to true
legendModel.setLegendFilter( &mapSettings, /*useExtent*/ true, *geom.data() );

QString testName2 = testName + "2";
QString report2 = mReport + "2";
_setStandardTestFont( settings );
_renderLegend( testName, &legendModel, settings );
QVERIFY( _verifyImage( testName, mReport ) );
_renderLegend( testName2, &legendModel, settings );
QVERIFY( _verifyImage( testName2, report2 ) );
}

void TestQgsLegendRenderer::testFilterByExpression()
Expand Down Expand Up @@ -497,9 +499,11 @@ void TestQgsLegendRenderer::testFilterByExpression()
legendModel.setLegendFilterByMap( 0 );
legendModel.setLegendFilter( &mapSettings, /*useExtent*/ false );

QString testName2 = testName + "2";
QString report2 = mReport + "2";
_setStandardTestFont( settings );
_renderLegend( testName, &legendModel, settings );
QVERIFY( _verifyImage( testName, mReport ) );
_renderLegend( testName2, &legendModel, settings );
QVERIFY( _verifyImage( testName2, report2 ) );
}

QTEST_MAIN( TestQgsLegendRenderer )
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f9c736c

Please sign in to comment.