Skip to content
Permalink
Browse files

Fix legends are empty if presets used with filtered legend (fix #13300)

  • Loading branch information
nyalldawson committed Oct 7, 2015
1 parent 8f04d22 commit dcbe0d370eb511510d66f490c74f21cf76e065f1
@@ -103,6 +103,12 @@ class QgsSymbolLayerV2Utils
static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element ) /Factory/;
static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );

/** Returns a string representing the symbol. Can be used to test for equality
* between symbols.
* @note added in QGIS 2.12
*/
static QString symbolProperties( QgsSymbolV2* symbol );

static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );

static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
@@ -537,14 +537,22 @@ void QgsComposerLegend::mapLayerStyleOverridesChanged()
return;

// map's style has been changed, so make sure to update the legend here
if ( mLegendFilterByMap )
{
// legend is being filtered by map, so we need to re run the hit test too
// as the style overrides may also have affected the visible symbols
updateFilterByMap();
}
else
{
mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );

mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );

Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLegendModel2->rootGroup()->findLayers() )
mLegendModel2->refreshLayerLegend( nodeLayer );
Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLegendModel2->rootGroup()->findLayers() )
mLegendModel2->refreshLayerLegend( nodeLayer );

adjustBoxSize();
update();
adjustBoxSize();
update();
}
}

void QgsComposerLegend::updateFilterByMap()
@@ -562,6 +562,7 @@ void QgsLayerTreeModel::setLegendFilterByMap( const QgsMapSettings* settings )
if ( settings && settings->hasValidSettings() )
{
mLegendFilterByMapSettings.reset( new QgsMapSettings( *settings ) );
mLegendFilterByMapSettings->setLayerStyleOverrides( mLayerStyleOverrides );
mLegendFilterByMapHitTest.reset( new QgsMapHitTest( *mLegendFilterByMapSettings ) );
mLegendFilterByMapHitTest->run();
}
@@ -1030,7 +1031,7 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::filterLegendNodes( const
{
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( node->layerNode()->layer() ) )
{
if ( mLegendFilterByMapHitTest->symbolsForLayer( vl ).contains( ruleKey ) )
if ( mLegendFilterByMapHitTest->symbolVisible( ruleKey, vl ) )
filtered << node;
}
}
@@ -2,10 +2,11 @@

#include "qgsmaplayerregistry.h"
#include "qgsrendercontext.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsrendererv2.h"
#include "qgspointdisplacementrenderer.h"
#include "qgsvectorlayer.h"

#include "qgssymbollayerv2utils.h"

QgsMapHitTest::QgsMapHitTest( const QgsMapSettings& settings )
: mSettings( settings )
@@ -50,9 +51,20 @@ void QgsMapHitTest::run()
painter.end();
}

bool QgsMapHitTest::symbolVisible( QgsSymbolV2* symbol, QgsVectorLayer* layer ) const
{
if ( !symbol || !layer || !mHitTest.contains( layer ) )
return false;

return mHitTest.value( layer ).contains( QgsSymbolLayerV2Utils::symbolProperties( symbol ) );
}

void QgsMapHitTest::runHitTestLayer( QgsVectorLayer* vl, SymbolV2Set& usedSymbols, QgsRenderContext& context )
{
bool hasStyleOverride = mSettings.layerStyleOverrides().contains( vl->id() );
if ( hasStyleOverride )
vl->styleManager()->setOverrideStyle( mSettings.layerStyleOverrides().value( vl->id() ) );

QgsFeatureRendererV2* r = vl->rendererV2();
bool moreSymbolsPerFeature = r->capabilities() & QgsFeatureRendererV2::MoreSymbolsPerFeature;
r->startRender( context, vl->fields() );
@@ -63,13 +75,25 @@ void QgsMapHitTest::runHitTestLayer( QgsVectorLayer* vl, SymbolV2Set& usedSymbol
while ( fi.nextFeature( f ) )
{
context.expressionContext().setFeature( f );

//make sure we store string representation of symbol, not pointer
//otherwise layer style override changes will delete original symbols and leave hanging pointers
if ( moreSymbolsPerFeature )
{
Q_FOREACH ( QgsSymbolV2* s, r->originalSymbolsForFeature( f, context ) )
usedSymbols.insert( s );
{
usedSymbols.insert( QgsSymbolLayerV2Utils::symbolProperties( s ) );
}
}
else
usedSymbols.insert( r->originalSymbolForFeature( f, context ) );
{
QgsSymbolV2* s = r->originalSymbolForFeature( f, context );
usedSymbols.insert( QgsSymbolLayerV2Utils::symbolProperties( s ) );
}
}
r->stopRender( context );

if ( hasStyleOverride )
vl->styleManager()->restoreOverrideStyle();
}

@@ -22,13 +22,24 @@ class CORE_EXPORT QgsMapHitTest

void run();

QSet<QgsSymbolV2*> symbolsForLayer( QgsVectorLayer* layer ) const { return mHitTest[layer]; }
/** Tests whether a symbol is visible for a specified layer.
* @param symbol symbol to find
* @param layer vector layer
* @note added in QGIS 2.12
*/
bool symbolVisible( QgsSymbolV2* symbol, QgsVectorLayer* layer ) const;

protected:

typedef QSet<QgsSymbolV2*> SymbolV2Set;
typedef QSet<QString> SymbolV2Set;
typedef QMap<QgsVectorLayer*, SymbolV2Set> HitTest;

/** Runs test for visible symbols within a layer
* @param vl vector layer
* @param usedSymbols set for storage of visible symbols
* @param context render context
* @note added in QGIS 2.12
*/
void runHitTestLayer( QgsVectorLayer* vl, SymbolV2Set& usedSymbols, QgsRenderContext& context );

QgsMapSettings mSettings;
@@ -1035,6 +1035,15 @@ QDomElement QgsSymbolLayerV2Utils::saveSymbol( const QString& name, QgsSymbolV2*
return symEl;
}

QString QgsSymbolLayerV2Utils::symbolProperties( QgsSymbolV2* symbol )
{
QDomDocument doc( "qgis-symbol-definition" );
QDomElement symbolElem = saveSymbol( "symbol", symbol, doc );
QString props;
QTextStream stream( &props );
symbolElem.save( stream, -1 );
return props;
}

bool QgsSymbolLayerV2Utils::createSymbolLayerV2ListFromSld( QDomElement& element,
QGis::GeometryType geomType,
@@ -157,6 +157,12 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );

/** Returns a string representing the symbol. Can be used to test for equality
* between symbols.
* @note added in QGIS 2.12
*/
static QString symbolProperties( QgsSymbolV2* symbol );

static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );

static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
@@ -165,6 +165,7 @@ ADD_QGIS_TEST(spatialindextest testqgsspatialindex.cpp)
ADD_QGIS_TEST(statisticalsummarytest testqgsstatisticalsummary.cpp)
ADD_QGIS_TEST(stringutilstest testqgsstringutils.cpp)
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
ADD_QGIS_TEST(symbolv2test testqgssymbolv2.cpp)
ADD_QGIS_TEST(vectordataprovidertest testqgsvectordataprovider.cpp)
ADD_QGIS_TEST(vectorlayercachetest testqgsvectorlayercache.cpp )
ADD_QGIS_TEST(vectorlayerjoinbuffer testqgsvectorlayerjoinbuffer.cpp )

0 comments on commit dcbe0d3

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