Skip to content
Permalink
Browse files

Ensure that project colors show correctly in legend and symbol previews

  • Loading branch information
nyalldawson committed Jan 12, 2019
1 parent ac10769 commit 0f42c8e30c3728023eeec0b0183b8fd131100b1e
@@ -393,6 +393,11 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
context.setForceVectorOutput( true );
context.setPainter( ctx ? ctx->painter : nullptr );

// use a minimal expression context for now
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
context.setExpressionContext( expContext );

//Consider symbol size for point markers
double height = settings.symbolSize().height();
double width = settings.symbolSize().width();
@@ -47,6 +47,13 @@ QgsLayoutItemLegend::QgsLayoutItemLegend( QgsLayout *layout )
// Connect to the main layertreeroot.
// It serves in "auto update mode" as a medium between the main app legend and this one
connect( mLayout->project()->layerTreeRoot(), &QgsLayerTreeNode::customPropertyChanged, this, &QgsLayoutItemLegend::nodeCustomPropertyChanged );

// If project colors change, we need to redraw legend, as legend symbols may rely on project colors
connect( mLayout->project(), &QgsProject::projectColorsChanged, this, [ = ]
{
invalidateCache();
update();
} );
}

QgsLayoutItemLegend *QgsLayoutItemLegend::create( QgsLayout *layout )
@@ -474,6 +474,14 @@ void QgsSymbol::drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext
QgsSymbolRenderContext symbolContext( context, outputUnit(), mOpacity, false, mRenderHints, nullptr, QgsFields(), mapUnitScale() );
symbolContext.setOriginalGeometryType( mType == Fill ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::UnknownGeometry );

if ( !customContext )
{
// if no render context was passed, build a minimal expression context
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
context.setExpressionContext( expContext );
}

Q_FOREACH ( QgsSymbolLayer *layer, mLayers )
{
if ( !layer->enabled() )
@@ -672,7 +672,7 @@ QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol *symbol, QSize size,
}

// If the context has no feature and there are DD properties,
// use a clone and clear all DDs: see issue #19096
// use a clone and clear some DDs: see issue #19096
// Applying a data defined size to a categorized layer hides its category symbol in the layers panel and legend
if ( symbol->hasDataDefinedProperties() &&
!( customContext
@@ -682,7 +682,13 @@ QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol *symbol, QSize size,
const QgsSymbolLayerList layers( symbol_noDD->symbolLayers() );
for ( const auto &layer : layers )
{
layer->dataDefinedProperties().clear();
for ( int i = 0; i < layer->dataDefinedProperties().count(); ++i )
{
QgsProperty &prop = layer->dataDefinedProperties().property( i );
// don't clear project color properties -- we want to show them in symbol previews
if ( prop.isActive() && !prop.isProjectColor() )
prop.setActive( false );
}
}
symbol_noDD->drawPreviewIcon( &painter, size, customContext );
}

0 comments on commit 0f42c8e

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