Skip to content
Permalink
Browse files

Only filter legend nodes that are checked.

Keep unchecked legend items when filtered by expression
  • Loading branch information
Hugo Mercier
Hugo Mercier committed Mar 14, 2016
1 parent 5676430 commit f9c736c9f8dff79f98e2abaff6efc1e967fb9f81
@@ -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
{
@@ -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 )
@@ -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 )
{
@@ -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() ) )
@@ -1108,7 +1111,7 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::filterLegendNodes( const
filtered << node;
}
}
else // unknown node type
else // unknown node type or unchecked
filtered << node;
}
}
@@ -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;
@@ -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()
@@ -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 )
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit f9c736c

Please sign in to comment.
You can’t perform that action at this time.